public async Task StartSpeechRecognitionAsync()
        {
            SpeechConfig config = GetRecognizerConfig();

            if (config == null)
            {
                return;
            }
            ResetState();
            DisposeRecognizer();

            DeviceInformation microphoneInput = await Util.GetDeviceInformation(DeviceClass.AudioCapture, SettingsHelper.Instance.MicrophoneName);

            using (AudioConfig audioConfig = AudioConfig.FromMicrophoneInput(microphoneInput.Id))
            {
                speechRecognizer                 = audioConfig != null ? new SpeechRecognizer(config, audioConfig) : new SpeechRecognizer(config);
                speechRecognizer.Recognizing    += OnRecognizing;
                speechRecognizer.Recognized     += OnRecognized;
                speechRecognizer.Canceled       += OnCanceled;
                speechRecognizer.SessionStarted += (s, e) =>
                {
                    recognizeCancellationTokenSource = new CancellationTokenSource();
                };

                await speechRecognizer.StartContinuousRecognitionAsync();
            }
        }
        private async void Start_Click(object sender, RoutedEventArgs e)
        {
            var status = "Ready!";

            try
            {
                var config = SpeechConfig.FromSubscription(SpeechKey.Text, SpeechRegion.Text);
                //var exe = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                //config.SetProperty(PropertyId.Speech_LogFilename, System.IO.Path.Combine(exe.DirectoryName, "log.txt"));
                recognizer = new SpeechRecognizer(config, AudioConfig.FromMicrophoneInput((Microphone.SelectedItem as ComboBoxItem).Tag.ToString()));

                recognizer.Recognizing += Recognizer_Recognizing;
                recognizer.Recognized  += Recognizer_Recognized;
                //recognizer.Canceled += Recognizer_Canceled;
                //recognizer.SessionStarted += Recognizer_SessionStarted;
                //recognizer.SessionStopped += Recognizer_SessionStopped;
                //recognizer.SpeechEndDetected += Recognizer_SpeechEndDetected;
                //recognizer.SpeechStartDetected += Recognizer_SpeechStartDetected;

                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                status = $"Error: {ex.Message}";
            }

            Dispatcher.Invoke(() =>
            {
                MessageBlock.Text   = status;
                Controls.Visibility = Visibility.Hidden;
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates Recognizer with baseline model and selected language:
        /// Creates a config with subscription key and selected region
        /// If input source is audio file, creates recognizer with audio file otherwise with default mic
        /// Waits on RunRecognition
        /// </summary>
        private async Task CreateBaseReco()
        {
            try
            {
                // Todo: suport users to specifiy a different region.
                var config = SpeechConfig.FromSubscription(this.SubscriptionKey, this.Region);
                config.SpeechRecognitionLanguage = this.RecognitionLanguage;
                //If proxy information has been entered, set the proxy
                if ((this.proxy_Host != null && this.proxy_Host.Length > 0) &&
                    (this.proxy_Port != null && this.proxy_Port.Length > 0))
                {
                    config.SetProxy(proxy_Host, int.Parse(proxy_Port));
                }

                SpeechRecognizer basicRecognizer;
                if (this.UseMicrophone)
                {
                    if (MicrophoneID == "Default")
                    {
                        using (basicRecognizer = new SpeechRecognizer(config))
                        {
                            await this.RunRecognizer(basicRecognizer, RecoType.Base, stopBaseRecognitionTaskCompletionSource).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        using (var audioInput = AudioConfig.FromMicrophoneInput(MicrophoneID))
                        {
                            //fromMicrophoneInput(string)
                            using (basicRecognizer = new SpeechRecognizer(config, audioInput))
                            {
                                await this.RunRecognizer(basicRecognizer, RecoType.Base, stopBaseRecognitionTaskCompletionSource).ConfigureAwait(false);
                            }
                        }
                    }
                }
                else
                {
                    using (var audioInput = AudioConfig.FromWavFileInput(wavFileName))
                    {
                        using (basicRecognizer = new SpeechRecognizer(config, audioInput))
                        {
                            await this.RunRecognizer(basicRecognizer, RecoType.Base, stopBaseRecognitionTaskCompletionSource).ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates Recognizer with custom model endpointId and selected language:
        /// Creates a config with subscription key and selected region
        /// If input source is audio file, creates recognizer with audio file otherwise with default mic
        /// Waits on RunRecognition
        /// </summary>
        private async Task CreateCustomReco()
        {
            // Todo: suport users to specifiy a different region.
            var config = SpeechConfig.FromSubscription(this.SubscriptionKey, this.Region);

            config.SpeechRecognitionLanguage = this.RecognitionLanguage;
            config.EndpointId = this.CustomModelEndpointId;
            //If proxy information has been entered, set the proxy
            if ((this.proxy_Host != null || this.proxy_Host.Length >= 0) &&
                (this.proxy_Port != null || this.proxy_Port.Length >= 0))
            {
                config.SetProxy(proxy_Host, int.Parse(proxy_Port));
            }

            SpeechRecognizer customRecognizer;

            if (this.UseMicrophone)
            {
                if (MicrophoneID == "Default")
                {
                    using (customRecognizer = new SpeechRecognizer(config))
                    {
                        await this.RunRecognizer(customRecognizer, RecoType.Custom, stopCustomRecognitionTaskCompletionSource).ConfigureAwait(false);
                    }
                }
                else
                {
                    using (var audioConfig = AudioConfig.FromMicrophoneInput(MicrophoneID))
                    {
                        using (customRecognizer = new SpeechRecognizer(config, audioConfig))
                        {
                            await this.RunRecognizer(customRecognizer, RecoType.Custom, stopCustomRecognitionTaskCompletionSource).ConfigureAwait(false);
                        }
                    }
                }
            }
            else
            {
                using (var audioInput = AudioConfig.FromWavFileInput(wavFileName))
                {
                    using (customRecognizer = new SpeechRecognizer(config, audioInput))
                    {
                        await this.RunRecognizer(customRecognizer, RecoType.Custom, stopCustomRecognitionTaskCompletionSource).ConfigureAwait(false);
                    }
                }
            }
        }