Repository navigation
Expand file tree
/
Copy pathTestTextToSpeech.cs
More file actions
executable file
·387 lines (331 loc) · 16.5 KB
/
Copy pathTestTextToSpeech.cs
File metadata and controls
executable file
·387 lines (331 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using UnityEngine;
using System.Collections;
using IBM.Watson.DeveloperCloud.Services.TextToSpeech.v1;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;
using FullSerializer;
using System;
using System.IO;
using System.Collections.Generic;
using IBM.Watson.DeveloperCloud.Connection;
namespace IBM.Watson.DeveloperCloud.UnitTests
{
public class TestTextToSpeech : UnitTest
{
private string _username = null;
private string _password = null;
private fsSerializer _serializer = new fsSerializer();
//private string _token = "<authentication-token>";
TextToSpeech _textToSpeech;
string _testString = "<speak version=\"1.0\"><say-as interpret-as=\"letters\">I'm sorry</say-as>. <prosody pitch=\"150Hz\">This is Text to Speech!</prosody><express-as type=\"GoodNews\">I'm sorry. This is Text to Speech!</express-as></speak>";
string _testConversationString = "<phoneme alphabet=\\\"ipa\\\" ph=\\\"\\u02c8\\u025b\\u0259\\u02c8\\u0279\\u0259n\\\">Arin</phoneme>";
string _createdCustomizationId;
CustomVoiceUpdate _customVoiceUpdate;
string _customizationName = "unity-example-customization";
string _customizationLanguage = "en-US";
string _customizationDescription = "A text to speech voice customization created within Unity.";
string _testWord = "Watson";
private bool _synthesizeTested = false;
private bool _synthesizeConversationTested = false;
private bool _getVoicesTested = false;
private bool _getVoiceTested = false;
private bool _getPronuciationTested = false;
private bool _getCustomizationsTested = false;
private bool _createCustomizationTested = false;
private bool _deleteCustomizationTested = false;
private bool _getCustomizationTested = false;
private bool _updateCustomizationTested = false;
private bool _getCustomizationWordsTested = false;
private bool _addCustomizationWordsTested = false;
private bool _deleteCustomizationWordTested = false;
private bool _getCustomizationWordTested = false;
public override IEnumerator RunTest()
{
LogSystem.InstallDefaultReactors();
VcapCredentials vcapCredentials = new VcapCredentials();
fsData data = null;
string result = null;
var vcapUrl = Environment.GetEnvironmentVariable("VCAP_URL");
var vcapUsername = Environment.GetEnvironmentVariable("VCAP_USERNAME");
var vcapPassword = Environment.GetEnvironmentVariable("VCAP_PASSWORD");
using (SimpleGet simpleGet = new SimpleGet(vcapUrl, vcapUsername, vcapPassword))
{
while (!simpleGet.IsComplete)
yield return null;
result = simpleGet.Result;
}
// Add in a parent object because Unity does not like to deserialize root level collection types.
result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");
// Convert json to fsResult
fsResult r = fsJsonParser.Parse(result, out data);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);
// Convert fsResult to VcapCredentials
object obj = vcapCredentials;
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);
// Set credentials from imported credntials
Credential credential = vcapCredentials.VCAP_SERVICES["text_to_speech"];
_username = credential.Username.ToString();
_password = credential.Password.ToString();
_url = credential.Url.ToString();
// Create credential and instantiate service
Credentials credentials = new Credentials(_username, _password, _url);
// Or authenticate using token
//Credentials credentials = new Credentials(_url)
//{
// AuthenticationToken = _token
//};
_textToSpeech = new TextToSpeech(credentials);
// Synthesize
Log.Debug("TestTextToSpeech.RunTest()", "Attempting synthesize.");
_textToSpeech.Voice = VoiceType.en_US_Allison;
_textToSpeech.ToSpeech(HandleToSpeechCallback, OnFail, _testString, true);
while (!_synthesizeTested)
yield return null;
// Synthesize Conversation string
Log.Debug("TestTextToSpeech.RunTest()", "Attempting synthesize a string as returned by Watson Conversation.");
_textToSpeech.Voice = VoiceType.en_US_Allison;
_textToSpeech.ToSpeech(HandleConversationToSpeechCallback, OnFail, _testConversationString, true);
while (!_synthesizeConversationTested)
yield return null;
// Get Voices
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get voices.");
_textToSpeech.GetVoices(OnGetVoices, OnFail);
while (!_getVoicesTested)
yield return null;
// Get Voice
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get voice {0}.", VoiceType.en_US_Allison);
_textToSpeech.GetVoice(OnGetVoice, OnFail, VoiceType.en_US_Allison);
while (!_getVoiceTested)
yield return null;
// Get Pronunciation
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get pronunciation of {0}", _testWord);
_textToSpeech.GetPronunciation(OnGetPronunciation, OnFail, _testWord, VoiceType.en_US_Allison);
while (!_getPronuciationTested)
yield return null;
// Get Customizations
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get a list of customizations");
_textToSpeech.GetCustomizations(OnGetCustomizations, OnFail);
while (!_getCustomizationsTested)
yield return null;
// Create Customization
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to create a customization");
_textToSpeech.CreateCustomization(OnCreateCustomization, OnFail, _customizationName, _customizationLanguage, _customizationDescription);
while (!_createCustomizationTested)
yield return null;
// Get Customization
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get a customization");
if (!_textToSpeech.GetCustomization(OnGetCustomization, OnFail, _createdCustomizationId))
Log.Debug("TestTextToSpeech.RunTest()", "Failed to get custom voice model!");
while (!_getCustomizationTested)
yield return null;
// Update Customization
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to update a customization");
Word[] wordsToUpdateCustomization =
{
new Word()
{
word = "hello",
translation = "hullo"
},
new Word()
{
word = "goodbye",
translation = "gbye"
},
new Word()
{
word = "hi",
translation = "ohioooo"
}
};
_customVoiceUpdate = new CustomVoiceUpdate()
{
words = wordsToUpdateCustomization,
description = "My updated description",
name = "My updated name"
};
if (!_textToSpeech.UpdateCustomization(OnUpdateCustomization, OnFail, _createdCustomizationId, _customVoiceUpdate))
Log.Debug("TestTextToSpeech.UpdateCustomization()", "Failed to update customization!");
while (!_updateCustomizationTested)
yield return null;
// Get Customization Words
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get a customization's words");
if (!_textToSpeech.GetCustomizationWords(OnGetCustomizationWords, OnFail, _createdCustomizationId))
Log.Debug("TestTextToSpeech.GetCustomizationWords()", "Failed to get {0} words!", _createdCustomizationId);
while (!_getCustomizationWordsTested)
yield return null;
// Add Customization Words
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to add words to a customization");
Word[] wordArrayToAddToCustomization =
{
new Word()
{
word = "bananna",
translation = "arange"
},
new Word()
{
word = "orange",
translation = "gbye"
},
new Word()
{
word = "tomato",
translation = "tomahto"
}
};
Words wordsToAddToCustomization = new Words()
{
words = wordArrayToAddToCustomization
};
if (!_textToSpeech.AddCustomizationWords(OnAddCustomizationWords, OnFail, _createdCustomizationId, wordsToAddToCustomization))
Log.Debug("TestTextToSpeech.AddCustomizationWords()", "Failed to add words to {0}!", _createdCustomizationId);
while (!_addCustomizationWordsTested)
yield return null;
// Get Customization Word
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to get the translation of a custom voice model's word.");
string customIdentifierWord = wordsToUpdateCustomization[0].word;
if (!_textToSpeech.GetCustomizationWord(OnGetCustomizationWord, OnFail, _createdCustomizationId, customIdentifierWord))
Log.Debug("TestTextToSpeech.GetCustomizationWord()", "Failed to get the translation of {0} from {1}!", customIdentifierWord, _createdCustomizationId);
while (!_getCustomizationWordTested)
yield return null;
// Delete Customization Word
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to delete customization word from custom voice model.");
string wordToDelete = "goodbye";
if (!_textToSpeech.DeleteCustomizationWord(OnDeleteCustomizationWord, OnFail, _createdCustomizationId, wordToDelete))
Log.Debug("TestTextToSpeech.DeleteCustomizationWord()", "Failed to delete {0} from {1}!", wordToDelete, _createdCustomizationId);
while (!_deleteCustomizationWordTested)
yield return null;
// Delete Customization
Log.Debug("TestTextToSpeech.RunTest()", "Attempting to delete a customization");
if (!_textToSpeech.DeleteCustomization(OnDeleteCustomization, OnFail, _createdCustomizationId))
Log.Debug("TestTextToSpeech.DeleteCustomization()", "Failed to delete custom voice model!");
while (!_deleteCustomizationTested)
yield return null;
Log.Debug("TestTextToSpeech.RunTest()", "Text to Speech examples complete.");
yield break;
}
void HandleToSpeechCallback(AudioClip clip, Dictionary<string, object> customData)
{
PlayClip(clip);
}
private void PlayClip(AudioClip clip)
{
if (Application.isPlaying && clip != null)
{
GameObject audioObject = new GameObject("AudioObject");
AudioSource source = audioObject.AddComponent<AudioSource>();
source.spatialBlend = 0.0f;
source.loop = false;
source.clip = clip;
source.Play();
GameObject.Destroy(audioObject, clip.length);
_synthesizeTested = true;
}
}
private void HandleConversationToSpeechCallback(AudioClip clip, Dictionary<string, object> customData)
{
if (clip != null)
{
_synthesizeConversationTested = true;
}
}
private void OnGetVoices(Voices voices, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetVoices()", "{0}", customData["json"].ToString());
Test(voices != null);
_getVoicesTested = true;
}
private void OnGetVoice(Voice voice, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetVoice()", "{0}", customData["json"].ToString());
Test(voice != null);
_getVoiceTested = true;
}
private void OnGetPronunciation(Pronunciation pronunciation, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetPronunciation()", "{0}", customData["json"].ToString());
Test(pronunciation != null);
_getPronuciationTested = true;
}
private void OnGetCustomizations(Customizations customizations, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetCustomizations()", "{0}", customData["json"].ToString());
Test(customizations != null);
_getCustomizationsTested = true;
}
private void OnCreateCustomization(CustomizationID customizationID, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnCreateCustomization()", "{0}", customData["json"].ToString());
_createdCustomizationId = customizationID.customization_id;
Test(customizationID != null);
_createCustomizationTested = true;
}
private void OnDeleteCustomization(bool success, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnDeleteCustomization()", "{0}", customData["json"].ToString());
_createdCustomizationId = null;
Test(success);
_deleteCustomizationTested = true;
}
private void OnGetCustomization(Customization customization, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetCustomization()", "{0}", customData["json"].ToString());
Test(customization != null);
_getCustomizationTested = true;
}
private void OnUpdateCustomization(bool success, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnUpdateCustomization()", "{0}", customData["json"].ToString());
Test(success);
_updateCustomizationTested = true;
}
private void OnGetCustomizationWords(Words words, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetCustomizationWords()", "{0}", customData["json"].ToString());
Test(words != null);
_getCustomizationWordsTested = true;
}
private void OnAddCustomizationWords(bool success, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnAddCustomizationWords()", "{0}", customData["json"].ToString());
Test(success);
_addCustomizationWordsTested = true;
}
private void OnDeleteCustomizationWord(bool success, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnDeleteCustomizationWord()", "{0}", customData["json"].ToString());
Test(success);
_deleteCustomizationWordTested = true;
}
private void OnGetCustomizationWord(Translation translation, Dictionary<string, object> customData)
{
Log.Debug("TestTextToSpeech.OnGetCustomizationWord()", "{0}", customData["json"].ToString());
Test(translation != null);
_getCustomizationWordTested = true;
}
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
{
Log.Error("ExampleRetrieveAndRank.OnFail()", "Error received: {0}", error.ToString());
}
}
}