Continuous sound input using PsychPortAudio

Hi all,

I’m trying to record audio using PsychPortAudio and noticed the following problems:

In my case, I’m recording music, and each recording is about 180 secs. I follow the PsychDemos’s BasicSoundInputDemo, and for each recording, I open the sound channel, record, append data, and then close the sound channel.

However, because I have many pieces of 180 secs each, I noticed that by the 2nd or 3rd piece, the recording started to have pops and in some cases severe distortion and tempo artifacts (matlab speeds up the recording and there are a lot of distortion present at the same time). The first recording is normally fine.

My guess is that this is a buffer issue—that I am not flushing my buffer after every recording. However, I noticed that in the DeleteBuffer documentation here: Psychtoolbox-3 - PsychPortAudio(‘DeleteBuffer’) , it specifies that I only need to do this when I’m doing a playback. In this case I am not doing a playback but rather recording.

Can someone offer any hints why the severe distortions happen? Do you have potential solutions for this issue?

Thank you so much in advance for your kind suggestions & generous help!

Best,
Emma

Please send a minimal example of your code that has the problem, and basic info like what operating system etc. I assume ptb is up to date?

Thank you for your response! Yes, I will put my code below.

I’m using PsychtoolboxVersion 3.0.18 and running the experiment using a Windows 10 machine. Before starting sound capture, I initialize PsychPortAudio with the following parameters:

%Initialize PsychPortAudio sound driver:
stim.audio.latLev = 1; 
rec.reqLatencyClass = 1; 
rec.mode = 2; %1==sound playback only; 2 == audio capture
rec.channels = 2; %number of channels
rec.freq = 44100;
rec.repetitions = 1; %0:infinite repetitions, ie.,until manually stopped via the ‘Stop’ subfunction
rec.when = 0; %starttime of playback: 0: immediately
rec.waitForStart = 1; % wait for sound onset (to speakers) to register startTime?
rec.amountToAllocateSecs = 4*60; % in seconds

InitializePsychSound(stim.audio.latLev);
parec = PsychPortAudio('Open', [], rec.mode, rec.reqLatencyClass, rec.freq, rec.channels); % use default device... 
PsychPortAudio('GetAudioData', parec, rec.amountToAllocateSecs); %Sound inputbuffer prepared/allocated for capture

And to start the continuous recording (and within the allocated buffer time duration), I first start the audio data and then append the captured audio data continuously until keyboard action is made:

recordedaudio = [];
PsychPortAudio('Start',parec,rec.repetitions,rec.when);

audiodata = PsychPortAudio('GetAudioData', parec);
nrsamples = size(audiodata, 2);
recordedaudio = [recordedaudio audiodata]; 

And as soon as keyboard action takes place, I stop the recording:

% Stop capture
PsychPortAudio('Stop', parec);
 % Perform a last fetch operation to get all remaining data
audiodata = PsychPortAudio('GetAudioData', parec);
recordedaudio = [recordedaudio audiodata];

And at the very end, the audio port is closed:

PsychPortAudio('Close', parec); 

The way I set the code up is that for every single piece I run the code from start to end, and separate recordings (let’s say 3 different pieces) requires me to run the code 3 times.

Do you see a problem with the buffer not being flushed between the recordings? Are there improvements/corrections that I need to make?

Thank you so much!

Looks as if it should work, nothing obviously wrong with your code or approach.
But then this is MS-Windows… Maybe rec.reqLatencyClass = 0; would do if you don’t care about latency or timing/timestamp precision, or a setting of 2 or 3 to change config.

Looking over this took me 14 minutes, so for up to ~16 minutes more paid advice,
help PsychPaidSupportAndServices. From there on extra hours support packages will be needed for any additional 60 minutes of help, at a price of 300 Euros per started hour, unless you are already eligible for discount.

-mario
[14 out of 0 minutes used]

Thank you mario for the suggestion!