0

I was making my personal assistant speech_recognition is working properly with headphones but without headphones on internal mic it is not recognising the voice.

Code:

with sr.Microphone() as source:
        print("Listening...")
        speak("Listening...")
        r.pause_threshold = 1  
        audio = r.listen(source)

OS: Ventura 13.4 (M1)

Ansh Tyagi
  • 116
  • 1
  • 15
  • Can you print the contents of the object `sr.Microphone()` please? – Kathy Reid Jun 19 '23 at 00:13
  • The _contents_ of the object, please – Kathy Reid Jun 19 '23 at 11:59
  • @KathyReid didn't understand that can you please Clear it for me – Ansh Tyagi Jun 19 '23 at 14:13
  • Please see https://stackoverflow.com/questions/1006169/how-do-i-look-inside-a-python-object for information on how to get the _attributes_ of a Python object. This is called "introspection". – Kathy Reid Jun 20 '23 at 00:16
  • ok what should I provide you? type or dir? – Ansh Tyagi Jun 20 '23 at 07:58
  • Which one shows the _contents_ of the object? – Kathy Reid Jun 20 '23 at 13:52
  • I think dir() `` ['CHUNK', 'MicrophoneStream', 'SAMPLE_RATE', 'SAMPLE_WIDTH', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'audio', 'device_index', 'format', 'get_pyaudio', 'list_microphone_names', 'list_working_microphones', 'pyaudio_module', 'stream'] `` – Ansh Tyagi Jun 20 '23 at 16:11
  • @KathyReid Also .__dict__ {'pyaudio_module': , 'device_index': 1, 'format': 8, 'SAMPLE_WIDTH': 2, 'SAMPLE_RATE': 48000, 'CHUNK': 1024, 'audio': None, 'stream': None} – Ansh Tyagi Jun 20 '23 at 16:13
  • Thanks @ansh - now, how do the values in the `__dict__` call compare when you have your headphones as the audio source? What is the same or different? – Kathy Reid Jun 22 '23 at 00:29
  • with headphones: {'pyaudio_module': , 'device_index': None, 'format': 8, 'SAMPLE_WIDTH': 2, 'SAMPLE_RATE': 48000, 'CHUNK': 1024, 'audio': None, 'stream': None} \n without: {'pyaudio_module': , 'device_index': None, 'format': 8, 'SAMPLE_WIDTH': 2, 'SAMPLE_RATE': 96000, 'CHUNK': 1024, 'audio': None, 'stream': None} – Ansh Tyagi Jun 22 '23 at 07:48
  • @KathyReid check above comment. – Ansh Tyagi Jun 23 '23 at 17:09
  • So, the only difference I see between the headphones and the internal microphone is the sample rate: the headphones are sampling at 48kHz and the internal mic is sampling at 96kHz. Is there a way to change the sample rate of your internal microphone to be 48kHz? Is the code expecting a particular frequency of the audio sample (like 48kHz)? Also, what is the output of the `audio` variable? What is the `type` of that variable? – Kathy Reid Jun 23 '23 at 19:53
  • yes I think module allows me to change the frequency – Ansh Tyagi Jun 24 '23 at 07:02
  • 1
    OK changing sample rate worked for me thanks @KathyReid – Ansh Tyagi Jun 24 '23 at 07:09
  • Glad to hear it worked, good luck with your project! :D – Kathy Reid Jun 24 '23 at 23:59

1 Answers1

1

Thanks to KathyReid for helping me in comments.

Actually it was really easy it worked by just change sample rate from 96KHz to 48KHz

Here is a code edit:

with sr.Microphone(sample_rate=48000) as source:
        print("Listening...")
        speak("Listening...")
        r.pause_threshold = 1  
        audio = r.listen(source)
Ansh Tyagi
  • 116
  • 1
  • 15