diff --git a/Examples/ExampleTextToSpeechV1.cs b/Examples/ExampleTextToSpeechV1.cs index 5ef3f7bd2..e3f213ac3 100644 --- a/Examples/ExampleTextToSpeechV1.cs +++ b/Examples/ExampleTextToSpeechV1.cs @@ -61,21 +61,7 @@ void Update() { if (Input.GetKeyDown(KeyCode.Return)) { - service.SynthesizeUsingWebsockets(textInput.text); - textInput.text = waitingText; - } - - while(service != null && !service.IsListening) - { - if (audioStream != null && audioStream.Length > 0) - { - Log.Debug("ExampleTextToSpeech", "Audio stream of {0} bytes received!", audioStream.Length.ToString()); // Use audioStream and play audio - // _recording = WaveFile.ParseWAV("myClip", audioStream); - // PlayClip(_recording); - } - textInput.text = placeholderText; - audioStream = null; - StartListening(); // need to connect because service disconnect websocket after transcribing https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-usingWebSocket#WSsend + Runnable.Run(ExampleSynthesize(textInput.text)); } } @@ -98,50 +84,17 @@ private IEnumerator CreateService() { service.SetServiceUrl(serviceUrl); } - - Active = true; } - private void OnError(string error) + #region Synthesize Example + private IEnumerator ExampleSynthesize(string text) { - Active = false; - - Log.Debug("ExampleTextToSpeech.OnError()", "Error! {0}", error); - } - - private void StartListening() - { - Log.Debug("ExampleTextToSpeech", "start-listening"); - service.Voice = allisionVoice; - service.OnError = OnError; - service.StartListening(OnSynthesize); - } - - public bool Active - { - get { return service.IsListening; } - set + if (string.IsNullOrEmpty(text)) { - if (value && !service.IsListening) - { - StartListening(); - } - else if (!value && service.IsListening) - { - Log.Debug("ExampleTextToSpeech", "stop-listening"); - service.StopListening(); - } - } - } - - private void OnSynthesize(byte[] result) { - Log.Debug("ExampleTextToSpeechV1", "Binary data received!"); - audioStream = ConcatenateByteArrays(audioStream, result); - } + text = synthesizeText; + Log.Debug("ExampleTextToSpeechV1", "Using default text, please enter your own text in dialog box!"); - #region Synthesize Without Websocket Connection - private IEnumerator ExampleSynthesize() - { + } byte[] synthesizeResponse = null; AudioClip clip = null; service.Synthesize( @@ -152,7 +105,7 @@ private IEnumerator ExampleSynthesize() clip = WaveFile.ParseWAV("myClip", synthesizeResponse); PlayClip(clip); }, - text: synthesizeText, + text: text, voice: allisionVoice, accept: synthesizeMimeType ); @@ -180,27 +133,5 @@ private void PlayClip(AudioClip clip) } } #endregion - - #region Concatenate Byte Arrays - private byte[] ConcatenateByteArrays(byte[] a, byte[] b) - { - if (a == null || a.Length == 0) - { - return b; - } - else if (b == null || b.Length == 0) - { - return a; - } - else - { - List list1 = new List(a); - List list2 = new List(b); - list1.AddRange(list2); - byte[] result = list1.ToArray(); - return result; - } - } - #endregion } }