-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.h
408 lines (355 loc) · 10.7 KB
/
experiment.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifndef GLOBALS_EXPERIMENT_H
#define GLOBALS_EXPERIMENT_H
#include <Bela.h>
#include "../qsdk/RTPacket.h"
#include "../qsdk/RTProtocol.h"
#include "./config.h"
#include "./globals.h"
#include "./qtm.h"
#include "./sound.h"
#include "./space.h"
bool prepare_sonification_condition() {
// make sure there's 3D data
bool dataAvailable;
if (!rtProtocol->Read3DSettings(dataAvailable)) return false;
// Start streaming from QTM
if (gStreamUDP) {
if (!rtProtocol->StreamFrames(CRTProtocol::RateAllFrames, 0, nPort, NULL,
CRTProtocol::cComponent3d)) {
printf("Error streaming from QTM\n");
return false;
}
} else {
// Start the 3D data stream.
if (!rtProtocol->StreamFrames(CRTProtocol::RateAllFrames, 0, 0, NULL,
CRTProtocol::cComponent3d)) {
printf("Error streaming from QTM\n");
return false;
}
}
printf("Started streaming 3D data...\n");
gStreaming = true;
if (!reindexMarkers(rtProtocol)) return false;
// start getting the 3D data.
// Bela_scheduleAuxiliaryTask(gFillBufferTask);
gSilence = true;
// Bela_deleteAllAuxiliaryTasks();
return true;
}
bool end_sonification_condition() {
// Stop streaming from QTM
if (!rtProtocol->StreamFramesStop()) {
printf("Error stopping streaming from QTM\n");
return false;
}
printf("Stopped streaming 3D data\n");
gStreaming = false;
gSilence = true;
return true;
}
void resetDuration() {
gCurrentTrialDuration = 0;
}
void resetTrial() {
resetDuration();
gTrialRunning = false;
startTonePlayed = false;
endTonePlayed = false;
gTrialDone = false;
gReadPtrOvertone = 0.0f;
gReadPtrUndertone = 0.0f;
gReadPtrUndertone2 = 0.0f;
gAmpModPtr = 0;
}
void startBreak() {
printf("Starting break\n");
resetDuration();
gOnBreak = true;
gBreakDone = false;
}
void endBreak() {
printf("Ending break\n");
gOnBreak = false;
gBreakDone = true;
}
void waitForButton() {
printf("\n\nPlease press the Bela button to continue...\n\n");
gWaitingForButtonPress = true;
gBelaButtonPressed = false;
}
void resetButton() {
gWaitingForButtonPress = false;
gBelaButtonPressed = false;
}
void startCondition() {
// resetTrial();
gCurrentTrialRep = 0;
gConditionDone = false;
}
void endCondition() {
gConditionDone = true;
}
void resetSine() {
gCurrentTonePhase = 0.0f;
}
void startStartTone() {
resetDuration();
resetSine();
gSilence = false;
startTonePlaying = true;
gCurrentToneIdx = 0;
gCurrentToneFreq = gTrialStartTones[gCurrentToneIdx];
}
void endStartTone() {
gSilence = true;
startTonePlaying = false;
startTonePlayed = true;
}
void startEndTone() {
resetDuration();
resetSine();
gSilence = false;
endTonePlaying = true;
gCurrentToneIdx = 0;
gCurrentToneFreq = gTrialEndTones[gCurrentToneIdx];
}
void endEndTone() {
gSilence = true;
endTonePlaying = false;
endTonePlayed = true;
}
void startExperiment() {
// start the experiment
gExperimentStarted = true;
printf("Experiment started.\n");
// print number of conditions
printf("Number of conditions: %d\n", NUM_CONDITIONS);
// print condition order from enum Condition
printf("Condition order: \n");
for (int i = 0; i < NUM_CONDITIONS; i++) {
printf("- %s: %d \n", gConditionLabels[i], gConditionOrder[i] + 1);
}
// print the number of trials
printf("Number of trials per condition (incl. practice): %d\n", NUM_TRIALS);
// print the durations for each trial
printf("Trial durations: ");
for (int i = 0; i < NUM_TRIALS; i++) {
printf("%d: %3.1f seconds, ", i + 1, gTrialDurationsSec[i]);
}
printf("\n");
// print pause duration
printf("Trial pause duration: %3.1f seconds\n\n", gBreakDurationSec);
// start the first trial
gCurrentConditionIdx = 0;
startCondition();
resetTrial();
// startBreak();
// if we're using bela to start / stop capture, do it here.
if (gControlQTMCapture) {
startCapture(rtProtocol);
printf("QTM capture started.\n");
}
sendEventLabel(rtProtocol, Labels::EXPERIMENT_START);
waitForButton();
}
void endExperiment() {
// experiment is finished, place marker and plan exit
// TODO: confirm type conversion
printf("Experiment ended.\n");
sendEventLabel(rtProtocol, Labels::EXPERIMENT_END);
// if we're using bela to start / stop capture, do it here.
if (gControlQTMCapture) {
stopCapture(rtProtocol);
printf("QTM capture stopped.\n");
}
printf("Exiting.\n");
Bela_requestStop();
}
void startTrial() {
printf("Sending trial start label...\n");
sendEventLabel(rtProtocol, Labels::TRIAL_START);
printf("Trial %d started.\n", gCurrentTrialRep);
resetTrial();
gTrialRunning = true;
}
void startTrialIfReady() {
if (gOnBreak && !gBreakDone) {
// this is a break between trials
if (gCurrentTrialDuration >= gBreakDurationSamples) {
endBreak();
} else {
return;
}
}
if (!startTonePlayed && !startTonePlaying) {
// start tone hasn't been played yet
printf("Starting sonification.\n");
prepare_sonification_condition();
printf("Playing start tone.\n");
startStartTone();
return;
}
if (!startTonePlayed) {
if (gCurrentTrialDuration >= gTrialStartToneDuration) {
if (gCurrentToneIdx + 1 >= gTrialStartTones.size()) {
printf("Start tone complete.\n");
endStartTone();
} else {
// play the next tone
resetDuration();
gCurrentToneIdx++;
gCurrentToneFreq = gTrialStartTones[gCurrentToneIdx];
gSilence = gCurrentToneFreq == 0.0f;
return;
}
} else {
return;
}
}
// tone and break are done, so now start the trial
printf("Sending trial condition label...\n");
if (gCurrentConditionIdx == Condition::NO_SONIFICATION) {
sendEventLabel(rtProtocol, ConditionLabels::NO_SONIFICATION);
printf("Condition %d: NO_SONIFICATION\n", gCurrentConditionIdx);
} else if (gCurrentConditionIdx == Condition::TASK_SONIFICATION) {
sendEventLabel(rtProtocol, ConditionLabels::TASK_SONIFICATION);
// printf("Starting sonification.\n");
// prepare_sonification_condition();
printf("Condition %d: TASK_SONIFICATION\n", gCurrentConditionIdx);
gSilence = false;
} else if (gCurrentConditionIdx == Condition::SYNC_SONIFICATION) {
sendEventLabel(rtProtocol, ConditionLabels::SYNC_SONIFICATION);
// printf("Starting sonification.\n");
// prepare_sonification_condition();
printf("Condition %d: SYNC_SONIFICATION\n", gCurrentConditionIdx);
gSilence = false;
}
startTrial();
if (!gSilence && gStreaming) {
Bela_scheduleAuxiliaryTask(gFillBufferTask);
}
}
void endTrial() {
resetTrial();
if (gCurrentConditionIdx >= gTrialCounts.size()) {
// we have completed all the trials, stop the experiment
gExperimentFinished = true;
}
}
void endTrialIfReady() {
if (gTrialDone) {
if (!endTonePlayed && !endTonePlaying) {
// start tone hasn't been played yet
printf("Playing end tone.\n");
startEndTone();
return;
}
if (endTonePlaying && gCurrentTrialDuration >= gTrialEndToneDuration) {
if (gCurrentToneIdx + 1 >= gTrialEndTones.size()) {
printf("End tone complete.\n");
endEndTone();
endTrial();
if (!gConditionDone) {
startBreak();
} else {
waitForButton();
}
end_sonification_condition();
return;
}
// play the next tone
resetDuration();
gCurrentToneIdx++;
gCurrentToneFreq = gTrialEndTones[gCurrentToneIdx];
gSilence = gCurrentToneFreq == 0.0f;
return;
}
} else if (gCurrentTrialDuration >= gTrialDurationsSamples[gCurrentTrialRep]) {
// now we have to keep running the experiment until it reaches the sample limit
gTrialDone = true;
printf("Trial %d ended.\n", gCurrentTrialRep);
gCurrentTrialRep++;
if (gCurrentConditionIdx != Condition::NO_SONIFICATION) {
gSilence = true;
}
if (gCurrentTrialRep >= gTrialCounts[gCurrentConditionIdx]) {
// we have completed all the reps for this trial, move on to the next one
gCurrentConditionIdx++;
endCondition();
}
printf("Sending trial end label...\n");
sendEventLabel(rtProtocol, Labels::TRIAL_END);
} else {
if (!gSilence && gStreaming) {
// just keep polling for 3D data.
Bela_scheduleAuxiliaryTask(gFillBufferTask);
}
}
}
// experiment runner
void runExperiment(void *) {
if (gWaitingForButtonPress) {
if (gBelaButtonPressed) {
printf("Button pressed, starting next condition...\n");
gWaitingForButtonPress = false;
startCondition();
resetButton();
}
return;
}
if (!gExperimentStarted && !gExperimentFinished) {
startExperiment();
} else if (!gTrialRunning) {
// trials will start if it's not during a break
startTrialIfReady();
} else if (!gExperimentFinished) {
// trials will end if they've run for the specified duration
endTrialIfReady();
}
if (gExperimentFinished) {
endExperiment();
return;
}
}
// update buffer of QTM data
void fillBuffer(void *) {
// if stream is not open, or we're silenced, don't do anything
if (!gStreaming || gSilence) return;
// Make sure we successfully get the data
if (!get3DPacket(rtProtocol, rtPacket, packetType)) return;
// this helps us when we're doing realtime playback, because it loops.
const unsigned int uPacketFrame = rtPacket->GetFrameNumber();
for (int i = 0; i < NUM_SUBJECTS; i++) {
// auto &prevPos = gPos3D[1][i];
auto &currPos = gPos3D[0][i];
// get the position of the marker
if (!rtPacket->Get3DMarker(gSubjMarker[i], currPos[0], currPos[1], currPos[2])) {
// the marker failed, we can try reindexing
printf("Marker %d failed, reindexing.\n", gSubjMarker[i]);
if (reindexMarkers(rtProtocol)) {
printf("Reindexing successful.\n");
// reindexing was successful, so we can try again
if (!rtPacket->Get3DMarker(gSubjMarker[i], currPos[0], currPos[1], currPos[2])) {
// the marker failed again, so we'll just set the position to 0
printf("Marker %d failed again, setting position to 0.\n", gSubjMarker[i]);
currPos[0] = 0.0f;
currPos[1] = 0.0f;
currPos[2] = 0.0f;
}
} else {
// reindexing failed, so we'll just set the position to 0
printf("Reindexing failed, setting position to 0.\n");
currPos[0] = 0.0f;
currPos[1] = 0.0f;
currPos[2] = 0.0f;
}
}
}
// update last processed frame
gLastFrame = uPacketFrame;
// swap the current and previous positions
// this means we always have the previous position in gPos3D[1]
// but overwrite gPos3D[0]
std::swap(gPos3D[0], gPos3D[1]);
}
#endif