Changing the sampling rate of audio

So I’m trying to play an audio file that has a sampling rate of 16kHz. I use the following code to set up the audio:

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

sound_names = ['01_16kHz.wav']

audio = sound.Sound()

When I want to play the audio I use the following code:

current_sound = sound_names.pop()
audio.setSound(current_sound)
audio.play()

I get the following error:

  File "C:\Program Files (x86)\PsychoPy3\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 365, in setSound
    blockSize=self.blockSize)
  File "C:\Program Files (x86)\PsychoPy3\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 96, in getStream
    blockSize=blockSize)
  File "C:\Program Files (x86)\PsychoPy3\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 135, in _getStream
    .format(label, list(self.keys())[0], sys.platform)
psychopy.exceptions.SoundFormatError: Tried to create audio stream 16000_1_128 but 44100_1_128 already exists and win32 doesn't support multiple portaudio streams

I don’t have any other audio files in the directory, just the 16kHz one. How can I change the stream to be 16kHz instead of 44.1kHz?

Hi @LelBartha , looks like you are using sound_device backend rather than PsychToolBox. Do you have a 32bit windows machine? If so, you cannot run the low latency PsychToolBox backend. You could:

  • convert all sounds used to a sampling rate of 44100.
  • Try audio = sound.Sound(sampleRate=16000)

Your the second point of your solution worked, thank you! How do you use the PsychToolBox back end? I’m not sure how to and I have a 64bit machine.

If you go to File > Preferences and set the order of your audio libs so PTB is first, this should prioritise PTB as your preferred library. Then, you can change the latency mode if required (although level 3 (aggressive low latency) is best for Windows). IF PTB cannot be loaded, you will receive a warning in the Coder output panel.

For some reason, there is no option to choose PTB. Says the only valid options are sounddevice, pyo, pysoundcard, and pygame.

You can add ‘PTB’ to the list manually, e.g., ['PTB', 'sounddevice'], and if it fails then PTB is not installed, perhaps because you have the 32bit version of PsychoPy installed.