17
17
18
18
using FullSerializer ;
19
19
using IBM . Watson . DeveloperCloud . Connection ;
20
+ using IBM . Watson . DeveloperCloud . DataTypes ;
20
21
using IBM . Watson . DeveloperCloud . Logging ;
21
22
using IBM . Watson . DeveloperCloud . Services . Assistant . v1 ;
23
+ using IBM . Watson . DeveloperCloud . Services . SpeechToText . v1 ;
22
24
using IBM . Watson . DeveloperCloud . Utilities ;
25
+ using System ;
23
26
using System . Collections ;
24
27
using System . Collections . Generic ;
25
28
using UnityEngine ;
@@ -37,49 +40,100 @@ public class ExampleCustomHeaders : MonoBehaviour
37
40
private string _assistantWorkspaceId ;
38
41
[ SerializeField ]
39
42
private string _assistantVersionDate ;
43
+ [ SerializeField ]
44
+ private string _speechToTextUsername ;
45
+ [ SerializeField ]
46
+ private string _speechToTextPassword ;
47
+ [ SerializeField ]
48
+ private string _speechToTextUrl ;
40
49
#endregion
50
+ private int _recordingRoutine = 0 ;
51
+ private string _microphoneID = null ;
52
+ private AudioClip _recording = null ;
53
+ private int _recordingBufferSize = 1 ;
54
+ private int _recordingHZ = 22050 ;
41
55
42
- private Assistant _service ;
56
+ private Assistant _assistant ;
43
57
private string _inputString = "Turn on the winshield wipers" ;
44
58
59
+ private SpeechToText _speechToText ;
60
+ Dictionary < string , object > speechToTextCustomData = null ;
61
+
45
62
void Start ( )
46
63
{
47
64
LogSystem . InstallDefaultReactors ( ) ;
48
65
49
- // Create credential and instantiate service
50
- Credentials credentials = new Credentials ( _assistantUsername , _assistantPassword , _assistantUrl ) ;
51
66
52
- _service = new Assistant ( credentials ) ;
53
- _service . VersionDate = _assistantVersionDate ;
67
+ //#region http custom headers
68
+ //// Create credential and instantiate Assistant service
69
+ //Credentials assistantCredentials = new Credentials(_assistantUsername, _assistantPassword, _assistantUrl);
54
70
55
- Dictionary < string , object > input = new Dictionary < string , object > ( ) ;
56
- input . Add ( "text" , _inputString ) ;
57
- MessageRequest messageRequest = new MessageRequest ( )
58
- {
59
- Input = input ,
60
- AlternateIntents = true
61
- } ;
71
+ //_assistant = new Assistant(assistantCredentials);
72
+ //_assistant.VersionDate = _assistantVersionDate;
73
+
74
+ //Dictionary<string, object> input = new Dictionary<string, object>();
75
+ //input.Add("text", _inputString);
76
+ //MessageRequest messageRequest = new MessageRequest()
77
+ //{
78
+ // Input = input,
79
+ // AlternateIntents = true
80
+ //};
81
+
82
+ //// Create customData object
83
+ //Dictionary<string, object> assistantCustomData = new Dictionary<string, object>();
84
+ //// Create a dictionary of custom headers
85
+ //Dictionary<string, string> assistantCustomHeaders = new Dictionary<string, string>();
86
+ //// Add to the header dictionary
87
+ //assistantCustomHeaders.Add("X-Watson-Metadata", "customer_id=some-assistant-customer-id");
88
+ //// Add the header dictionary to the custom data object
89
+ //assistantCustomData.Add(Constants.String.CUSTOM_REQUEST_HEADERS, assistantCustomHeaders);
90
+
91
+ //// Logging what we will send
92
+ //if (assistantCustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
93
+ //{
94
+ // Log.Debug("ExampleCustomHeader.Start()", "Assistant custom request headers:");
95
+ // foreach (KeyValuePair<string, string> kvp in assistantCustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary<string, string>)
96
+ // {
97
+ // Log.Debug("ExampleCustomHeader.Start()", "\t{0}: {1}", kvp.Key, kvp.Value);
98
+ // }
99
+ //}
100
+
101
+ //// Call service using custom data object
102
+ //_assistant.Message(OnMessage, OnFail, _assistantWorkspaceId, messageRequest, customData: assistantCustomData);
103
+ //#endregion
62
104
63
- // Create custom data object
64
- Dictionary < string , object > customData = new Dictionary < string , object > ( ) ;
105
+ #region websocket custom headers
106
+ // Websocket custom headers
107
+ // Create credential and instantiate Speech to Text service
108
+ Credentials speechToTextCredentials = new Credentials ( _speechToTextUsername , _speechToTextPassword , _speechToTextUrl ) ;
109
+
110
+ _speechToText = new SpeechToText ( speechToTextCredentials ) ;
111
+
112
+ // Create customData object
113
+ speechToTextCustomData = new Dictionary < string , object > ( ) ;
65
114
// Create a dictionary of custom headers
66
- Dictionary < string , string > customHeaders = new Dictionary < string , string > ( ) ;
67
- // Add to the header dictionary
68
- customHeaders . Add ( "X-Watson-Metadata" , "customer_id=some-customer-id" ) ;
115
+ Dictionary < string , string > speechToTextCustomHeaders = new Dictionary < string , string > ( ) ;
116
+ // Add header to the dictionary
117
+ speechToTextCustomHeaders . Add ( "X-Watson-Metadata" , "customer_id=some-speech-to-text -customer-id" ) ;
69
118
// Add the header dictionary to the custom data object
70
- customData . Add ( Constants . String . CUSTOM_REQUEST_HEADERS , customHeaders ) ;
119
+ speechToTextCustomData . Add ( Constants . String . CUSTOM_REQUEST_HEADERS , speechToTextCustomHeaders ) ;
71
120
72
- if ( customData . ContainsKey ( Constants . String . CUSTOM_REQUEST_HEADERS ) )
121
+ // Logging what we will send
122
+ if ( speechToTextCustomData . ContainsKey ( Constants . String . CUSTOM_REQUEST_HEADERS ) )
73
123
{
74
- Log . Debug ( "ExampleCustomHeader.Start()" , "Custom Request headers:" ) ;
75
- foreach ( KeyValuePair < string , string > kvp in customData [ Constants . String . CUSTOM_REQUEST_HEADERS ] as Dictionary < string , string > )
124
+ Log . Debug ( "ExampleCustomHeader.Start()" , "Speech to text custom request headers:" ) ;
125
+ foreach ( KeyValuePair < string , string > kvp in speechToTextCustomData [ Constants . String . CUSTOM_REQUEST_HEADERS ] as Dictionary < string , string > )
76
126
{
77
127
Log . Debug ( "ExampleCustomHeader.Start()" , "\t {0}: {1}" , kvp . Key , kvp . Value ) ;
78
128
}
79
129
}
80
130
81
131
// Call service using custom data object
82
- _service . Message ( OnMessage , OnFail , _assistantWorkspaceId , messageRequest , customData : customData ) ;
132
+ //_speechToText.StartListening(OnRecognize, customData: speechToTextCustomData);
133
+ Active = true ;
134
+
135
+ StartRecording ( ) ;
136
+ #endregion
83
137
}
84
138
85
139
private void OnMessage ( MessageResponse response , Dictionary < string , object > customData )
@@ -97,9 +151,138 @@ private void OnMessage(MessageResponse response, Dictionary<string, object> cust
97
151
}
98
152
}
99
153
154
+ private void OnRecognize ( SpeechRecognitionEvent results , Dictionary < string , object > customData )
155
+ {
156
+ Log . Debug ( "ExampleCustomHeader.OnRecognize()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
157
+
158
+ if ( customData != null )
159
+ {
160
+ if ( customData . ContainsKey ( Constants . String . RESPONSE_HEADERS ) )
161
+ {
162
+ Log . Debug ( "ExampleCustomHeader.OnRecognize()" , "Response headers:" ) ;
163
+
164
+ foreach ( KeyValuePair < string , string > kvp in customData [ Constants . String . RESPONSE_HEADERS ] as Dictionary < string , string > )
165
+ {
166
+ Log . Debug ( "ExampleCustomHeader.OnRecognize()" , "\t {0}: {1}" , kvp . Key , kvp . Value ) ;
167
+ }
168
+ }
169
+ }
170
+ }
171
+
100
172
private void OnFail ( RESTConnector . Error error , Dictionary < string , object > customData )
101
173
{
102
174
Log . Debug ( "ExampleCustomHeader.OnFail()" , "Response: {0}" , customData [ "json" ] . ToString ( ) ) ;
103
175
Log . Error ( "ExampleCustomHeader.OnFail()" , "Error received: {0}" , error . ToString ( ) ) ;
104
176
}
177
+ public bool Active
178
+ {
179
+ get { return _speechToText . IsListening ; }
180
+ set
181
+ {
182
+ if ( value && ! _speechToText . IsListening )
183
+ {
184
+ _speechToText . DetectSilence = true ;
185
+ _speechToText . EnableWordConfidence = true ;
186
+ _speechToText . EnableTimestamps = true ;
187
+ _speechToText . SilenceThreshold = 0.01f ;
188
+ _speechToText . MaxAlternatives = 0 ;
189
+ _speechToText . EnableInterimResults = true ;
190
+ _speechToText . OnError = OnError ;
191
+ _speechToText . InactivityTimeout = - 1 ;
192
+ _speechToText . ProfanityFilter = false ;
193
+ _speechToText . SmartFormatting = true ;
194
+ _speechToText . SpeakerLabels = false ;
195
+ _speechToText . WordAlternativesThreshold = null ;
196
+ _speechToText . StartListening ( OnRecognize , customData : speechToTextCustomData ) ;
197
+ }
198
+ else if ( ! value && _speechToText . IsListening )
199
+ {
200
+ _speechToText . StopListening ( ) ;
201
+ }
202
+ }
203
+ }
204
+
205
+ private void StartRecording ( )
206
+ {
207
+ if ( _recordingRoutine == 0 )
208
+ {
209
+ UnityObjectUtil . StartDestroyQueue ( ) ;
210
+ _recordingRoutine = Runnable . Run ( RecordingHandler ( ) ) ;
211
+ }
212
+ }
213
+
214
+ private void StopRecording ( )
215
+ {
216
+ if ( _recordingRoutine != 0 )
217
+ {
218
+ Microphone . End ( _microphoneID ) ;
219
+ Runnable . Stop ( _recordingRoutine ) ;
220
+ _recordingRoutine = 0 ;
221
+ }
222
+ }
223
+
224
+ private void OnError ( string error )
225
+ {
226
+ Active = false ;
227
+
228
+ Log . Debug ( "ExampleStreaming.OnError()" , "Error! {0}" , error ) ;
229
+ }
230
+
231
+ private IEnumerator RecordingHandler ( )
232
+ {
233
+ Log . Debug ( "ExampleStreaming.RecordingHandler()" , "devices: {0}" , Microphone . devices ) ;
234
+ _recording = Microphone . Start ( _microphoneID , true , _recordingBufferSize , _recordingHZ ) ;
235
+ yield return null ; // let _recordingRoutine get set..
236
+
237
+ if ( _recording == null )
238
+ {
239
+ StopRecording ( ) ;
240
+ yield break ;
241
+ }
242
+
243
+ bool bFirstBlock = true ;
244
+ int midPoint = _recording . samples / 2 ;
245
+ float [ ] samples = null ;
246
+
247
+ while ( _recordingRoutine != 0 && _recording != null )
248
+ {
249
+ int writePos = Microphone . GetPosition ( _microphoneID ) ;
250
+ if ( writePos > _recording . samples || ! Microphone . IsRecording ( _microphoneID ) )
251
+ {
252
+ Log . Error ( "ExampleStreaming.RecordingHandler()" , "Microphone disconnected." ) ;
253
+
254
+ StopRecording ( ) ;
255
+ yield break ;
256
+ }
257
+
258
+ if ( ( bFirstBlock && writePos >= midPoint )
259
+ || ( ! bFirstBlock && writePos < midPoint ) )
260
+ {
261
+ // front block is recorded, make a RecordClip and pass it onto our callback.
262
+ samples = new float [ midPoint ] ;
263
+ _recording . GetData ( samples , bFirstBlock ? 0 : midPoint ) ;
264
+
265
+ AudioData record = new AudioData ( ) ;
266
+ record . MaxLevel = Mathf . Max ( Mathf . Abs ( Mathf . Min ( samples ) ) , Mathf . Max ( samples ) ) ;
267
+ record . Clip = AudioClip . Create ( "Recording" , midPoint , _recording . channels , _recordingHZ , false ) ;
268
+ record . Clip . SetData ( samples , 0 ) ;
269
+
270
+ _speechToText . OnListen ( record ) ;
271
+
272
+ bFirstBlock = ! bFirstBlock ;
273
+ }
274
+ else
275
+ {
276
+ // calculate the number of samples remaining until we ready for a block of audio,
277
+ // and wait that amount of time it will take to record.
278
+ int remaining = bFirstBlock ? ( midPoint - writePos ) : ( _recording . samples - writePos ) ;
279
+ float timeRemaining = ( float ) remaining / ( float ) _recordingHZ ;
280
+
281
+ yield return new WaitForSeconds ( timeRemaining ) ;
282
+ }
283
+
284
+ }
285
+
286
+ yield break ;
287
+ }
105
288
}
0 commit comments