-1

How do I turn on audio input for all device indexes using a Speech Recognition Library? As I want to pass in the audio for testing and there might be possibility that the library uses a different audio input device. How do I let it take the audio input from all the indexes?

An0n1m1ty
  • 448
  • 4
  • 17

2 Answers2

0

You can use your microphone as a default audio input device below is a code snippet:

import speech_recognition as sr
r=sr.Recognizer() # this is a recognizer which recognize our voice3
with sr.Microphone() as source: # in this we are using a microphone to record our voicecmd
  speak.speak("What can i do for you!") # this a speak invoke method w3hich ask us something
  print("Ask me Something!") # this a print statement which come on console to ask something
  audio=r.listen(source,timeout=60,phrase_time_limit=3)

data = ""
try:
        """
        this is a try block it will recognize it our voice and say what we have told
        """
        data= r.recognize_google(audio,language="en-US")
        print("dynamo think you said!" + "  "+data) # this will print on your console what will going to recognize by google apis
except:
        """
        this is a except block which except the error which come in try block and the code is not able to run it will pass a value
        """
        print("not able to listen you or your microphone is not good")
        exit()
ghovat
  • 1,033
  • 1
  • 12
  • 38
0

First, You require the following things installed on your system. 1. Python 2. Speech Recognition Package 3. PyAudio

Now, You can run this Code for know your Version

import speech_recognition as s_r
print(s_r.__version__)

Output

3.8.1

It will print the current version of your speech recognition package.

Then, Set microphone to accept sound :

my_mic = s_r.Microphone()

Here you have to pass the parameter device_index=?

To recognize input from the microphone you have to use a recognizer class. Let’s just create one.

r = s_r.Recognizer()

Now, I Convert the Sound Speech into Text In Python

To convert using Google speech recognition we can use the following line:

r.recognize_google(audio)

It will return a string with some texts. ( It will convert your voice to texts and return that as a string.

You can simply print it using the below line:

print(r.recognize_google(audio))

Now the full program will look like this:

import speech_recognition as s_r
print(s_r.__version__) # just to print the version not required
r = s_r.Recognizer()
my_mic = s_r.Microphone(device_index=1) #my device index is 1, you have to put your device index
with my_mic as source:
    print("Say now!!!!")
    audio = r.listen(source) #take voice input from the microphone
print(r.recognize_google(audio)) #to print voice into text

If you run this should you get an Output.

But after waiting a few moments if you don’t get any output, check your internet connection.

Nikhil B
  • 93
  • 6