Inquiry Regarding PsychPortAudio('Stop') Function

Dear Mario Kleiner,

I have a quick question regarding the PsychPortAudio('Stop') function. According to the help documentation, this function is supposed to return four output parameters: [audioStartTimeEnd, endPositionSecs, xruns, audioStopTime]. However, on my Apple M3 running macOS 14.7 (23H124), the parameters endPositionSecs and audioStopTime always return 0, which should reflect the stop time in seconds.

Below, I have included a snippet of my script that demonstrates the issue:

matlab

soundDevs = getOptimizedSoundDev(1, false);  
soundDevs.fs = soundDevs.DefaultSampleRate;  

estimatedMaxDur = 50 * 2.1; % 50 trials * 2.1 sec/trial  

pahandle = PsychPortAudio('Open', soundDevs.DeviceIndex, 2, 2, soundDevs.DefaultSampleRate, 1, estimatedMaxDur);  
PsychPortAudio('GetAudioData', pahandle, estimatedMaxDur); % Preparing the sound input buffer for capture  
recordedAudio = [];  
allAbsRecPos = [];  

% Start recording  
audioStartTime = PsychPortAudio('Start', pahandle, [], 0, 1);  
fprintf('%s\n', 'Starting recording for 5 seconds...');  
WaitSecs(5);  

% Stop recording  
audioStopSignTime1 = GetSecs;  
[audioStartTimeEnd, endPositionSecs, xruns, audioStopTime] = PsychPortAudio('Stop', pahandle, 1);  
audioStopSignTime2 = GetSecs;  

% Get audio data  
[tempAudioData, absRecPosition] = PsychPortAudio('GetAudioData', pahandle);  

if ~isempty(tempAudioData)  
    recordedAudio = [recordedAudio, tempAudioData];  
    % Print debug info  
    fprintf('Recorded audio size: %d, Temp audio data size: %d\n', size(recordedAudio, 2), size(tempAudioData, 2));  
    allAbsRecPos = [allAbsRecPos, absRecPosition];  
end  

% Close the audio device  
PsychPortAudio('Close', pahandle);  

save debug_temp;  

Could you please advise on how to obtain the endPositionSecs and audioStopTime parameters correctly instead of receiving 0?

Thank you for your assistance (My support authentication token is: e6946a5c-a341-4d64-a29e-23cac6b06d39).

Best regards,

Yang

Hi,

thanks for financially supporting us!

So, as the help text for PsychPortAudio Stop? correctly states, estStopTime = what your code calls audioStopTime is only the estimated stop time for audio playback, ie. time of audio offset.

The help text for endPositionSecs is wrong! endPositionSecs is also only the duration of played out sound during playback, but for a pure recording/capture session like in your code, it is not defined/always zero. For a combined playback+capture duplex session, it would represent duration of played out sound. I suspect I wanted to implement it for both playback and capture, wrote the help text accordingly, but then realized the ambiguity if one uses a audio-duplex session with simultaneous playback and capture, and dropped the idea, but forgot to change the help text. Sorry! I’ll have to think if this can be improved for capture, or if it is better to just change the help text to be accurate in future releases.

Anyway, status = PsychPortAudio('GetStatus', pahandle); to the rescue. It returns a status struct with information like the field RecordedSecs for what you expected in endPositionSecs, ie. duration of recorded sound, and the separate field PositionSecs for the duration of played back sound in a playback or duplex session.

There is no mechanism implemented for getting the estimated stop time of audio capture though, only for playback.

In general, when it comes to scheduling the timing, you’ll find that defining requested start/stop times etc. usually only affects playback, but not capture.

All this timestamp behaviour explained here is identical for all operating systems, not macOS specific.

Hope it helps
-mario