Codes

%Reference:https://github.com/MuSAELab/muse_osc, Raymundo Cassani

%%%%%%%%%%%%%%%%%%%%

% clear all;

% close all;

%Use [audioRecorded,audioIndex,eegData,accData] = osc_server_muse_record_order(how many samples you want)

%to run this function. for about half minute, 5000 samples should be fine.

%If you don’t wait the function to finish, you may not get all data you

%want.

function [recordingData,audioData,eegRecord,accRecord] = osc_server_muse_record_order(samples);

% Check if the Instrumentation Control Toolbox is present

tbName = ‘Instrument Control Toolbox’;

verInfo = ver;

tbFlag = any(strcmp(tbName, {verInfo.Name}));

% Obtain release year

release = sscanf(version(‘-release’),‘%d%s’);

releaseYear = release(1);

% OCS Paths

% Note that these paths dependes of the muse-io.exe version, in this ca

% V3.4.0

oscPathV3_6_5{1,1} = ‘/muse/eeg’;

oscPathV3_6_5{1,2} = ‘fff’;

oscPathV3_6_5{2,1} = ‘/muse/acc’;

oscPathV3_6_5{2,2} = ‘fff’;

oscPathV3_6_5{3,1} = ‘/muse/config’;

oscPathV3_6_5{3,2} = ‘s’;

% Server parameters

% These parameters configure where the data fom muse-io.exe will be sent

ip = ‘127.0.0.1’; %Localhost

port = 5000;  %TCP Port (default port is 5000)

timeoutSec = 10; %In seconds

% Starts muse-io.exe

% Preset 14 set the Muse headset to deliver 4 channels:

% {‘TP9’; ‘FP1’; ‘FP2’; ‘TP10’}

system([‘start “Running: muse-io.exe –preset 14” “C:\Program Files (x86)\Muse\muse-io.exe” –preset 14 –osc osc.tcp://localhost:’ num2str(port)]);

%unix(‘open muse-io’);

% This flag (tcpFlag) indicates if the TCP connection will be done using the

% TCP/IP objects(tcpFlag == true) Instrumentation Control Toolbox and Relase >2011 are needed;

% OR using

% Java ServerSocker (tcpFlag == false)

tcpFlag = tbFlag && releaseYear > 2011;

if tcpFlag

    tcpServer=tcpip(ip, port, ‘NetworkRole’, ‘server’);

    tcpServer.InputBufferSize = 5000;

    tcpServer.Timeout = timeoutSec;

    %Open a connection. This will not return until a connection is received.

    fopen(tcpServer);

else

    import java.net.ServerSocket

    import java.net.InetSocketAddress

    import java.io.*

    serverISA = InetSocketAddress(ip, port);

    serverSSocket = ServerSocket(port,0,serverISA.getAddress);

    serverSSocket.setSoTimeout(timeoutSec*1000);

    %Open a connection. This will not return until a connection is received.

    serverSocket = serverSSocket.accept;

    serverInputStream = serverSocket.getInputStream();

    serverDIS = DataInputStream(serverInputStream);

end

%Size of buffers to Read EEG ACC

%As the EEG output sampling frequency is 220Hz

fse = 220;

fsa = 50;

secBuffer = 10;

eegName = {‘TP9’; ‘FP1’; ‘FP2’; ‘TP10’};

eegBuffer = zeros([fse*secBuffer,numel(eegName)]);

%%%%% RECORD DATA %%%%%%

eegRecord = zeros([samples,numel(eegName)]);

dataCounter = 0;

%%%%%%%%%%%%%%%%%%%%%%%%

accName = {‘ACC_X’; ‘ACC_Y’; ‘ACC_Z’};

accBuffer = zeros([fsa*secBuffer,numel(accName)]);

accRecord = zeros([samples,numel(accName)]);

eegCounter = 0;

plot1 = true;

conf1 = true;

figure()

%%%%%%%%%record%%%%%%%%%

audioCounter = 0;

audioData = zeros(samples,1);

recIn = audiorecorder;

disp(‘Start Recording Now!’)

record(recIn);

%%%%%%%%%%%%%%%%%%%%%%%%

runCounter = 0;

while runCounter <= samples

    if tcpFlag

        try %Catch Matlab error

            a = fread(tcpServer, 4);  %How large is the package (# bytes)

        catch err;

            break

        end

        bytesToRead = double(swapbytes(typecast(uint8(a),‘int32’)));

        try %Catch Matlab error

            bytesData = fread(tcpServer,bytesToRead);

        catch err;

            break

        end

        

    else %Utilising Java Classes

        for ind = 1:4 %How large is the package (# bytes)

            try

            a(ind) = DISread(serverDIS,‘uint8’);

            catch e %catch “Java exception occurred”

                break

            end

        end

        bytesToRead = double(swapbytes(typecast(uint8(a),‘int32’)));

        bytesData = zeros(bytesToRead,1);

        for ind = 1:bytesToRead

            try

                bytesData(ind) = DISread(serverDIS,‘uint8’);

            catch e; %catch “Java exception occurred”

                break

            end

        end

        if ind ~= bytesToRead

            break

        end

    end

    

    [oscPath, oscTag, oscData] = splitOscMessage(bytesData);

    data = oscFormat(oscTag,oscData);

    

    switch oscPath

        case oscPathV3_6_5{1,1} %The message contains EEG data

         eegBuffer = [cell2mat(data);eegBuffer(1:(end-1), :)];

         eegCounter = eegCounter+1;    

         %%%%%%%%%%% RECORD DATA %%%%%%%

         eegRecord = [cell2mat(data);eegRecord(1:(end-1),:)];

         dataCounter = dataCounter+1;

         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

         currentData = recIn.CurrentSample;

         audioData = [currentData; audioData(1:(end-1),:)];

         audioCounter = audioCounter +1;

         runCounter = runCounter+1;

         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        case oscPathV3_6_5{2,1} %The message contains Acceleration data

         accBuffer = [cell2mat(data);accBuffer(1:(end-1), :)];

         %%%%%%%%%%% RECORD DATA %%%%%%%

         accRecord = [cell2mat(data);accRecord(1:(end-1), :)];

         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        case oscPathV3_6_5{3,1} %The message contains Configuration data

        %Show configuration

         if conf1

           conf = data{1}(2:end-2);

           Ctmp = textscan(conf,‘%s’,‘delimiter’,‘,’);

           C = strrep( Ctmp{1}, ‘”‘,);

           msgbox(C,‘Muse Configuration’ );

           conf1 = false;

         end

        otherwise

        %Do nothing    

        %More cases can be added to treat other paths   

    end

      

%Plot every 44 EEG samples approx 200ms

    if eegCounter == 44

        if plot1

         subplot(2,1,1);

         time = 0:1/fse:secBuffer-1/fse;

         h1 = plot(time,eegBuffer);

         legend(eegName, ‘Location’,‘EastOutside’);

         xlabel(‘Time (s)’)

         ylabel(‘Voltage (uV)’)   

         subplot(2,1,2);

         time = 0:1/fsa:secBuffer-1/fsa;

         h2= plot(time,accBuffer);

         xlabel(‘Time (s)’)

         ylabel(‘Acceleration (mG)’)

         legend(h2, accName, ‘Location’,‘EastOutside’);

        

         plot1 = false;

        

        else

         cell1 = (num2cell(eegBuffer,1))’;

         set(h1,{‘ydata’},cell1);

         cell2 = (num2cell(accBuffer,1))’;

         set(h2,{‘ydata’},cell2);

        end

    drawnow;   

    eegCounter = 0;

    end % if eegCounter   

    

    %%%%%%%%%%%%%% RECORD DATA %%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end %while true

%stop(record);

recordingData = getaudiodata(recIn);

if tcpFlag

    fclose(tcpServer);

    delete(tcpServer);

else

    serverSocket.close();

    serverSSocket.close();

end

display(‘End of Acquisition’);

end