@@ -61,21 +61,7 @@ void Update()
61
61
{
62
62
if ( Input . GetKeyDown ( KeyCode . Return ) )
63
63
{
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 ) ) ;
79
65
}
80
66
}
81
67
@@ -98,50 +84,17 @@ private IEnumerator CreateService()
98
84
{
99
85
service . SetServiceUrl ( serviceUrl ) ;
100
86
}
101
-
102
- Active = true ;
103
87
}
104
88
105
- private void OnError ( string error )
89
+ #region Synthesize Example
90
+ private IEnumerator ExampleSynthesize ( string text )
106
91
{
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 ) )
124
93
{
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!" ) ;
141
96
142
- #region Synthesize Without Websocket Connection
143
- private IEnumerator ExampleSynthesize ( )
144
- {
97
+ }
145
98
byte [ ] synthesizeResponse = null ;
146
99
AudioClip clip = null ;
147
100
service . Synthesize (
@@ -152,7 +105,7 @@ private IEnumerator ExampleSynthesize()
152
105
clip = WaveFile . ParseWAV ( "myClip" , synthesizeResponse ) ;
153
106
PlayClip ( clip ) ;
154
107
} ,
155
- text : synthesizeText ,
108
+ text : text ,
156
109
voice : allisionVoice ,
157
110
accept : synthesizeMimeType
158
111
) ;
@@ -180,27 +133,5 @@ private void PlayClip(AudioClip clip)
180
133
}
181
134
}
182
135
#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
205
136
}
206
137
}
0 commit comments