Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Media.SpeechRecognition;
using Windows.Media.SpeechSynthesis;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using UnityEngine.Windows;
using UnityPlayer;
namespace Plugin.Windows10
{
public class VoiceSpeech
{
static SpeechRecognizer speechRecognizer = null;
static bool isListening = false;
public static string Output;
public static MediaElement Media;
static List<string> questions = new List<string>();
static List<string> answers = new List<string>();
public static async void StartListening(object sender, EventArgs e)
{
try
{
Dictionary<string, string> qaList = sender as Dictionary<string, string>;
questions.Clear();
answers.Clear();
foreach (var entity in qaList)
{
questions.Add(entity.Key);
answers.Add(entity.Value);
}
speechRecognizer = new SpeechRecognizer();
speechRecognizer.Constraints.Add(new SpeechRecognitionListConstraint(questions, "AlwaysOnGrammar"));
SpeechRecognitionCompilationResult compilationResult = await speechRecognizer.CompileConstraintsAsync();
if (compilationResult.Status != SpeechRecognitionResultStatus.Success)
throw new Exception();
speechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
speechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
Debug.WriteLine("Starting to listen");
await speechRecognizer.ContinuousRecognitionSession.StartAsync();
isListening = true;
}
catch (Exception ex)
{
isListening = false;
}
}
public static async void StopListening(object sender, EventArgs e)
{
if (speechRecognizer != null)
{
try {
if (isListening)
{
Debug.WriteLine("Stopping to listen");
speechRecognizer.ContinuousRecognitionSession.Completed -= ContinuousRecognitionSession_Completed;
speechRecognizer.ContinuousRecognitionSession.ResultGenerated -= ContinuousRecognitionSession_ResultGenerated;
await speechRecognizer.ContinuousRecognitionSession.StopAsync();
isListening = false;
}
}
catch (Exception ex)
{
speechRecognizer.Dispose();
speechRecognizer = null;
isListening = false;
}
}
}
private static async Task Speak(string text)
{
SpeechSynthesizer synth = new SpeechSynthesizer();
SpeechSynthesisStream synthStream =
await synth.SynthesizeTextToStreamAsync(text);
Media.SetSource(synthStream, synthStream.ContentType);
}
private static async void VoiceResponse(string resp)
{
if (isListening)
{
await speechRecognizer.ContinuousRecognitionSession.StopAsync();
isListening = false;
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Speak(resp);
});
await speechRecognizer.ContinuousRecognitionSession.StartAsync();
isListening = true;
}
}
private static async void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
Debug.WriteLine("Result Generated");
int index = 0;
foreach(string q in questions)
{
if (args.Result.Text == q)
{
VoiceResponse(answers[index]);
/*add*/
InvokeUnity(index);
/**/
}
index++;
}
}
private static void ContinuousRecognitionSession_Completed(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionCompletedEventArgs args)
{
Debug.WriteLine("Completed");
if (args.Status != SpeechRecognitionResultStatus.Success)
{
if (args.Status == SpeechRecognitionResultStatus.TimeoutExceeded)
{
}
}
}
/*add*/
private static void InvokeUnity(int index) {
switch (index) {
case 0:
InvokeUnity_generateFlare();
break;
case 1:
InvokeUnity_proceedFlare();
break;
}
}
/**/
/*add*/
private static void InvokeUnity_generateFlare() {
if (UnityPlayer.AppCallbacks.Instance.IsInitialized()) {
UnityPlayer.AppCallbacks.Instance.InvokeOnAppThread(new UnityPlayer.AppCallbackItem(() => {
Communications.generateFlareFromXAML();
}
), false);
}
}
private static void InvokeUnity_proceedFlare() {
if (UnityPlayer.AppCallbacks.Instance.IsInitialized()) {
UnityPlayer.AppCallbacks.Instance.InvokeOnAppThread(new UnityPlayer.AppCallbackItem(() => {
Communications.proceedFlareFromXAML();
}
), false);
}
}
/**/
/*add*/
public delegate void UnityEvent(object arg);
public sealed class Communications {
public static void generateFlareFromXAML() {
UnityEngine.GameObject go = UnityEngine.GameObject.Find("CortanaController");
go.GetComponent<InvokedByCortana>().generateFlare();
}
public static void proceedFlareFromXAML() {
UnityEngine.GameObject go = UnityEngine.GameObject.Find("CortanaController");
go.GetComponent<InvokedByCortana>().proceedFlare();
}
public static void SetEvent(UnityEvent e) {
UnityEngine.GameObject go = UnityEngine.GameObject.Find("CortanaController");
if (go != null) {
go.GetComponent<InvokedByCortana>().onEvent = new InvokedByCortana.OnEvent(e);
} else {
throw new Exception("Flare not found, have exported the correct scene?");
}
}
}
/**/
}
}
@flushpot1125

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.