Exemple #1
0
        public async Task RecognizeSpeechAsync()
        {
            var config = GetClient();

            using var recognizer = new SpeechRecognizer(config);
            Console.WriteLine("Say something...");
            var result = await recognizer.RecognizeOnceAsync();

            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                Console.WriteLine($"We recognized: {result.Text}");
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
            }
        }
        private static async Task Recognize(SpeechRecognizer recognizer)
        {
            var result = await recognizer.RecognizeOnceAsync();

            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                Console.WriteLine($"Recognized: {result.Text}");
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine("Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"Cancelled due to reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"Error code={cancellation.ErrorCode}");
                    Console.WriteLine($"Error details={cancellation.ErrorDetails}");
                    Console.WriteLine($"Did you update the subscription info?");
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Recognize speech from a WAV audio file using azure cognitive services
        /// </summary>
        /// <param name="wavFile">The path to the WAV file</param>
        public async Task <string> RecognizeSpeechAsync(string wavFile)
        {
            var config = SpeechConfig.FromEndpoint(new Uri(SecretData.AzureSpeechEndpoint), SecretData.AzureSpeechToken);

            using var audioInput = AudioConfig.FromWavFileInput(wavFile);
            using var recognizer = new SpeechRecognizer(config, audioInput);
            _logger.LogInfo($"Recognizing: { wavFile }");
            var result = await recognizer.RecognizeOnceAsync();

            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                return(result.Text);
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                throw new Exception($"NOMATCH: Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                if (cancellation.Reason == CancellationReason.Error)
                {
                    _logger.LogError($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    _logger.LogError($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    _logger.LogError($"CANCELED: Did you update the subscription info?");
                }
                throw new Exception($"CANCELED: Reason={cancellation.Reason}");
            }

            return("");
        }
Exemple #4
0
        public static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

            using (var audioConfig = AudioConfig.FromWavFileInput(@"YourFilePath"))
                using (var recognizer = new SpeechRecognizer(config, audioConfig))
                {
                    var result = await recognizer.RecognizeOnceAsync();

                    if (result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"We recognized: {result.Text}");
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = CancellationDetails.FromResult(result);
                        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                            Console.WriteLine($"CANCELED: Did you update the subscription info?");
                        }
                    }
                }
        }
        /// <summary>
        /// 判斷回傳執行結果狀態
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool CheckReason(dynamic result)
        {
            // 發生錯誤
            if (result.Reason == ResultReason.Canceled)
            {
                var typeName = result.GetType().Name;

                // 發生錯誤,提示訊息
                if (typeName == "RecognitionResult")
                {
                    this.DisPlayMessage(CancellationDetails.FromResult(result));
                }

                if (typeName == "SpeechSynthesisResult")
                {
                    this.DisPlayMessage(SpeechSynthesisCancellationDetails.FromResult(result));
                }
            }

            // 如果 執行結果為 語音轉文字類型
            // 設定 辨識後的文字
            if (result as RecognitionResult != null)
            {
                recognizeText = result.Text;
            }

            // 無法辨識輸入結果
            if (result.Reason == ResultReason.NoMatch)
            {
                recognizeText = $"無法辨識輸入語音,請重新輸入\n";
            }

            return(true);
        }
Exemple #6
0
        private static async void ProcessRecognition(SpeechRecognitionResult srr)
        {
            var result = srr;

            // Checks result.
            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                if (result.Text.IndexOf("Stop") != -1)
                {
                    await Recognizer.StopContinuousRecognitionAsync();
                }
                Console.WriteLine($"We recognized: {result.Text}");
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
            }
        }
        public static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("5dbb936323894a3abead86291b52d1b4", "centralus");

            using (var audioInput = AudioConfig.FromWavFileInput(@"C:\Users\sergi\revature\00_csharp\Translator\sample.wav"))
            {
                using (var recognizer = new SpeechRecognizer(config, audioInput))
                {
                    Console.WriteLine("Recognizing first result...");
                    var result = await recognizer.RecognizeOnceAsync();

                    if (result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"We recognized: {result.Text}");
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = CancellationDetails.FromResult(result);
                        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                            Console.WriteLine($"CANCELED: Did you update the subscription info?");
                        }
                    }
                }
            }
        }
Exemple #8
0
        static void ProcessResult(SpeechRecognitionResult result)
        {
            switch (result.Reason)
            {
            case ResultReason.RecognizedSpeech:
                // The file contained speech that was recognized and the transcription will be output
                // to the terminal window
                Console.WriteLine($"We recognized: {result.Text}");
                break;

            case ResultReason.NoMatch:
                // No recognizable speech found in the audio file that was supplied.
                // Out an informative message
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                break;

            case ResultReason.Canceled:
                // Operation was cancelled
                // Output the reason
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
                break;
            }
        }
        private string GetSpeechAsText()
        {
            SpeechRecognitionResult result = Speech.Instance.Recognize();

            if (Speech.Instance.HasSpeech(result))
            {
                Console.WriteLine("Recognized: " + result.Text);
                return(Speech.Instance.GetText(result));
            }
            else if (Speech.Instance.HasNoMatch(result))
            {
                Console.WriteLine("<No match>");
                Speech.Instance.Speak(NO_RESULT_RESPONSE);
                return(GetSpeechAsText());
            }
            else if (Speech.Instance.HasCancelledDueToEndOfStream(result))
            {
                Console.WriteLine("Error: End of Stream");
                Speech.Instance.Speak(ERROR_RESPONSE);
                return(GetSpeechAsText());
            }
            else if (Speech.Instance.HasCancelledWithErrors(result))
            {
                Console.WriteLine("Error Code: " + CancellationDetails.FromResult(result).ErrorCode + "; Details: " + CancellationDetails.FromResult(result).ErrorDetails);
                Speech.Instance.Speak(ERROR_RESPONSE);
                return(GetSpeechAsText());
            }
            else
            {
                Console.WriteLine("Result: " + result.Reason);
                Speech.Instance.Speak(NO_RESULT_RESPONSE);
                return(GetSpeechAsText());
            }
        }
Exemple #10
0
        public async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("aade2640ad4f4650a31c1335d4704c4e", "westus");

            config.SpeechRecognitionLanguage = "ko-KR";

            using (var recognizer = new SpeechRecognizer(config))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    TextSet(result.Text);
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    MessageBox.Show("NoMatch");
                    MaMaState(false);
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    MessageBox.Show(cancellation.Reason.ToString());

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        MessageBox.Show("Error");
                        MaMaState(false);
                    }
                }
            }
            return;
        }
Exemple #11
0
        public async Task <string> RecognizeSpeechAsync()
        {
            using (var recognizer = new SpeechRecognizer(config))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    return(result.Text);
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
                return(null);
            }
        }
Exemple #12
0
        private async Task <List <SpeechRecognitionResult> > SR(string filename, SpeechConfig config)
        {
            var stopRecognition = new TaskCompletionSource <bool>();
            List <SpeechRecognitionResult> listspeechrecognitionresult = new List <SpeechRecognitionResult>();

            using (var audioInput = AudioConfig.FromWavFileInput(filename))
                using (var recognizer = new SpeechRecognizer(config, audioInput))
                {
                    recognizer.Recognized += (s, e) => listspeechrecognitionresult.Add(e.Result);
                    recognizer.Canceled   += (s, e) =>
                    {
                        listspeechrecognitionresult.Add(e.Result);
                        stopRecognition.TrySetResult(false);
                    };
                    recognizer.SessionStopped += (s, e) => stopRecognition.TrySetResult(false);
                    await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                    await stopRecognition.Task;
                    await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);

                    var x = CancellationDetails.FromResult(listspeechrecognitionresult[0]);
                    if (x.Reason == CancellationReason.Error)
                    {
                        System.Windows.Forms.MessageBox.Show("Transcription error: " + x.ErrorDetails);
                    }
                    return(listspeechrecognitionresult);
                }
        }
        public static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription(SUBSCRIPTION_KEY, REGION);

            // Creates a speech recognizer.
            using (var recognizer = new SpeechRecognizer(config))
            {
                Console.WriteLine("Say something...");

                var result = await recognizer.RecognizeOnceAsync();

                // Checks result.
                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"We recognized: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }
Exemple #14
0
        public static async Task <string> RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("cdd4859020d94d1ab919ad18e313cab5", "westus2");

            using (var recognizer = new SpeechRecognizer(config))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    //Console.WriteLine($"You said: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    //Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    //Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    //if (cancellation.Reason == CancellationReason.Error)
                    //{
                    //    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    //    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    //    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    //}
                }
                return(result.Text);
            }
        }
        public async Task RecognizeShortSpeechAsync()
        {
            Console.WriteLine("-------------------------Short Speech Start------------------------");
            Console.WriteLine("Say something...");

            // silence for 15 seconds.
            using (var recognizer = new SpeechRecognizer(m_config))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"We recognized: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
            Console.WriteLine("-------------------------Short Speech End--------------------------");
        }
Exemple #16
0
        public async Task <Result <string> > Recognize(string filePath)
        {
            // Credenciais SpeechToText criado no Azure
            var config = SpeechConfig.FromSubscription("YourSpeechToTextKey", "YourRegion");

            config.SpeechRecognitionLanguage = "pt-br";

            using (var audioInput = AudioConfig.FromWavFileInput(filePath))
            {
                using (var recognizer = new SpeechRecognizer(config, audioInput))
                {
                    var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                    if (result.Reason == ResultReason.RecognizedSpeech)
                    {
                        return(new Result <string>(result.Text));
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        return(new Result <string>(result.Text, false, "Falha no reconhecimento do áudio!"));
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = CancellationDetails.FromResult(result);
                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            return(new Result <string>(result.Text, false, $"Motivo: {cancellation.Reason}. Detalhes: {cancellation.ErrorDetails}"));
                        }
                        return(new Result <string>(result.Text, false, $"Motivo: {cancellation.Reason}."));
                    }
                }
            }
            return(new Result <string>(null, false, "Erro desconhecido!"));
        }
        async Task RecognizeSpeechAsync()
        {
            var config =
                SpeechConfig.FromSubscription(
                    "61752b18a5c44efcb28e7ede3d03240e",
                    "eastus");

            using var recognizer = new SpeechRecognizer(config, "ru-RU");
            WriteLn("Say something...");
            var result = await recognizer.RecognizeOnceAsync();

            switch (result.Reason)
            {
            case ResultReason.RecognizedSpeech:
                WriteLn($"We recognized: {result.Text}");
                break;

            case ResultReason.NoMatch:
                WriteLn($"NOMATCH: Speech could not be recognized.");
                break;

            case ResultReason.Canceled:
                var cancellation = CancellationDetails.FromResult(result);
                WriteLn($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    WriteLn($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    WriteLn($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    WriteLn($"CANCELED: Did you update the subscription info?");
                }
                break;
            }
        }
Exemple #18
0
        static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

            using (var audioInput = AudioConfig.FromWavFileInput("whatstheweatherlike.wav"))
                using (var recognizer = new SpeechRecognizer(config, audioInput))
                {
                    Console.WriteLine("Recognizing first result...");
                    var result = await recognizer.RecognizeOnceAsync();

                    switch (result.Reason)
                    {
                    case ResultReason.RecognizedSpeech:
                        Console.WriteLine($"We recognized: {result.Text}");
                        break;

                    case ResultReason.NoMatch:
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                        break;

                    case ResultReason.Canceled:
                        var cancellation = CancellationDetails.FromResult(result);
                        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                            Console.WriteLine($"CANCELED: Did you update the subscription info?");
                        }
                        break;
                    }
                }
        }
Exemple #19
0
        public static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("3e3aea608df74736855bf7bf92596e43", "eastus2");

            config.SpeechRecognitionLanguage = "es-ES";
            var audioConfig = AudioConfig.FromWavFileInput("24375.wav");

            using (var recognizer = new SpeechRecognizer(config, audioConfig))
            {
                Console.WriteLine("Say...");
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"Text recognized {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine("No recognized");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellationDetails = CancellationDetails.FromResult(result);
                    Console.WriteLine($"Speech recognition canceled: {cancellationDetails.Reason}");

                    if (cancellationDetails.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"ErrorCode {cancellationDetails.ErrorCode}");
                        Console.WriteLine($"ErrorDetails {cancellationDetails.ErrorDetails}");
                    }
                }
            }
        }
Exemple #20
0
        public static async void VoiceToTextOnce(TextBox tb, CheckBox ckb)
        {
            var    config = SpeechConfig.FromSubscription(s1, s2);
            string rlt    = "";

            using (var recognizer = new SpeechRecognizer(config))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    rlt = Regex.Replace(result.Text, @"[^ A-Za-z0-9]+", "");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    MessageBox.Show("NOMATCH: Speech could not be recognized.", "Easy Appointment", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    // "CANCELED: Reason={cancellation.Reason}";

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        MessageBox.Show("CANCELED: " + cancellation.ErrorDetails, "Easy Appointment", MessageBoxButton.OK, MessageBoxImage.Error);
                        //Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        //Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
            Action <TextBox, String, CheckBox, bool> updateAction = new Action <TextBox, string, CheckBox, bool>(UpdateTb);

            tb.Dispatcher.BeginInvoke(updateAction, tb, rlt, ckb, false);;
        }
Exemple #21
0
        private static void Recognizer_Recognizing(object sender, SpeechRecognitionEventArgs e)
        {
            var result = e.Result;

            if (result.Reason == ResultReason.RecognizingSpeech)
            {
                Console.WriteLine($"We recognized: {result.Text}");
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
            }
        }
        public async Task <string> ConvertSpeechtoTextFromMicrophone()
        {
            SpeechConfig config = SpeechConfig.FromSubscription(Subscription.key, Subscription.region);
            string       result = string.Empty;

            using (var recognizer = new SpeechRecognizer(config))
            {
                var recognizerAsync = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                if (recognizerAsync.Reason == ResultReason.RecognizedSpeech)
                {
                    result += recognizerAsync.Text;
                }
                else if (recognizerAsync.Reason == ResultReason.NoMatch)
                {
                    result += ("Error");
                }
                else if (recognizerAsync.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(recognizerAsync);
                    result += ($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        result += ($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        result += ($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        result += ($"CANCELED: Did you update the subscription info?");
                    }
                }
            }

            return(result);
        }
Exemple #23
0
        public async Task <string> RecognizeSpeechFromFileAsync(string path)
        {
            var config = SpeechConfig.FromSubscription("3e3aea608df74736855bf7bf92596e43", "eastus2");

            config.SpeechRecognitionLanguage = "es-ES";
            var audioConfig = AudioConfig.FromWavFileInput(path); // "24375.wav");

            using (var recognizer = new SpeechRecognizer(config, audioConfig))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    return($"Text recognized: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    return($"No speech recognized");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellationDetails = CancellationDetails.FromResult(result);

                    if (cancellationDetails.Reason == CancellationReason.Error)
                    {
                        return($"ErrorCode {cancellationDetails.ErrorCode}\n" +
                               $"ErrorDetails {cancellationDetails.ErrorDetails}");
                    }

                    return($"Speech recognition canceled: {cancellationDetails.Reason}");
                }

                return("Unknown error");
            }
        }
Exemple #24
0
        private void _speechRecognizer_Recognized(object sender, SpeechRecognitionEventArgs e)
        {
            var result = e.Result;

            // Checks result.
            if (result.Reason == ResultReason.RecognizedSpeech)
            {
                if (!String.IsNullOrWhiteSpace(result.Text))
                {
                    Console.WriteLine($"- {result.Text}");
                    _reconizedSpeeches.Add(
                        new ReconizedSpeech
                    {
                        Result = result.Text
                    });
                }
            }
            else if (result.Reason == ResultReason.NoMatch)
            {
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
            }
        }
        // Intent recognition using microphone.
        public static async Task RecognitionWithMicrophoneAsync()
        {
            // <intentRecognitionWithMicrophone>
            // Creates an instance of a speech config with specified subscription key
            // and service region. Note that in contrast to other services supported by
            // the Cognitive Services Speech SDK, the Language Understanding service
            // requires a specific subscription key from https://www.luis.ai/.
            // The Language Understanding service calls the required key 'endpoint key'.
            // Once you've obtained it, replace with below with your own Language Understanding subscription key
            // and service region (e.g., "westus").
            // The default language is "en-us".
            var config = SpeechConfig.FromSubscription("YourLanguageUnderstandingSubscriptionKey", "YourLanguageUnderstandingServiceRegion");

            // Creates an intent recognizer using microphone as audio input.
            using (var recognizer = new IntentRecognizer(config))
            {
                // Creates a Language Understanding model using the app id, and adds specific intents from your model
                var model = LanguageUnderstandingModel.FromAppId("YourLanguageUnderstandingAppId");
                recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName1", "id1");
                recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName2", "id2");
                recognizer.AddIntent(model, "YourLanguageUnderstandingIntentName3", "any-IntentId-here");

                // Starts recognizing.
                Console.WriteLine("Say something...");

                // Performs recognition. RecognizeOnceAsync() returns when the first utterance has been recognized,
                // so it is suitable only for single shot recognition like command or query. For long-running
                // recognition, use StartContinuousRecognitionAsync() instead.
                var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                // Checks result.
                if (result.Reason == ResultReason.RecognizedIntent)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent Id: {result.IntentId}.");
                    Console.WriteLine($"    Language Understanding JSON: {result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult)}.");
                }
                else if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    Console.WriteLine($"    Intent not recognized.");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
            // </intentRecognitionWithMicrophone>
        }
        public static async Task RecognizeSpeechByMicAsync()
        {
            var config = SpeechConfig.FromSubscription("f7b1af1399664520806f4b0724765a40", "eastus");

            using (var recognizer = new SpeechRecognizer(config))
            {
                var result = await recognizer.RecognizeOnceAsync();

                if (result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"We recognized: {result.Text}");
                }
                else if (result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }
        public static async Task RecognizeSpeechAsync()
        {
            var config =
                SpeechConfig.FromSubscription(
                    "8afdd1b2184d4cb794b264fafba98868", "eastus");

            using var recognizer = new SpeechRecognizer(config);

            var result = await recognizer.RecognizeOnceAsync();

            switch (result.Reason)
            {
            case ResultReason.RecognizedSpeech:
                Console.WriteLine($"We recognized: {result.Text}");
                break;

            case ResultReason.NoMatch:
                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                break;

            case ResultReason.Canceled:
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                    Console.WriteLine($"CANCELED: Did you update the subscription info?");
                }
                break;
            }
        }
        public static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("04d78025d6c14834ba9888b8d307843c", "eastus");

            using (var audioInput = AudioConfig.FromWavFileInput("./hawking01.wav"))

                using (var recognizer = new SpeechRecognizer(config, audioInput))
                {
                    Console.WriteLine("Recognizing first result...");
                    var result = await recognizer.RecognizeOnceAsync();

                    switch (result.Reason)
                    {
                    case ResultReason.RecognizedSpeech:
                        Console.WriteLine($"We recognized: {result.Text}");
                        break;

                    case ResultReason.NoMatch:
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                        break;

                    case ResultReason.Canceled:
                        var cancellation = CancellationDetails.FromResult(result);
                        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                            Console.WriteLine($"CANCELED: Did you update the subscription info?");
                        }
                        break;
                    }
                }
        }
        static async Task RecognizeSpeechAsync()
        {
            var config = SpeechConfig.FromSubscription("your subcription key", "region(eastus, westus etc)");

            using var audioInput = AudioConfig.FromWavFileInput(@"Your directory path\sample.wav");
            using var recognizer = new SpeechRecognizer(config, audioInput);
            Console.WriteLine("Recognizing first result");
            var result = await recognizer.RecognizeOnceAsync();

            switch (result.Reason)
            {
            case ResultReason.RecognizedSpeech:
                Console.WriteLine($"We've recognized: {result.Text}");
                Console.ReadLine();
                break;

            case ResultReason.NoMatch:
                Console.WriteLine("NOMATCH: Speech could not be recognized");
                Console.ReadLine();
                break;

            case ResultReason.Canceled:
                var cancellation = CancellationDetails.FromResult(result);
                Console.WriteLine($"CANCELED: Reason = {cancellation.Reason}");
                Console.ReadLine();
                if (cancellation.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELED: ErrorCode = {cancellation.ErrorCode}");
                    Console.WriteLine($"CANCELED: ErrorDetails = {cancellation.Reason}");
                    Console.WriteLine($"CANCELED: Did you update your subscription info?");
                    Console.ReadLine();
                }
                break;
            }
        }
        private async void btnRecord_Click(object sender, EventArgs e)
        {
            btnRecord.BackColor = Color.LightGreen;
            // other fun language codes:
            //fr-FR
            //ja-JP
            //hi-IN
            //de-DE
            var autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig.FromLanguages(new string[] { "fr-FR", "hi-IN" });

            using (var recognizer = new SpeechRecognizer(SpeechConfig.FromSubscription("cb35ce20eade4be2a74a36ab2e9d0ac1", "eastus"), autoDetectSourceLanguageConfig))
            {
                var speechRecognitionResult = await recognizer.RecognizeOnceAsync();

                if (speechRecognitionResult.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(speechRecognitionResult);
                    MessageBox.Show("Error: " + cancellation);
                    this.Close();
                    return;
                }
                var autoDetectSourceLanguageResult = AutoDetectSourceLanguageResult.FromResult(speechRecognitionResult);
                var detectedLanguage = autoDetectSourceLanguageResult.Language;
                btnRecord.BackColor = default(Color);
                // detectedLanguage passed on to the OptionsMenu form
                formOptionsMenu = new OptionsMenu(detectedLanguage);
                //pop up Options Menu
                formOptionsMenu.Show();
            }
            btnRecord.Click += new EventHandler(this.btnRecord_Click);
        }