async Task <CallServiceResponse <TTServiceResponse> > ICallServices <TTServiceResponse> .CallServiceAsync(Uri url, System.Collections.Generic.Dictionary <string, string> apiArgs) { Log.WriteLine("GenericServiceAsync: url:" + url); CallServiceResponse <TTServiceResponse> response = new CallServiceResponse <TTServiceResponse>(service); foreach (Settings.Request request in service.requests.Where(p => p.argType == "url")) { response.Request = request; // init when response object is newed up? await HttpMethods.CallApiAsync(response, url, apiArgs); switch (request.response.type) { case "binary": System.IO.File.WriteAllBytes(response.FileName + ".bin", response.ResponseBytes); break; case "json": await HttpMethods.ExtractResultAsync(response); break; default: break; } return(response); } throw new Exception("GenericServiceAsync: url: no match for request: type:" + service.classInterface); }
public static string ParseAnalyzerStringized; // Parse service only public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs) { // so far text and apiArgs are unused. The purpose of this call is to make a one-time invocation to retrieve Microsoft's parse analyzer IDs. Log.WriteLine("MicrosoftCognitiveParseAnalyzersServices: CallServiceAsync"); CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Request = Array.Find(service.requests, p => p.argType == "text"); await HttpMethods.CallApiAsync(response, text, apiArgs); #if true // TODO: implement ExtractResultAsync? await HttpMethods.ExtractResultAsync(response); ParseAnalysers = response.ResponseResult.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // split at ", " into array ParseAnalyzerStringized = " \"" + string.Join("\", \"", ParseAnalysers) + "\" "; // rejoing array into a string. each item must be quoted and separated by a comma. #else ParseAnalysers = new System.Collections.Generic.List <string>(); foreach (JToken tokAnalyzerResult in response.ResponseJToken) { ParseAnalysers.Add(tokAnalyzerResult["id"].ToString()); ParseAnalyzerStringized += "'" + tokAnalyzerResult["id"].ToString() + "', "; } ParseAnalyzerStringized = ParseAnalyzerStringized.Substring(0, ParseAnalyzerStringized.Length - 2); // remove trailing "', " #endif return(response); }
public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(byte[] audioBytes, System.Collections.Generic.Dictionary <string, string> apiArgs) { Log.WriteLine("HoundifySpeechToTextServices: audioBytes.Length:" + audioBytes.Length); //IntentServiceResponse response = new IntentServiceResponse(service); CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Request = Array.Find(service.requests, p => p.argType == "binary"); await HoundifyServices.HoundifyPostAsync(service, response, HttpMethods.MakeUri(service.requests[0], apiArgs, null, ""), audioBytes, apiArgs); return(response); }
public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs) { Log.WriteLine("HoundifyIntentServices: text:" + text); //IntentServiceResponse response = new IntentServiceResponse(service); CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Request = Array.Find(service.requests, p => p.argType == "text"); await HoundifyServices.HoundifyPostAsync(service, response, HttpMethods.MakeUri(service.requests[0], apiArgs, null, text), new byte[0], apiArgs); return(response); }
//private static System.Speech.Recognition.SpeechRecognizedEventArgs WakeUpWordResults; public static async System.Threading.Tasks.Task <string> MicrophoneToTextAsync() { CallServices <ISpeechToTextService, ISpeechToTextServiceResponse> wSTT = new CallServices <ISpeechToTextService, ISpeechToTextServiceResponse>(null); System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(Options.options.locale.language); using (System.Speech.Recognition.SpeechRecognitionEngine RecognitionEngine = new System.Speech.Recognition.SpeechRecognitionEngine(ci)) { RecognitionEngine.SetInputToDefaultAudioDevice(); RecognitionEngine.LoadGrammar(new System.Speech.Recognition.DictationGrammar()); System.Speech.Recognition.RecognitionResult WakeUpWordResult = RecognitionEngine.Recognize(); if (WakeUpWordResult == null) { return(null); } using (System.IO.FileStream waveStream = new System.IO.FileStream(Options.options.tempFolderPath + Options.options.audio.speechSynthesisFileName, System.IO.FileMode.Create)) { WakeUpWordResult.Audio.WriteToWaveStream(waveStream); waveStream.Flush(); waveStream.Close(); } byte[] bytes = await Helpers.ReadBytesFromFileAsync(Options.options.audio.speechSynthesisFileName); System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); string text = WakeUpWordResult.Text; int sampleRate = await Audio.GetSampleRateAsync(Options.options.tempFolderPath + Options.options.audio.speechSynthesisFileName); System.Collections.Generic.Dictionary <string, string> apiArgs = new System.Collections.Generic.Dictionary <string, string>() { { "sampleRate", sampleRate.ToString() } }; foreach (ISpeechToTextService STT in new FindServices <ISpeechToTextService>(Options.commandservices["SpeechToText"].preferredServices).PreferredOrderingOfServices) { // ISpeechToTextService STT = (ISpeechToTextService)constructor.Invoke(Type.EmptyTypes);; SpeechToTextServiceResponse r = await STT.SpeechToTextServiceAsync(bytes, apiArgs); if (r.StatusCode == 200) { text = r.ResponseResult; Console.WriteLine(r.Service.name + ":\"" + text + "\" Total Elapsed ms:" + r.TotalElapsedMilliseconds + " Request Elapsed ms:" + r.RequestElapsedMilliseconds); return(text); } else { Console.WriteLine(r.Service.name + " not available."); } } CallServiceResponse <ISpeechToTextServiceResponse> response = await wSTT.CallServiceAsync(bytes, apiArgs); text = response.ResponseResult; Console.WriteLine("Windows STT (default):\"" + text + "\" Total Elapsed ms:" + response.TotalElapsedMilliseconds + " Request Elapsed ms:" + response.RequestElapsedMilliseconds); return(text); } }
public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs) { Log.WriteLine("text:" + text); CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Service = service; response.Request = Array.Find(service.requests, p => p.argType == "text"); response.stopWatch.Start(); response.ResponseBytes = await TextToSpeech.TextToSpeechServiceAsync(text, apiArgs); response.StatusCode = 200; response.stopWatch.Stop(); response.TotalElapsedMilliseconds = response.RequestElapsedMilliseconds = response.stopWatch.ElapsedMilliseconds; Log.WriteLine("Total Elapsed milliseconds:" + response.TotalElapsedMilliseconds); return(response); }
public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(byte[] audioBytes, System.Collections.Generic.Dictionary <string, string> apiArgs) { Log.WriteLine("audio file length:" + audioBytes.Length); CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Service = service; response.Request = Array.Find(service.requests, p => p.argType == "binary"); response.stopWatch.Start(); response.ResponseResult = await SpeechToText.SpeechToTextServiceAsync(audioBytes, apiArgs); response.StatusCode = 200; response.stopWatch.Stop(); response.TotalElapsedMilliseconds = response.RequestElapsedMilliseconds = response.stopWatch.ElapsedMilliseconds; Log.WriteLine("Total Elapsed milliseconds:" + response.TotalElapsedMilliseconds); return(response); }
public virtual System.Collections.Generic.IEnumerable <System.Threading.Tasks.Task <CallServiceResponse <TServiceResponse> > > RunAllPreferredGenericServicesAsync(System.Collections.Generic.IEnumerable <ICallServices <TServiceResponse> > runs, Uri url, System.Collections.Generic.Dictionary <string, string> apiArgs) { System.Collections.Generic.List <System.Threading.Tasks.Task <CallServiceResponse <TServiceResponse> > > tasks = new System.Collections.Generic.List <Task <CallServiceResponse <TServiceResponse> > >(); foreach (ICallServices <TServiceResponse> run in runs) { // TODO: Time consuming puzzle. Never solved. I want to replace dynamic cast with a strong type, how? tasks.Add(System.Threading.Tasks.Task.Run(() => ((dynamic)run).CallServiceAsync(url, apiArgs)).ContinueWith((c) => { CallServiceResponse <TServiceResponse> r = c.Result.Result; if (r.StatusCode != 200) { Console.WriteLine(r.Service.name + " Generic (async): Failed with StatusCode of " + r.StatusCode); } else if (r.Request.response.type == "binary") { if (r.ResponseResult != null) { throw new Exception("RunAllPreferredGenericServicesRun: ResponseResult not null"); } if (r.ResponseBytes == null) { throw new Exception("RunAllPreferredGenericServicesRun: ResponseBytes is null"); } Console.WriteLine(r.Service.name + ": Generic (async): response length:" + r.ResponseBytes.Length + ": Total " + r.TotalElapsedMilliseconds + "ms Request " + r.RequestElapsedMilliseconds + "ms"); } else { if (r.ResponseBytes != null) { throw new Exception("RunAllPreferredGenericServicesRun: ResponseBytes not null"); } if (r.ResponseResult == null) { throw new Exception("RunAllPreferredGenericServicesRun: ResponseResult is null"); } Console.WriteLine(r.Service.name + ": Generic (async): ResponseResult:" + r.ResponseResult + ": Total " + r.TotalElapsedMilliseconds + "ms Request " + r.RequestElapsedMilliseconds + "ms"); } return(r); })); } return(tasks); }
public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(string text, System.Collections.Generic.Dictionary <string, string> apiArgs) { Log.WriteLine("MicrosoftCognitiveParseServices: Text:" + text); if (MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers == null) // calling ParseAnalyzersService. It's a dependency of ParseService. { CallServiceResponse <IGenericServiceResponse> responseAnalyzers = new CallServiceResponse <IGenericServiceResponse>(service); MicrosoftCognitiveParseAnalyzersServices parseAnalyersService = new MicrosoftCognitiveParseAnalyzersServices(Options.services["MicrosoftCognitiveParseAnalyzersService"].service); responseAnalyzers = await parseAnalyersService.CallServiceAsync(text, apiArgs); if (MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers == null || MicrosoftCognitiveParseAnalyzersServices.ParseAnalysers.Length == 0 || string.IsNullOrWhiteSpace(MicrosoftCognitiveParseAnalyzersServices.ParseAnalyzerStringized)) { throw new InvalidOperationException(); // can't continue without at least one Analyzer } } CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Request = Array.Find(service.requests, p => p.argType == "text"); System.Collections.Generic.List <Tuple <string, string> > jsonSubstitutes = new System.Collections.Generic.List <Tuple <string, string> >() { new Tuple <string, string>("{AnalyzerStringized}", MicrosoftCognitiveParseAnalyzersServices.ParseAnalyzerStringized), }; await HttpMethods.CallApiAsync(response, null, null, jsonSubstitutes, text, apiArgs); // example of using jsonSubstitutes await HttpMethods.ExtractResultAsync(response); Newtonsoft.Json.Linq.JArray arrayOfResults = Newtonsoft.Json.Linq.JArray.Parse(response.ResponseResult); foreach (Newtonsoft.Json.Linq.JToken s in arrayOfResults) { ConstituencyTreeNode root = ParseHelpers.ConstituencyTreeFromText(s.ToString()); string textTree = ParseHelpers.TextFromConstituencyTree(root); if (textTree != s.ToString()) { throw new FormatException(); } string words = ParseHelpers.WordsFromConstituencyTree(root); string[] printLines = ParseHelpers.FormatConstituencyTree(root); foreach (string p in printLines) { Console.WriteLine(p); } } return(response); }
public override async System.Threading.Tasks.Task <CallServiceResponse <IGenericServiceResponse> > CallServiceAsync(byte[] audioBytes, System.Collections.Generic.Dictionary <string, string> apiArgs) { int sampleRate = int.Parse(apiArgs["sampleRate"]); Log.WriteLine("audio file length:" + audioBytes.Length + " sampleRate:" + sampleRate); CallServiceResponse <IGenericServiceResponse> response = new CallServiceResponse <IGenericServiceResponse>(service); response.Request = Array.Find(service.requests, p => p.argType == "binary"); await HttpMethods.CallApiAsync(response, null, null, null, audioBytes, apiArgs); string JobID = response.ResponseJToken.SelectToken(".jobID").ToString(); Dictionary <string, string> dict = Helpers.stringToDictionary(service.requests[0].data.value, '&', '='); string ApiKey = dict["apikey"]; // TODO: move url to settings file string JobUrl = $"https://api.havenondemand.com/1/job/result/{JobID}?apikey={ApiKey}"; // using C# 6.0 string interpolation await HttpMethods.CallApiAuthAsync(response, new Uri(JobUrl), "", null); await HttpMethods.ExtractResultAsync(response); return(response); }
public static async System.Threading.Tasks.Task <string[]> WaitForWakeUpWordThenRecognizeRemainingSpeechAsync(string[] WakeUpWords) { Console.WriteLine("Say the wakeup word (" + string.Join(" ", WakeUpWords) + ") followed by the request ..."); CallServices <ISpeechToTextService, ISpeechToTextServiceResponse> wSTT = new CallServices <ISpeechToTextService, ISpeechToTextServiceResponse>(null); System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); // continues until return System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(Options.options.locale.language); while (true) { using (System.Speech.Recognition.SpeechRecognitionEngine RecognitionEngine = new System.Speech.Recognition.SpeechRecognitionEngine(ci)) { RecognitionEngine.SetInputToDefaultAudioDevice(); // build wakeup word grammar System.Speech.Recognition.GrammarBuilder wakeUpWordBuilder = new System.Speech.Recognition.GrammarBuilder(); wakeUpWordBuilder.Append(new System.Speech.Recognition.Choices(WakeUpWords)); // build words-after-wakeup word grammar System.Speech.Recognition.GrammarBuilder wordsAfterWakeUpWordBuilder = new System.Speech.Recognition.GrammarBuilder(); wordsAfterWakeUpWordBuilder.AppendWildcard(); System.Speech.Recognition.SemanticResultKey wordsAfterWakeUpWordKey = new System.Speech.Recognition.SemanticResultKey("wordsAfterWakeUpWordKey", wordsAfterWakeUpWordBuilder); wakeUpWordBuilder.Append(new System.Speech.Recognition.SemanticResultKey("wordsAfterWakeUpWordKey", wordsAfterWakeUpWordBuilder)); // initialize recognizer, wait for result, save result to file System.Speech.Recognition.Grammar g = new System.Speech.Recognition.Grammar(wakeUpWordBuilder); RecognitionEngine.LoadGrammar(g); if (Options.options.wakeup.initialSilenceTimeout == -1) { RecognitionEngine.InitialSilenceTimeout = TimeSpan.FromTicks(Int32.MaxValue); // never timeout } else { RecognitionEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(Options.options.wakeup.initialSilenceTimeout); // timesout after this much silence } RecognitionEngine.EndSilenceTimeout = TimeSpan.FromSeconds(Options.options.wakeup.endSilenceTimeout); // maximum silence allowed after hearing wakeup word #if true // experimenting with Babble and other timeouts RecognitionEngine.BabbleTimeout = TimeSpan.FromSeconds(0); #else RecognitionEngine.BabbleTimeout = TimeSpan.FromTicks(UInt32.MaxValue); #endif System.Speech.Recognition.RecognitionResult WakeUpWordResult = RecognitionEngine.Recognize(); // RecognitionResult is null when some unidentified timeout expires around 30 seconds. Can't find a way to make timeouts infinite so just looping. if (WakeUpWordResult == null) { continue; } using (System.IO.FileStream waveStream = new System.IO.FileStream(Options.options.tempFolderPath + Options.options.audio.speechSynthesisFileName, System.IO.FileMode.Create)) { WakeUpWordResult.Audio.WriteToWaveStream(waveStream); waveStream.Flush(); waveStream.Close(); } Console.WriteLine("Wake up word detected (" + WakeUpWordResult.Words[0].Text + "): confidence:" + WakeUpWordResult.Confidence + " Elapsed Ms:" + stopWatch.ElapsedMilliseconds); if (WakeUpWordResult.Confidence >= Options.options.wakeup.confidence) { byte[] bytes = await Helpers.ReadBytesFromFileAsync(Options.options.audio.speechSynthesisFileName); string text = WakeUpWordResult.Text; int sampleRate = await Audio.GetSampleRateAsync(Options.options.tempFolderPath + Options.options.audio.speechSynthesisFileName); System.Collections.Generic.Dictionary <string, string> apiArgs = new System.Collections.Generic.Dictionary <string, string>() { { "sampleRate", sampleRate.ToString() } }; #if false // for testing await windows.SpeechToTextAsync(bytes, apiArgs); Console.WriteLine("Windows STT (demo):\"" + windows.ResponseResult + "\" Total Elapsed ms:" + windows.TotalElapsedMilliseconds + " Request Elapsed ms:" + windows.RequestElapsedMilliseconds); #endif System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); foreach (ISpeechToTextService STT in new FindServices <ISpeechToTextService>(Options.commandservices["SpeechToText"].preferredServices).PreferredOrderingOfServices) { // ISpeechToTextService STT = (ISpeechToTextService)constructor.Invoke(Type.EmptyTypes); SpeechToTextServiceResponse r = await STT.SpeechToTextServiceAsync(bytes, apiArgs); if (r.StatusCode == 200) { text = r.ResponseResult; Console.WriteLine(r.Service.name + ":\"" + text + "\" Total Elapsed ms:" + r.TotalElapsedMilliseconds + " Request Elapsed ms:" + r.RequestElapsedMilliseconds); return(text.Split(" ".ToCharArray(), StringSplitOptions.None)); } else { Console.WriteLine(r.Service.name + " not available."); } } CallServiceResponse <ISpeechToTextServiceResponse> response = await wSTT.CallServiceAsync(bytes, apiArgs); text = response.ResponseResult; Console.WriteLine("Windows STT (default):\"" + text + "\" Total Elapsed ms:" + response.TotalElapsedMilliseconds + " Request Elapsed ms:" + response.RequestElapsedMilliseconds); return(text.Split(" ".ToCharArray(), StringSplitOptions.None)); } } } }