Skip to content

Commit 42a28d6

Browse files
committed
feat(TextToSpeech): add websocket support for text to speech
1 parent e678236 commit 42a28d6

File tree

4 files changed

+432
-6
lines changed

4 files changed

+432
-6
lines changed

Examples/ExampleTextToSpeechV1.cs

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Collections;
2424
using System.Collections.Generic;
2525
using UnityEngine;
26+
using UnityEngine.UI;
2627
using IBM.Cloud.SDK;
2728

2829

@@ -38,10 +39,14 @@ public class ExampleTextToSpeechV1 : MonoBehaviour
3839
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/text-to-speech/api\"")]
3940
[SerializeField]
4041
private string serviceUrl;
41-
private TextToSpeechService service;
42-
private string allisionVoice = "en-US_AllisonVoice";
42+
private TextToSpeechService _service;
43+
private string allisionVoice = "en-US_AllisonV3Voice";
4344
private string synthesizeText = "Hello, welcome to the Watson Unity SDK!";
4445
private string synthesizeMimeType = "audio/wav";
46+
public Text textInput;
47+
private int _recordingRoutine = 0;
48+
private bool _textEntered = false;
49+
private AudioClip _recording = null;
4550
#endregion
4651

4752
#region PlayClip
@@ -65,6 +70,31 @@ private void Start()
6570
{
6671
LogSystem.InstallDefaultReactors();
6772
Runnable.Run(CreateService());
73+
textInput = GetComponent<Text>();
74+
}
75+
76+
void Update()
77+
{
78+
foreach (char c in Input.inputString)
79+
{
80+
if (c == '\b') // has backspace/delete been pressed?
81+
{
82+
if (textInput.text.Length != 0)
83+
{
84+
textInput.text = textInput.text.Substring(0, textInput.text.Length - 1);
85+
}
86+
}
87+
else if ((c == '\n') || (c == '\r')) // enter/return
88+
{
89+
print("User entered the text: " + textInput.text);
90+
_service.OnListen(textInput.text);
91+
textInput.text = "";
92+
}
93+
else
94+
{
95+
textInput.text += c;
96+
}
97+
}
6898
}
6999

70100
private IEnumerator CreateService()
@@ -81,21 +111,83 @@ private IEnumerator CreateService()
81111
yield return null;
82112
}
83113

84-
service = new TextToSpeechService(authenticator);
114+
_service = new TextToSpeechService(authenticator);
85115
if (!string.IsNullOrEmpty(serviceUrl))
86116
{
87-
service.SetServiceUrl(serviceUrl);
117+
_service.SetServiceUrl(serviceUrl);
118+
}
119+
120+
Active = true;
121+
StartListening();
122+
// Runnable.Run(ExampleSynthesize());
123+
}
124+
125+
private void OnError(string error)
126+
{
127+
Active = false;
128+
129+
Log.Debug("ExampleTextToSpeech.OnError()", "Error! {0}", error);
130+
}
131+
132+
public bool Active
133+
{
134+
get { return _service.IsListening; }
135+
set
136+
{
137+
if (value && !_service.IsListening)
138+
{
139+
Log.Debug("start-", "listening");
140+
_service.Voice = allisionVoice;
141+
_service.OnError = OnError;
142+
_service.StartListening(OnSynthesize);
143+
}
144+
else if (!value && _service.IsListening)
145+
{
146+
Log.Debug("stop", "listening");
147+
_service.StopListening();
148+
}
149+
}
150+
}
151+
152+
private void StartListening()
153+
{
154+
if (_recordingRoutine == 0)
155+
{
156+
UnityObjectUtil.StartDestroyQueue();
157+
// _recordingRoutine = Runnable.Run(SynthesizeHandler());
88158
}
159+
}
160+
161+
// private IEnumerator SynthesizeHandler()
162+
// {
163+
// yield return null; // let _recordingRoutine get set..
164+
165+
// Log.Debug("ExampleTextToSpeechV1", "Text entered: {0}, {1}", synthesizeText, _textEntered);
166+
// while (_recordingRoutine != 0)
167+
// {
168+
// Log.Debug("ExampleTextToSpeechV1", "Text entered: {0}", synthesizeText);
169+
// if (_textEntered)
170+
// {
171+
// _service.OnListen(synthesizeText);
172+
// _textEntered = false;
173+
// textInput.text = "";
174+
// }
175+
// }
176+
// yield break;
177+
// }
89178

90-
Runnable.Run(ExampleSynthesize());
179+
private void OnSynthesize(byte[] result) {
180+
Log.Debug("ExampleTextToSpeechV1", "Synthesize done!");
181+
_recording = WaveFile.ParseWAV("myClip", result);
182+
PlayClip(_recording);
91183
}
92184

93185
#region Synthesize
94186
private IEnumerator ExampleSynthesize()
95187
{
96188
byte[] synthesizeResponse = null;
97189
AudioClip clip = null;
98-
service.Synthesize(
190+
_service.Synthesize(
99191
callback: (DetailedResponse<byte[]> response, IBMError error) =>
100192
{
101193
synthesizeResponse = response.Result;

Examples/ExampleTextToSpeechV1.unity

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ GameObject:
130130
m_Component:
131131
- component: {fileID: 275913467}
132132
- component: {fileID: 275913465}
133+
- component: {fileID: 275913468}
134+
- component: {fileID: 275913466}
133135
m_Layer: 0
134136
m_Name: ExampleTextToSpeechV1
135137
m_TagString: MainCamera
@@ -151,6 +153,39 @@ MonoBehaviour:
151153
m_EditorClassIdentifier:
152154
iamApikey:
153155
serviceUrl: https://stream.watsonplatform.net/text-to-speech/api
156+
textInput: {fileID: 0}
157+
--- !u!114 &275913466
158+
MonoBehaviour:
159+
m_ObjectHideFlags: 0
160+
m_CorrespondingSourceObject: {fileID: 0}
161+
m_PrefabInstance: {fileID: 0}
162+
m_PrefabAsset: {fileID: 0}
163+
m_GameObject: {fileID: 275913464}
164+
m_Enabled: 1
165+
m_EditorHideFlags: 0
166+
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
167+
m_Name:
168+
m_EditorClassIdentifier:
169+
m_Material: {fileID: 0}
170+
m_Color: {r: 1, g: 1, b: 1, a: 1}
171+
m_RaycastTarget: 1
172+
m_OnCullStateChanged:
173+
m_PersistentCalls:
174+
m_Calls: []
175+
m_FontData:
176+
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
177+
m_FontSize: 14
178+
m_FontStyle: 0
179+
m_BestFit: 0
180+
m_MinSize: 10
181+
m_MaxSize: 40
182+
m_Alignment: 0
183+
m_AlignByGeometry: 0
184+
m_RichText: 1
185+
m_HorizontalOverflow: 0
186+
m_VerticalOverflow: 0
187+
m_LineSpacing: 1
188+
m_Text:
154189
--- !u!224 &275913467
155190
RectTransform:
156191
m_ObjectHideFlags: 0
@@ -170,6 +205,14 @@ RectTransform:
170205
m_AnchoredPosition: {x: 901, y: 468}
171206
m_SizeDelta: {x: 1802, y: 936}
172207
m_Pivot: {x: 0.5, y: 0.5}
208+
--- !u!222 &275913468
209+
CanvasRenderer:
210+
m_ObjectHideFlags: 0
211+
m_CorrespondingSourceObject: {fileID: 0}
212+
m_PrefabInstance: {fileID: 0}
213+
m_PrefabAsset: {fileID: 0}
214+
m_GameObject: {fileID: 275913464}
215+
m_CullTransparentMesh: 0
173216
--- !u!1 &627186881
174217
GameObject:
175218
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)