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

Saeshav Subash 0 Reputation points
2024-11-08T03:08:41.2733333+00:00

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)
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,817 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 26,245 Reputation points Microsoft Employee
    2024-11-15T04:27:21.4166667+00:00

    @Saeshav Subash Thanks for getting back. I tried the same above sample, which you had shared, and it worked fine on my Visual Studio Code. So, I believe this is environment specific and not related to the SDK or Azure speech.

    See below:
    User's image

    .

    .

    Plan 1:

    On Windows, make sure that your application has permission to access the microphone. You can check this by going to Settings -> Privacy -> Microphone and ensuring that "Allow apps to access your microphone" is enabled and that Python (or your IDE) is listed and enabled in the app list.

    User's image

    .

    .

    Plan 2:

    Default Microphone Configuration:

    Ensure that the default microphone is set correctly. You can set this in Control Panel -> Hardware and Sound -> Sound -> Recording tab. Right-click on your microphone and select "Set as Default Device".

    .

    .

    Plan 3:

    Update Drivers:

    Ensure that your microphone drivers are up to date. You can update them through the Device Manager.

    .

    .

    Plan 4:

    Code Adjustments:

    • Specify the audio input device explicitly in your code. You can list available audio devices and select the appropriate one. Below is an example:
    def get_microphone(): 
    # List available audio input devices 
    audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True) return audio_config
    .
    .
    
    # Get the audio input from the default microphone 
    audio_config = get_microphone() speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    

    .

    .

    Plan 5:

    For your scenario, enable the logging as shown below:

    speech_config.set_property(speechsdk.PropertyId.Speech_LogFilename, "LogfilePathAndName")

    This might give a detailed log for the cause of the issue.

    .

    Awaiting your reply.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.