コード例 #1
0
 public TranscriptionOutput(string text, Boolean transcriptionSuccess, AudioSegment segment, User speaker = null)
 {
     Text                 = text;
     StartOffset          = (int)segment.StartOffset;
     EndOffset            = (int)segment.EndOffset;
     TranscriptionSuccess = transcriptionSuccess;
     Segment              = segment;
     Speaker              = speaker;
 }
コード例 #2
0
        private async Task RecognitionWithPullAudioStreamAsync()
        {
            var stopRecognition = new TaskCompletionSource <int>();
            var entireAudio     = Controller.FileSplitter.GetEntireAudio();

            using var audioInput = AudioConfig.FromStreamInput(entireAudio.AudioStream);
            // Creates a speech recognizer using audio stream input.
            using var speech_recogniser = new SpeechRecognizer(Controller.SpeechConfig, audioInput);
            // Subscribes to events. Subscription is important, otherwise recognition events aren't handled.
            speech_recogniser.Recognizing += (s, e) =>
            {
                //
            };
            speech_recogniser.Recognized += (s, e) =>
            {
                string transcribedText = "";

                Boolean resultAvailable = false;
                Boolean success         = false;

                if (e.Result.Reason == ResultReason.RecognizedSpeech)
                {
                    resultAvailable = true;
                    Console.WriteLine($"RECOGNIZED: Text = {e.Result.Text}\n");
                    transcribedText = e.Result.Text;                                      //Write transcription text to result
                    success         = true;                                               //Set flag to indicate that transcription succeeded.                                                //Reset error counter
                }
                else if (e.Result.Reason == ResultReason.NoMatch)
                {
                    resultAvailable = true;
                    Console.WriteLine($">\tNOMATCH: Speech could not be recognized.");
                    transcribedText = $"NOMATCH: Speech could not be recognized.";        //Write fail message to result
                }
                if (resultAvailable)
                {
                    /* Start and end offsets in milliseconds from 0, which is beginning of audio. Note
                     * conversion from ticks to milliseconds.*/
                    long startOffset = e.Result.OffsetInTicks / 10000L;
                    long endOffset   = startOffset + (long)e.Result.Duration.TotalMilliseconds;

                    //CRITICAL section. Add the result to transcriptionOutputs wrapped in a TranscriptionOutput object.
                    lock (_lockObj)
                    {
                        /*Split the audio based on start and end offset of the identified phrase. Note access to shared stream. */
                        AudioSegment segment = Controller.FileSplitter.SplitAudio((ulong)startOffset, (ulong)endOffset);
                        TranscriptionOutputs.Add(startOffset, new TranscriptionOutput(transcribedText, success, segment));
                    } //END CRITICAL section.
                }
            };

            speech_recogniser.Canceled += (s, e) =>
            {
                Console.WriteLine($">\tCANCELED: Reason = {e.Reason}");
                Console.WriteLine(">\tTranscription Complete.");

                if (e.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($">\tCANCELED: ErrorCode={e.ErrorCode}");
                    Console.WriteLine($">\tCANCELED: ErrorDetails={e.ErrorDetails}");
                    Console.WriteLine($">\tCANCELED: Make Sure to Update Subscription Info");
                }
                stopRecognition.TrySetResult(0);
            };

            speech_recogniser.SessionStarted += (s, e) =>
            {
                //Console.WriteLine(">\tSession Started.");
            };

            speech_recogniser.SessionStopped += (s, e) =>
            {
                //Console.WriteLine(">\tSession Stopped.");
                stopRecognition.TrySetResult(0);
            };

            // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
            await speech_recogniser.StartContinuousRecognitionAsync().ConfigureAwait(false);

            // Waits for completion.
            // Use Task.WaitAny to keep the task rooted.
            Task.WaitAny(new[] { stopRecognition.Task });


            // Stops recognition.
            await speech_recogniser.StopContinuousRecognitionAsync().ConfigureAwait(false);
        }