Skip to content

Commit 8a0491b

Browse files
authored
Merge pull request #643 from watson-developer-cloud/tts-example-play
fix(TTS): update TTS example to play music and using normal synthesize
2 parents eabc856 + 9006680 commit 8a0491b

File tree

1 file changed

+8
-77
lines changed

1 file changed

+8
-77
lines changed

Examples/ExampleTextToSpeechV1.cs

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,7 @@ void Update()
6161
{
6262
if (Input.GetKeyDown(KeyCode.Return))
6363
{
64-
service.SynthesizeUsingWebsockets(textInput.text);
65-
textInput.text = waitingText;
66-
}
67-
68-
while(service != null && !service.IsListening)
69-
{
70-
if (audioStream != null && audioStream.Length > 0)
71-
{
72-
Log.Debug("ExampleTextToSpeech", "Audio stream of {0} bytes received!", audioStream.Length.ToString()); // Use audioStream and play audio
73-
// _recording = WaveFile.ParseWAV("myClip", audioStream);
74-
// PlayClip(_recording);
75-
}
76-
textInput.text = placeholderText;
77-
audioStream = null;
78-
StartListening(); // need to connect because service disconnect websocket after transcribing https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-usingWebSocket#WSsend
64+
Runnable.Run(ExampleSynthesize(textInput.text));
7965
}
8066
}
8167

@@ -98,50 +84,17 @@ private IEnumerator CreateService()
9884
{
9985
service.SetServiceUrl(serviceUrl);
10086
}
101-
102-
Active = true;
10387
}
10488

105-
private void OnError(string error)
89+
#region Synthesize Example
90+
private IEnumerator ExampleSynthesize(string text)
10691
{
107-
Active = false;
108-
109-
Log.Debug("ExampleTextToSpeech.OnError()", "Error! {0}", error);
110-
}
111-
112-
private void StartListening()
113-
{
114-
Log.Debug("ExampleTextToSpeech", "start-listening");
115-
service.Voice = allisionVoice;
116-
service.OnError = OnError;
117-
service.StartListening(OnSynthesize);
118-
}
119-
120-
public bool Active
121-
{
122-
get { return service.IsListening; }
123-
set
92+
if (string.IsNullOrEmpty(text))
12493
{
125-
if (value && !service.IsListening)
126-
{
127-
StartListening();
128-
}
129-
else if (!value && service.IsListening)
130-
{
131-
Log.Debug("ExampleTextToSpeech", "stop-listening");
132-
service.StopListening();
133-
}
134-
}
135-
}
136-
137-
private void OnSynthesize(byte[] result) {
138-
Log.Debug("ExampleTextToSpeechV1", "Binary data received!");
139-
audioStream = ConcatenateByteArrays(audioStream, result);
140-
}
94+
text = synthesizeText;
95+
Log.Debug("ExampleTextToSpeechV1", "Using default text, please enter your own text in dialog box!");
14196

142-
#region Synthesize Without Websocket Connection
143-
private IEnumerator ExampleSynthesize()
144-
{
97+
}
14598
byte[] synthesizeResponse = null;
14699
AudioClip clip = null;
147100
service.Synthesize(
@@ -152,7 +105,7 @@ private IEnumerator ExampleSynthesize()
152105
clip = WaveFile.ParseWAV("myClip", synthesizeResponse);
153106
PlayClip(clip);
154107
},
155-
text: synthesizeText,
108+
text: text,
156109
voice: allisionVoice,
157110
accept: synthesizeMimeType
158111
);
@@ -180,27 +133,5 @@ private void PlayClip(AudioClip clip)
180133
}
181134
}
182135
#endregion
183-
184-
#region Concatenate Byte Arrays
185-
private byte[] ConcatenateByteArrays(byte[] a, byte[] b)
186-
{
187-
if (a == null || a.Length == 0)
188-
{
189-
return b;
190-
}
191-
else if (b == null || b.Length == 0)
192-
{
193-
return a;
194-
}
195-
else
196-
{
197-
List<byte> list1 = new List<byte>(a);
198-
List<byte> list2 = new List<byte>(b);
199-
list1.AddRange(list2);
200-
byte[] result = list1.ToArray();
201-
return result;
202-
}
203-
}
204-
#endregion
205136
}
206137
}

0 commit comments

Comments
 (0)