Skip to content

Commit

Permalink
fix(TTS): update TTS example to play music and using normal synthesize
Browse files Browse the repository at this point in the history
  • Loading branch information
mamoonraja committed Jun 17, 2020
1 parent eabc856 commit 9006680
Showing 1 changed file with 8 additions and 77 deletions.
85 changes: 8 additions & 77 deletions Examples/ExampleTextToSpeechV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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(
Expand All @@ -152,7 +105,7 @@ private IEnumerator ExampleSynthesize()
clip = WaveFile.ParseWAV("myClip", synthesizeResponse);
PlayClip(clip);
},
text: synthesizeText,
text: text,
voice: allisionVoice,
accept: synthesizeMimeType
);
Expand Down Expand Up @@ -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<byte> list1 = new List<byte>(a);
List<byte> list2 = new List<byte>(b);
list1.AddRange(list2);
byte[] result = list1.ToArray();
return result;
}
}
#endregion
}
}

0 comments on commit 9006680

Please sign in to comment.