psychopy.org | Reference | Downloads | Github

Proper way to loop audio signals

Hey there,

What I want: to loop a 1-second ‘A’ tone plus a 3-second silence interval for five iterations.

What I’ve done:

# set audio engine
from psychopy import prefs
prefs.hardware['audioLib'] = ['PTB']

# import modules
from psychopy import sound, visual

# check audio engine
print(sound.Sound)

# window object
my_window = visual.Window(fullscr = True)

# durations
dur_sound   = 1                                                                     # seconds
dur_silence = 3                                                                     # seconds        
fra_silence = int(round((dur_sound + dur_silence)/my_window.monitorFramePeriod))    # frames

# sound object
my_sound  = sound.Sound(value = 'A', secs = dur_sound, volume = 0.05, sampleRate = 44100)

# store tones onsets
onsets = []

# main loop
for i in range(5):
    t_next_flip = my_window.getFutureFlipTime(clock = 'ptb')    # next flip time in seconds
    onsets.append(t_next_flip)                                  # append tone onset to list
    my_sound.play(when = t_next_flip)                           # schedule tone 
    for frame in range(fra_silence):
        my_window.flip()
    
# exit window
my_window.close()

# remove baseline time
onsets = [x - onsets[0] for x in onsets]

Why I did it: I used sound scheduling and PTB library following the recommendations found here.

What I want to know:

  1. Is this a proper way to manage audio playing? I wanted to have the best precission setting the onsets of the tones, that’s why I used frames as time unit.

  2. If I introduce a component to get user-feedback via keyboard (to measure reaction times), will I get too much delay?