Last active
December 23, 2023 23:26
-
-
Save janne-s/06f66c4cb065e08b9dd7 to your computer and use it in GitHub Desktop.
Emotiv EPOC Java EEGLog raw eeg data to OSC for Max/MSP / Processing etc. The code is set to send OSC data at the address /eegraw to port 6969.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Build from the Java example project according the instructions: EmotivResearch/docs/Examples/Java/EEGLog | |
// OSC functionality: https://github.com/hoijui/JavaOSC | |
import com.sun.jna.Pointer; | |
import com.sun.jna.ptr.IntByReference; | |
import com.illposed.osc.*; | |
import java.net.InetAddress; | |
import java.util.List; | |
import java.util.ArrayList; | |
public class EEGLog { | |
private static OSCPortOut sender; | |
public static void main(String[] args) | |
{ | |
Pointer eEvent = Edk.INSTANCE.EE_EmoEngineEventCreate(); | |
Pointer eState = Edk.INSTANCE.EE_EmoStateCreate(); | |
IntByReference userID = null; | |
IntByReference nSamplesTaken= null; | |
short composerPort = 1726; | |
int option = 1; | |
int state = 0; | |
float secs = 1; | |
boolean readytocollect = false; | |
userID = new IntByReference(0); | |
nSamplesTaken = new IntByReference(0); | |
try | |
{ | |
sender = new OSCPortOut(InetAddress.getLocalHost(), 6969); | |
} | |
catch (Exception e) | |
{ | |
System.out.println("OSCPortOut ERROR: "+ e); | |
} | |
switch (option) { | |
case 1: | |
{ | |
if (Edk.INSTANCE.EE_EngineConnect("Emotiv Systems-5") != EdkErrorCode.EDK_OK.ToInt()) { | |
System.out.println("Emotiv Engine start up failed."); | |
return; | |
} | |
break; | |
} | |
case 2: | |
{ | |
System.out.println("Target IP of EmoComposer: [127.0.0.1] "); | |
if (Edk.INSTANCE.EE_EngineRemoteConnect("127.0.0.1", composerPort, "Emotiv Systems-5") != EdkErrorCode.EDK_OK.ToInt()) { | |
System.out.println("Cannot connect to EmoComposer on [127.0.0.1]"); | |
return; | |
} | |
System.out.println("Connected to EmoComposer on [127.0.0.1]"); | |
break; | |
} | |
default: | |
System.out.println("Invalid option..."); | |
return; | |
} | |
Pointer hData = Edk.INSTANCE.EE_DataCreate(); | |
Edk.INSTANCE.EE_DataSetBufferSizeInSec(secs); | |
System.out.print("Buffer size in secs: "); | |
System.out.println(secs); | |
System.out.println("Start receiving EEG Data!"); | |
while (true) | |
{ | |
state = Edk.INSTANCE.EE_EngineGetNextEvent(eEvent); | |
// New event needs to be handled | |
if (state == EdkErrorCode.EDK_OK.ToInt()) | |
{ | |
int eventType = Edk.INSTANCE.EE_EmoEngineEventGetType(eEvent); | |
Edk.INSTANCE.EE_EmoEngineEventGetUserId(eEvent, userID); | |
// Log the EmoState if it has been updated | |
if (eventType == Edk.EE_Event_t.EE_UserAdded.ToInt()) | |
if (userID != null) | |
{ | |
System.out.println("User added"); | |
Edk.INSTANCE.EE_DataAcquisitionEnable(userID.getValue(),true); | |
readytocollect = true; | |
} | |
} | |
else if (state != EdkErrorCode.EDK_NO_EVENT.ToInt()) { | |
System.out.println("Internal error in Emotiv Engine!"); | |
break; | |
} | |
if (readytocollect) | |
{ | |
Edk.INSTANCE.EE_DataUpdateHandle(0, hData); | |
Edk.INSTANCE.EE_DataGetNumberOfSample(hData, nSamplesTaken); | |
if (nSamplesTaken != null) | |
{ | |
if (nSamplesTaken.getValue() != 0) { | |
//System.out.print("Updated: "); | |
//System.out.println(nSamplesTaken.getValue()); | |
double[] data = new double[nSamplesTaken.getValue()]; | |
for (int sampleIdx=0 ; sampleIdx<nSamplesTaken.getValue() ; ++ sampleIdx) { | |
final List<Object> oscArgs = new ArrayList<Object>(1); | |
for (int i = 0 ; i < 14 ; i++) { | |
Edk.INSTANCE.EE_DataGet(hData, i, data, nSamplesTaken.getValue()); | |
double yourAwesomeBrainValue = data[sampleIdx]; | |
oscArgs.add((float)yourAwesomeBrainValue); | |
//System.out.print(data[sampleIdx]); | |
//System.out.print(","); | |
} | |
final OSCMessage message = new OSCMessage("/eegraw", oscArgs); | |
try | |
{ | |
sender.send(message); | |
} catch (Exception e) | |
{ | |
System.out.println("OSC MESSAGE SENDING ERROR: "+ e); | |
} | |
//System.out.println(); | |
} | |
} | |
} | |
} | |
} | |
Edk.INSTANCE.EE_EngineDisconnect(); | |
Edk.INSTANCE.EE_EmoStateFree(eState); | |
Edk.INSTANCE.EE_EmoEngineEventFree(eEvent); | |
System.out.println("Disconnected!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment