Esempio n. 1
0
        private SpeechTranslateClientOptions GetSpeechTranslateClientOptions(string sourceLanguage, string targetLanguage)
        {
            this.correlationId = Guid.NewGuid().ToString("D").Split('-')[0].ToUpperInvariant();

            // Setup speech translation client options
            var options = new SpeechTranslateClientOptions()
            {
                TranslateFrom = sourceLanguage,
                TranslateTo   = targetLanguage
            };

            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        = SpeechClient.Features.TimingInfo.ToString();
            options.Profanity       = ProfanityFilter.Strict.ToString();
            options.Experimental    = false;

            return(options);
        }
Esempio n. 2
0
        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);
                    });
                }
            });
        }