Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError: Audio source must be entered before listening, see documentation for AudioSource; are you using source outside of a with statement? #765

Open
SAAD0206 opened this issue Sep 5, 2024 · 2 comments
Assignees

Comments

@SAAD0206
Copy link

SAAD0206 commented Sep 5, 2024

import pyttsx3
import speech_recognition as sr

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")



# text to speech
def speak(audio):
    engine.say(audio)
    print(audio)
    engine.runAndWait()

def take_command():
    rec = sr.Recognizer()
    with sr.Microphone() as source:
        print("listening")
        rec.pause_threshold = 1
        audio = rec.listen(source)

    try:
        print("RECOGNIZING...")
        words = rec.recognize_google_cloud(audio,language="en-in")
        print(f"user said: {words}")

    except Exception as exp:
        speak("say that again please...")
        return "none"
    return words



if __name__ == "__main__":
    take_command()
    #speak("MY NAME IS JARVIS I AM HERE TO ASSIST U")
  1. (How do you make the issue happen? Does it happen every time you try it?) every time i run this code
  2. (Make sure to go into as much detail as needed to reproduce the issue. Posting your code here can help us resolve the problem much faster!)
  3. (If there are any files, like audio recordings, don't forget to include them.)

Expected behaviour

it should be listening my voice command

Actual behaviour

'NoneType' object has no attribute 'close'
  File "C:\Users\atif\Documents\PYTHON\JARVIS.py", line 20, in take_command
    audio = rec.listen(source)
            ^^^^^^^^^^^^^^^^^^
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:

System information

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

My system is . (For example, "Ubuntu 16.04 LTS x64", "Windows 10 x64", or "macOS Sierra".)

My Python version is . (You can check this by running python -V.)

My Pip version is . (You can check this by running pip -V.)

My SpeechRecognition library version is . (You can check this by running python -c "import speech_recognition as sr;print(sr.__version__)".)

My PyAudio library version is / I don't have PyAudio installed. (You can check this by running python -c "import pyaudio as p;print(p.__version__)".)

My microphones are: (You can check this by running python -c "import speech_recognition as sr;print(sr.Microphone.list_microphone_names())".)

My working microphones are: (You can check this by running python -c "import speech_recognition as sr;print(sr.Microphone.list_working_microphones())".)

I installed PocketSphinx from . (For example, from the Debian repositories, from Homebrew, or from the source code.)

@JeziahAnon
Copy link

JeziahAnon commented Sep 14, 2024

It states that the source is used outside of the with scope. Are you sure that the try/except block and the rec.listen(source) are inside the "with" block? Its just unclear because of the formatting on github :0

I have it like this and it works.

import pyttsx3
import speech_recognition as sr

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")

def speak(audio):
    engine.say(audio)
    print(audio)
    engine.runAndWait()

def take_command():
    rec = sr.Recognizer()
    
    with sr.Microphone() as source:
        print("listening")
        rec.pause_threshold = 1
        audio = rec.listen(source)

        try:
            print("RECOGNIZING...")
            words = rec.recognize_google(audio,language="en-in")
            print(f"user said: {words}")

        except Exception as exp:
            speak("say that again please...")
            return "none"
        return words

if __name__ == "__main__":
    take_command()

@ftnext
Copy link
Collaborator

ftnext commented Sep 15, 2024

@JeziahAnon Thanks. I added python syntax highlight

@SAAD0206 Please check JeziahAnon's reply

@ftnext ftnext changed the title python error AssertionError: Audio source must be entered before listening, see documentation for AudioSource; are you using source outside of a with statement? Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants