How to read the OSC messages from muse-io help for code in C++ or Delphi XE7


#1

Hi All,

Is there some one who has done some coding in C++ or Delphi to read the OSC messages from the muse-io and like to share it?
I still struggle in Delphi and C++ to read the OSC messages from the muse-io and like to have some help.

The code I’m using is just reading the first part of the int or real.

Here is my code from Delphi XE7

unit MuseOSCDecoder;

interface

uses Classes, SysUtils;

type
TOSCMessage = class (TObject)
public
Address : string;
Datatype : string;
OutString: string;
OutUntyped : string;
OutInt: integer;
OutSingle: single;
procedure decodeOSCMessage(AData : TStream);
end;

implementation

function ReadOSCString(AData:TStream):string;
var
s: String;
a: array[1…4] of byte;
i,j : integer;
endfound : boolean;
begin
endfound := false;
s := ‘’;
repeat
j := AData.Read(a,4);
for i := 1 to j do
// if a[i] > 0
if a[i] > 0
then s := s+chr(a[i])
else endfound := true;
// until endfound or (j < 4);
until endfound or (j < 4);
result := s;
end;

function ReadOSCLong(AData:TStream):int64;
var
a,b: array[1…8] of byte;
i,j : integer;
begin
j := AData.Read(a,8);
for i := 1 to j do b[9-i] := a[i];
result := int64(b);
end;

function ReadOSCInt(AData:TStream):integer;
var
a,b: array[1…4] of byte;
i,j : integer;
begin
for i := 1 to 4 do b[i] := 0;
j := AData.Read(a,4);
for i := 1 to j do b[5-i] := a[i];
result := integer(b);
end;

function ReadOSCSingle(AData:TStream):single;
var
a,b: array[1…4] of byte;
i,j : integer;
begin
j := AData.Read(a,4);
for i := 1 to j do b[5-i] := a[i];
result := single(b);
end;

procedure TOSCMessage.decodeOSCMessage(AData : TStream);
var
adr,s,ss: String;
fbundle : boolean;
i,j,k : integer;
a : byte;
begin
s := ReadOSCString(AData);
fbundle := s = ‘#bundle’;
if fBundle
then begin
k := ReadOscInt(AData);
adr := ReadOSCString(AData);
end else begin
adr := s;
k := 1;
end;
// while k > 0
while k > 0
do begin
s := ReadOSCString(AData);
// if (s>’’)
if (s > ‘’)
then begin
if (s[1] = ‘,’)
then begin
for i := 2 to length(s) do
case char(s[i]) of
’f’ : begin
outsingle := ReadOSCSingle(AData);
datatype := ‘single’;
address := adr;
end;
‘i’ : begin
outint := ReadOSCInt(AData);
datatype := ‘integer’;
address := adr;
end;
‘s’ : begin
outstring := ReadOSCString(AData);
datatype := ‘string’;
address := adr;
end
end;
end else begin
ss := ‘’;
for i := 1 to length(s) do
ss := ss+inttohex(ord(s[i]),2)+’ ';
ss := ss+ '00 ‘;
for i := Adata.Position to AData.Size-1 do
begin
j := AData.Read(a,1);
// if j > 0 then ss := ss+inttohex(a,2)+’ ‘;
if j > 0 then ss := ss+inttohex(a,2)+’ ';
end;
OutUntyped := ss;
datatype := ‘Untyped’;
end
end;
if fBundle
then begin
k := ReadOscInt(AData);
end else
k := 0;
end;
end;

end.

This is a part of the the output.

/muse/eeg single 524.748718261719
/muse/eeg/quantization integer 64
/muse/eeg single 1577.5361328125
/muse/eeg single 1156.42114257813
/muse/eeg single 314.191253662109
/muse/eeg single 314.191253662109
/muse/eeg single 1366.97863769531
/muse/eeg single 1682.81494140625
/muse/elements/alpha_relative single NAN
/muse/elements/beta_relative single NAN
/muse/elements/delta_relative single NAN
/muse/elements/gamma_relative single NAN
/muse/elements/theta_relative single NAN
/muse/elements/horseshoe single 4
/muse/elements/is_good integer 0
/muse/elements/blink integer 0
/muse/elements/jaw_clench integer 0
/muse/elements/touching_forehead integer 0
/muse/elements/experimental/concentration single 0
/muse/elements/experimental/mellow single 0
/muse/elements/raw_fft0 single -4.34254741668701
/muse/elements/raw_fft1 single -9.4347038269043
/muse/elements/raw_fft2 single -7.17806911468506
/muse/elements/raw_fft3 single -6.83833646774292

Do I miss read the documentation?

Thanks


#2

Hi HvM

s := ReadOSCString(AData);
// if (s>'')
if (s > '')
then begin
if (s[1] = ',')
then begin
for i := 2 to length(s) do

I have never developed in Delphi, but from the part of your code above, it seems to me that you are breaking the reading of the full line when a comma is found. Commas are part of the line as they are used to separate the values. I believe the end of line is marked with a CR or CR+LF. (0x0D or 0x0D + 0x0A)

The number of values per line can vary, depending on the type of data (path)
eeg, alpha, beta, …, horseshoe, is_good – all have four values (one for each channel)
accelerometer has 3 values
blink, jaw_clench, touching_forehead, just 1 value
and raw_fftx has 129 value per line, see bellow.

/muse/eeg, 881.7095, 850.45483, 860.3247, 886.6444
/muse/dsp/elements/alpha, 0.26169658, 0.27628663, 0.20323566, 0.13173503
/muse/dsp/elements/beta, 0.13697891, 0.1830878, 0.37757125, 0.14257035
/muse/dsp/elements/delta, 0.47707188, 0.25556466, 0.15382615, 0.59089386
/muse/dsp/elements/gamma, 0.062288847, 0.13622159, 0.21913691, 0.08889408
/muse/dsp/elements/theta, 0.0619638, 0.14883932, 0.046230037, 0.045906685
/muse/dsp/elements/horseshoe, 1.0, 1.0, 1.0, 1.0
/muse/dsp/elements/is_good, 1, 1, 1, 1
/muse/dsp/blink, 0
/muse/dsp/elements/jaw_clench, 0
/muse/dsp/elements/touching_forehead, 1
/muse/dsp/elements/raw_fft0, 0.30863896, 0.81836003, 0.43603227, -0.1489837, -0.32464743, -1.1751207, -1.1673592, -0.61906123, -1.2021106, -2.4202297, -1.132239, -0.37004024, -0.7803701, -0.8271893, -1.11295, -1.3570758, -1.3486773, -1.61259, -1.0382524, -1.0914437, -1.6795686, -1.1206609, -1.2394232, -1.7282149, -1.9336224, -1.2820944, -1.2937943, -1.838095, -1.62031, -1.5486296, -1.2222582, -1.2133143, -1.3481542, -2.2815847, -2.1404183, -1.6808664, -1.7491682, -1.387449, -1.8807998, -1.2128663, -1.5265036, -2.03265, -1.9674158, -2.1131852, -1.4875745, -1.8375704, -2.1549256, -1.9692738, -2.445908, -1.6645788, -1.3736012, -1.9824588, -2.0711656, -1.8261783, -1.3148392, -1.2125654, -1.8546041, -2.037582, -1.4803419, -1.9520409, -1.9784213, -1.3404294, -1.4107807, -2.0750406, -2.4083438, -1.9013463, -1.937078, -2.3304925, -2.3730407, -3.15997, -2.9571385, -2.6643145, -3.0788295, -2.7277286, -2.2966273, -2.7495952, -2.9994771, -2.3791025, -1.6814346, -2.0043247, -2.222536, -2.5646644, -2.0400975, -1.6000702, -1.7056507, -2.009025, -2.1540074, -2.406646, -2.2743273, -1.6794913, -2.4875758, -2.055718, -1.8675643, -2.337074, -1.7725965, -1.817607, -1.3302343, -1.5422353, -1.8843529, -1.884061, -1.685705, -1.5084519, -2.1709278, -2.8628085, -2.2582364, -2.0108037, -1.730062, -1.9643487, -2.0251818, -1.6682049, -1.7540983, -1.6558353, -1.6892251, -1.2869523, -2.0310965, -1.6147745, -1.9734181, -2.1475453, -1.8862826, -2.040901, -2.412557, -3.4168737, -2.0150769, -2.077702, -2.7063751, -2.202187, -2.216531, -2.8808525, -3.114492
/muse/dsp/elements/raw_fft1, -0.7544606, -0.38161975, -0.8973408, -1.1890366, -1.1578671, -0.9194751, -0.75478286, -0.87360674, -1.2519963, -1.6214533, -1.2567981, -1.0011148, -1.5063319, -1.7532682, -1.3718289, -1.636146, -2.1712348, -1.6843916, -1.3868619, -2.2985995, -2.3704371, -1.8553699, -1.7630486, -1.6758401, -2.2200835, -2.1284235, -1.5593956, -1.8446026, -1.7712855, -2.0391216, -2.0848215, -2.5161636, -2.3971014, -2.1815073, -1.7967758, -1.6534438, -1.5679656, -2.278771, -2.2924035, -2.9047527, -2.2193336, -2.149483, -2.5226154, -1.5335883, -1.5085719, -2.528135, -2.3431792, -2.086399, -1.8911766, -1.6494194, -2.0128062, -2.0599139, -1.7834755, -2.0146635, -1.9572339, -1.9528061, -1.9631101, -2.2180924, -2.3846, -1.7176832, -2.1957102, -2.097139, -2.2192612, -2.209305, -2.544023, -2.1062393, -1.9880948, -2.1536734, -2.2483504, -2.8106966, -3.3562453, -3.2080798, -2.7520835, -3.442676, -2.379713, -2.477454, -2.431189, -2.5034986, -2.1248355, -1.87962, -1.9091551, -2.6310518, -2.4308615, -2.4197972, -2.2214284, -2.1710286, -2.3207748, -2.2126822, -2.163187, -2.5373569, -2.6363769, -2.314586, -2.3182836, -2.3601398, -2.4383671, -2.1953475, -2.5128798, -2.2507253, -1.7942133, -2.1534297, -2.2441456, -1.9214226, -1.9329987, -2.030317, -2.5296733, -2.7091515, -2.486005, -2.6342828, -2.7270923, -2.015424, -1.8189867, -1.8133976, -2.3050332, -2.040896, -2.1917577, -1.8240719, -1.8125566, -2.3148704, -2.7363505, -2.5317912, -2.6875196, -3.3072577, -2.3918889, -2.4171135, -3.1401677, -3.373559, -3.7297142, -3.352488, -2.8629963

HTH, Eduardo.


#3

Thanks Eduardo,

I have solved the problem and get all data, still no relation between the ‘/muse/dsp/elements/raw_fft0’ frequency and the ‘/muse/eeg’ uV.
We need to have the Freqency and related power from every sensor to work with the muse.

If there is any document where I can get this let me know.