pythonlinuxazureazure-speech

Speech SDK trowing error: Exception with an error code: 0xe (SPXERR_MIC_NOT_AVAILABLE)


I have a small script in python:

def listen(language):
speech_config.speech_recognition_language=language
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
print("Speak into your microphone.")
speech_recognition_result = speech_recognizer.recognize_once_async().get()
if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
    print("Recognized: {}".format(speech_recognition_result.text))
    return speech_recognition_result.text

This code is derived from the QuickStart SpeechSDK Tutorial found here:https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-speech-to-text?tabs=linux%2Cterminal&pivots=programming-language-python

It works perfectly under my MacOS Ventura 13.0 detecting speech. However, when I try it on my Linux Machine running Ubuntu 22.10 using the exact same hardware for audio detection it throws the following error:

    Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/home/fabian/Schreibtisch/Chatbot/gui.py", line 184, in <lambda>
    command=lambda: main(set_language,"doctor"),
  File "/home/fabian/Schreibtisch/Chatbot/ada_dialog_functions.py", line 233, in main
    gender_age=ask_age_gender(language,language_voice_id,language_country_id)
  File "/home/fabian/Schreibtisch/Chatbot/ada_dialog_functions.py", line 188, in ask_age_gender
    answer=listen(language_country_id)
  File "/home/fabian/Schreibtisch/Chatbot/ada_dialog_functions.py", line 15, in listen
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
  File "/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/speech.py", line 1004, in __init__
    _call_hr_fn(
  File "/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/interop.py", line 62, in _call_hr_fn
    _raise_if_failed(hr)
  File "/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/interop.py", line 55, in _raise_if_failed
    __try_get_error(_spx_handle(hr))
  File "/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/interop.py", line 50, in __try_get_error
    raise RuntimeError(message)
RuntimeError: Exception with error code: 
[CALL STACK BEGIN]

/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.extension.audio.sys.so(+0xe0f1) [0x7f53e240e0f1]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1c9cf8) [0x7f53e31c9cf8]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0xf4c35) [0x7f53e30f4c35]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x18251b) [0x7f53e318251b]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0xe7ad3) [0x7f53e30e7ad3]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1c9cf8) [0x7f53e31c9cf8]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0xf4c35) [0x7f53e30f4c35]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x180101) [0x7f53e3180101]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x18334f) [0x7f53e318334f]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1309e7) [0x7f53e31309e7]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1309e7) [0x7f53e31309e7]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x15aeb1) [0x7f53e315aeb1]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x134d3d) [0x7f53e3134d3d]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1dc61d) [0x7f53e31dc61d]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x130815) [0x7f53e3130815]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1f87f6) [0x7f53e31f87f6]
/home/fabian/.local/lib/python3.10/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(recognizer_create_speech_recognizer_from_config+0xf2) [0x7f53e30d121b]
[CALL STACK END]

Exception with an error code: 0xe (SPXERR_MIC_NOT_AVAILABLE)

I tried changing the microphone using the device_name= "hw:CARD=U0x4b40x306,DEV=0 however this did not work either. Most of the solutions for this bug do not mention my combination of OS and Language, therefore are not really applicable.


Solution

  • I tried in my environment and got the below results:

    Initially, I got the same error when I run the python script in linux machine.

    enter image description here

    packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(recognizer_create_speech_recognizer_from_config+0xf2)[0x7fd4507d121b] [CALL STACK END]Exception with an error code: 0xe (SPXERR_MIC_NOT_AVAILABLE)

    The above error occurs either the mic is not recognized or not detected by the speech recognition system.

    In Linux check with arecord -l you can check the sound card you were using.

    In my environment, I installed the pulseaudio in the Linux machine, it worked for me.

    Command:

    sudo apt-get install alsa-base pulseaudio
    

    Console: enter image description here

    After I installed pulseaudio, Now I tried with the same python script in my environment and it worked perfectly.

    Code:

    import os
    import azure.cognitiveservices.speech as speechsdk
    
    def recognize_from_microphone():
        # This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
        speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
        speech_config.speech_recognition_language="en-US"
    
        audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    
        print("Speak into your microphone.")
        speech_recognition_result = speech_recognizer.recognize_once_async().get()
    
        if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
            print("Recognized: {}".format(speech_recognition_result.text))
        elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
            print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
        elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = speech_recognition_result.cancellation_details
            print("Speech Recognition canceled: {}".format(cancellation_details.reason))
            if cancellation_details.reason == speechsdk.CancellationReason.Error:
                print("Error details: {}".format(cancellation_details.error_details))
                print("Did you set the speech resource key and region values?")
    
    recognize_from_microphone()
    

    Output: enter image description here

    Reference:

    1. Sound Input device (microphone) not working
    2. pulseaudio - Headset microphone not working on Ubuntu 20.04