/// <summary> /// Converts the text value to an audio file and then plays the audio file. /// </summary> /// <param name="textToSpeak">stores the text to be converted to audio</param> public async Task <bool> SpeakTheTextAsync(string textToSpeak) { _misty.ChangeLED(0, 255, 255, null); // light blue IsBusy = true; // Set busy flag so others will wait until not busy speaking the text. try { _misty.SkillLogger.LogVerbose($"MistySpeechApi : IN speakTheTextAsync -- textToSpeak = XX{textToSpeak}XX"); // Parameters to send with request to Google TTS API. // For more information see the Google TTS API developer docs: // https://cloud.google.com/text-to-speech/docs/reference/rest/v1beta1/text/synthesize var arguments = @"{ 'input': { 'text': '" + textToSpeak + @"'}, 'voice': { 'languageCode': 'en-US', 'ssmlGender': 'FEMALE' }, 'audioConfig': { 'audioEncoding': 'LINEAR16', 'effectsProfileId': [ 'handset-class-device' ], 'pitch': 0, 'speakingRate': 0.91 } }"; _misty.SkillLogger.LogVerbose("SendExternalRequestAsync - Sending TextToSpeak to Google TTS Api"); ISendExternalRequestResponse sdata = await _misty.SendExternalRequestAsync("POST", MistyApiInfo.GoogleTextToSpeechUrl, "Bearer", GoogleAuthToken, arguments, false, false, null, "application/json"); _misty.SkillLogger.LogVerbose("SendExternalRequestAsync - BACK FROM Google TTS Api"); string jsonStrResponse = sdata.Data.Data.ToString(); Newtonsoft.Json.Linq.JObject jsonResp = Newtonsoft.Json.Linq.JObject.Parse(jsonStrResponse); byte[] audio_data = Convert.FromBase64String(jsonResp["audioContent"].ToString()); string audio_filename = "tts_response.wav"; // Save the base64 audio file. _misty.SaveAudio(audio_filename, audio_data, true, true, null); _misty.Wait(5000); _misty.ChangeLED(0, 255, 0, null); // Green return(true); } catch (Exception ex) { _misty.SkillLogger.Log($"MistySpeechApi: IN SpeechResponseCallback: => Exception", ex); return(true); } finally { IsBusy = false; } }
private void ProcessQueryResult(object sender, string stringResult) { JObject dynamicResponse = JObject.Parse(stringResult); string color = ((string)dynamicResponse["queryResult"]?["parameters"]?["Color"])?.ToLower(); string position = ((string)dynamicResponse["queryResult"]?["parameters"]?["Direction"])?.ToLower(); string outputAudioString = (string)dynamicResponse["outputAudio"]; byte[] outputAudio = Convert.FromBase64String(outputAudioString); try { JsonConverter[] converters = new JsonConverter[2] { new ProtoByteStringConverter(), new OutputAudioEncodingConverter() }; DetectIntentResponse response = JsonConvert.DeserializeObject <DetectIntentResponse>(stringResult, converters); string intent = response?.QueryResult?.Intent?.DisplayName; string defaultResponse = response?.QueryResult?.FulfillmentText; switch (intent) { case "ChangeLED": if (color != null) { Color argbColor = Color.FromName(color); _misty.ChangeLED(argbColor.R, argbColor.G, argbColor.B, null); } break; case "Joke": _misty.PlayAudio(defaultResponse, 30, null); break; case "Move your arms": if (position == "up") { _misty.MoveArms(-90, -90, 50, 50, null, AngularUnit.Degrees, null); } if (position == "down") { _misty.MoveArms(90, 90, 50, 50, null, AngularUnit.Degrees, null); } _misty.Halt(null, null); break; case "MoveHeadPosition": if (position == "up") { _misty.MoveHead(-100, 0, 0, 50, AngularUnit.Degrees, null); } if (position == "down") { _misty.MoveHead(100, 0, 0, 50, AngularUnit.Degrees, null); } if (position == "right") { _misty.MoveHead(0, 0, -100, 50, AngularUnit.Degrees, null); } if (position == "left") { _misty.MoveHead(0, 0, 100, 50, AngularUnit.Degrees, null); } break; default: _misty.SaveAudio("tts.wav", outputAudio, true, true, null); break; } } catch (Exception ex) { } }