Your code is missing the parentheses required to get the sound to play - i.e. you have one_tone.play
rather than one_tone.play()
.
That should be OK - the key thing is that the loop iterates continuously, rather than using the core.wait
functionality to interrupt execution. This requires the code to be written in a bit of a different way, but should be possible.
For example, say you wanted your tones to be playing every three seconds while you are collecting the reaction time for an orientation detection task (stimuli presented in random order) and also collecting mouse clicks for a secondary task (I agree with @daniel.riggs1 that a more precise specification of your requirements would be helpful). Something like the below should let you do that:
import random
import numpy as np
import psychopy.visual
import psychopy.sound
import psychopy.event
import psychopy.core
win = psychopy.visual.Window(size=[400, 400], units="pix", fullscr=False)
sound_playing = False
sound_duration = 0.2
sound_iti = 3.0
tone = psychopy.sound.Sound(secs=sound_duration)
stim_oris = [0, 45, 90, 135]
n_stim = len(stim_oris)
stims = [
psychopy.visual.GratingStim(
win=win,
size=[200, 200],
sf=5.0 / 200.0,
ori=stim_ori
)
for stim_ori in stim_oris
]
random.shuffle(stims)
stim_drawn = [False] * n_stim
mouse = psychopy.event.Mouse()
clock = psychopy.core.Clock()
trial_clock = psychopy.core.Clock()
i_trial = 0
while i_trial < n_stim:
# start playing if it is within the initial period, and isn't already playing
if np.mod(clock.getTime(), sound_iti) < sound_duration:
if (not sound_playing):
tone.play()
sound_playing = True
else:
sound_playing = False
# handle stimulus etc. for current trial
if not stim_drawn[i_trial]:
stims[i_trial].draw()
win.flip()
trial_clock.reset()
stim_drawn[i_trial] = True
# check and handle keyboard and mouse
keys = psychopy.event.getKeys(timeStamped=trial_clock)
if keys:
for (key, key_rt) in keys:
print "Pressed {k:s} with RT {rt:.3f}".format(k=key, rt=key_rt)
i_trial += 1
if any(mouse.getPressed()):
print "Mouse click registered"
win.close()