Saeshav Subash
Saeshav Subash

Reputation: 1

How to fix Exception with an error code: 0xe (SPXERR_MIC_NOT_AVAILABLE)

I have built a chatbot bot framework and am now looking to integrate speech functionality for the bot. I am trying to run the below code from ms learn quickstart for speech sdk using python.

import os
import azure.cognitiveservices.speech as speechsdk
# Explicitly set the values if not already set in the environment
os.environ["SPEECH_KEY"] = "838f0db5598343dcbd8b0d42587501c4"
os.environ["SPEECH_REGION"] = "southeastasia"

SPEECH_KEY = os.getenv("SPEECH_KEY")
SPEECH_REGION = os.getenv("SPEECH_REGION")

def recognize_from_microphone():
    
    # This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
    speech_config = speechsdk.SpeechConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)
    speech_config.speech_recognition_language="en-US"
    print(SPEECH_KEY)
    print(SPEECH_REGION)

    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()

I keep getting the error below. The code works when i run it on Jupyter notebook but does not work when i run on powershell in vscode. Both are using the same environment and interpreter. Any idea if there are specific permission on vscode to enable in order for microphone to work?

Python version - Python 3.11.10

Azure cognitive speech version - 1.41.1

RuntimeError: Exception with error code:
[CALL STACK BEGIN]

    > CreateModuleObject
    - CreateModuleObject
    - audio_config_get_audio_processing_options
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - audio_config_get_audio_processing_options
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring
    - pal_string_to_wstring

[CALL STACK END]

Exception with an error code: 0xe (SPXERR_MIC_NOT_AVAILABLE)

I have double checked my default input device and it works on voice recorder and even jupyter notebook, just not when running on powershell terminal on vscode. I am running on windows 11.

Upvotes: 0

Views: 197

Answers (1)

Suresh Chikkam
Suresh Chikkam

Reputation: 3448

Initially, When I run on PowerShell in vscode I too got the same error.

enter image description here

This above error in Azure Cognitive Speech SDK comes when the microphone is unavailable or not accessible by the SDK.

On Windows 11, Firstly check that VS Code has access to the microphone:

  • Go to Settings > Privacy & security > Microphone.

  • Check that Microphone access is enabled for both Desktop apps and Microsoft Visual Studio Code specifically.

enter image description here

Then after I used the same code which you have used in the above It worked well.

PowerShell in vs code:

enter image description here

Upvotes: 0

Related Questions