/
SoundHCI.cs
164 lines (138 loc) · 5.85 KB
/
SoundHCI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using libStreamSDK;
using System;
using System.IO;
using System.Media;
using System.Threading;
namespace oddball_experiment
{
public class SoundHCI
{
private SoundPlayer player1000 = null;
private string wavFile1000 = "./1000hz_100ms.wav";
private SoundPlayer player2000 = null;
private string wavFile2000 = "./2000hz_100ms.wav";
private StreamWriter wave_writer = null;
private StreamWriter sound_log_writer = null;
private const int EXPERIMENT_DURATION_SEC = 360;
private const float INTERVAL_SEC = 0.8f;
private const float LOW_SOUND_RATIO = 0.2f;
private bool isExperimentFinished = false;
public SoundHCI()
{
player1000 = new System.Media.SoundPlayer(wavFile1000);
player2000 = new System.Media.SoundPlayer(wavFile2000);
string dateStr = getDateStrForFilename();
wave_writer = new StreamWriter("./raw_mind_wave" + dateStr + ".csv", true);
sound_log_writer = new StreamWriter("./sound_timing_log" + dateStr + ".csv", true);
}
private void playHighSync()
{
sound_log_writer.WriteLine(getUnixtimeInMillisec().ToString() + ",2,0"); // log of sound start
player2000.PlaySync();
sound_log_writer.WriteLine(getUnixtimeInMillisec().ToString() + ",2,1"); // log of sound end
}
private void playLowSync()
{
sound_log_writer.WriteLine(getUnixtimeInMillisec().ToString() + ",1,0"); // log of sound start
player1000.PlaySync();
sound_log_writer.WriteLine(getUnixtimeInMillisec().ToString() + ",1,1"); // log of sound end
}
private void soundLoop()
{
Random rnd = new Random(1000);
long start_time = getUnixtimeInMillisec();
while(getUnixtimeInMillisec() - start_time < EXPERIMENT_DURATION_SEC * 1000)
{
int rnd_val = rnd.Next(10);
if(rnd_val <= 1)
{
playLowSync();
}
else
{
playHighSync();
}
sound_log_writer.Flush();
Thread.Sleep((int)(INTERVAL_SEC * 1000));
}
sound_log_writer.Close();
isExperimentFinished = true;
}
private long getUnixtimeInMillisec()
{
DateTimeOffset baseDt = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
long unixtime = (long) ((DateTimeOffset.Now - baseDt).Ticks / 10000.0); // convert to milli secods integer
// return String.Format("{0:##########.###}", unixtime);
return unixtime;
}
private String getDateStrForFilename()
{
DateTime dt = DateTime.Now;
return dt.ToString("yyyyMMdd_HHmmss");
}
private String getDateStrForLog()
{
DateTime dt = DateTime.Now;
return dt.ToString("yyyy/MM/dd HH:mm:ss:fff");
}
public void startExperiment()
{
NativeThinkgear thinkgear = new NativeThinkgear();
/* Print driver version number */
Console.WriteLine("Version: " + NativeThinkgear.TG_GetVersion());
/* Get a connection ID handle to ThinkGear */
int connectionID = NativeThinkgear.TG_GetNewConnectionId();
Console.WriteLine("Connection ID: " + connectionID);
if (connectionID < 0)
{
Console.WriteLine("ERROR: TG_GetNewConnectionId() returned: " + connectionID);
return;
}
int errCode = 0;
/* Attempt to connect the connection ID handle to serial port "COM5" */
//string comPortName = "\\\\.\\COM5";
string comPortName = "\\\\.\\COM6";
errCode = NativeThinkgear.TG_Connect(connectionID,
comPortName,
NativeThinkgear.Baudrate.TG_BAUD_57600,
NativeThinkgear.SerialDataFormat.TG_STREAM_PACKETS);
if (errCode < 0)
{
Console.WriteLine("ERROR: TG_Connect() returned: " + errCode);
return;
}
///* Read 10 ThinkGear Packets from the connection, 1 Packet at a time */
int packetsRead = 0;
Console.WriteLine("auto read test begin:");
errCode = NativeThinkgear.TG_EnableAutoRead(connectionID, 1);
if (errCode == 0)
{
packetsRead = 0;
Thread sound_th = new Thread(new ThreadStart(soundLoop));
sound_th.Start();
while (isExperimentFinished == false)
{
/* If raw value has been updated ... */
if (NativeThinkgear.TG_GetValueStatus(connectionID, NativeThinkgear.DataType.TG_DATA_RAW) != 0)
{
float raw_val = NativeThinkgear.TG_GetValue(connectionID, NativeThinkgear.DataType.TG_DATA_RAW);
wave_writer.WriteLine(getDateStrForLog() + "," + getUnixtimeInMillisec().ToString() + "," + raw_val.ToString());
packetsRead++;
}
}
errCode = NativeThinkgear.TG_EnableAutoRead(connectionID, 0); //stop
Console.WriteLine("auto read test stoped: " + errCode);
}
else
{
Console.WriteLine("auto read test failed: " + errCode);
}
NativeThinkgear.TG_Disconnect(connectionID); // disconnect test
/* Clean up */
NativeThinkgear.TG_FreeConnection(connectionID);
/* End program */
Console.WriteLine("please type Enter key to finish this program.");
Console.ReadLine();
}
}
}