Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

Please help me find and fix mistake in Python script for speech recognition. Script…

  1. User

    Please help me find and fix mistake in Python script for speech recognition. Script records only first 10 seconds of mic:

    import sounddevice as sd from scipy.io.wavfile import write import speech_recognition as sr import os

    Function to record audio

    def record_audio(filename): fs = 44100 # Sample rate print('Recording… Press Enter to stop.') myrecording = sd.rec(int(10 * fs), samplerate=fs, channels=2, dtype='int16') input() # Wait for the user to press Enter to stop recording sd.stop() # Stop the recording write(filename, fs, myrecording) # Save recording to file

    Function to recognize speech from an audio file

    def recognize_speech_from_file(audio_filename, recognizer, language): with sr.AudioFile(audio_filename) as source: audio_data = recognizer.record(source) print('Recognizing speech…') try: # Recognize speech using Google Web Speech API text = recognizer.recognize_google(audio_data, language=language) return text except sr.UnknownValueError: return 'Google Web Speech API could not understand the audio' except sr.RequestError: return 'Could not request results from Google Web Speech API'

    Main code

    def main(): # Check if 'Enter' has been pressed for the first time input('Press Enter to start audio capture…')

    # Record audio from the microphone
    audio_filename = 'recording.wav'
    record_audio(audio_filename)
    
    # Check if 'Enter' has been pressed to stop recording
    input('Recording stopped. Press Enter to start speech recognition…')
    
    # Initialize the recognizer
    recognizer = sr.Recognizer()
    
    # Recognize speech
    recognized_text = recognize_speech_from_file(audio_filename, recognizer, language='ru-RU')
    
    # Output the recognized text
    print('Recognized text:')
    print(recognized_text)
    
    # Delete the temporary audio file
    os.remove(audio_filename)
    

    main()

  2. Assistant

    The script records only the first 10 seconds of microphone input because of the explicit instruction int(10 * fs) in the sd.rec() call within the record_audio function. This sets the recording duration to 10 seconds multiplied by the sample rate fs.

    To fix the script so it records until the user presses Enter to stop, you need to modify the script to record in a non-blocking way and check for input asynchronously. Here's a modified version of the record_audio function:

    import sounddevice as sd
    from scipy.io.wavfile import write
    import speech_recognition as sr
    import os
    import threading
    
    # Function to record audio
    def record_audio(filename):
        fs = 44100  # Sample rate
        print('Recording… Press Enter to stop.')
    
        # Start recording in a non-blocking way
        myrecording = sd.rec(samplerate=fs, channels=2, dtype='int16', blocking=False)
        
        # A simple function to wait for Enter press
        def wait_for_enter():
            input()
            sd.stop()  # Stop the recording
    
        # Start the wait_for_enter function in a separate thread
        t = threading.Thread(target=wait_for_enter)
        t.start()
        t.join()  # Wait for the thread to finish, i.e., for Enter to be pressed
    
        # Save the recorded data that has been buffered until now
        write(filename, fs, myrecording)
    
    # The rest of your program remains the same
    
    # Main function entry point
    if __name__ == "__main__":
        main()
    

    With these changes, the script starts recording in a non-blocking way and then starts a separate thread to wait for the Enter key. When Enter is pressed, the wait_for_enter function calls sd.stop(), stopping the recording. Meanwhile, the recording continues until it is explicitly stopped. After stopping the recording, the audio data buffered up to that point is saved to the specified file.

    Remember that with this approach, there might be a small delay between the point when Enter is pressed and when recording actually stops because of buffering and thread scheduling. This should not be a significant issue for most use cases, but if precise timing is crucial, you may need a more sophisticated approach.