PsychPortAudio freezes after first sound

Hello.
I experience an issue with PsychPortAudio on Ubuntu 22.04.4 LTS. I use the PTB 3.0.19.64715640, and PsychPortAudio 3.0.19.64657228.

When running an experiment that used to work fine on another system, PsychPortAudio freezes when filling the audio buffer. The first sound plays fine, but Matlab gets stuck on the second scheduling and I cannot even stop the script. Here’s an example that reproduces the issue:

%% compute sounds
channels = 1;
sampleFreq = 44100;
volume = 1;
durStimSec = 0.2;
d = PsychPortAudio('GetDevices');
for dev=1:length(d)
	if strcmp(d(dev).DeviceName, 'default')
		deviceId = d(dev).DeviceIndex;
		break;
	end
end
ppa = PsychPortAudio('Open', deviceId, 1, 0, sampleFreq, channels);
PsychPortAudio('Volume', ppa, volume);
sndBuf1 = rand(1,durStimSec*sampleFreq);
sndBuf2 = rand(1,durStimSec*sampleFreq);
%% schedule sound 1
PsychPortAudio('FillBuffer', ppa, sndBuf1);
sndStartTime1 = PsychPortAudio('Start', ppa, 1, 0, 0);
pause(1);
%% schedule sound 2
PsychPortAudio('FillBuffer', ppa, sndBuf2);
sndStartTime2 = PsychPortAudio('Start', ppa, 1, 0, 0);
PsychPortAudio('Close', ppa);

Here Matlab gets stuck on PsychPortAudio('FillBuffer', ppa, sndBuf2);. Am I doing anything wrong? The same code used to work without problems on a different setup.
Thanks.

I can reproduce your problem on Ubuntu 22.04 with latest PTB. As a workaround, it worked to use the “sysdefault” device (which PsychPortAudio would select here if no device id was specified) instead of “default”.

The “pulse” device apparently has the same problem. Apt-installing the pipewire-pulse package also seemed to fix the problem, but there may be side effects that I do not see (so not a recommendation). I suspect the problem lies somewhere in the pulse vs. pipewire conundrum.

Ok thanks for the confirmation. I should have mentioned that on the other setup I was using ‘sysdefault’ instead of ‘default’. On this new setup, ‘sysdefault’ is not listed as one of the available devices when calling PsychPortAudio('GetDevices'), and I assumed that ‘default’ was the same.

Following your suggestion, I called PPA without specifying a DeviceId (PsychPortAudio('Open', [], ...) and this fixed the problem. I don’t know anything about the underlying libraries to play sounds so I’m afraid I can’t help much further to identify the source of the problem. But at least I can run my code now, so thanks.

Not specifying an index is the best choice unless you have a need to specify a specific one. By itself, PsychPortAudio will select a low-level hardware device for exclusive use for lowest latency and best timing precision. It will suspend Pulseaudio audio devices to get low-level access.

The ‘pulse’ device freezes therefore, because you’d try to play back via a Pulseaudio backed device, which has been suspended. ‘default’, and I think ‘sysdefault’, often/usually also map to ‘pulse’, so the same freeze happens.

Ubuntu 22.04-LTS and earlier use Pulseaudio by default, so this is the logic.
Ubuntu 24.04-LTS has switched to Pipewire, so the ‘default’/‘sysdefault’ likely maps to a logical Pipewire device, which is not suspended by our current driver code, so things may not freeze. Otoh. this means that the actual exclusive low-level access, that PsychPortAudio desires for optimal timing and control, may fail if some sound was played within a few seconds before PsychPortAudio(‘Open’, …), e.g., via GStreamer movie playback or some other sound application on the system.

There are loose ends here. Some general modernization of this stuff didn’t progress fast enough in the last two years due to the lack of available work time due to lack of financial funding. If the future new business model works, and we’d have a sane funding source, I could pick up work on this again at some point.