2

I'm trying to run a simple python speech recognition program. The code is as follows:

import speech_recognition as sr
import pyttsx3 


r = sr.Recognizer()  

engine = pyttsx3.init("nsss",True)

engine.say("hello")
engine.runAndWait()


while True:
    try:
        print("starting process")
        with sr.Microphone() as source2:    
            r.adjust_for_ambient_noise(source2, duration=0.2)  
            print("completed adjustment")
            audio2 = r.listen(source2) 
            MyText = r.recognize_google(audio2) 
            MyText = MyText.lower() 
            print("Did you say "+MyText) 
            SpeakText(MyText) 
    
    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))
    except sr.UnknownValueError:
        print("an unknown error occured")

When run, the test to speech says "Hello" successfully but then throws

||PaMacCore (AUHAL)|| Failed to open AUHAL component.||PaMacCore (AUHAL)|| Error on line 1263: err='-50', msg=Unknown Error
Traceback (most recent call last):
  File "test1.py", line 16, in <module>
    with sr.Microphone() as source2:    
  File "/Users/markus/Library/Python/3.6/lib/python/site-packages/speech_recognition/__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "/Users/markus/Library/Python/3.6/lib/python/site-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/Users/markus/Library/Python/3.6/lib/python/site-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9986] Internal PortAudio error

I have tried uninstalling and reinstalling portaudio with brew.

-- EDIT --

I ran into similar issues with portaudio in musescore, running the program using sudo solved the issue.

Big Dumb
  • 120
  • 1
  • 7

2 Answers2

3

`brew uninstall portaudio

brew install portaudio --HEAD`

that eliminate this error on Mac Big sur

northman23
  • 44
  • 4
  • 1
    Nice... first I did brew unlink portaudio and then rebuilt and it asked for mic permissions. Linked to portaudio #514 https://github.com/PortAudio/portaudio/issues/514 – pythlang Mar 24 '21 at 14:24
  • @northman23 thanks this solved my problem. Can you please explain why this happened? – Fariman Kashani Apr 11 '21 at 11:57
  • 1
    @FarimanKashani. Okay, glad it works for you too! I don't know what --HEAD does exactly, but I read this: In git using the --HEAD will grab all of the latest commits from the source repo. The problem with this is sometimes the latest revision will be in an inconsistant or unbuildable state, so use at your own risk. – northman23 Apr 12 '21 at 14:49
0

Can you confirm that your microphone works fine and it's not being affected by audio issue after installing Mojave, reference here.

Your code seems fine. It looks like the error is stemming from some faults in your microphone system. Make sure that you have correctly followed the instructions from the package index page. Notably,

PyAudio (for microphone users) PyAudio is required if and only if you want to use microphone input (Microphone). PyAudio version 0.2.11+ is required, as earlier versions have known memory management bugs when recording from microphones in certain situations.

You should also see some of the solutions posted on this thread here - Re: Error code -9986 Internal PortAudio error

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • I am using Pyaudio version 0.2.11, with the default system mic. I have been able to record audio using external programs (Garageband, audacity), so I believe my mic should be working? I have followed the setup instructions correctly as well. – Big Dumb Jul 07 '20 at 02:48
  • You can try reinstalling `speechRecognition` module again and see if that helps – AzyCrw4282 Jul 07 '20 at 14:31
  • Reinstalled it, still no difference. – Big Dumb Jul 10 '20 at 15:19
  • hmm. I suppose then this error is primarily to do with your system. – AzyCrw4282 Jul 10 '20 at 15:22