Playing sound on key press (crashing)

OS: Win 10 x64
PsychoPy version** (e.g. 1.84.x): 2021.2.3

Hi All,

I’m creating a task where key presses play a sound. I have 5 keys mapped to 5 piano tones (saved as .wav). It works fairly well on my laptop by I’m having some issues on another PC. The notes will play at first but if you play too fast it quickly crashes. Note: The experiment doesn’t actually crash but the sounds stop playing, and pressing the keys plays the Windows error sound instead. I’m not sure if this is a resources issue or a coding issue. But I was hoping someone could tell me if the code I’m using is the most efficient way of doing this, in case it is a computational resources problem.

In a Begin Experiment tab I have this code to (hopefully) use the ptb library and to preload my sounds.

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

c = sound.Sound('c_v120.wav')
d = sound.Sound('d_v120.wav')
e = sound.Sound('e_v120.wav')
f = sound.Sound('f_v120.wav')
g = sound.Sound('g_v120.wav')

Would this be better in a Before Experiment tab?

Then in an Each Frame tab I have the following code:

keys = event.getKeys()
if keys:

    if 'b' in keys:
    if '7' in keys:
    if '8' in keys:
    if '9' in keys:
    if 'p' in keys:

Again, this works fine on my laptop (I can play the notes as fast as I want). But crashes on another PC. Am I importing ptb the right way? Because I know my laptop is set to use ptb by default but the other PC isn’t, so that could be a problem.
Or could that PC just not be capable of doing too many things at once? Appreciate any ideas or even just tips to improve the code!

Side Bug Ques: When I use the sound code above and create a routine that continues by keypress or button press, the press (ending the routine) plays all of those sounds at once and I can’t think of why that would happen.