How to Implement Continuous Voice Input Using Python SpeechRecognition Module

In the realm of voice-controlled applications, seamless user interaction is essential. One way to achieve continuous voice input without repeatedly running a script is by implementing an infinite loop. This article explores the utilization of an infinite loop in Python, specifically focusing on the SpeechRecognition library to capture and print voice inputs.

1. The Example Source Code.

  1. Let’s delve into the code snippet below, which utilizes the SpeechRecognition library.
  2. The primary goal is to create a perpetual loop that stands by for voice commands and then processes them accordingly.
  3. Below is the example source code.
    import speech_recognition as sr
    def continuous_voice_input():
        # Initialize SpeechRecognition by create an instance of Recognizer class.
        speech = sr.Recognizer()
        # We use an infinite loop to receive user voice input to implement continuous voice Input.
        while True:
            print('Please say something while Python is listening to you...')
            # Initialize the variable to store voice input
            voice_input = ""
            # Utilize a microphone as the source of audio input.
            with sr.Microphone() as source:
                # Adjust the env to filter the noise.
                    # Listen to the audio input
                    audio = speech.listen(source)
                    # Recognize the voice input using Google Speech Recognition
                    voice_input = speech.recognize_google(audio)
                except sr.UnknownValueError:
                    # Ignore if the speech recognition couldn't understand the audio
                except sr.RequestError:
                    # Ignore if there is an issue with the Google Speech Recognition service
                except sr.WaitTimeoutError:
                    # Ignore if there's a timeout while waiting for speech input
                # Print out the text converted from the recognized voice input
                print(f'You just said: {voice_input}.')
                # If user say the command "goodbye" then say Goodbye to the user and break the loop.
                if voice_input.lower() == "goodbye":
    if __name__ == "__main__":
  4. Output.
    PS C:\Work> python .\
    Please say something while Python is listening to you...
    You just said: hello.
    Please say something while Python is listening to you...
    You just said: how are you today.
    Please say something while Python is listening to you...
    You just said: what's the weather like today.
    Please say something while Python is listening to you...
    You just said: I cannot go to work.
    Please say something while Python is listening to you...
    You just said: goodbye.

2. Understanding the Code.

2.1 Standby Mode.

  1. The script begins with a while loop, placing it in standby mode until it receives voice input.
  2. The message “Please say something while Python is listening to you…” is printed at each iteration to indicate the script’s readiness.

2.2 Voice Input Handling.

  1. An empty string (`voice_input`) is defined at the start of each iteration to avoid mix-ups from previous inputs.
  2. Exception handling is employed during the connection to the Google speech-recognition server to prevent crashes in case of errors.

2.3 Continuous Processing.

  1. The recognized voice input is printed at each iteration, allowing users to verify if the speech recognition software captured their voice accurately.

2.4 Termination Condition.

  1. To prevent the script from running indefinitely, a condition is set to check for the command “goodbye“.
  2. If detected, the script prints a farewell message and breaks out of the loop.

3. Conclusion.

  1. Implementing an infinite loop for continuous voice input enhances the user experience in voice-controlled applications.
  2. By understanding and customizing the provided Python script, developers can create robust voice interfaces that respond seamlessly to user commands without the need for manual script reruns.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.