Issues related to sounddevice backend in 1.85.0

Hi all,

I’ve started testing the new pre-lease version hoping the newly implemented sounddevice backend will help deliver audio stimuli with shorter latencies.

I’m testing it both on a Windows 7 machine and various Mac OSX machines (running 10.11 and 10.12) using their respective standalone versions.

On MacOSX I’m running into major problems that seem to be connected to the sounddevice library - see the error message below triggered by from psychopy import sound .
Do I need to download/install any additional libraries/modules to run this PsychoPy version? Manually installing sounddevice via pip didn’t help with the PsychoPy issue. I can now use the sounddevice library without problems outside of Psychopy (using Python3.6 via Anaconda).

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/sound/__init__.py", line 72, in <module>
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/psychopy/sound/backend_sounddevice.py", line 12, in <module>
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/sounddevice/__init__.py", line 1, in <module>
  File "/Applications/PsychoPy2.app/Contents/Resources/lib/python2.7/sounddevice/sounddevice.py", line 227, in <module>
  File "cffi/api.pyc", line 105, in cdef
  File "cffi/api.pyc", line 119, in _cdef
  File "cffi/cparser.pyc", line 299, in parse
  File "cffi/cparser.pyc", line 304, in _internal_parse
  File "cffi/cparser.pyc", line 260, in _parse
  File "cffi/cparser.pyc", line 40, in _get_parser
  File "pycparser/c_parser.pyc", line 116, in __init__
  File "pycparser/ply/yacc.pyc", line 3293, in yacc
  File "pycparser/ply/yacc.pyc", line 2938, in validate_all
  File "pycparser/ply/yacc.pyc", line 2982, in validate_modules
  File "inspect.pyc", line 690, in getsourcelines
  File "inspect.pyc", line 538, in findsource
IOError: could not get source code 

Using Windows 7 I was actually able to get PsychoPy to run using the new sounddevice library, however here I ran into a different issue which seems to be related to the new way in which the audio stream is initiated.

Here, using a previously tested script I replaced the pyo backend with sounddevice and got the following error messages when trying to play a dichotic (2-channel) audio stimulus:

From cffi callback <function callback_wrapper at 0x1409E1F0>:
Traceback (most recent call last):
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\sounddevice\sounddevice.py", line 1447, in callback_wrapper
    return _wrap_callback(callback, data, frames, time, status)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\sounddevice\sounddevice.py", line 2361, in _wrap_callback
    callback(*args)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.85.0rc3-py2.7.egg\psychopy\sound\backend_sounddevice.py", line 184, in callback
    toSpk[:len(dat), :] += dat  # add to out stream
ValueError: non-broadcastable output operand with shape (128,1) doesn't match the broadcast shape (128,2)

Any ideas how to go about this issue? Do I need to setup/initiate the out stream before I can present a stimulus using sound.play( ) ?

Any help regarding either of the two issues is greatly appreciated!
Sarah

Thanks for trying it out Sarah. For the OS X error I can see the same problem and already trying to work out how to fix it.

For the windows broadcasting error I wonder if you could provide a minimal script (or sound file) for me to test. I imagine you’re trying something I haven’t tried yet like a mono sound followed by a dichotic sound or vice-versa? Are you using arrays or sound files?

cheers

Hi Jon,

thanks for your reply.

Regarding the broadcasting error, I can provide one/several of my stimuli but I’m not authorised to attach a .wav file. The corresponding script is fairly complex but I’m happy to send it to you if it helps. In this particular experiment I’m presenting only dichotic sounds (one per trial) and I’m playing them from .wav sound files.

Here are some parts of the script that may be relevant:

# Initialize components for Routine "sound"
soundClock = core.Clock()
ISI3 = core.StaticPeriod(screenHz=expInfo['frameRate'], name='ISI3')
fixcross = visual.TextStim(win=win, name='fixcross',text=u'+',
    font=u'Arial',pos=(0, 0), height=60, color=u'black')
stimulus = sound.Sound('A', secs=-1)
stimulus.setVolume(volume_level) 

Then later, the sound file is set during a static period and then played as a trigger is sent to the EEG:

       # *ISI3* period
        if frameN >= 0 and ISI3.status == NOT_STARTED:
            # keep track of start time/frame for later
            ISI3.tStart = t
            ISI3.frameNStart = frameN  # exact frame index
            ISI3.start(30*frameDur)
        elif ISI3.status == STARTED:  # one frame should pass before updating params and completing
            # updating other components during *ISI1*
            stimulus.setSound(stim, secs=stimulus.getDuration())
            # component updates done
            ISI3.complete()  # finish the static period
       
        # *fixcross* updates
        if frameN >= 1 and fixcross.status == NOT_STARTED:
            # keep track of start time/frame for later
            fixcross.tStart = t
            fixcross.frameNStart = frameN  # exact frame index
            fixcross.setAutoDraw(True)
        if fixcross.status == STARTED and t >= (1.0 + (fixcross.tStart + stimulus.getDuration())):
            fixcross.setAutoDraw(False)
            
        # start/stop stimulus
        if frameN >= 32 and stimulus.status == NOT_STARTED:
            # keep track of start time/frame for later
            stimulus.tStart = t
            stimulus.frameNStart = frameN  # exact frame index
            win.callOnFlip(sendTrigger, int(cond_code))
            stimulus.play()  # start the sound (it finishes automatically)

Thanks for your work on this - I’m very excited about the changes to the sound module.
Sarah

The first error (IOError: could not get source code) looks very much like this:

https://github.com/spatialaudio/python-sounddevice/issues/68

… which led to this:

https://github.com/eliben/pycparser/issues/160

… and was fixed with this:

https://github.com/eliben/pycparser/pull/161

However, there hasn’t been a pycparser release since then, but it should (hopefully!) work with the master version.

I think I might be able to get around it by making sure that the py files are included for py coarser target than just the pyc files

Hello all,

This is a different topic than above, but it still falls under “issues with sounddevice” so I thought this might be a good thread. I should also mention that I use PsychoPy 1.85.1, but the issue is the same as with 1.85.0.

My experiment involves pure sounds that need to be changed adaptability depending on participant input, so generate the sounds like this:

from psychopy import prefs
prefs.general['audioLib'] = ['sounddevice']

from psychopy import sound

low_tone = sound.Sound(value=600, secs = 0.2)
high_tone = sound.Sound(value=600 + float(comp_tone), secs = 0.2)

high_tone.play() 

Unfortunately, there are prominent clicks / pop sounds at the beginning and the end of it. I have read that this may be caused by a lack of zero-crossings at the edges, but I don’t know how to change that.

If I play a pre-generated wave file with the same specifications (e.g., from here) the sound is smooth. The problem is also not present when I use pyo to generate the sound, but unfortunately pyo does not provide the accurate timing I need for my experiment.

Any ideas how I can avoid those initial and final clicks?
Thank you very much for your help!

Hi Jon.
I have also had the same problem as above running psychopy with the SoundDevice in Windows 10.
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\sounddevice.py”, line 1008, in callback_ptr
return _wrap_callback(callback, data, frames, time, status)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\sounddevice.py”, line 2773, in _wrap_callback
callback(*args)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\sound\backend_sounddevice.py”, line 184, in callback
toSpk[:len(dat), :] += dat # add to out stream
ValueError: non-broadcastable output operand with shape (128,1) doesn’t match the broadcast shape (128,2)

Did you find a specific solution? I am quite new to psychopy however i have used 32bit wav files through a .xlsx spreadsheet with two conditions columns.
These have then been placed in a loop around the two sound conditions with looped questionaires inbetween.
I have also used $(name of column in .xlsx) to specify the list of stimuli to be played in both sound conditions.

The script runs, but the sound files are not producing any sound.

Cheers Tobias

Could you try the brand new 1.90.0 where @dvbridges has made a number of fixes to the audio outputs from sounddevice?

Hi Jon,
I tested MovieStim3 in 1.90.0 and the similar error of sounddevice occured.
It works without any problems with my audio files. e.g.,

# this works
astims = sound.Sound(sound_path, sampleRate=44100, stereo=True)

But using MovieStim3 the sound was not playing and the error occured.
This error also caused problems for the following sounds. That is, sounds before the MovieStim3 played smoothly but sounds after it did not play.

From cffi callback <function callback_ptr at 0x0000000017FBABA8>:
Traceback (most recent call last):
  File "E:\Anaconda3\envs\psychopy19\lib\site-packages\sounddevice.py", line 740, in callback_ptr
    return _wrap_callback(callback, data, frames, time, status)
  File "E:\Anaconda3\envs\psychopy19\lib\site-packages\sounddevice.py", line 2515, in _wrap_callback
    callback(*args)
  File "E:\Anaconda3\envs\psychopy19\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 194, in callback
    toSpk[:len(dat), :] += dat  # add to out stream
ValueError: non-broadcastable output operand with shape (128,1) doesn't match the broadcast shape (128,2)

I’m using Python 2.7 via Anaconda.

Updates:
After I made several attempts to solve it, it suddenly works (!) and I have no idea why :woman_facepalming:
I literally did nothing more than updating some packages.
MovieStim3 magically works for me. My packages are as follows:

# Name                    Version                   Build  Channel
asn1crypto                0.24.0                   py27_0    conda-forge
attrs                     17.4.0                     py_0    conda-forge
autopep8                  1.3.3                      py_0    conda-forge
backports                 1.0              py27h6492d98_1  
backports.functools_lru_cache 1.5                      py27_1  
backports.shutil_get_terminal_size 1.0.0                      py_3    conda-forge
backports_abc             0.5              py27h0ec6b72_0  
bzip2                     1.0.6                     vc9_1  [vc9]  conda-forge
ca-certificates           2018.1.18                     0    conda-forge
certifi                   2018.1.18                py27_0    conda-forge
cffi                      1.11.5                   py27_0    conda-forge
chardet                   3.0.4                    py27_0    conda-forge
colorama                  0.3.9                    py27_0    conda-forge
configobj                 5.0.6                    py27_0  
configparser              3.5.0                    py27_0    conda-forge
coverage                  4.5.1                    py27_0    conda-forge
cryptography              2.2.1                    py27_0    conda-forge
cycler                    0.10.0           py27h59acbbf_0  
decorator                 4.0.11                   py27_0    conda-forge
egi                       0.9.0                     <pip>
enum34                    1.1.6                    py27_1    conda-forge
et_xmlfile                1.0.1                    py27_0    conda-forge
ffmpeg                    3.4.2                         0    conda-forge
flake8                    3.5.0                    py27_0    conda-forge
flake8-docstrings         1.3.0                      py_1    conda-forge
flake8-polyfill           1.0.2                      py_0    conda-forge
freetype                  2.8.1                     vc9_0  [vc9]  conda-forge
funcsigs                  1.0.2                      py_2    conda-forge
functools32               3.2.3.2          py27h0cdbcdb_1  
future                    0.16.0                    <pip>
futures                   3.2.0            py27h8b2aecd_0  
gevent                    1.3a1                    py27_0    conda-forge
greenlet                  0.4.13                   py27_0    conda-forge
hdf5                      1.10.1                    vc9_2  [vc9]  conda-forge
icc_rt                    2017.0.4             h97af966_0  
icu                       58.2                      vc9_0  [vc9]  conda-forge
idna                      2.6                      py27_1    conda-forge
imageio                   2.1.2                    py27_0    conda-forge
intel-openmp              2018.0.0                      8  
iolabs                    3.2                      py27_0    conda-forge
ipaddress                 1.0.18                   py27_0    conda-forge
ipykernel                 4.8.2                    py27_0    conda-forge
ipyparallel               6.1.1                    py27_1    conda-forge
ipython                   5.5.0                    py27_0    conda-forge
ipython_genutils          0.2.0                    py27_0    conda-forge
jdcal                     1.3                      py27_0    conda-forge
jedi                      0.11.1                   py27_0    conda-forge
jpeg                      9b                        vc9_2  [vc9]  conda-forge
json-tricks               3.11.3                    <pip>
jupyter_client            5.2.3                    py27_0    conda-forge
jupyter_core              4.4.0                      py_0    conda-forge
kiwisolver                1.0.1            py27h9815e84_0  
libiconv                  1.15                      vc9_0  [vc9]  conda-forge
libpng                    1.6.34                    vc9_0  [vc9]  conda-forge
libtiff                   4.0.9                     vc9_0  [vc9]  conda-forge
libwebp                   0.5.2                     vc9_7  [vc9]  conda-forge
libxml2                   2.9.5                     vc9_2  [vc9]  conda-forge
libxslt                   1.1.32                    vc9_0  [vc9]  conda-forge
lxml                      4.2.1                    py27_0    conda-forge
matplotlib                2.2.2            py27h8803d4e_0  
mccabe                    0.6.1                    py27_0    conda-forge
mkl                       2018.0.2                      1  
more-itertools            4.1.0                      py_0    conda-forge
moviepy                   0.2.3.2                  py27_0    conda-forge
msgpack-python            0.5.5                    py27_0    conda-forge
numexpr                   2.6.4                    py27_1    conda-forge
numpy                     1.13.3           py27hda5cba4_3  
olefile                   0.45.1                   py27_0  
opencv                    3.2.0             np113py27_204    conda-forge
openpyxl                  2.5.1                    py27_0    conda-forge
openssl                   1.0.2n                    vc9_0  [vc9]  conda-forge
pandas                    0.22.0           py27hc56fc5f_0  
parso                     0.1.1                      py_0    conda-forge
pathlib2                  2.3.0                    py27_0    conda-forge
patsy                     0.5.0                    py27_0    conda-forge
pickleshare               0.7.4                    py27_0    conda-forge
pillow                    5.0.0            py27h901f87c_0  
pip                       9.0.1                    py27_5  
pluggy                    0.6.0                      py_0    conda-forge
portaudio                 19.6.0                        0    conda-forge
prompt_toolkit            1.0.15                   py27_0    conda-forge
psutil                    5.4.3                    py27_0    conda-forge
PsychoPy                  1.90.0                    <pip>
psychopy-ext              0.6.0.4                   <pip>
py                        1.5.3                      py_0    conda-forge
pycodestyle               2.3.1                    py27_0    conda-forge
pycparser                 2.18                     py27_0    conda-forge
pydocstyle                2.1.1                    py27_0    conda-forge
pyflakes                  1.6.0                    py27_0    conda-forge
pygame                    1.9.3                     <pip>
pyglet                    1.2.4                    py27_0    conda-forge
pygments                  2.2.0                    py27_0    conda-forge
pyopengl                  3.1.1a1             np113py27_0  
pyopenssl                 17.5.0                   py27_0    conda-forge
pyosf                     1.0.4                    py27_0    conda-forge
pyparallel                0.2.2                     <pip>
pyparsing                 2.2.0            py27hc7d9fa6_1  
pypiwin32                 223                       <pip>
pyqt                      5.6.0                    py27_4    conda-forge
pyserial                  3.4                      py27_0    conda-forge
pysocks                   1.6.8                    py27_1    conda-forge
PySoundCard               0.5.2                     <pip>
PySoundFile               0.9.0.post1               <pip>
pytables                  3.4.2                    py27_7    conda-forge
pytest                    3.5.0                    py27_0    conda-forge
python                    2.7.14              h4a10d90_30  
python-bidi               0.4.0                     <pip>
python-dateutil           2.7.0                    py27_0  
python-sounddevice        0.3.10                     py_0    conda-forge
python-vlc                3.0.102                   <pip>
pytz                      2018.3                   py27_0  
pywin32                   223                       <pip>
pyxid                     1.0                      py27_0    conda-forge
pyyaml                    3.12                     py27_1    conda-forge
pyzmq                     17.0.0                   py27_3    conda-forge
qt                        5.6.2                     vc9_1  [vc9]  conda-forge
requests                  2.18.4                   py27_1    conda-forge
scandir                   1.7                      py27_0    conda-forge
scipy                     1.0.0            py27h7cd1477_0  
seaborn                   0.8.1                    py27_0    conda-forge
setuptools                39.0.1                   py27_0    conda-forge
simplegeneric             0.8.1                    py27_0    conda-forge
singledispatch            3.4.0.3          py27h3f9d112_0  
sip                       4.18                     py27_1    conda-forge
six                       1.11.0           py27ha5e1701_1  
snowballstemmer           1.2.1                    py27_0    conda-forge
SoundFile                 0.10.1                    <pip>
sqlite                    3.22.0                    vc9_0  [vc9]  conda-forge
statsmodels               0.8.0                    py27_0    conda-forge
tk                        8.6.7                     vc9_0  [vc9]  conda-forge
tobii-research            1.3.1                     <pip>
tornado                   5.0                      py27_0  
tqdm                      4.11.2                   py27_0    conda-forge
traitlets                 4.3.2                    py27_0    conda-forge
urllib3                   1.22                     py27_0    conda-forge
vc                        9                    h7299396_1  
vs2008_runtime            9.00.30729.1         hfaea7d5_1  
wcwidth                   0.1.7                    py27_0    conda-forge
wheel                     0.30.0           py27ha643586_1  
win_inet_pton             1.0.1                    py27_1    conda-forge
win_unicode_console       0.5                      py27_0    conda-forge
wincertstore              0.2              py27hf04cefb_0  
wxpython                  4.0.0rc1.dev3440+0f9b36e          py27_0    conda-forge
xlrd                      1.1.0                      py_2    conda-forge
yaml                      0.1.7                     vc9_0  [vc9]  conda-forge
zlib                      1.2.11                    vc9_0  [vc9]  conda-forge
zmq                       0.0.0                     <pip>

@yh-luo your problem looks different. That looks like a problem clash between stereo and mono sounds. e.g. a stereo sound was found but a mono sound was expected, or vice versa.

Hi,
I have a similar problem. When I try to play the .wav files from a excel list the error “ValueError: non-broadcastable output operand with shape (128,1) doesn’t match the broadcast shape (128,2)” accurs. This does not happen if I put the wav files directly in the Program.

this Problem stated here is very similar to mine. (I already use Psychopy1.90.1)

Thanks so much in advance for your help!

1 Like

We have the same problem here. Using an .xlsx file to specify conditions and sound file results in the error mentioned by @Tobias_Bashevkin. Referring to the soundfiles directly, it works fine. Changing the audio library to pyo, there is no error message.

We are using Windows 10 and PsychoPy Version 1.90.2

The same problem is also described here:

Hello,

I have been struggling with a similar bug, receiving the same error. In my case, I have figured out that if my first trial has sound and no video, the program will crash with the aforementioned error when trying to play the video.

If I play a video in my first trial, no bug occurs.

I really don’t understand why, but at least I have found a work-around by always playing a video before an audio (might it be just a grey filler).

I use version 1.90.2 on Windows 10. This happens with the builder mode.

I hope this helps somehow,

William

Hi, from the issues you’ve mentioned, would you recommend using stereo over mono (has to be on 1.85.2)? I’m new to PsychoPy (and love it!). For an academic project I have to specify what I do before doing it… I’m not looking for help on it, just to forsee issues with audio not working before I commit to doing something on paper. From what I’ve read, mono can break. So, I’ve made my audio stimuli by converting stereo to mono (so the voice isn’t split over two speakers) and back to stereo so its the same thing coming out both. Thanks so much for your time. Ellie