| Reference | Downloads | Github

Audio Crashing on Ubuntu

Hi all,

I’m trying to run a bunch of different audio experiments but the audio keeps crashing. I’ve tried all the audio libraries but none are seeming to work. I’m running these experiments on this razer laptop installed with Ubuntu 16.04. I’ve tried Psychopy versions 3.2.4, 2020.1 and 2020.2.10. Below are some of the outputs I get from when they crash

sounddevice sound library:

# When experiment is running and it crashes in the middle
Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1735
Expression 'AlsaOpen( &alsaApi->baseHostApiRep, params, streamDir, &self->pcm )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1902
Expression 'PaAlsaStreamComponent_Initialize( &self->playback, alsaApi, outParams, StreamDirection_Out, NULL != callback )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2170
Expression 'PaAlsaStream_Initialize( stream, alsaHostApi, inputParameters, outputParameters, sampleRate, framesPerBuffer, callback, streamFlags, userData )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2835

# One instance when trying to launch experiment
sounddevice.PortAudioError: Error opening OutputStream: Device unavailable [PaErrorCode -9985]

# Another instance of trying to launch the experiment
Hello from the pygame community.
Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2043
Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2717
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2838
Traceback (most recent call last):
  File "/home/hbml/Documents/Tasks/UltimateLocalizer/", line 31, in <module>
  File "/home/hbml/Documents/Tasks/UltimateLocalizer/Tasks/AuditoryLocalizer/", line 94, in run
    soundStim = sound.Sound(soundmat.T, sampleRate=fs, stereo=True)
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/psychopy/sound/", line 318, in __init__
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/psychopy/sound/", line 365, in setSound
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/psychopy/sound/", line 96, in getStream
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/psychopy/sound/", line 140, in _getStream
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/psychopy/sound/", line 171, in __init__
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/", line 1455, in __init__
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/", line 861, in __init__
    'Error opening {0}'.format(self.__class__.__name__))
  File "/home/hbml/anaconda3/envs/psychopy/lib/python3.6/site-packages/", line 2653, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening OutputStream: Invalid sample rate [PaErrorCode -9997]

PTB sound library:

# Trying to launch the experiment
Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1735
Expression 'AlsaOpen( hostApi, parameters, streamDir, &pcm )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1767
PTB-ERROR: Failed to open audio device -1. PortAudio reports this error: Device unavailable 
PTB-ERROR: Could not open audio device, most likely because it is already in use by a previous call to
PTB-ERROR: PsychPortAudio('Open', ...). You can open each audio device only once per session. If you need
PTB-ERROR: multiple independent devices simulated on one physical audio device, look into use of audio
PTB-ERROR: slave devices. See help for this by typing 'PsychPortAudio OpenSlave?'.
Error in function Open: 	Usage error
Audio device unavailable. Most likely tried to open device multiple times.
PsychPortAudio:Open: Usage:

The other two libraries, pyo and pygame, don’t work at all. However Psychtoolbox in Matlab works just fine and supplies this message which may help:

PTB-INFO: Using modified PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4
PTB-INFO: Choosing deviceIndex 0 [HDA Intel PCH: ALC256 Analog (hw:0,0)] as default output audio device.
PTB-INFO: New audio device -1 with handle 0 opened as PortAudio stream:
PTB-INFO: For 2 channels Playback: Audio subsystem is ALSA, Audio device name is HDA Intel PCH: ALC256 Analog (hw:0,0)
PTB-INFO: Real samplerate 44100.000000 Hz. Input latency 0.000000 msecs, Output latency 4.988662 msecs.

Any ideas what could be the problem? I’m thinking it has something to do with a linux library (namely Alsa) but am not sure and don’t want to mess with any linux libraries and further complicate things. Any advice is appreciated.

Have a good day,


did you ever solve this problem?

Not really. Oddly what was working was Psychtoolbox and it sort of worked (I forget now) when I called the Psychtoolbox audio library in python like this:

from psychtoolbox import audio, GetSecs

stream = audio.Stream(freq=48000, channels=2,latency_class=3)

stream.fill_buffer(audStream) # audStream is a numpy array here

soundOnset = stream.start(wait_for_start=1)

Hope this helps,


thanks! I’ll play around with this.

in case anyone else sees this, I’m having a similar weird problem where I can play tones (like a Sound(300) situation with psychtoolbox) but when I try to play an audio file (Sound('mytone.wav')) I get the same errors as above.