forked from fedefalag/combiEmo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotion_voice_localizer.m
More file actions
442 lines (347 loc) · 15.2 KB
/
emotion_voice_localizer.m
File metadata and controls
442 lines (347 loc) · 15.2 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
% (C) Copyright 2020 Federica Falagiarda
%%% Voice Localizer for the CombiEmo Exp %%%
expName = 'voice_localizer_combiemo';
expStart = GetSecs;
%%% some useful variables/parameters %%%
% background color and fixation color
white = 255;
black = 0;
bgColor = black;
fixColor = black;
textColor = white;
%%% input info
subjNumber = input('Subject number:'); % subject number
subjAge = input('Age:');
nReps = input('Number of repetitions:'); % number or reps of this localizer, ideally 12+ %
if isempty(nReps)
nReps = 12;
end
%%% SET UP OUTPUT FILES %%%
dataFileName = [cd '/data/subj' num2str(subjNumber) '_' expName '.txt'];
% format for the output od the data %
formatString = '%d, %d, %s, %s, %1.3f, %1.3f, %1.3f \n';
keypressFormatString = '%d, %s, %1.3f, \n';
baselineFormatString = '%1.3f \n';
% open a file for reading AND writing
% permission 'a' appends data without deleting potential existing content
if exist(dataFileName, 'file') == 0
dataFile = fopen(dataFileName, 'a');
% header
fprintf(dataFile, ['Experiment:\t' expName '\n']);
fprintf(dataFile, ['date:\t' datestr(now) '\n']);
fprintf(dataFile, ['Subject:\t' num2str(subjNumber) '\n']);
fprintf(dataFile, ['Age:\t' num2str(subjAge) '\n']);
% data
fprintf(dataFile, '%s \n', 'block, trial, emotion, actor, ISIduration, stimduration, timestamp');
fclose(dataFile);
end
%%% INITIALIZE SCREEN AND START THE STIMULI PRESENTATION %%%
% basic setup checking
AssertOpenGL;
% This sets a PTB preference to possibly skip some timing tests: a value
% of 0 runs these tests, and a value of 1 inhibits them. This
% should always be set to 0 for actual experiments
Screen('Preference', 'SkipSyncTests', 0);
% Screen('Preference', 'SkipSyncTests', 2);
Screen('Preference', 'ConserveVRAM', 4096);
% define default font size for all text uses (i.e. DrawFormattedText fuction)
Screen('Preference', 'DefaultFontSize', 28);
% OpenWindow
% [mainWindow, screenRect] = Screen('OpenWindow', max(screenVector), bgColor, [0 0 1000 700], 32, 2);
[mainWindow, screenRect] = Screen('OpenWindow', max(Screen('Screens')), bgColor, [], 32, 2);
% estimate the monitor flip interval for the onscreen window
interFrameInterval = Screen('GetFlipInterval', mainWindow); % in seconds
msInterFrameInterval = interFrameInterval * 1000; % in ms
% timings in my stimuli presentation
fileDuration = 1 - interFrameInterval / 3;
ISI = 0.1 - interFrameInterval / 3;
% get width and height of the screen
screenVector = Screen('Screens');
[widthWin, heightWin] = Screen('WindowSize', mainWindow);
widthDis = Screen('DisplaySize', max(screenVector));
% Priority(MaxPriority(mainWindow));
% to overcome the well-known randomisation problem
RandStream.setGlobalStream (RandStream('mt19937ar', 'seed', sum(100 * clock)));
% hide mouse cursor
HideCursor(mainWindow);
% % Listening enabled and any output of keypresses to Matlabs windows is
% % suppressed (see ref. page for ListenChar)
% ListenChar(-1);
KbName('UnifyKeyNames');
% build structure for all stimuli needed in this localizer
%
stimNameVoices = {'A27ne.wav', 'A27di.wav', 'A27fe.wav', 'A27ha.wav', 'A27sa.wav', 'A30ne.wav', 'A30di.wav', ...
'A30fe.wav', 'A30ha.wav', 'A30sa.wav', 'A32ne.wav', 'A32di.wav', 'A32fe.wav', 'A32ha.wav', 'A32sa.wav', 'A33ne.wav', ...
'A33di.wav', 'A33fe.wav', 'A33ha.wav', 'A33sa.wav'};
stimEmotion = repmat(1:5, 1, 4);
stimActor = [repmat(27, 1, 5), repmat(30, 1, 5), repmat(32, 1, 5), repmat(33, 1, 5)];
blockTypeVoices = 1; % 1 for voices, 2 for objects
stimNameObjects = {'waterpour.wav', 'carignition.wav', 'mixing.wav', 'egg.wav', 'bikebell.wav', 'applause.wav', ...
'engine.wav', 'grinder.wav', 'sharpener.wav', 'opencan.wav', 'churchbell.wav', 'hairdryer.wav', 'keyboard.wav', ...
'phone.wav', 'river.wav', 'saw.wav', 'thunder.wav', 'toothbrush.wav', 'traffic.wav', 'wind.wav'};
blockTypeObjects = 2;
%% Build structures for stimuli presentation
nStim = 20; % per block
voices = struct;
for v = 1:nStim
voices(v).stimulusname = stimNameVoices{v};
[voices(v).y, voices(v).freq] = audioread([cd '/auditory_stim/' voices(v).stimulusname]);
voices(v).wavedata = voices(v).y';
voices(v).nrchannels = size(voices(v).wavedata, 1);
voices(v).emotion = stimEmotion(v);
voices(v).actor = stimActor(v);
voices(v).blocktype = blockTypeVoices;
end
objects = struct;
for o = 1:nStim
objects(o).stimulusname = stimNameObjects{o};
[objects(o).y, objects(o).freq] = audioread([cd '/auditory_stim/' objects(o).stimulusname]);
objects(o).wavedata = objects(o).y';
objects(o).nrchannels = size(objects(o).wavedata, 1);
objects(o).blocktype = blockTypeObjects;
end
%% Insert the task stimuli as extra trials
% vector with block numbers
allBlocks = 1:nReps;
% randomly select a third of the blocks to have 2 1-back stimuli for the voices %
twoBackStimBlocksVoices = datasample(allBlocks, round(length(allBlocks) / 3), 'Replace', false);
% from the remaining blocks, select another third to have one 1-back stimulus %
remainingBlocksVoices = setdiff(allBlocks, twoBackStimBlocksVoices);
oneBackStimBlocksVoices = datasample(remainingBlocksVoices, round(length(allBlocks) / 3), 'Replace', false);
% the unselected blocks will have no 1-back stimuli
zeroBackStimBlocksVoices = setdiff(remainingBlocksVoices, oneBackStimBlocksVoices);
% randomly select a third of the blocks to have 2 1-back stimuli for the objects%
twoBackStimBlocksObjects = datasample(allBlocks, round(length(allBlocks) / 3), 'Replace', false);
% from the remaining blocks, select another third to have one 1-back stimulus %
remainingBlocksObjects = setdiff(allBlocks, twoBackStimBlocksObjects);
oneBackStimBlocksObjects = datasample(remainingBlocksObjects, round(length(allBlocks) / 3), 'Replace', false);
% the unselected blocks will have no 1-back stimuli
zeroBackStimBlocksObjects = setdiff(remainingBlocksObjects, oneBackStimBlocksObjects);
%% Presentation code
% triggers
cfg = struct;
cfg.testingDevice = 'mri';
cfg.triggerKey = 's';
cfg.numTriggers = 1;
cfg.win = mainWindow;
cfg.text.color = textColor;
cfg.bids.MRI.RepetitionTime = 2.55;
waitForTrigger(cfg);
% disable listening on the keyboard - needed for KbQueue
ListenChar(-1);
for rep = 1:nReps
Screen('FillRect', mainWindow, bgColor);
DrawFormattedText(mainWindow, '+', 'center', 'center', textColor);
[~, ~, lastEventTime] = Screen('Flip', mainWindow);
% acquire some base line
if rep == 1 || rep == round(nReps / 2 + 1)
dataFile = fopen(dataFileName, 'a');
fprintf(dataFile, baselineFormatString, GetSecs);
fclose(dataFile);
WaitSecs(10);
end
% define an index, a, that knows which kind of block/rep it is for number of one back tasks %
if ismember(rep, zeroBackStimBlocksVoices)
a = 0;
elseif ismember(rep, oneBackStimBlocksVoices)
a = 1;
elseif ismember(rep, twoBackStimBlocksVoices)
a = 2;
end
% a different index for objects
if ismember(rep, zeroBackStimBlocksObjects)
w = 0;
elseif ismember(rep, oneBackStimBlocksObjects)
w = 1;
elseif ismember(rep, twoBackStimBlocksObjects)
w = 2;
end
% and choose randomly which trial will be repeated in this block (if any)
backTrialsVoices = sort(randperm(20, a));
backTrialsObjects = sort(randperm(20, w));
% pseudorandomization made based on emotion vector
[pseudoEmoVector, pseudoEmoIndex] = pseudorandptb(stimEmotion);
for ind = 1:nStim
voices(pseudoEmoIndex(ind)).pseudorandindex = ind;
end
% turn struct into table to reorder it
tablevoices = struct2table(voices);
pseudorandtablevoices = sortrows(tablevoices, 'pseudorandindex');
% convert into structure to use in the trial/ stimui loop below
pseudorandVoices = table2struct(pseudorandtablevoices);
% add 1-back trials to the structure
pseudorandVoicesBack = pseudorandVoices;
for b = 1:(length(stimEmotion) + a)
if a == 1
if b <= backTrialsVoices
pseudorandVoicesBack(b) = pseudorandVoices(b);
elseif b == backTrialsVoices + 1
pseudorandVoicesBack(b) = pseudorandVoices(backTrialsVoices);
elseif b > backTrialsVoices + 1
pseudorandVoicesBack(b) = pseudorandVoices(b - 1);
end
elseif a == 2
if b <= backTrialsVoices(1)
pseudorandVoicesBack(b) = pseudorandVoices(b);
elseif b == backTrialsVoices(1) + 1
pseudorandVoicesBack(b) = pseudorandVoices(backTrialsVoices(1));
elseif b == backTrialsVoices(2) + 2
pseudorandVoicesBack(b) = pseudorandVoices(backTrialsVoices(2));
elseif b > backTrialsVoices(1) + 1 && b < backTrialsVoices(2) + 2
pseudorandVoicesBack(b) = pseudorandVoices(b - 1);
elseif b > backTrialsVoices(2) + 2
pseudorandVoicesBack(b) = pseudorandVoices(b - 2);
end
end
end
% shuffle object structure (pseudorand needed based on some feature?)%
randObjects = Shuffle(objects);
% add 1-back trials to the structure
randObjectsBack = randObjects;
for b = 1:(length(stimEmotion) + w)
if w == 1
if b <= backTrialsObjects
randObjectsBack(b) = objects(b);
elseif b == backTrialsObjects + 1
randObjectsBack(b) = objects(backTrialsObjects);
elseif b > backTrialsObjects + 1
randObjectsBack(b) = objects(b - 1);
end
elseif w == 2
if b <= backTrialsObjects(1)
randObjectsBack(b) = objects(b);
elseif b == backTrialsObjects(1) + 1
randObjectsBack(b) = objects(backTrialsObjects(1));
elseif b == backTrialsObjects(2) + 2
randObjectsBack(b) = objects(backTrialsObjects(2));
elseif b > backTrialsObjects(1) + 1 && b < backTrialsObjects(2) + 2
randObjectsBack(b) = objects(b - 1);
elseif b > backTrialsObjects(2) + 2
randObjectsBack(b) = objects(b - 2);
end
end
end
% stimuli presentation loop
for trial = 1:nStim + a
% fixation cross
DrawFormattedText(mainWindow, '+', 'center', 'center', textColor);
Screen('Flip', mainWindow);
% start queuing for triggers and subject's keypresses (flush previous queue) %
KbQueue('flush');
KbQueue('start', {'s', 'c'});
if pseudorandVoicesBack(trial).nrchannels < 2
wavedata = [pseudorandVoicesBack(trial).wavedata; pseudorandVoicesBack(trial).wavedata];
nrchannels = 2;
end
try
% Try with the 'freq'uency we wanted:
pahandle = PsychPortAudio('Open', [], [], 0, freq, nrchannels);
catch
% Failed. Retry with default frequency as suggested by device:
psychlasterror('reset');
pahandle = PsychPortAudio('Open', [], [], 0, [], nrchannels);
end
% Fill the audio playback buffer with the audio data 'wavedata':
PsychPortAudio('FillBuffer', pahandle, wavedata);
% Start audio playback for 'repetitions' repetitions of the sound data,
% start it immediately (0) and wait for the playback to start, return onset
% timestamp.
stimStart = GetSecs;
PsychPortAudio('Start', pahandle, 1, 0, 1);
% Stay in a little loop for the file duration:
t1 = 0;
while t1 < fileDuration
[keyIsDown, time, key] = KbCheck;
% if keyIsDown
% break
% end
t2 = GetSecs;
t1 = t2 - stimStart;
end
% Stop playback:
PsychPortAudio('Stop', pahandle);
% Close the audio device:
PsychPortAudio('Close', pahandle);
stimEnd = GetSecs;
Screen('Flip', mainWindow, stimEnd + ISI);
% save timestamps to output file
pressCodeTime = KbQueue('stop', expStart);
howManyKeyInputs = size(pressCodeTime);
dataFile = fopen(dataFileName, 'a');
for p = 1:howManyKeyInputs(2)
fprintf(dataFile, keypressFormatString, pressCodeTime(1, p), KbName(pressCodeTime(1, p)), pressCodeTime(2, p));
end
fprintf(dataFile, formatString, rep, trial, num2str(pseudorandVoicesBack(trial).emotion), num2str(pseudorandVoicesBack(trial).actor), GetSecs - stimEnd, stimEnd - stimStart, stimStart);
fclose(dataFile);
end
% stimuli presentation loop for objects
for trial = 1:nStim + w
% fixation cross
DrawFormattedText(mainWindow, '+', 'center', 'center', textColor);
Screen('Flip', mainWindow);
% start queuing for triggers and subject's keypresses (flush previous queue) %
KbQueue('flush');
KbQueue('start', {'s', 'c'});
% audio presentation
if randObjectsBack(trial).nrchannels < 2
wavedata = [randObjectsBack(trial).wavedata; randObjectsBack(trial).wavedata];
nrchannels = 2;
end
try
% Try with the 'freq'uency we wanted:
pahandle = PsychPortAudio('Open', [], [], 0, freq, nrchannels);
catch
% Failed. Retry with default frequency as suggested by device:
psychlasterror('reset');
pahandle = PsychPortAudio('Open', [], [], 0, [], nrchannels);
end
% Fill the audio playback buffer with the audio data 'wavedata':
PsychPortAudio('FillBuffer', pahandle, wavedata);
% Start audio playback for 'repetitions' repetitions of the sound data,
% start it immediately (0) and wait for the playback to start, return onset
% timestamp.
stimStart = GetSecs;
PsychPortAudio('Start', pahandle, 1, 0, 1);
% Stay in a little loop for the file duration:
t1 = 0;
while t1 < fileDuration
[keyIsDown, time, key] = KbCheck;
% if keyIsDown
% break
% end
t2 = GetSecs;
t1 = t2 - stimStart;
end
% Stop playback:
PsychPortAudio('Stop', pahandle);
% Close the audio device:
PsychPortAudio('Close', pahandle);
stimEnd = GetSecs;
Screen('Flip', mainWindow, stimEnd + ISI);
% save timestamps to output file
pressCodeTime = KbQueue('stop', expStart);
howManyKeyInputs = size(pressCodeTime);
dataFile = fopen(dataFileName, 'a');
for p = 1:howManyKeyInputs(2)
fprintf(dataFile, keypressFormatString, pressCodeTime(1, p), KbName(pressCodeTime(1, p)), pressCodeTime(2, p));
end
fprintf(dataFile, formatString, rep, trial, '0', randObjectsBack(trial).stimulusname, GetSecs - stimEnd, stimEnd - stimStart, stimStart);
fclose(dataFile);
end
% more baseline
if rep == nReps
dataFile = fopen(dataFileName, 'a');
fprintf(dataFile, baselineFormatString, GetSecs);
fclose(dataFile);
WaitSecs(10);
end
end
DrawFormattedText(mainWindow, 'end of localizer :)', 'center', 'center', textColor);
Screen('Flip', mainWindow);
expEnd = GetSecs;
disp('Voice localizer duration:');
disp((expEnd - expStart) / 60);
waitForKb('space');
ListenChar(0);
ShowCursor;
sca;