Keyboard response, a second too much?
Hello guys,
I wrote a script which has to do the following things:
1. play a sound and wait for 3.5 seconds
2. after these 3.5 seconds, wait for the operator to press a key with a time window of 4 seconds. Instead of proceeding after the keypress, wait the whole 4 seconds.
3. repeat step 1 and 2 for a certain amount of times
My script for that:
def sound_play():
j=1
while j<11:
sound = "0" + str(j) + "_Set1_einfach_3.5sec.wav"
src = pool[sound]
my_sampler = sampler(src, volume=1)
my_sampler.play()
t1=time.clock()
#print(t1)
j=j + 1
self.sleep(3500)
start_time = time.clock() * 1000
key_response = keyboard(keylist = ['a'], timeout=4000)
key, end_time = key_response.get_key()
#end_time = end_time - 1000
var.response = key
var.response_time = end_time - start_time
diff = 4000 - int(var.response_time)
clock.sleep(diff)
return
the following problem appaers: when the operator presses 'a' immidiatly after the 3.5 seconds of soundplaying there is still a response_time of >1000 milliseconds. I found out that the end_time I get from get_key() is one second more than it should be but I don't know why?! Fixing it by just subtracting 1000 ms from end_time semms not right for me.
Do you guys have any idea?
Thanks a lot!
Comments
Hi Flippy,
It seems that you sleep for 3500 ms because that's how long the sample is. Is that correct? If so, then it's much better to use blocking playback, which will cause
my_sampler.play()
to pause until the sample is done playing.You can do this with theblock
playback keyword:Does that make sense?
Other than that, you have some oddities in your script; for example,
time.clock()
should probably beclock.time()
.See also:
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!