Intermittent distraction sound


I’m completely new to both Python and PsychPy so I apologise if this seems rather trivial.
I’m setting up a serial recall test as part of an experiment that I’m going to use to form the basis of my MSc project. So far I have set up a serial recall task with two loops each containing 36 trails. I’d like to have an intermittent distraction sound that plays a tone for one second, then is silent for one second and then plays the tone for another second, with this repeating until the routine ends. I’m aware I could create multiple sounds in the builder and have them all start at different times and last for one second each but I’d like to construct some more efficient custom code.

This is what I’ve got so far but I could use some direction:

In begin experiment:

Initialize distraction_sound for experiment

trialClock = core.Clock()
distraction_sound = sound.Sound(‘A’, secs=1.0, stereo=True, hamming=True,

#Prepare routine

reset timers

t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock=“now”)
trialClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1

update distraction_sound for each repeat

distraction_sound.setSound(‘A’, secs=1.0, hamming=True)
distraction_sound.setVolume(1, log=False)

-------Run Routine -------

while continueRoutine and routineTimer.getTime() > 0:
# get current time
t = trialClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=trialClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# start/stop distraction_sound
if distraction_sound.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
distraction_sound.frameNStart = frameN # exact frame index
distraction_sound.tStart = t # local t and not account for scr refresh
distraction_sound.tStartRefresh = tThisFlipGlobal # on global time
#play distraction sound, when=win) # sync with win flip
if distraction_sound.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > distraction_sound.tStartRefresh + 1.0-frameTolerance:
# keep track of stop time/frame for later
distraction_sound.tStop = t # not accounting for scr refresh
distraction_sound.frameNStop = frameN # exact frame index
win.timeOnFlip(distraction_sound, ‘tStopRefresh’) # time at next scr refresh
distraction_sound.stop() # .stop(when=?)

-------Ending Routine -------

for thisComponent in trialComponents:
if hasattr(thisComponent, “setAutoDraw”):
distraction_sound.stop() # ensure distraction_sound has stopped at end of routine
#Adds data about start and stop times of distraction_sound if uncommented
#thisExp.addData(‘distaction_sound.started’, distraction_sound.tStartRefresh)
#thisExp.addData(‘distraction_sound.stopped’, distraction_sound.tStopRefresh)

Currently, all this does is beep once and I’m completely unsure of how to go about achieving my goal. I’ve though about creating a loop with two sounds where one is silent, or attempting to get the sound to play for one second, every two seconds. Essentially, I’m a bit lost but if anyone has some ideas I’d be happy to give them a try.

Python 3.6.6 (v3.6.6:4cf1f54eb7) [MSC v.1900 64 bit (AMD64)] on win32
PsychoPy version 2020.2.10 for 64 bit windows