speech_recognition: Error when running simple speech recognition code

Steps to reproduce

Running this code:

import speech_recognition as sr
import pyttsx3
import webbrowser
import wikipedia
import wolframalpha
import os
from termcolor import colored, cprint


ENGINE = pyttsx3.init('dummy')
VOICES = ENGINE.getProperty('voices')
ENGINE.setProperty('voice', VOICES[0].id)
ACTIVATION_WORD = "echo"

def Command_recognition():
    listener = sr.Recognizer()
    os.system('clear')
    activated_text = colored('ACTIVATED','light_green', attrs=['blink', 'bold'])
    print(activated_text)
    
    with sr.Microphone(device_index=1) as source:
        listener.pause_threshold = 2
        command = listener.listen(source)
    
    try:
        listening_text = colored('LISTENING...','light_green', attrs=['blink'])
        print(listening_text)
        query = listener.recognize_google_cloud(command, language='en_gb')
    except Exception as exception:
        error_text = colored('ERROR','red', attrs=['blink', 'bold'])
        print('\n'+error_text)
        print(exception)
        quit()
    return query


query1 = Command_recognition()
print(query1)

Expected behaviour

Run normally without issues and print speech input

Actual behaviour

crashed with this error code: ||PaMacCore (AUHAL)|| AUHAL component not found.Traceback (most recent call last): File “/Users/ryan/Desktop/stuff/code/Project Echo/main.py”, line 29, in Command_recognition command = listener.listen(source) ^^^^^^^^^^^^^^^^^^^^^^^ File “/Users/ryan/anaconda3/lib/python3.11/site-packages/speech_recognition/init.py”, line 466, in listen assert source.stream is not None, “Audio source must be entered before listening, see documentation for AudioSource; are you using source outside of a with statement?” AssertionError: Audio source must be entered before listening, see documentation for AudioSource; are you using source outside of a with statement?

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/Users/ryan/Desktop/stuff/code/Project Echo/main.py”, line 43, in <module> query1 = Command_recognition() ^^^^^^^^^^^^^^^^^^^^^ File “/Users/ryan/Desktop/stuff/code/Project Echo/main.py”, line 27, in Command_recognition with sr.Microphone(device_index=1) as source: File “/Users/ryan/anaconda3/lib/python3.11/site-packages/speech_recognition/init.py”, line 189, in exit self.stream.close() ^^^^^^^^^^^^^^^^^ AttributeError: ‘NoneType’ object has no attribute ‘close’

System information

(Delete all the statements that don’t apply.)

My MacOS Sonoma 14.4.1

My Python version is 3.11.0

My Pip version is 23.1.2

My SpeechRecognition library version is 3.10.3

My PyAudio library version is 0.2.14

My microphones are: [‘Ryan’s iPhone 15 Pro Microphone’, ‘MacBook Pro Microphone’, ‘MacBook Pro Speakers’]

My working microphones are: the code outputs this: ||PaMacCore (AUHAL)|| AUHAL component not found.||PaMacCore (AUHAL)|| AUHAL component not found.{}

Also to note I am using a Anaconda environment

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26

Most upvoted comments

Hey @General-Hex, This error typically indicates issues in accessing the audio hardware. To resolve this, please ensure that your audio hardware is properly connected and configured. Also, make sure that your device isn’t blocking the access request.

For further reference and potential solutions, you can check out this discussion on GitHub: Link to the issue.

@thedarknight01 I SOLVED IT

the problem is that your iphone keeps connecting/disconnecting from your macbook meaning it keeps going into index 0 of the list of devices.

Please attempt these commands as the standard installation seems to be encountering issues. This alternative method often resolves many problems.

brew uninstall portaudio
brew install portaudio --HEAD