Audio stimuli changed in pitch?

This is a still a live cross-platform issue with standalone desktop versions, but I think I’ve identified the source of the problem.

  1. The PTB backend defaults to 48K sample rate, as you can see here at the _getDefaultSampleRate() method.

  2. When the builder compiles the sound elements of an experiment, it first creates a sound object with an “A” tone, and only later changes the sound to point to file specified in builder.

  3. Changing the file with the setSound() method doesn’t change the sample rate of the sound object.

  4. If your file is sampled at 44.1K, then it will be played back at the default 48K.

Here’s how to replicate the problem with the file provided by aisa2

from psychopy import prefs, logging
prefs.hardware['audioLib'] = ['ptb']

logging.console.setLevel(logging.DEBUG)  # get messages about the sound lib as it loads

from psychopy import sound, core

print('Using %s (with %s) for sounds' % (sound.audioLib, sound.audioDriver))

goose_sound = sound.Sound('A', stereo= True, hamming=True, secs=-1)
goose_sound.setSound("goose_sound.wav")
goose_sound.play() # play the sound

core.wait(2)
print('done')

core.quit()

My work around is to compile the experiment to a python script first, and add to every instance of sound.Sound(…) the option sampleRate=44100. E.g. for the above example, the following replacement gives the correct playback:

goose_sound = sound.Sound('A', stereo= True, hamming=True, secs=-1, sampleRate=44100)

This is of course super hacky and isn’t a long-term solution. Another option for people facing this issue or planning to use PTB going forward is to record all your audio at 48K. The ideal longer-term solution, I think, is to handle setSound() in a better way.

1 Like