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.
- Let’s delve into the code snippet below, which utilizes the SpeechRecognition library.
- The primary goal is to create a perpetual loop that stands by for voice commands and then processes them accordingly.
- 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. speech.adjust_for_ambient_noise(source) try: # 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 pass except sr.RequestError: # Ignore if there is an issue with the Google Speech Recognition service pass except sr.WaitTimeoutError: # Ignore if there's a timeout while waiting for speech input pass # 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": print('Goodbye!') break if __name__ == "__main__": continuous_voice_input()
- Output.
PS C:\Work> python .\continuous_voice_input.py 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. Goodbye!
2. Understanding the Code.
2.1 Standby Mode.
- The script begins with a while loop, placing it in standby mode until it receives voice input.
- 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.
- An empty string (`voice_input`) is defined at the start of each iteration to avoid mix-ups from previous inputs.
- Exception handling is employed during the connection to the Google speech-recognition server to prevent crashes in case of errors.
2.3 Continuous Processing.
- 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.
- To prevent the script from running indefinitely, a condition is set to check for the command “goodbye“.
- If detected, the script prints a farewell message and breaks out of the loop.
3. Conclusion.
- Implementing an infinite loop for continuous voice input enhances the user experience in voice-controlled applications.
- 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.