23
23
using System . Collections ;
24
24
using System . Collections . Generic ;
25
25
using UnityEngine ;
26
+ using UnityEngine . UI ;
26
27
using IBM . Cloud . SDK ;
27
28
28
29
@@ -38,10 +39,14 @@ public class ExampleTextToSpeechV1 : MonoBehaviour
38
39
[ Tooltip ( "The service URL (optional). This defaults to \" https://gateway.watsonplatform.net/text-to-speech/api\" " ) ]
39
40
[ SerializeField ]
40
41
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 " ;
43
44
private string synthesizeText = "Hello, welcome to the Watson Unity SDK!" ;
44
45
private string synthesizeMimeType = "audio/wav" ;
46
+ public Text textInput ;
47
+ private int _recordingRoutine = 0 ;
48
+ private bool _textEntered = false ;
49
+ private AudioClip _recording = null ;
45
50
#endregion
46
51
47
52
#region PlayClip
@@ -65,6 +70,31 @@ private void Start()
65
70
{
66
71
LogSystem . InstallDefaultReactors ( ) ;
67
72
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
+ }
68
98
}
69
99
70
100
private IEnumerator CreateService ( )
@@ -81,21 +111,83 @@ private IEnumerator CreateService()
81
111
yield return null ;
82
112
}
83
113
84
- service = new TextToSpeechService ( authenticator ) ;
114
+ _service = new TextToSpeechService ( authenticator ) ;
85
115
if ( ! string . IsNullOrEmpty ( serviceUrl ) )
86
116
{
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());
88
158
}
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
+ // }
89
178
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 ) ;
91
183
}
92
184
93
185
#region Synthesize
94
186
private IEnumerator ExampleSynthesize ( )
95
187
{
96
188
byte [ ] synthesizeResponse = null ;
97
189
AudioClip clip = null ;
98
- service . Synthesize (
190
+ _service . Synthesize (
99
191
callback : ( DetailedResponse < byte [ ] > response , IBMError error ) =>
100
192
{
101
193
synthesizeResponse = response . Result ;
0 commit comments