How do I make my speech_recognition pause threshold work correctly

Viewed 1624

I am trying to take audio input and save it as .wav file.
My code is the following:

import speech_recognition as sr
def save():
    r=sr.Recognizer()
    r.pause_threshold = 0.6
    with sr.Microphone() as source:
        print('Yes')
        
        audio=r.listen(source)
    with open('test.wav','wb') as wav:
         wav.write(audio.get_wav_data())
save()

If I save and run this file it listens forever or till I terminate the code.
Can anybody help me.
Thank you
-Levers

1 Answers
import speech_recognition as sr
def save():
    r=sr.Recognizer()
    r.pause_threshold = 0.6
    with sr.Microphone() as source:
        print('Yes')
        r.adjust_for_ambient_noise(source)
        audio=r.listen(source)
    with open('test.wav','wb') as wav:
         wav.write(audio.get_wav_data())
save()
Related