Skip to content

Commit ad15336

Browse files
authored
Merge pull request #136 from watson-developer-cloud/devExp-541-textToSpeech
Dev exp 541 text to speech
2 parents b2d864d + 8e5d3f4 commit ad15336

9 files changed

Lines changed: 1692 additions & 61 deletions

File tree

Config.json.enc

0 Bytes
Binary file not shown.

Examples/ServiceExamples/Scripts/ExampleTextToSpeech.cs

Lines changed: 225 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using UnityEngine;
1919
using IBM.Watson.DeveloperCloud.Services.TextToSpeech.v1;
20+
using IBM.Watson.DeveloperCloud.Logging;
2021

2122
public class ExampleTextToSpeech : MonoBehaviour
2223
{
@@ -26,9 +27,116 @@ public class ExampleTextToSpeech : MonoBehaviour
2627

2728
void Start ()
2829
{
29-
m_TextToSpeech.Voice = VoiceType.en_US_Allison;
30-
m_TextToSpeech.ToSpeech(m_TestString, HandleToSpeechCallback, true);
31-
}
30+
LogSystem.InstallDefaultReactors();
31+
32+
//// Get Voices
33+
//Log.Debug("ExampleTextToSpeech", "Attempting to get voices.");
34+
//m_TextToSpeech.GetVoices(OnGetVoices);
35+
36+
//// Get Voice
37+
////string selectedVoice = "en-US_AllisonVoice";
38+
//Log.Debug("ExampleTextToSpeech", "Attempting to get voice {0}.", VoiceType.en_US_Allison);
39+
//m_TextToSpeech.GetVoice(OnGetVoice, VoiceType.en_US_Allison);
40+
41+
//// Get Pronunciation
42+
//string testWord = "Watson";
43+
//Log.Debug("ExampleTextToSpeech", "Attempting to get pronunciation of {0}", testWord);
44+
//m_TextToSpeech.GetPronunciation(OnGetPronunciation, testWord, VoiceType.en_US_Allison);
45+
46+
// Get Customizations
47+
//Log.Debug("ExampleTextToSpeech", "Attempting to get a list of customizations");
48+
//m_TextToSpeech.GetCustomizations(OnGetCustomizations);
49+
50+
// Create Customization
51+
//Log.Debug("ExampleTextToSpeech", "Attempting to create a customization");
52+
//string customizationName = "unity-example-customization";
53+
//string customizationLanguage = "en-US";
54+
//string customizationDescription = "A text to speech voice customization created within Unity.";
55+
//m_TextToSpeech.CreateCustomization(OnCreateCustomization, customizationName, customizationLanguage, customizationDescription);
56+
57+
// Delete Customization
58+
//Log.Debug("ExampleTextToSpeech", "Attempting to delete a customization");
59+
//string customizationIdentifierToDelete = "1476ea80-5355-4911-ac99-ba39162a2d34";
60+
//if (!m_TextToSpeech.DeleteCustomization(OnDeleteCustomization, customizationIdentifierToDelete))
61+
// Log.Debug("ExampleTextToSpeech", "Failed to delete custom voice model!");
62+
63+
// Get Customization
64+
//Log.Debug("ExampleTextToSpeech", "Attempting to get a customization");
65+
//string customizationIdentifierToGet = "1476ea80-5355-4911-ac99-ba39162a2d34";
66+
//if (!m_TextToSpeech.GetCustomization(OnGetCustomization, customizationIdentifierToGet))
67+
// Log.Debug("ExampleTextToSpeech", "Failed to get custom voice model!");
68+
69+
// Update Customization
70+
Log.Debug("ExampleTextToSpeech", "Attempting to update a customization");
71+
Word word0 = new Word();
72+
word0.word = "hello";
73+
word0.translation = "hullo";
74+
Word word1 = new Word();
75+
word1.word = "goodbye";
76+
word1.translation = "gbye";
77+
Word word2 = new Word();
78+
word2.word = "hi";
79+
word2.translation = "ohiooo";
80+
Word[] words = { word0, word1, word2 };
81+
CustomVoiceUpdate customVoiceUpdate = new CustomVoiceUpdate();
82+
customVoiceUpdate.words = words;
83+
customVoiceUpdate.description = "My updated description";
84+
customVoiceUpdate.name = "My updated name";
85+
string customizationIdToUpdate = "1476ea80-5355-4911-ac99-ba39162a2d34";
86+
if (!m_TextToSpeech.UpdateCustomization(OnUpdateCustomization, customizationIdToUpdate, customVoiceUpdate))
87+
Log.Debug("ExampleTextToSpeech", "Failed to update customization!");
88+
89+
// Get Customization Words
90+
//Log.Debug("ExampleTextToSpeech", "Attempting to get a customization's words");
91+
//string customIdentifierToGetWords = "1476ea80-5355-4911-ac99-ba39162a2d34";
92+
//if (!m_TextToSpeech.GetCustomizationWords(OnGetCustomizationWords, customIdentifierToGetWords))
93+
// Log.Debug("ExampleTextToSpeech", "Failed to get {0} words!", customIdentifierToGetWords);
94+
95+
// Add Customization Words
96+
//Log.Debug("ExampleTextToSpeech", "Attempting to add words to a customization");
97+
//string customIdentifierToAddWords = "1476ea80-5355-4911-ac99-ba39162a2d34";
98+
//Words words = new Words();
99+
//Word word0 = new Word();
100+
//word0.word = "bananna";
101+
//word0.translation = "bunanna";
102+
//Word word1 = new Word();
103+
//word1.word = "orange";
104+
//word1.translation = "arange";
105+
//Word word2 = new Word();
106+
//word2.word = "tomato";
107+
//word2.translation = "tomahto";
108+
//Word[] wordArray = { word0, word1, word2 };
109+
//words.words = wordArray;
110+
//if (!m_TextToSpeech.AddCustomizationWords(OnAddCustomizationWords, customIdentifierToAddWords, words))
111+
// Log.Debug("ExampleTextToSpeech", "Failed to add words to {0}!", customIdentifierToAddWords);
112+
113+
// Delete Customization Word
114+
//Log.Debug("ExampleTextToSpeech", "Attempting to delete customization word from custom voice model.");
115+
//string customIdentifierToDeleteWord = "1476ea80-5355-4911-ac99-ba39162a2d34";
116+
//string wordToDelete = "goodbye";
117+
//if (!m_TextToSpeech.DeleteCustomizationWord(OnDeleteCustomizationWord, customIdentifierToDeleteWord, wordToDelete))
118+
// Log.Debug("ExampleTextToSpeech", "Failed to delete {0} from {1}!", wordToDelete, customIdentifierToDeleteWord);
119+
120+
// Get Customization Word
121+
//Log.Debug("ExampleTextToSpeech", "Attempting to get the translation of a custom voice model's word.");
122+
//string customIdentifierToGetWord = "1476ea80-5355-4911-ac99-ba39162a2d34";
123+
//string customIdentifierWord = "hello";
124+
//if (!m_TextToSpeech.GetCustomizationWord(OnGetCustomizationWord, customIdentifierToGetWord, customIdentifierWord))
125+
// Log.Debug("ExampleTextToSpeech", "Failed to get the translation of {0} from {1}!", customIdentifierWord, customIdentifierToGetWord);
126+
127+
// Add Customization Word - This is not working. The PUT method is not supported by Unity.
128+
//Log.Debug("ExampleTextToSpeech", "Attempting to add a single word and translation to a custom voice model.");
129+
//string customIdentifierToAddWordAndTranslation = "1476ea80-5355-4911-ac99-ba39162a2d34";
130+
//string word = "grasshopper";
131+
//string translation = "guhrasshoppe";
132+
//if (!m_TextToSpeech.AddCustomizationWord(OnAddCustomizationWord, customIdentifierToAddWordAndTranslation, word, translation))
133+
// Log.Debug("ExampleTextToSpeech", "Failed to add {0}/{1} to {2}!", word, translation, customIdentifierToAddWordAndTranslation);
134+
135+
136+
//m_TextToSpeech.Voice = VoiceType.en_US_Allison;
137+
//m_TextToSpeech.ToSpeech(m_TestString, HandleToSpeechCallback, true);
138+
139+
}
32140

33141
void HandleToSpeechCallback (AudioClip clip)
34142
{
@@ -49,4 +157,118 @@ private void PlayClip(AudioClip clip)
49157
GameObject.Destroy(audioObject, clip.length);
50158
}
51159
}
160+
161+
private void OnGetVoices(Voices voices)
162+
{
163+
Log.Debug("ExampleTextToSpeech", "-----OnGetVoices-----");
164+
foreach (Voice voice in voices.voices)
165+
Log.Debug("ExampleTextToSpeech", "Voice | name: {0} | gender: {1} | language: {2} | customizable: {3} | description: {4}.", voice.name, voice.gender, voice.language, voice.customizable, voice.description);
166+
Log.Debug("ExampleTextToSpeech", "-----OnGetVoices-----");
167+
}
168+
169+
private void OnGetVoice(Voice voice)
170+
{
171+
Log.Debug("ExampleTextToSpeech", "-----OnGetVoice-----");
172+
Log.Debug("ExampleTextToSpeech", "Voice | name: {0} | gender: {1} | language: {2} | customizable: {3} | description: {4}", voice.name, voice.gender, voice.language, voice.customizable, voice.description);
173+
Log.Debug("ExampleTextToSpeech", "-----OnGetVoice-----");
174+
}
175+
176+
private void OnGetPronunciation(Pronunciation pronunciation)
177+
{
178+
Log.Debug("ExampleTextToSpeech", "-----OnGetPronunciation-----");
179+
Log.Debug("ExampleTextToSpeech", "Pronunciation: {0}.", pronunciation.pronunciation);
180+
Log.Debug("ExampleTextToSpeech", "-----OnGetPronunciation-----");
181+
}
182+
183+
private void OnGetCustomizations(Customizations customizations, string data)
184+
{
185+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomizations-----");
186+
if(data != default(string))
187+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
188+
foreach (Customization customization in customizations.customizations)
189+
Log.Debug("ExampleTextToSpeech", "Customization: name: {0} | customization_id: {1} | language: {2} | description: {3} | owner: {4} | created: {5} | last modified: {6}", customization.name, customization.customization_id, customization.language, customization.description, customization.owner, customization.created, customization.last_modified);
190+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomizations-----");
191+
}
192+
193+
private void OnCreateCustomization(CustomizationID customizationID, string data)
194+
{
195+
Log.Debug("ExampleTextToSpeech", "-----OnCreateCustomization-----");
196+
if (data != default(string))
197+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
198+
Log.Debug("ExampleTextToSpeech", "CustomizationID: id: {0}.", customizationID.customization_id);
199+
Log.Debug("ExampleTextToSpeech", "-----OnCreateCustomization-----");
200+
}
201+
202+
private void OnDeleteCustomization(bool success, string data)
203+
{
204+
Log.Debug("ExampleTextToSpeech", "-----OnDeleteCustomization-----");
205+
if (data != default(string))
206+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
207+
Log.Debug("ExampleTextToSpeech", "Success: {0}.", success);
208+
Log.Debug("ExampleTextToSpeech", "-----OnDeleteCustomization-----");
209+
}
210+
211+
private void OnGetCustomization(Customization customization, string data)
212+
{
213+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomization-----");
214+
if (data != default(string))
215+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
216+
Log.Debug("ExampleTextToSpeech", "Customization: name: {0} | customization_id: {1} | language: {2} | description: {3} | owner: {4} | created: {5} | last modified: {6}", customization.name, customization.customization_id, customization.language, customization.description, customization.owner, customization.created, customization.last_modified);
217+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomization-----");
218+
}
219+
220+
private void OnUpdateCustomization(bool success, string data)
221+
{
222+
Log.Debug("ExampleTextToSpeech", "-----OnUpdateCustomization-----");
223+
if (data != default(string))
224+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
225+
Log.Debug("ExampleTextToSpeech", "Success: {0}.", success);
226+
Log.Debug("ExampleTextToSpeech", "-----OnUpdateCustomization-----");
227+
}
228+
229+
private void OnGetCustomizationWords(Words words, string data)
230+
{
231+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomizationWords-----");
232+
if (data != default(string))
233+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
234+
foreach (Word word in words.words)
235+
Log.Debug("ExampleTextToSpeech", "Word: {0} | Translation: {1}.", word.word, word.translation);
236+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomizationWords-----");
237+
}
238+
239+
private void OnAddCustomizationWords(bool success, string data)
240+
{
241+
Log.Debug("ExampleTextToSpeech", "-----OnAddCustomizationWords-----");
242+
if (data != default(string))
243+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
244+
Log.Debug("ExampleTextToSpeech", "Success: {0}.", success);
245+
Log.Debug("ExampleTextToSpeech", "-----OnAddCustomizationWords-----");
246+
}
247+
248+
private void OnDeleteCustomizationWord(bool success, string data)
249+
{
250+
Log.Debug("ExampleTextToSpeech", "-----OnDeleteCustomizationWord-----");
251+
if (data != default(string))
252+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
253+
Log.Debug("ExampleTextToSpeech", "Success: {0}.", success);
254+
Log.Debug("ExampleTextToSpeech", "-----OnDeleteCustomizationWord-----");
255+
}
256+
257+
private void OnGetCustomizationWord(Translation translation, string data)
258+
{
259+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomizationWord-----");
260+
if (data != default(string))
261+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
262+
Log.Debug("ExampleTextToSpeech", "Translation: {0}.", translation.translation);
263+
Log.Debug("ExampleTextToSpeech", "-----OnGetCustomizationWord-----");
264+
}
265+
266+
private void OnAddCustomizationWord(bool success, string data)
267+
{
268+
Log.Debug("ExampleTextToSpeech", "-----OnAddCustomizationWord-----");
269+
if (data != default(string))
270+
Log.Debug("ExampleTextToSpeech", "data: {0}", data);
271+
Log.Debug("ExampleTextToSpeech", "Success: {0}.", success);
272+
Log.Debug("ExampleTextToSpeech", "-----OnAddCustomizationWord-----");
273+
}
52274
}

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ GameObject:
352352
m_Icon: {fileID: 0}
353353
m_NavMeshLayer: 0
354354
m_StaticEditorFlags: 0
355-
m_IsActive: 1
355+
m_IsActive: 0
356356
--- !u!114 &859102723
357357
MonoBehaviour:
358358
m_ObjectHideFlags: 0
@@ -633,7 +633,7 @@ GameObject:
633633
m_Icon: {fileID: 0}
634634
m_NavMeshLayer: 0
635635
m_StaticEditorFlags: 0
636-
m_IsActive: 0
636+
m_IsActive: 1
637637
--- !u!114 &1740459832
638638
MonoBehaviour:
639639
m_ObjectHideFlags: 0
@@ -658,6 +658,47 @@ Transform:
658658
m_Children: []
659659
m_Father: {fileID: 0}
660660
m_RootOrder: 1
661+
--- !u!1 &1979050314
662+
GameObject:
663+
m_ObjectHideFlags: 0
664+
m_PrefabParentObject: {fileID: 0}
665+
m_PrefabInternal: {fileID: 0}
666+
serializedVersion: 4
667+
m_Component:
668+
- 4: {fileID: 1979050316}
669+
- 114: {fileID: 1979050315}
670+
m_Layer: 0
671+
m_Name: ExampleDialog
672+
m_TagString: Untagged
673+
m_Icon: {fileID: 0}
674+
m_NavMeshLayer: 0
675+
m_StaticEditorFlags: 0
676+
m_IsActive: 0
677+
--- !u!114 &1979050315
678+
MonoBehaviour:
679+
m_ObjectHideFlags: 0
680+
m_PrefabParentObject: {fileID: 0}
681+
m_PrefabInternal: {fileID: 0}
682+
m_GameObject: {fileID: 1979050314}
683+
m_Enabled: 1
684+
m_EditorHideFlags: 0
685+
m_Script: {fileID: 11500000, guid: e8b52562568714a48a89536c62fb978e, type: 3}
686+
m_Name:
687+
m_EditorClassIdentifier:
688+
m_DialogID: 61343031353936302d333963322d346436622d393537312d333863373461656366666664
689+
--- !u!4 &1979050316
690+
Transform:
691+
m_ObjectHideFlags: 0
692+
m_PrefabParentObject: {fileID: 0}
693+
m_PrefabInternal: {fileID: 0}
694+
m_GameObject: {fileID: 1979050314}
695+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
696+
m_LocalPosition: {x: 0, y: 1, z: -10}
697+
m_LocalScale: {x: 1, y: 1, z: 1}
698+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
699+
m_Children: []
700+
m_Father: {fileID: 0}
701+
m_RootOrder: 2
661702
--- !u!1 &2004886371
662703
GameObject:
663704
m_ObjectHideFlags: 0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void HandleOnRecognize (SpeechResultList result)
9696
```
9797

9898
### Text to Speech
99-
Use the [Text to Speech][text_to_speech] service to get the available voices to synthesize. The Text to Speech service also supports Speech Synthesis Markup Language ([SSML][ssml]). In addition, the service supports a service-specific [expressive SSML][expressive_ssml] element.
99+
Use the [Text to Speech][text_to_speech] service to get the available voices to synthesize. The Text to Speech service also supports Speech Synthesis Markup Language ([SSML][ssml]). In addition, the service supports a service-specific [expressive SSML][expressive_ssml] element. See Text To Speech service examples for examples on how to create custom Text to Speech voice models.
100100

101101
```cs
102102
TextToSpeech m_TextToSpeech = new TextToSpeech();

Scripts/Connection/RESTConnector.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,18 @@ private IEnumerator ProcessRequestQueue()
442442
int nErrorCode = -1;
443443
int nSeperator = www.error.IndexOf(' ');
444444
if (nSeperator > 0 && int.TryParse(www.error.Substring(0, nSeperator).Trim(), out nErrorCode))
445-
bError = nErrorCode != 200;
445+
{
446+
switch (nErrorCode)
447+
{
448+
case 200:
449+
case 201:
450+
bError = false;
451+
break;
452+
default:
453+
bError = true;
454+
break;
455+
}
456+
}
446457

447458
if (bError)
448459
Log.Error("RESTConnector", "URL: {0}, ErrorCode: {1}, Error: {2}, Response: {3}", url, nErrorCode, www.error,
@@ -456,11 +467,11 @@ private IEnumerator ProcessRequestQueue()
456467
Log.Error("RESTConnector", "Request timed out for URL: {0}", url);
457468
bError = true;
458469
}
459-
if (!bError && (www.bytes == null || www.bytes.Length == 0))
470+
/*if (!bError && (www.bytes == null || www.bytes.Length == 0))
460471
{
461472
Log.Warning("RESTConnector", "No data recevied for URL: {0}", url);
462473
bError = true;
463-
}
474+
}*/
464475

465476

466477
// generate the Response object now..
@@ -582,7 +593,7 @@ private void ProcessRequest()
582593
#if ENABLE_DEBUGGING
583594
Log.Debug("RESTConnector", "DELETE Request SENT: {0}", URL);
584595
#endif
585-
Success = deleteResp.StatusCode == HttpStatusCode.OK;
596+
Success = deleteResp.StatusCode == HttpStatusCode.OK || deleteResp.StatusCode == HttpStatusCode.NoContent;
586597
#if ENABLE_DEBUGGING
587598
Log.Debug("RESTConnector", "DELETE Request COMPLETE: {0}", URL);
588599
#endif

0 commit comments

Comments
 (0)