0

i'm trying to write a hearing test that will play sounds at increasingly loud volumes until it has traversed its list of volumes or there is input from the user, indicating they heard the sound. To do this I'm trying to get the script to ask for input while still looping to increase the volumes, but normally input() will stop the script. And threads seem to stop working after the firs loop-over. This is what I came up with so far:

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)

To keep the script from being extremely long it refers to two functions that I didn't define here.

1
  • Well, it's in a separate thread. Isn't that the idea of threading? Commented May 3, 2016 at 22:22

1 Answer 1

1

Few problems with your thread. When creating your thread and passing the target you are doing

thread = threading.Thread(target = tone_stopper())

This is calling the function. You should pass the target like this.

thread = threading.Thread(target = tone_stopper)

You're also checking if thread == True. You should be checking thread.is_alive() if you want to check if the thread is alive.

But, all you want here is to see if the user has entered anything with the input prompt. In which case, the thread will terminate.

So, you should simply check if not thread.is_alive()

Here's a completely useless example of simply showing that the thread terminates when a user hits enter.

import threading

def test_thread():

    input("Press enter when you hear the sound.")

def test_func():

    while thread.is_alive():
        pass

    print("Thread is over. User heard a sound.")

thread = threading.Thread(target=test_thread)
thread.daemon = True
thread.start()
test_func()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply! Would there be a way to 'revive' the thread after checking if its alive? Otherwise this method could only be used once if the user heard a sound, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.