[solved]Playing beep during showing of a picture; trial ends after 6 seconds not when key is pressed
Hi there,
Can you help me correct my code? (I think I will need to use the sleep function, but don't know how..)
What I want:
-every trial starts with a fixation dot for 1 second
-then a picture shows for 6 seconds
-during the picture a beep plays (at SOA 1 or 4 seconds)
-person presses key 'z' or '/' to respond to the beep (high or low)
-(trial ends when picture is shown for 6 seconds, not when key is pressed after the beep)
-next trial...
So far I have made this code:
#first a sketchpad with a fixation dot for 1 sec
#then the following inline script:
from openexp.synth import synth
#create empty canvas to draw stimulus on in prepare phase
my_canvas = exp.canvas = self.offline_canvas()
my_canvas.set_bgcolor('white')
my_canvas.clear()
#find right picture for current trial
if self.get('pict_valence') == 'positive':
path_positive = exp.get_file('positive.png')
my_canvas.image(path_positive)
elif self.get('pict_valence') == 'neutral':
path_neutral = exp.get_file('neutral.png')
my_canvas.image(path_neutral)
elif self.get('pict_valence') == 'negative':
path_negative = exp.get_file('negative.png')
my_canvas.image(path_negative)
#define low and high tone (beep)
if exp.get('tone_pitch') == 'high':
exp.beep = synth(exp, freq= 2000, osc = "square", length=500)
elif exp.get('tone_pitch') == 'low':
exp.beep = synth(exp, freq= 400, osc = "square", length=500)
#define SOA for the tone
SOA = exp.get("tone_SOA")
#play tone
self.sleep(abs(SOA))
exp.beep.play()
self.sleep(6000 - abs(SOA))
Comments
Hi,
First of all: you're almost there!
Second: let's clean up the code a bit, to make sure the experiment runs a bit more smoothly. Since you use the pictures and the sounds throughout the entire experiment, it makes more sense to initialize them at the start of the experiment. To do so, add an inline_script item to the very beginning of your experiment, and add the following contents:
Now, in the trial sequence, insert the following code in the Run phase of an inline_script item (placed directly after the fixation_dot item):
(small disclaimer: code is untested, as I'm in a bit of a rush: please do not hesitate to post any potential errors arising when you run the scripts!)
Dear Edwin,
Thank you so much for your help.
It works :-)
I now need to add a small extra thing: there are 12 positive, 12 negative and 24 neutral pictures from which I randomly want to take one each trial.
I thought I could make lists of numbers, shuffle them and each time add the first item of this list to the image path (I named all images positive1.png, positive2.png, etc..)
But the canvasses are made in the beginning.
Can you help me?
Maybe make lists of canvasses in the beginning and later take a random one of this list?
Or do somethig with a if loop in the beginning?
Here is the code which I have made now (In the wrong place now).
Again, almost there!
This should be in the Prepare Phase of an inline_script at the start of your experiment:
This following should be in the Prepare Phase of the inline_script in your trial sequence:
And the following should be in the Run Phase of the same inline_script:
Good luck!