public void Decode_ReturnsMessage_WhenByArray() { Message expectedMessage = new Message( new Dictionary <string, string>() { { "Host", "google.com" } }, new byte[0] ); BinaryMessageEncoder encoder = new BinaryMessageEncoder(); byte[] serializedMessage = encoder.Encode(expectedMessage); BinaryMessageDecoder decoder = new BinaryMessageDecoder(); Message actualMessage = decoder.Decode(serializedMessage); Assert.NotNull(actualMessage); }
private void Disconnect() { if (this.currentState != UiState.Connected) { return; } UpdateUiState(UiState.Disconnecting); miniwindow.Hide(); if (recorder != null) { recorder.StopRecording(); recorder.DataAvailable -= OnRecorderDataAvailable; recorder.Dispose(); recorder = null; } if (streamAudioFromFileInterrupt != null) { streamAudioFromFileInterrupt.Cancel(); streamAudioFromFileInterrupt = null; } if (player != null) { player.Stop(); player.Dispose(); player = null; } // Close the audio file if logging if (audioSent != null) { audioSent.Flush(); audioSent.Dispose(); audioSent = null; } if (this.audioReceived != null) { this.audioReceived.Dispose(); this.audioReceived = null; } var task = s2smtClient.Disconnect() .ContinueWith((t) => { if (t.IsFaulted) { this.Log(t.Exception, "E: Disconnect call to client failed."); } s2smtClient.Dispose(); s2smtClient = null; }) .ContinueWith((t) => { if (t.IsFaulted) { this.Log(t.Exception, "E: Disconnected but there were errors."); } else { this.Log("I: Disconnected."); } this.SafeInvoke(() => { this.AutoSaveLogs(); this.UpdateUiState(UiState.ReadyToConnect); }); }); }
private void Connect() { if (this.currentState != UiState.ReadyToConnect) { return; } Stopwatch watch = Stopwatch.StartNew(); UpdateUiState(UiState.Connecting); if (ShowMiniWindow.IsChecked.Value) { miniwindow.Show(); } //This section is putting default values in case there are missing values in the UI // Minimal validation if (this.IsMissingInput(this.FromLanguage.SelectedItem, "source language")) { return; } if (this.IsMissingInput(this.ToLanguage.SelectedItem, "target language")) { return; } //if (this.IsMissingInput(this.Voice.SelectedItem, "voice")) return; if (this.IsMissingInput(this.Profanity.SelectedItem, "profanity filter")) { return; } if (this.IsMissingInput(this.Mic.SelectedItem, "microphone")) { return; } if (this.IsMissingInput(this.Speaker.SelectedItem, "speaker")) { return; } if (this.LogAutoSave.IsChecked.Value) { this.autoSaveFrom = this.Logs.Items.Count; } string tag = ((ComboBoxItem)Mic.SelectedItem).Tag as string; string audioFileInputPath = null; if (tag == "File") { audioFileInputPath = this.AudioFileInput.Text; if (!File.Exists(audioFileInputPath)) { SetMessage(String.Format("Invalid audio source: selected file does not exist."), "", MessageKind.Error); UpdateUiState(UiState.ReadyToConnect); return; } } bool shouldSuspendInputAudioDuringTTS = this.CutInputAudioCheckBox.IsChecked.HasValue ? this.CutInputAudioCheckBox.IsChecked.Value : false; this.correlationId = Guid.NewGuid().ToString("D").Split('-')[0].ToUpperInvariant(); // Setup speech translation client options SpeechClientOptions options; string voicename = ""; if (this.Voice.SelectedItem != null) { voicename = ((ComboBoxItem)this.Voice.SelectedItem).Tag.ToString(); } options = new SpeechTranslateClientOptions() { TranslateFrom = ((ComboBoxItem)this.FromLanguage.SelectedItem).Tag.ToString(), TranslateTo = ((ComboBoxItem)this.ToLanguage.SelectedItem).Tag.ToString(), Voice = voicename, }; options.Hostname = baseUrl; options.AuthHeaderKey = "Authorization"; options.AuthHeaderValue = ""; // set later in ConnectAsync. options.ClientAppId = new Guid("EA66703D-90A8-436B-9BD6-7A2707A2AD99"); options.CorrelationId = this.correlationId; options.Features = GetFeatures().ToString().Replace(" ", ""); options.Profanity = ((SpeechClient.ProfanityFilter)Enum.Parse(typeof(SpeechClient.ProfanityFilter), ((ComboBoxItem)this.Profanity.SelectedItem).Tag.ToString(), true)).ToString(); // Setup player and recorder but don't start them yet. WaveFormat waveFormat = new WaveFormat(16000, 16, 1); // WaveProvider for incoming TTS // We use a rather large BVufferDuration because we need to be able to hold an entire utterance. // TTS audio is received in bursts (faster than real-time). textToSpeechBytes = 0; playerTextToSpeechWaveProvider = new BufferedWaveProvider(waveFormat); playerTextToSpeechWaveProvider.BufferDuration = TimeSpan.FromMinutes(5); ISampleProvider sampleProvider = null; if (audioFileInputPath != null) { // Setup mixing of audio from input file and from TTS playerAudioInputWaveProvider = new BufferedWaveProvider(waveFormat); var srce1 = new Pcm16BitToSampleProvider(playerTextToSpeechWaveProvider); var srce2 = new Pcm16BitToSampleProvider(playerAudioInputWaveProvider); var mixer = new MixingSampleProvider(srce1.WaveFormat); mixer.AddMixerInput(srce1); mixer.AddMixerInput(srce2); sampleProvider = mixer; } else { recorder = new WaveIn(); recorder.DeviceNumber = (int)((ComboBoxItem)Mic.SelectedItem).Tag; recorder.WaveFormat = waveFormat; recorder.DataAvailable += OnRecorderDataAvailable; sampleProvider = playerTextToSpeechWaveProvider.ToSampleProvider(); } player = new WaveOut(); player.DeviceNumber = (int)((ComboBoxItem)Speaker.SelectedItem).Tag; player.Init(sampleProvider); this.audioBytesSent = 0; string logAudioFileName = null; if (LogSentAudio.IsChecked.Value || LogReceivedAudio.IsChecked.Value) { string logAudioPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Properties.Settings.Default.OutputDirectory); try { Directory.CreateDirectory(logAudioPath); } catch { this.AddItemToLog(string.Format("Could not create folder {0}", logAudioPath)); } if (LogSentAudio.IsChecked.Value) { logAudioFileName = System.IO.Path.Combine(logAudioPath, string.Format("audiosent_{0}.wav", this.correlationId)); } if (LogReceivedAudio.IsChecked.Value) { string fmt = System.IO.Path.Combine(logAudioPath, string.Format("audiotts_{0}_{{0}}.wav", this.correlationId)); this.audioReceived = new BinaryMessageDecoder(fmt); } } ConnectAsync(options, shouldSuspendInputAudioDuringTTS).ContinueWith((t) => { if (t.IsFaulted || t.IsCanceled || !s2smtClient.IsConnected()) //t.isfaulted OR t.iscancelled OR NOT s2smtclient.isconnected() do the following { this.Log(t.Exception, "E: Unable to connect: cid='{0}', elapsedMs='{1}'.", this.correlationId, watch.ElapsedMilliseconds); this.SafeInvoke(() => { this.AutoSaveLogs(); this.UpdateUiState(UiState.ReadyToConnect); }); } else { // Start playing incoming audio player.Play(); // Start recording and sending if (logAudioFileName != null) { audioSent = new WaveFileWriter(logAudioFileName, waveFormat); this.Log("I: Recording outgoing audio in {0}", logAudioFileName); } // Send the WAVE header s2smtClient.SendBinaryMessage(new ArraySegment <byte>(GetWaveHeader(waveFormat))); if (audioFileInputPath != null) { streamAudioFromFileInterrupt = new CancellationTokenSource(); Task.Run(() => this.StreamFile(audioFileInputPath, streamAudioFromFileInterrupt.Token)) .ContinueWith((x) => { if (x.IsFaulted) { this.Log(x.Exception, "E: Error while playing audio from input file."); } else { this.Log("I: Done playing audio from input file."); } }); } else { // Start sending audio from the recoder. recorder.StartRecording(); } this.Log("I: Connected: cid='{0}', elapsedMs='{1}'.", this.correlationId, watch.ElapsedMilliseconds); this.SafeInvoke(() => this.UpdateUiState(UiState.Connected)); } }).ContinueWith((t) => { if (t.IsFaulted) { Log(t.Exception, "E: Failed to start sending audio."); this.SafeInvoke(() => { this.AutoSaveLogs(); this.UpdateUiState(UiState.ReadyToConnect); }); } }); }