Problem with audio libraries

MacOS M1
Python 3.8.20
PsychoPy Version: 2024.2.1

I’m running into a problem generating sound no matter what sound library I use
sounddevice seems to work fine on its own, but not with PsychoPy
PTB doesn’t import on it’s own
I get errors using pyo and pygame too

has anyone else had this problem? maybe there is a compatibility issue with M1?

here is my code:


from psychopy import sound, core

from psychopy import prefs

prefs.hardware['audioLib'] = ['sounddevice']

sound_stim = sound.Sound(value='A', secs=1.0) # Play a tone

sound_stim.play()

core.wait(2)

here is the error:

Traceback (most recent call last):
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/hardware/manager.py", line 250, in addDevice
    device = cls(*args, **kwargs)
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/hardware/speaker.py", line 27, in __init__
    if index < 0 or index not in available_index:
TypeError: '<' not supported between instances of 'NoneType' and 'int'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "psynote2.py", line 6, in <module>
    sound_stim = sound.Sound(value='A', secs=1.0)  # Play a tone
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/sound/backend_pygame.py", line 156, in __init__
    self.speaker = self._parseSpeaker(speaker)
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/sound/_base.py", line 132, in _parseSpeaker
    device = DeviceManager.addDevice(
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/hardware/manager.py", line 253, in addDevice
    raise ManagedDeviceError(
psychopy.hardware.manager.ManagedDeviceError: '<' not supported between instances of 'NoneType' and 'int'
2.0940  ERROR   Error encountered while loading `psychopy-sounddevice`. Check logs for more information.
2.2622  WARNING         We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['PTB', 'sounddevice', 'pyo', 'pygame'] (in that order).

It looks like it’s trying to use pygame as its sound library instead, which is weird.

I’m assuming you have the sounddevice plugin installed (tools → plugins), though I think you would probably hit a different error if you didn’t.

What happens when you try to use PTB, exactly?

ty for replying Jonathan,

yes, I have it installed (psychopy-sounddevice)
but I keep getting this error:

2.0781  ERROR   Error encountered while loading `psychopy-sounddevice`. Check logs for more information.

I get this error when setting prefs.hardware[‘audioLib’] = [‘PTB’]:

Traceback (most recent call last):
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/hardware/manager.py", line 250, in addDevice
    device = cls(*args, **kwargs)
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/hardware/speaker.py", line 27, in __init__
    if index < 0 or index not in available_index:
TypeError: '<' not supported between instances of 'NoneType' and 'int'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "psynote2.py", line 7, in <module>
    sound_stim = sound.Sound(value=440, secs=1.0)
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/sound/backend_pygame.py", line 156, in __init__
    self.speaker = self._parseSpeaker(speaker)
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/sound/_base.py", line 132, in _parseSpeaker
    device = DeviceManager.addDevice(
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy/hardware/manager.py", line 253, in addDevice
    raise ManagedDeviceError(
psychopy.hardware.manager.ManagedDeviceError: '<' not supported between instances of 'NoneType' and 'int'
2.0175  ERROR   Error encountered while loading `psychopy-sounddevice`. Check logs for more information.
2.1838  WARNING         We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['PTB', 'sounddevice', 'pyo', 'pygame'] (in that order).

and I get this error when just running a script that imports psychtoolbox:

Traceback (most recent call last):
  File "psynote3.py", line 1, in <module>
    import psychtoolbox as ptb
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychtoolbox/__init__.py", line 28, in <module>
    from .PsychHID import PsychHID
ImportError: dlopen(/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychtoolbox/PsychHID.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '_libusb_bulk_transfer'

(I already have libusb installed which is mentioned in this error)

Are you running this all from the PsychoPy app or some kind of pip or conda installation?

I’m running into this issue when running the code in my own IDE, I installed the libraries with pip in a pyenv enviornment
I also have the PsychoPy app (it works fine there)

Right. This, or something like it, has come up a couple of times before.

It doesn’t look like there’s a proper solution for it unfortunately, but you could look at the discussion in those threads and see if there is anything helpful. As a general rule the easiest solution is to just run it out of the app if you can. It’s possible to add libraries to the app if there’s something that doesn’t come with PsychoPy that you need for an experiment.

1 Like

psychtoolbox
If you’re installing on Mac Silicon (ARM) then you must switch to intel mode python (ie use rosetta to mimic intel) in order to use psychtoolbox. It hasn’t been compiled for ARM. When you install standalone you get an intel-only python which is why it works

sounddevice
Not clear why this isn’t working but maybe similar issue (I’ve never tried installing sounddevice on an ARM python so I don’t know). You could try:

$ python 
>>> import sounddevice
>>> import soundfile
>>> import psychopy_sounddevice

and let us know what errors you get

1 Like

I ran just these imports and got this:

1.9257 	ERROR 	Support for the `sounddevice` audio backend is not available this session. Please install `psychopy-sounddevice` and restart the session to enable support.
2.0801 	WARNING 	We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['PTB', 'sounddevice', 'pyo', 'pygame'] (in that order).

and also reinstalled psychopy-sounddevice, same error

I get this error running my own code:

Traceback (most recent call last):
  File "psynote2.py", line 9, in <module>
    sound_stim = sound.Sound(value=440, secs=1.0)
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy_sounddevice/backend_sounddevice.py", line 328, in __init__
    self.setSound(value, secs=self.secs, octave=self.octave,
  File "/Users/delacroix/.pyenv/versions/3.8.20/envs/psyenv/lib/python3.8/site-packages/psychopy_sounddevice/backend_sounddevice.py", line 382, in setSound
    label, s = streams.getStream(sampleRate=self.sampleRate,
TypeError: cannot unpack non-iterable NoneType object

So there was no error import sounddevice and soundfile, but then an error message (with no traceback from importing psychopy_sounddevice)? I’m afraid I’m stumped in that case

ah, thank you either way. I guess I’ll just use the app for now