Hyunju Shim
Hyunju Shim

Reputation: 11

Can I control the start & finish time when I use speech-recognition in python?

I did coding as below. however I want to know whether there is some ways to control the recording duration. I actually, want to have a program which has a start & finish buttons so that I can control to record. I know that it is like an elementary question. but I really need to solve it. help me~ how should I compensate this problem?

import speech_recognition as sr

r = sr.Recognizer()
mic = sr.Microphone()

show = input("enter text: ")

print("Read text\a")

with mic as source:
    audio = r.listen(source)

print("recorded\a")
print('Result: ', r.recognize_google(audio, language='ko-KR'))

Upvotes: 1

Views: 2586

Answers (2)

Orlando G
Orlando G

Reputation: 327

# Create the recognizer_instance:
r = sr.Recognizer()

# Set up the recognizer object 
r.pause_threshold = 5 # This is in seconds, this will control the end time of the record after the last sound was made 

#Start the record 
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source=source, timeout=5)

Upvotes: 0

ivan_pozdeev
ivan_pozdeev

Reputation: 36036

As per recognizer_instance.listen doc, it's a blocking call (i.e. the program doesn't continue until it's done) and the only way to stop the recording is to not talk for recognizer_instance.pause_threshold (0.8s by default).

To be able to do anything else while recording is active, you need to use recognizer_instance.listen_in_background. It still uses the same signal recognition logic but keeps recording phrases in a loop until you tell it to stop. This implies that the signal recognition logic is supposed to be reliable enough for all practical purposes. If it fails in your case, you probably need to adjust initial energy_threshold.

FWIW, you can record manually with raw pyaudio, then use the resulting file or raw data to construct AudioData.

Upvotes: 1

Related Questions