Audio working on jupyter notebook but not in script

OS: Windows 11
Psychopy version: ‘2024.2.1’

Hi,

I am trying to play a sound through headphones (all works when using the default speakers).
While the code below works fine and the sound plays when executed in a jupyter notebook, it produces an error when executed from a script (see below).
The conda environment is the same, the code is the same.

Any idea what could be the problem?

I have already resampled the sound to match the headphones sampling rate (and again the code works in jupyter notebook).

Thanks

Federica

from psychopy import prefs

prefs.hardware['audioLib'] = 'ptb'
prefs.hardware['audioDevice'] = 'Headphones (WH-CH710N)'

from psychopy import sound

mysound = sound.Sound("C:/Users/EEG/Downloads/scream_48kHz.wav", volume=0.1, loops=-1)
mysound.play()

Error when launching from script

PTB-ERROR: Failed to open audio device 7. PortAudio reports this error: Invalid device
PTB-ERROR: Desired audio parameters likely unsupported by audio device.
PTB-ERROR: This could be, e.g., due to an unsupported combination of audio sample rate, audio channel count/allocation, or audio sample format.
Error in function Open: Usage error
Failed to open PortAudio audio device due to unsupported combination of audio parameters.
PsychPortAudio:Open: Usage:

pahandle = PsychPortAudio(‘Open’ [, deviceid][, mode][, reqlatencyclass][, freq][, channels][, buffersize][, suggestedLatency][, selectchannels][, specialFlags=0]);
PTB-ERROR: Failed to open audio device -1. PortAudio reports this error: Invalid device
PTB-ERROR: Desired audio parameters likely unsupported by audio device.
PTB-ERROR: This could be, e.g., due to an unsupported combination of audio sample rate, audio channel count/allocation, or audio sample format.
Error in function Open: Usage error
Failed to open PortAudio audio device due to unsupported combination of audio parameters.
PsychPortAudio:Open: Usage:

pahandle = PsychPortAudio(‘Open’ [, deviceid][, mode][, reqlatencyclass][, freq][, channels][, buffersize][, suggestedLatency][, selectchannels][, specialFlags=0]);
Traceback (most recent call last):
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 228, in init
audio.Stream.init(self, device_id=deviceID, mode=mode+8,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychtoolbox\audio.py”, line 97, in init
self.handle = PsychPortAudio(‘Open’, device_id, mode,
Exception: Failed to open PortAudio audio device due to unsupported combination of audio parameters.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\EEG\Desktop\heart_n_breathing\test_sound.py”, line 8, in
mysound = sound.Sound(“C:/Users/EEG/Downloads/scream_48kHz.wav”, volume=0.1)
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 334, in init
self.setSound(value, secs=self.secs, octave=self.octave,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 423, in setSound
_SoundBase.setSound(self, value, secs, octave, hamming, log)
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound_base.py”, line 209, in setSound
self._setSndFromFile(p)
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 459, in _setSndFromFile
self._setSndFromArray(sndArr)
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 497, in _setSndFromArray
self.track = audio.Slave(self.stream.handle, data=self.sndArr,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 609, in stream
label, s = streams.getStream(sampleRate=self.sampleRate,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 141, in getStream
return self._getStream(sampleRate,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 188, in _getStream
self[label] = _MasterStream(sampleRate, channels, blockSize,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychopy\sound\backend_ptb.py”, line 249, in init
audio.Stream.init(self, mode=mode+8,
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychtoolbox\audio.py”, line 97, in init
self.handle = PsychPortAudio(‘Open’, device_id, mode,
Exception: Failed to open PortAudio audio device due to unsupported combination of audio parameters.
Exception ignored in: <function Stream.del at 0x000001D1A9DE7EB0>
Traceback (most recent call last):
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychtoolbox\audio.py”, line 236, in del
self.close()
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychtoolbox\audio.py”, line 146, in close
raise err
File “C:\Users\EEG\miniconda3\envs\mne_lsl\lib\site-packages\psychtoolbox\audio.py”, line 139, in close
PsychPortAudio(‘Close’, self.handle)
AttributeError: ‘_MasterStream’ object has no attribute ‘handle’

After further testing, it seems like the problem disappears when connecting the headset to the laptop via cable, as opposed to connecting via Bluetooth, (even when executing the code from a script).
I would still prefer to use it wirelessly, so I would appreciate any insights you might have on the issue above.

Thank you,

Federica