Esempio n. 1
0
        private void Button_Stop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                waveSource.StopRecording();
                // Close Wave (not needed under synchronous situation)
                if (waveSource != null)
                {
                    waveSource.Dispose();
                    waveSource = null;
                }

                var response = request.GetResponse();
                using (var responseStream = response.GetResponseStream())
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        var         responseJsonText = streamReader.ReadToEnd(); // The result in JSON format, with pronunciation score
                        ScoreResult result           = JsonConvert.DeserializeObject <ScoreResult>(responseJsonText);

                        if (null != result && "Success" == result.RecognitionStatus)
                        {
                            NBestItem nBestItem = result.NBest[0];
                            var       pronScore = JsonConvert.SerializeObject(nBestItem);

                            PronScoreWebBrowser.InvokeScript("generatePronScoreTable", pronScore);
                            PronScoreWebBrowser.InvokeScript("generatePhoneScoreTable", pronScore);
                            PronScoreWebBrowser.Visibility = Visibility.Visible;
                            List <WordsItem> witems = result.NBest[0].Words;
                            for (int i = 0; i < witems.Count; i++)
                            {
                                WordsItem w = witems[i];
                                if (w.AccuracyScore <= 60.0)
                                {
                                    ChangeColor(Colors.Red, ReferenceText, w.Word.ToLower(), w.AccuracyScore);
                                }
                                else if (w.AccuracyScore <= 70.0)
                                {
                                    ChangeColor(Colors.Orange, ReferenceText, w.Word.ToLower(), w.AccuracyScore);
                                }
                                else
                                {
                                    ChangeColor(Colors.Green, ReferenceText, w.Word.ToLower(), w.AccuracyScore);
                                }
                            }
                        }
                        else
                        {
                            if (null != result)
                            {
                                MessageBox.Show($"Recognition status: {result.RecognitionStatus}");
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                progressRing.IsActive = false;
                StartBut.Visibility   = Visibility.Visible;
                StopBut.Visibility    = Visibility.Collapsed;
            }
        }
        private void Button_Stop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                waveSource.StopRecording();
                // Close Wave(Not needed under synchronous situation)
                if (waveSource != null)
                {
                    waveSource.Dispose();
                    waveSource = null;
                }

                if (waveFile != null)
                {
                    waveFile.Dispose();
                    waveFile = null;
                }

                //var audioFile = @"GoodMorning.wav"; // The audio file in 16khz 16bit PCM format
                //var referenceText = "Good Morning!";
                var pronScoreParamsJson  = $"{{\"ReferenceText\":\"{referenceText}\",\"GradingSystem\":\"HundredMark\",\"Dimension\":\"Comprehensive\",\"EnableMiscue\":\"True\"}}";
                var pronScoreParamsBytes = Encoding.UTF8.GetBytes(pronScoreParamsJson);
                var pronScoreParams      = Convert.ToBase64String(pronScoreParamsBytes);

                var request = (HttpWebRequest)HttpWebRequest.Create($"https://{region}.stt.speech.microsoft.com/speech/recognition/interactive/cognitiveservices/v1?language={language}&pronunciationScoreParams={pronScoreParams}");
                request.SendChunked     = true;
                request.Accept          = @"application/json;text/xml";
                request.Method          = "POST";
                request.ProtocolVersion = HttpVersion.Version11;
                request.ContentType     = @"audio/wav; codecs=audio/pcm; samplerate=16000";
                request.Headers["Ocp-Apim-Subscription-Key"] = subscriptionKey;
                request.AllowWriteStreamBuffering            = false;
                using (var fs = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
                {     /*
                       * Open a request stream and write 1024 byte chunks in the stream one at a time.
                       */
                    byte[] buffer    = null;
                    int    bytesRead = 0;
                    using (Stream requestStream = request.GetRequestStream())
                    {   /*
                         * Read 1024 raw bytes from the input audio file
                         */
                        buffer = new Byte[checked ((uint)Math.Min(1024, (int)fs.Length))];
                        while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                        }
                        // Flush
                        requestStream.Flush();
                    }
                }
                var response = request.GetResponse();
                using (var responseStream = response.GetResponseStream())
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        var responseJsonText = streamReader.ReadToEnd(); // The result in JSON format, with pronunciation score

                        ScoreResult result = JsonConvert.DeserializeObject <ScoreResult>(responseJsonText);

                        if (null != result && "Success" == result.RecognitionStatus)
                        {
                            //PronScore.Text = result.NBest[0].PronScore.ToString();
                            //AccuracyScore.Text = result.NBest[0].AccuracyScore.ToString();
                            //FluencyScore.Text = result.NBest[0].FluencyScore.ToString();
                            //CompletionScore.Text = result.NBest[0].CompletionScore.ToString();

                            //generatePronScoreTable();generatePhoneScoreTable();


                            NBestItem nBestItem = result.NBest[0];


                            var pronScore = JsonConvert.SerializeObject(nBestItem);

                            PornScoreWebBrowser.InvokeScript("generatePronScoreTable", pronScore);
                            PornScoreWebBrowser.InvokeScript("generatePhoneScoreTable", pronScore);
                            PornScoreWebBrowser.Visibility = Visibility.Visible;
                            List <WordsItem> witems = result.NBest[0].Words;
                            for (int i = 0; i < witems.Count; i++)
                            {
                                WordsItem w = witems[i];
                                if (w.AccuracyScore <= 60.0)
                                {
                                    ChangeColor(Colors.Red, ReferenceText, w.Word, w.AccuracyScore);
                                }
                                else if (w.AccuracyScore <= 70.0)
                                {
                                    ChangeColor(Colors.Orange, ReferenceText, w.Word, w.AccuracyScore);
                                }
                                else
                                {
                                    ChangeColor(Colors.Green, ReferenceText, w.Word, w.AccuracyScore);
                                }
                            }
                        }
                        else
                        {
                            if (null != result)
                            {
                                MessageBox.Show(result.RecognitionStatus);
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                progressRing.IsActive = false;
                StartBut.Visibility   = Visibility.Visible;
                StopBut.Visibility    = Visibility.Collapsed;
            }
        }