我想做一个脚本,用来衡量参与者按回车键或空格键的速度,只有当他们听到声音文件中2/30的声音时。 因此,有时用户不必按任何键,脚本仍会转到下一个声音文件。我该怎么做?我现在拥有的是:(不是声音文件,而是文本文件):
# Grounding of Words Experiment #
#Import libraries
import re
import glob
from psychopy import sound, visual, event, data, core, gui # imports a module for visual presentation and one for controlling events like key presses
# ID, age, gender box display
myDlg = gui.Dlg(title="Experiment") #, pos=(400,400)
myDlg.addField('ID:')
myDlg.addField('Age:')
myDlg.addField('Gender:', choices = ['Female', 'Male'])
myDlg.show()#you have to call show() for a Dlg
if myDlg.OK:
ID = myDlg.data[0]
Age = myDlg.data[1]
Gender = myDlg.data[2]
else:
core.quit()
trial=0
#Creates the outfile, that will be the file containing our data, the name of the file, as saved on the computer is the filename
out_file="Grounding_experiment_results.csv"
#Creates the header for the data
header="trial,ID,Gender,Age,Word,rt,SpaceKlik\n"
#opens the outfile in writemode
with open(out_file,"w") as f:
f.write(header)#writes the header in the outfile
# define window
win = visual.Window(fullscr=True) # defines a window using default values (= gray screen, fullscr=False, etc)
# Instruction box display
def instruct(txt):
instructions = visual.TextStim(win, text=txt, height = 0.05) # create an instruction text
instructions.draw() # draw the text stimulus in a "hidden screen" so that it is ready to be presented
win.flip() # flip the screen to reveal the stimulus
event.waitKeys() # wait for any key press
instruct('''
Welcome to the experiment!
You will be hearing different words.
Whenever you hear the word "Klik" and "Kast" please press the left mouse button.
Whenever you hear any other word - do nothing.
Try to be as fast and accurate as possible.
Please put on the headphones.
The experiment will take 5 minutes.
Press any key to start the experiment''')
# Play sound
# Function that makes up a trial
trial(word):
global trial
trial += 1
if word in ["Klik", "Press", "Throw"]:
condition = "press"
else :
condition = "no_press"
event.clearEvents()
for frame in range(90):
text = visual.TextStim(win, text=word, height = 0.05)
text.draw() # draw the text stimulus in a "hidden screen" so that it is ready to be presented
time_start=win.flip()
try:
key, time_key=event.getKeys(keyList=['space', 'escape'], timeStamped = True)[0] # wait for any key press
except IndexError:
key = "0"
rt = "NA"
else:
if key=='escape':
core.quit()
rt = time_key - time_start
if key == "space" and condition=="press":
accuracy = 1
elif key == "0" and condition=="no_press":
accuracy = 1
else:
accuracy = 0
with open(out_file,"a") as f:
f.write("{},{},{},{},{},{},{},{}\n".format(trial,ID,Gender,Age,word,accuracy,rt,SpaceKlik))
# s = sound.Sound('sound.wav')
# s.play()
# Register space bar press or mouse click
# Measure reaction time
# Check to see if answer is correct to sound - certain sound files are "klik". Others "kast", "løb", "sko" and so on
# Write csv logfile with coloumns: "ID", "Gender", "Word", "Correct/incorrect", "Reaction time", "Space/click"
我最终都会精神错乱。感谢贵方的合作。你知道吗
好的,很好-谢谢!现在我继续尝试开发你的脚本。我增加了一份试用名单,现在我不太确定该怎么办。我的脚本如下所示:
缩进有问题,因此不是所有的东西都在适当级别的试用函数中运行。您可能可以删除异常处理来更直接地检查是否做出了响应。然后使用
else:
子句处理for
循环完成但未按下任何键时发生的情况。这避免了必须处理在任何给定帧上发生的非响应的逻辑(如果不久之后将发生响应,则这可能没有任何意义)。你知道吗类似这样的通用伪代码:
相关问题 更多 >
编程相关推荐