/// <summary> /// Initializes new instance of WitMic /// </summary> /// <param name="witPipedStream">Stream to write audio to</param> /// <param name="detectSpeechStop">Voice activity detection feature</param> public WitMic(WitPipedStream witPipedStream, bool detectSpeechStop) { this.witPipedStream = witPipedStream; this.detectSpeechStop = detectSpeechStop; microphone = Microphone.Default; if (microphone == null) { WitLog.Log("Did you enabled ID_CAP_MICROPHONE in WMAppManifest.xml?"); return; } witDetectTalking = new WitVadWrapper(8.0, 16000, 60); microphone.BufferDuration = TimeSpan.FromMilliseconds(100); speech = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)]; microphone.BufferReady += microphone_BufferReady; updateTimer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1) }; updateTimer.Tick += (s, e) => { FrameworkDispatcher.Update(); }; }
/// <summary> /// Gets intent and entities from a text string /// </summary> /// <returns>Unprocessed result from a server</returns> public async Task <string> GetAsync() { return(await Task.Run <string>(async() => { try { var socket = new StreamSocket(); var writer = new DataWriter(socket.OutputStream); var reader = new DataReader(socket.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(String.Format("GET /message?q={0} HTTP/1.1", HttpUtility.UrlEncode(message))); stringBuilder.AppendLine("Host: api.wit.ai"); stringBuilder.AppendLine(String.Format("Authorization: Bearer {0}", accessToken)); stringBuilder.AppendLine(""); string headers = stringBuilder.ToString(); byte[] headersPayload = Encoding.UTF8.GetBytes(headers); await socket.ConnectAsync(new HostName("wit.ai"), "443", SocketProtectionLevel.SslAllowNullEncryption); writer.WriteBytes(headersPayload); WitLog.Log("Sending text..."); await writer.StoreAsync(); await writer.FlushAsync(); var count = await reader.LoadAsync(UInt16.MaxValue); string response = reader.ReadString(count); int bodyStartIndex = response.IndexOf("\r\n\r\n"); if (bodyStartIndex != -1) { string body = response.Substring(bodyStartIndex + 4); WitLog.Log(body); return body; } else { return null; } } catch { return null; } })); }
/// <summary> /// Writes data array to the stream /// </summary> /// <param name="data">Data to write</param> public void Write(byte[] data) { if (IsInputCompleted) { WitLog.Log("Did you closed WitPipedStream accidentally?"); return; } lock (sync) { stream.Seek(0, SeekOrigin.End); stream.Write(data, 0, data.Length); } }
private async void MicButton_Click(object sender, System.Windows.RoutedEventArgs e) { if (AccessToken == null) { WitLog.Log("WitMicButton", "Did you set your AccessToken propeply?"); return; } IsToggled = !IsToggled; if (IsToggled) { if (CaptureVoiceIntentStarted != null) { CaptureVoiceIntentStarted(this, EventArgs.Empty); } wit = new Wit(AccessToken, DetectSpeechStop); WitResponse witResponse = await wit.CaptureVoiceIntent(); IsToggled = false; if (CaptureVoiceIntentCompleted != null) { CaptureVoiceIntentCompleted(this, witResponse); } } else { if (wit != null) { wit.StopCaptureVoiceIntent(); } if (CaptureVoiceIntentStopped != null) { CaptureVoiceIntentStopped(this, EventArgs.Empty); } } }
/// <summary> /// Called when new audio chuck is ready to use /// </summary> /// <param name="sender">Microphone instance</param> /// <param name="e">Event arguments</param> private void microphone_BufferReady(object sender, EventArgs e) { int microphoneDataSize = microphone.GetData(speech); int talking = witDetectTalking.Talking(speech.AsBuffer(), speech.Length); witPipedStream.Write(speech); if (detectSpeechStop) { if (talking == 1) { WitLog.Log("Start talking detected"); } else if (talking == 0) { WitLog.Log("Stop talking detected"); StopRecording(); } } }
/// <summary> /// Uploads data with a chunks /// </summary> /// <returns>Unprocessed result from a server</returns> public async Task <string> UploadAsync() { return(await Task.Run <string>(async() => { try { var socket = new StreamSocket(); var writer = new DataWriter(socket.OutputStream); var reader = new DataReader(socket.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(String.Format("POST /speech?v={0} HTTP/1.1", Version)); stringBuilder.AppendLine("Host: api.wit.ai"); stringBuilder.AppendLine("Accept: applicaiton/json"); stringBuilder.AppendLine(String.Format("Authorization: Bearer {0}", accessToken)); stringBuilder.AppendLine(String.Format("Content-Type: {0};encoding={1};bits={2};rate={3};endian={4}", type, encoding, bits, rate, order == ByteOrder.LITTLE_ENDIAN ? "little" : "big")); stringBuilder.AppendLine("Transfer-Encoding: chunked"); stringBuilder.AppendLine("Connection: Keep-Alive"); string headers = stringBuilder.ToString(); byte[] headersPayload = Encoding.UTF8.GetBytes(headers); await socket.ConnectAsync(new HostName("wit.ai"), "443", SocketProtectionLevel.SslAllowNullEncryption); writer.WriteBytes(headersPayload); byte[] data; WitLog.Log("Sending speech..."); while (true) { data = witPipedStream.Read(); if (data.Length > 0) { byte[] chunkHeader = Encoding.UTF8.GetBytes("\r\n" + data.Length.ToString("X") + "\r\n"); writer.WriteBytes(chunkHeader); writer.WriteBytes(data); await writer.StoreAsync(); await writer.FlushAsync(); } else if (!witPipedStream.IsInputCompleted) { await Task.Delay(50); } else { break; } } byte[] lastChunkHeader = Encoding.UTF8.GetBytes("\r\n0\r\n\r\n"); writer.WriteBytes(lastChunkHeader); await writer.StoreAsync(); await writer.FlushAsync(); var count = await reader.LoadAsync(UInt16.MaxValue); string response = reader.ReadString(count); int bodyStartIndex = response.IndexOf("\r\n\r\n"); if (bodyStartIndex != -1) { string body = response.Substring(bodyStartIndex + 4).Trim(); WitLog.Log(body); return body; } else { return null; } } catch { return null; } })); }