[open] Running timers for two stimuli concurrently with inline script
Hello all, I'm quite new to programming and to Open Sesame (thanks for the software and all the feedback you give on here!) so my apologies if this question seems very basic.
I'm currently designing an experiment where I will have two forms of sound stimuli being played to the participants. One form is a metronome that will repeat every 500ms. The second form is a randomly chosen sound that will play back at a randomly chosen time interval. At the moment I have both of these as if statements embedded in a while loop. Each of the pieces of code will work individually but when executed together the metronome plays every 500ms until the randomly chosen sound plays, at which time the metronome completely stops.
Here is the code as it currently is:
time_range = range(3000, 6000)
play_time = random.choice(time_range) # Choose a time value between 3-6s
print play_time
start_time = self.time() # Initialise a starting clock value
deviants = [deviant1, deviant2, deviant3, deviant4, deviant5]
reaction_keys = keyboard(exp, keylist = ['space'], timeout = 2000)
iteration = 0
metronome_timer = self.time()
while iteration < 5:
deviant_time = self.time() - start_time
reaction_timer = self.time()
metro_range = self.time() - metronome_timer
if metronome_timer + 500 == self.time():
metronome_timer = self.time()
print metronome_timer
metronome.play()
outlet.push_sample([2]) # LSL Marker
if deviant_time == play_time:
deviant_chosen = random.choice(deviants) # Choose sound
deviant_chosen.play() # Play sound
outlet.push_sample([1]) # LSL Marker
if deviant_chosen == deviant1:
print 'Deviant 1 played'
if deviant_chosen == deviant2:
print 'Deviant 2 played'
if deviant_chosen == deviant3:
print 'Deviant 3 played'
if deviant_chosen == deviant4:
print 'Deviant 4 played'
if deviant_chosen == deviant5:
print 'Deviant 5 played'
response, timestamp = reaction_keys.get_key()
response_time = self.time() - reaction_timer
#while response_time < 2000: # This while loop is used for gathering reaction times still doesn't work without it
# if response != None:
# response_time = self.time() - reaction_timer
# print 'Response time is %d' %response_time
# break
# else:
# print 'User did not respond'
# response_time = self.time() - reaction_timer
if deviant_chosen in deviants:
deviants.remove(deviant_chosen) # Remove used items from list
play_time = random.choice(time_range)
start_time = self.time()
iteration = iteration + 1
if iteration == 5:
standard.stop()
exp.set('response', response)
exp.set('response_time', response_time)
From the reading I've done it seems as though threading may be necessary to execute this but as a novice coder I would appreciate any help pointing me towards the simplest solution.
Thanks!
Comments
Hi Tom,
I don't think threading will be necessary. Probably this can all be fixed by using conditions in a smart way.
One thing that isn't clear to me is how often/how long is this
loop
supposed to last? Until participants responded, or is there some timeout?Here a bit of code, that might simplify a few things.
I haven't tested the code. It's probably not entirely correct, but I still hope it is useful for you. Let me know, if you need more advice, or if I misunderstood your intentions.
Good luck.
Eduard
Hi Eduard,
Thank you very much! This has been very helpful. I've had to alter your code slightly to get it closer to what I want it to do, and remove a few parts that were giving error messages. The length of the
loop
is dictated by the deviants; i.e. when all five deviants have been played theloop
should finish. To this end I have moved some of the code around. I have also specified that the metronome should play at exactly 500ms. The audio playback now seems to be working well so thank you very much.I do have a follow up question that I hope you can help me with regarding collecting keyboard responses. What I want is to have two separate keyboard responses with the space bar recording the response time after the deviant stimulus and the shift key recording how early or late the participant's estimation of the metronome time is. You mentioned above that these should be within the
while
loop but I am unsure of how to code them. Any advice you could offer would be much appreciated. Here's my code as it is currently:By the way the "standard" within the code is a longer piece of audio that is playing constantly in the background.
Cheers,
Tom
Hi Tom,
You can have to separate
keyboard
items that allow either for ashift
or aspace
response. Inside thewhile
loop you would then do keyboard.get_key() twice and save each result in a separate variable.Does this make sense?
Eduard