Problem with .getKeys() function

Hello there! I am using Python and PsychoPy to write a code for my button-click test. The general idea is to gather responses from the keypresses. Participants are supposed to listen to the audio stimuli and respond with pressing “Space” button while listening.
The problem is that I can not get the time of the keypresses. I try “psychopy.event.getKeys” function, specifyng the “timeStamped” parameter.

The Code itself:

# -*- coding: utf-8 -*-

import psychopy.visual
import psychopy.event
import psychopy.core
import psychopy.gui
import wave
import pyaudio

results_file = '_results.txt'
response_time = []

# Collect the participant info
gui = psychopy.gui.Dlg()
gui.addField('Participant: ')
gui.show()

# Create the output data file
for i in gui.data:
    results_file =  i + results_file
output_file = open(results_file, mode = 'w')

# Set the window parameters
win = psychopy.visual.Window(
	size = [600, 600],
	units = 'pix',
	fullscr = False,
    color = [1,1,1],
    monitor='testMonitor'
)

text = psychopy.visual.TextStim(
    win = win,
    text = 'Press any key to start',
    color = [-1, -1, -1]
)

text.draw()

win.flip()

psychopy.event.waitKeys()

win.flip()

# audio opening & details

n = 1024
sound_stim = wave.open('stim_audio.wav', 'rb')
num_samples = sound_stim.getnframes()
print('Num Samples: ' + str(num_samples))
sample_rate = sound_stim.getframerate()
print('Sample Rate: ' + str(sample_rate) + 's/sec')
duration = round(num_samples / sample_rate, 4)
print('Duration (s): ' + str(duration))
print('Channels: '+ str(sound_stim.getnchannels()))

# instantiate pyAudio
p = pyaudio.PyAudio()

# create a stream
stream = p.open(format = p.get_format_from_width(sound_stim.getsampwidth()),
    channels = sound_stim.getnchannels(),
    rate = sample_rate,
    output = True)

data = sound_stim.readframes(n)

while data:
    stream.write(data)
    data = sound_stim.readframes(n)

# get the space-key response time    
Clock = psychopy.core.Clock()    
keys = psychopy.event.getKeys(keyList=['space'], timeStamped=Clock)

stream.stop_stream()
stream.close()

# Saving data
for i in keys:
    output_file.write(str(i[1]) +'\n')

p.terminate()

#Create an escape option
text_2 = psychopy.visual.TextStim(
    win = win,
    text = 'Press "up" to save the results',
    color = [-1, -1, -1]
)

text_2.draw()

win.flip()

exit_keys = psychopy.event.waitKeys(timeStamped = False, keyList = ['up'])

output_file.close()
sound_stim.close()
win.close()

According to the algorythm it would be correct to place the block

Clock = psychopy.core.Clock()    
keys = psychopy.event.getKeys(keyList=['space'], timeStamped=Clock)

before

# audio opening & details
n = 1024
sound_stim = wave.open('stim_audio.wav', 'rb')

However, if I change its position it stops saving data. But even with the position as in the example code I get the list with float type numbers, which are not somehow related to the “psychopy.core.Clock()” I set before. I will appreciate any idea why that is happening and any advice of what I should do to get the time of the keypresses in milliseconds. Thank you.

.getKeys() is an instantaneous check of the keyboard queue, so it doesn’t usually make sense to call it just once: it needs to be in a loop so that it gets called repeatedly until a response is made.

You should also look into PsychoPy’s own Sound module for opening and playing sounds. At the moment you seem to be operating at a very low level, reading in data continuously. The PsychoPy Sound module lets you operate at a higher level of abstraction: just .play() the sound and then move on, freeing you up to draw stimuli, check for responses and so on.

You’d be well served by working through some more of the demos available from the demos menu in the Coder view to pick up more of how to do each of these things in a PsychoPy style.

2 Likes