How do i set up the audioConfig right with my Microphone?

Patrick Eiden 1 Reputation point
2021-01-12T15:49:44.81+00:00

Hey,

im trying to set up a recognizer in order to record my microphone. The mic gets detected and the session starts. Buts thats it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using UnityEngine.Audio;

public class MicrosoftVoiceRecognition : MonoBehaviour
{
//task completion source to stop recording mic
private static TaskCompletionSource<int> stopRecognition = new TaskCompletionSource<int>();

private AudioSource audioSource;

public AudioMixerGroup mixerGroupMicrophone, mixerGroupMaster;

void Start()
{
    if (Microphone.devices.Length > 0)
    {
        FromMic();
    }
    else
    {
        Debug.Log("No mic detected!");
    }

}

public async Task FromMic()
{
    var speechConfig = SpeechConfig.FromSubscription(keyValue, region);

    stopRecognition = new TaskCompletionSource<int>();

    using (var audioConfig = AudioConfig.FromDefaultMicrophoneInput())
    //using (var audioConfig = AudioConfig.FromMicrophoneInput(audio))
    using (var recognizer = new SpeechRecognizer(speechConfig, audioConfig))
    {
        if(audioConfig == null)
        {
            Debug.Log("Audioconfig null");
        }
        recognizer.Recognizing += (s, e) =>
        {
            Debug.Log(e.Result.Text);
        };

        recognizer.Recognized += (s, e) =>
        {
            Debug.Log("recognized");
            if (e.Result.Reason == ResultReason.RecognizedSpeech)
            {
                //LuisManager instance = new LuisManager();
                StartCoroutine(LuisManager.instance.CalculateCommand(e.Result.Text));
                Debug.Log(e.Result.Text);
            }
            else if (e.Result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
        };

        recognizer.Canceled += (s, e) =>
        {
            Console.WriteLine($"CANCELED: Reason={e.Reason}");

            if (e.Reason == CancellationReason.Error)
            {
                Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                Console.WriteLine($"CANCELED: Did you update the subscription info?");
            }

            stopRecognition.TrySetResult(0);
        };

        recognizer.SessionStarted += (s, e) => {
            Debug.Log("\n    Session started event.");
        };

        recognizer.SessionStopped += (s, e) =>
        {
            Debug.Log("\n    Session stopped event.");
            stopRecognition.TrySetResult(0);
        };

        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

        // Waits for completion. Use Task.WaitAny to keep the task rooted.
        Task.WaitAny(new[] { stopRecognition.Task });

        await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
    }


}

}

Thanks for your help!
best regards

Patrick

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,519 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,583 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 43,656 Reputation points Microsoft Employee
    2021-01-13T09:46:21.63+00:00

    @Patrick Eiden This might be a syntax issue with your code, your audioconfig does not have a semi colon at the end.

     using (var audioConfig = AudioConfig.FromDefaultMicrophoneInput())  
    

    The sample code should help you correct this and define the config correctly.

    using var audioConfig = AudioConfig.FromDefaultMicrophoneInput();  
    using var recognizer = new SpeechRecognizer(speechConfig, audioConfig);