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