Various crashes of experiment when sound files are included

Hi all,

I´ve been experiencing various problems with playing sound files in PsychoPy (Coder), depending on the system on which the experiment is running and on the PsychoPy version.

The script per se works without any problems but as soon as I include sound files, the experiment crashes (on some machines). I´ve been running my experiments on PsychoPy 1.84.0 so far and on the windows10 machine on which I programmed the current experiment, it works perfectly fine even with sound files included. However, there are times when I have to restart the program several times until it works and - although everything works - the following error message is shown in the output window:

pyo version 0.8.0 (uses single precision)
portaudio error in Pa_OpenStream: Unanticipated host error
Portaudio error: Unanticipated host errorportaudio error in Pa_AbortStream: Invalid stream pointer
portaudio error in Pa_CloseStream: Invalid stream pointer
Server not booted.
The Server must be booted!

When I tried to run the same experiment with another windows machine (win7) but the same PsychoPy version (v1.84.0), the experiment ran a couple of times but suddenly, without changing anything, it crashed as soon as the run button was pressed (producing a windows error message: “pythonw.exe has stopped working”). At other times, only the experiment crashes but not PsychoPy, with the error message “Couldn´t measure a consistent frame rate. Is your graphics card set to sync to vertical blank? Are you running other processes on your computer?” (As recommended here: https://groups.google.com/forum/#!topic/psychopy-users/FYVH1uoU4kE, I tried the coder demo “TimeByFrames.py” - I´ve also attached the results to this post).

On another windows machine (win 10, PsychoPy v1.84.0), the experiment breaks with the error message “Attribute error: No audio API found. Try installing pyo 0.6.8+, or pygame 1.8+”
(in the PsychoPy Preferences, there are pyo and pygame listed in the audio library (in this order, but I´ve already tried out setting pygame before pyo and it doesn´t work either) and all settings are equal to those on which the experiment works.

Lastly, I´ve tried out different other versions of PsychoPy but all of them led to different error messages (see attachments), amongst others (v1.85.6) a unicode decode error which does not make any sense to me, as the script has been working fine for years and I cannot imagine that “encSound” causes a unicode error?
With v1.90.2, the output window says "Attribute Error: OrderedDict obect has no attribute ‘TrialNum’ ". Again, there has never been a problem with a missing TrialNum attribute so far …

So by now, I´m more than confused and would be very grateful if somebody could help me find the one problem that leads to all these different error messages and crashes…
Regarding the sound components within my code, it looks as follows:

from psychopy import locale_setup, visual, core, data, event, logging, sound, gui, parallel
[…]
encSound = sound.Sound()
[…]
for thisEncoding in encoding:
[…]
encComponents.append(encSound)
encSound.setSound(soundpath)
if t >= 1.0 and encSound.status == NOT_STARTED:
encSound.tStart = t
encSound.frameNStart = frameN
encSound.play()
if encSound.status == STARTED and t >= (1.0 + (8.5-win.monitorFramePeriod*0.75)):
encSound.stop()

Thank you for your help in advance!


What kinds of sound files are you trying to play?

My suspicion is that this is part of the broader category of problems I keep running into that I call “Pyo crashes everything”. If it’s pyo specifically that’s causing you trouble, you could try the following at the top of your script.

from psychopy import prefs
prefs.general['audioLib'] = ['pygame']
from psychopy import sound

This will force your code to use pygame and not pyo. However I suspect you’ll still get some issues because pygame sound isn’t exactly reliable either. You can force it to use only pyo the same way (just replace ‘pygame’ with ‘pyo’) and at least figure out if one or the other is more reliable.

Thank you for your prompt reply! I tried what you recommended, first with pygame, then pyo, and finally pysoundcard.

  • pygame:

AttributeError: No audio API found. Try installing pyo 0.6.8+, or pygame 1.8+

  • pyo:
    runs perfectly as before on my machine (but not on the others), again with the error message:

pyo version 0.8.0 (uses single precision)
portaudio error in Pa_OpenStream: Unanticipated host error
Portaudio error: Unanticipated host errorportaudio error in Pa_AbortStream: Invalid stream pointer
portaudio error in Pa_CloseStream: Invalid stream pointer

Server not booted.
The Server must be booted!

So I guess what happened before is that pyo was selected automatically.

  • pysoundcard: The experiment doesn´t crash but the audios sound as if Darth Vader himself had read the sentences …

Regarding the audio files: I´m using .wav files of about 400-500 kb.

Really appreciate your help, thanks again!

Hi,

I had a similar problem at some point (on psychopy1.81). The problem was greatly resolved by installing pygame separately (download pygame.whl to your pc, Open Cmd.exe, type “python -m pip install pathToPygame.whl”). Then you can use the code proposed by Jonathan to force to use pygame.

I also had a wait time after the sound for 200 ms or so (but my files were super short).

Hope that helps.

Hi,

pyo 0.8.0 is very old (current is 0.9.0), I’d recommend to update since lot of work have been done to improve reliability on Windows.

The error from portaudio means either the soundcard is already used by another application or that there is a mismatch between pyo’s sampling rate or number of channels and the system’s settings. An easy solution is to switch pyo’s backend driver to “directsound” (defaults to WASAPI). In psychopy preferences, select “Primary Sound Driver” as the audio device. That should be enough.

Hi there,

thanks for your help! I managed to manually install pygame as you recommended but it only worked with the PsychoPy 1.90.+ versions. However, now I cannot try if the sounds work, as with v1.90.2, the experiment crashes as soon as the .csv list referring to the sound files is loaded (using the TrialHandler), leading to the following error:

File “”, line 1, in
AttributeError: ‘OrderedDict’ object has no attribute ‘trialNum’

…, where trialNum is the first variable/column in the .csv file. This error message appears with all kinds of csv files, so it´s not a specific error in the file.
This has never happened with any other PsychoPy version … Does anyone have an idea how to solve this issue? I know this question has already been asked by others but none of the recommendations has led to a solution in my case.
Thanks!

Hi,
thanks, this would explain the portaudio error. It might be a stupid question but how would I switch pyo´s backend driver? Sorry, I haven´t run into these problems before …
Interestingly, this portaudio error occurs only with the combination of PsychoPy version and hardware with which it is (in the majority of cases) working anyway. But maybe it would make the program at least more stable…
Thanks for your help!

Hi,
In PsychoPy’s preferences, you should be able to select “Primary Sound Driver” as the audio device.
Olivier

Ah, ok, I´ve already tried that. I thought switching the backend driver would require an additional step but it´s the same as what you described in the following sentence.
Worked, but only on one machine again …

I finally found a solution to solve the problem at least at one computer. I installed v1.90.0, copied the pygame folder, then installed v1.84.0, pasted the pygame folder to the path in which it was in the 1.90.0 version, then did what mna suggested, forced PsychoPy to use pygame at the beginning of the code as jonathan.kominsky suggested, and finally, somehow, it worked.
Thanks everyone!

For

AttributeError: ‘OrderedDict’ object has no attribute ‘trialNum’

see if this helps: