Skip to content

Commit 60f3534

Browse files
committed
send request headers
1 parent 0834da6 commit 60f3534

File tree

3 files changed

+209
-26
lines changed

3 files changed

+209
-26
lines changed

Examples/ServiceExamples/Scripts/ExampleCustomHeaders.cs

Lines changed: 205 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
using FullSerializer;
1919
using IBM.Watson.DeveloperCloud.Connection;
20+
using IBM.Watson.DeveloperCloud.DataTypes;
2021
using IBM.Watson.DeveloperCloud.Logging;
2122
using IBM.Watson.DeveloperCloud.Services.Assistant.v1;
23+
using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1;
2224
using IBM.Watson.DeveloperCloud.Utilities;
25+
using System;
2326
using System.Collections;
2427
using System.Collections.Generic;
2528
using UnityEngine;
@@ -37,49 +40,100 @@ public class ExampleCustomHeaders : MonoBehaviour
3740
private string _assistantWorkspaceId;
3841
[SerializeField]
3942
private string _assistantVersionDate;
43+
[SerializeField]
44+
private string _speechToTextUsername;
45+
[SerializeField]
46+
private string _speechToTextPassword;
47+
[SerializeField]
48+
private string _speechToTextUrl;
4049
#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;
4155

42-
private Assistant _service;
56+
private Assistant _assistant;
4357
private string _inputString = "Turn on the winshield wipers";
4458

59+
private SpeechToText _speechToText;
60+
Dictionary<string, object> speechToTextCustomData = null;
61+
4562
void Start()
4663
{
4764
LogSystem.InstallDefaultReactors();
4865

49-
// Create credential and instantiate service
50-
Credentials credentials = new Credentials(_assistantUsername, _assistantPassword, _assistantUrl);
5166

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);
5470

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
62104

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>();
65114
// 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");
69118
// 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);
71120

72-
if (customData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
121+
// Logging what we will send
122+
if (speechToTextCustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
73123
{
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>)
76126
{
77127
Log.Debug("ExampleCustomHeader.Start()", "\t{0}: {1}", kvp.Key, kvp.Value);
78128
}
79129
}
80130

81131
// 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
83137
}
84138

85139
private void OnMessage(MessageResponse response, Dictionary<string, object> customData)
@@ -97,9 +151,138 @@ private void OnMessage(MessageResponse response, Dictionary<string, object> cust
97151
}
98152
}
99153

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+
100172
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
101173
{
102174
Log.Debug("ExampleCustomHeader.OnFail()", "Response: {0}", customData["json"].ToString());
103175
Log.Error("ExampleCustomHeader.OnFail()", "Error received: {0}", error.ToString());
104176
}
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+
}
105288
}

Examples/ServiceExamples/Scripts/ExampleStreaming.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private IEnumerator RecordingHandler()
170170
yield break;
171171
}
172172

173-
private void OnRecognize(SpeechRecognitionEvent result)
173+
private void OnRecognize(SpeechRecognitionEvent result, Dictionary<string, object> customData)
174174
{
175175
if (result != null && result.results.Length > 0)
176176
{
@@ -204,7 +204,7 @@ private void OnRecognize(SpeechRecognitionEvent result)
204204
}
205205
}
206206

207-
private void OnRecognizeSpeaker(SpeakerRecognitionEvent result)
207+
private void OnRecognizeSpeaker(SpeakerRecognitionEvent result, Dictionary<string, object> customData)
208208
{
209209
if (result != null)
210210
{

Examples/ServiceExamples/Scripts/ExampleStreamingSplitSamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private IEnumerator RecordingHandler()
218218
yield break;
219219
}
220220

221-
private void OnRecognize(SpeechRecognitionEvent result)
221+
private void OnRecognize(SpeechRecognitionEvent result, Dictionary<string, object> customData)
222222
{
223223
if (result != null && result.results.Length > 0)
224224
{
@@ -252,7 +252,7 @@ private void OnRecognize(SpeechRecognitionEvent result)
252252
}
253253
}
254254

255-
private void OnRecognizeSpeaker(SpeakerRecognitionEvent result)
255+
private void OnRecognizeSpeaker(SpeakerRecognitionEvent result, Dictionary<string, object> customData)
256256
{
257257
if (result != null)
258258
{

0 commit comments

Comments
 (0)