Recording audio segment in each trial

Hello!

I have 300 trials of 20 seconds each, and want to record participants’ voice responses during the 18th-20th second of each trial. I’ve modified the “BasicSoundInputDemo”, but I encounter the following issues:

  1. The while loop (below) takes more than 2 seconds so I’m not sure at which timepoint precisely the recording starts. I want it to start exactly at second 18 and last until the end of second 20.

  2. Even if I say: while ((length(recordedaudio) / 48000) < 2), my recording is not always 2 seconds, but 2.01 or 2.02…

Here is the code:
pahandle = PsychPortAudio(‘Open’, [], 2, 1, [], 2);
PsychPortAudio(‘GetAudioData’, pahandle, 5);
PsychPortAudio(‘Start’, pahandle, 0, 0, 1);
recordedaudio = [];
while ((length(recordedaudio) / 48000) < 2)

% Wait a second...
WaitSecs(1);

% Retrieve pending audio data from the drivers internal ringbuffer:
audiodata = PsychPortAudio('GetAudioData', pahandle);

% And attach it to our full sound vector:
recordedaudio = [recordedaudio audiodata];

end
psychwavwrite(transpose(recordedaudio), 48000, 16, [fname ‘.wav’])
PsychPortAudio(‘Stop’, pahandle);

I’m using: PsychtoolboxVersion 3.0.17 on Linux, Matlab2016.

Please let me know if you have any suggestion on how to record without delays, a certain number of seconds or if you have any question on my description.

Thank you very much in advance,
Ioanna


Ioanna Zioga, PhD
Postdoctoral Researcher
Donders Centre for Cognitive Neuroimaging
Radboud University, Netherlands