【发布时间】:2016-05-03 22:12:48
【问题描述】:
我正在尝试编写一个听力测试,它将以越来越大的音量播放声音,直到它遍历其音量列表或用户输入,表明他们听到了声音。 为此,我试图让脚本在仍然循环以增加音量的同时请求输入,但通常 input() 会停止脚本。并且线程似乎在第一次循环后停止工作。到目前为止,这是我想出的:
def tone_stopper():
"""This function will take as input a pressed key on the keyboard and give
True as output"""
test = input("Press enter when you hear a tone: ")
if test == " ":
return True
def call_play_file(frequency, vol_list):
"""This function will play a frequency and stop when the user presses
a button or stop when it reaches the loudest volume for a frequency,
it takes as an input a frequency and a list of volumes
and returns the loudness at which the sound was playing when a button was
pressed"""
for volume in vol_list: #plays a tone and then repeats it a different vol
tone_generator(frequency, volume)
play_file('hearingtest.wav')
if thread == True:
return frequency, volume
return frequency, "didn't hear" #in case no button is pressed
thread = threading.Thread(target = tone_stopper())
thread.setDaemon(True)
thread.start()
vol_list = [4, 8, 12];
freq_left_right = random_freq_list(200, 18000, 500)
startplaying = call_play_file(freq_left_right, vol_list)
为了避免脚本过长,它引用了两个我没有在这里定义的函数。
【问题讨论】:
-
嗯,它在一个单独的线程中。这不就是线程的想法吗?