예제 #1
0
        private static async Task <string> UploadAudioAndStartRemoteTranscription(string key, string region)
        {
            AudioStreamFormat audioStreamFormat;

            var config = SpeechConfig.FromSubscription(key, region);

            config.SetProperty("ConversationTranscriptionInRoomAndOnline", "true");
            config.SetServiceProperty("transcriptionMode", "RealTimeAndAsync", ServicePropertyChannel.UriQueryParameter);
            var waveFilePullStream = OpenWavFile(@"katiesteve.wav", out audioStreamFormat);
            var audioInput         = AudioConfig.FromStreamInput(AudioInputStream.CreatePullStream(waveFilePullStream, audioStreamFormat));

            var meetingId = Guid.NewGuid().ToString();

            using (var conversation = await Conversation.CreateConversationAsync(config, meetingId))
            {
                using (var conversationTranscriber = TrackSessionId(new ConversationTranscriber(audioInput)))
                {
                    await conversationTranscriber.JoinConversationAsync(conversation);

                    await conversation.AddParticipantAsync("OneUserByUserId");

                    var user = User.FromUserId("CreateUserFromId and then add it");
                    await conversation.AddParticipantAsync(user);

                    var result = await GetRecognizerResult(conversationTranscriber, meetingId);
                }
            }
            return(meetingId);
        }
예제 #2
0
        public AudioSegment(byte[] audioData, long startOffset, long endOffset,
                            uint sampleRate = SAMPLE_RATE, byte bitsPerSample = BITS_PER_SAMPLE, byte channels = CHANNELS)
        {
            MemoryStream      tempStream   = new MemoryStream(audioData);
            AudioStreamFormat streamFormat = AudioStreamFormat.GetWaveFormatPCM(sampleRate, bitsPerSample, channels);

            AudioStream = AudioInputStream.CreatePullStream(new BinaryAudioStreamReader(tempStream), streamFormat);

            AudioData   = audioData;
            StartOffset = startOffset;
            EndOffset   = endOffset;
        }
예제 #3
0
        public static async Task ConversationWithPullAudioStreamAsync(string filePath, string region, string key)
        {
            // Creates an instance of a speech config with specified subscription key and service region
            // Replace with your own subscription key and region
            var config = SpeechConfig.FromSubscription(key, region);

            config.SetProperty("ConversationTranscriptionInRoomAndOnline", "true");
            var stopTranscription = new TaskCompletionSource <int>();

            var callback   = Helper.CreateWavReader(filePath);
            var pullStream = AudioInputStream.CreatePullStream(callback, AudioStreamFormat.GetWaveFormatPCM(16000, 16, 8));

            // Create an audio stream from a wav file or from the default microphone if you want to stream live audio from the supported devices
            // Replace with your own audio file name and Helper class which implements AudioConfig using PullAudioInputStreamCallback
            using (var audioInput = AudioConfig.FromStreamInput(pullStream))
            {
                var meetingId = Guid.NewGuid().ToString();
                using (var conversation = await Conversation.CreateConversationAsync(config, meetingId).ConfigureAwait(false))
                {
                    // Create a conversation transcriber using audio stream input
                    using (var conversationTranscriber = new ConversationTranscriber(audioInput))
                    {
                        await conversationTranscriber.JoinConversationAsync(conversation);

                        // Subscribe to events
                        conversationTranscriber.Transcribing += (s, e) =>
                        {
                            //Console.WriteLine($"TRANSCRIBING: Text={e.Result.Text}");
                        };

                        conversationTranscriber.Transcribed += (s, e) =>
                        {
                            if (e.Result.Reason == ResultReason.RecognizedSpeech)
                            {
                                if (!String.IsNullOrWhiteSpace(e.Result.Text))
                                {
                                    Console.WriteLine($"TRANSCRIBED: Text={e.Result.Text}, UserID={e.Result.UserId}");
                                }
                            }
                            else if (e.Result.Reason == ResultReason.NoMatch)
                            {
                                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                            }
                        };

                        conversationTranscriber.Canceled += (s, e) =>
                        {
                            Console.WriteLine($"CANCELED: Reason={e.Reason}");

                            if (e.Reason == CancellationReason.Error)
                            {
                                Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                                Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                                Console.WriteLine($"CANCELED: Did you update the subscription info?");
                                stopTranscription.TrySetResult(0);
                            }
                        };

                        conversationTranscriber.SessionStarted += (s, e) =>
                        {
                            Console.WriteLine("\nSession started event.");
                        };

                        conversationTranscriber.SessionStopped += (s, e) =>
                        {
                            Console.WriteLine("\nSession stopped event.");
                            Console.WriteLine("\nStop recognition.");
                            stopTranscription.TrySetResult(0);
                        };

                        // Add participants to the conversation.
                        // Create voice signatures using REST API described in the earlier section in this document.
                        // Voice signature needs to be in the following format:
                        // { "Version": <Numeric string or integer value>, "Tag": "string", "Data": "string" }

                        /*
                         * var speakerA = Participant.From("Katie", "en-us", KatieSignature);
                         * var speakerB = Participant.From("Steve", "en-us", SteveSignature);
                         * await conversation.AddParticipantAsync(speakerA);
                         * await conversation.AddParticipantAsync(speakerB);
                         */

                        // Starts transcribing of the conversation. Uses StopTranscribingAsync() to stop transcribing when all participants leave.
                        await conversationTranscriber.StartTranscribingAsync().ConfigureAwait(false);

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

                        // Stop transcribing the conversation.
                        await conversationTranscriber.StopTranscribingAsync().ConfigureAwait(false);
                    }
                }
            }
        }
        public static async Task TranslationWithFileAsync(this SpeechTranslationConfig config, byte[] wavBytes, string fromLanguage, IEnumerable <string> targetLanguages, Voice voice, string outputFilename)
        {
            var synthesizingWriter = new SynthesizingWriter(outputFilename);

            config.SpeechRecognitionLanguage = fromLanguage;
            config.VoiceName = voice.ToString();
            targetLanguages.ToList().ForEach(config.AddTargetLanguage);

            using var audioInput = AudioConfig.FromStreamInput(
                      AudioInputStream.CreatePullStream(
                          new BinaryAudioStreamReader(
                              new MemoryStream(
                                  wavBytes))));

            using var recognizer = new TranslationRecognizer(config, audioInput);
            var stopTranslation = new TaskCompletionSource <int>();

            recognizer.Recognizing += (s, e) =>
            {
                Console.WriteLine($"RECOGNIZING in '{fromLanguage}': Text={e.Result.Text}");
                foreach (var element in e.Result.Translations)
                {
                    Console.WriteLine($"    TRANSLATING into '{element.Key}': {element.Value}");
                }
            };

            recognizer.Recognized += (s, e) => {
                if (e.Result.Reason == ResultReason.TranslatedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED in '{fromLanguage}': Text={e.Result.Text}");
                    foreach (var element in e.Result.Translations)
                    {
                        Console.WriteLine($"    TRANSLATED into '{element.Key}': {element.Value}");
                    }
                }
                else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                    Console.WriteLine($"    Speech not translated.");
                }
                else if (e.Result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                }
            };

            recognizer.Canceled += (s, e) =>
            {
                Console.WriteLine($"CANCELED: Reason={e.Reason}");

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

                stopTranslation.TrySetResult(0);
            };

            recognizer.SpeechStartDetected += (s, e) => { Console.WriteLine("\nSpeech start detected event."); };
            recognizer.SpeechEndDetected   += (s, e) => { Console.WriteLine("\nSpeech end detected event."); };
            recognizer.SessionStarted      += (s, e) => { Console.WriteLine("\nSession started event."); };
            recognizer.SessionStopped      += (s, e) => {
                Console.WriteLine("\nSession stopped event.");
                Console.WriteLine($"\nStop translation.");
                stopTranslation.TrySetResult(0);
            };

            recognizer.Synthesizing += synthesizingWriter.Synthesizing;

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

            await Task.WhenAny(new[] { stopTranslation.Task });

            // Stops translation.
            await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(continueOnCapturedContext: false);
        }
예제 #5
0
        public async void testFunc()
        {
            // Define Output Path and Create a new WaveInEvent
            var outputFilePath = @"C:\Users\Joe\source\repos\Monotone\Monotone\bin\x86\Debug x86\audio.wav";
            var waveInEvent    = new WaveInEvent();

            waveInEvent.DeviceNumber = 0;

            var bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(8000, 1));

            // Prepare the fileWriter
            WaveFileWriter fileWriter = new WaveFileWriter(outputFilePath, waveInEvent.WaveFormat);

            // Set-up the Azure speech configuration with our subscription info and enable dictation capabilities
            var speechConfig = SpeechConfig.FromSubscription("b8305ebbfce64754a0150547a076a0be", "westus");

            speechConfig.EnableDictation();
            speechConfig.SetProfanity(ProfanityOption.Raw);
            speechConfig.OutputFormat = OutputFormat.Detailed;

            // Set-Up Audio Configuration using Audio Callback method for NAudio Capture
            NAudioCompatibileAudioCallback1 audioCallback = new NAudioCompatibileAudioCallback1(ref bufferedWaveProvider);
            //var audioStreamCallback = AudioInputStream.CreatePullStream(audioCallback, AudioStreamFormat.GetDefaultInputFormat());
            var audioStreamCallback = AudioInputStream.CreatePullStream(audioCallback,
                                                                        AudioStreamFormat.GetWaveFormatPCM((uint)waveInEvent.WaveFormat.SampleRate,
                                                                                                           (byte)waveInEvent.WaveFormat.BitsPerSample,
                                                                                                           (byte)waveInEvent.WaveFormat.Channels));

            var audioConfig = AudioConfig.FromStreamInput(audioStreamCallback);

            // Initialize the SpeechRecognizer API
            var recognizer = new SpeechRecognizer(speechConfig, audioConfig);

            // Declar a TaskCompletionSource to help shutdown the continuous speech processing later
            var stopRecognition = new TaskCompletionSource <int>();

            // Recognizer Event-Based handeling
            #region Recognizer Event-Based Handeling

            recognizer.Recognizing += (s, e) =>
            {
                Console.WriteLine($"RECOGNIZING: Text ={e.Result.Text}");
            };

            recognizer.Recognized += (s, e) =>
            {
                if (e.Result.Reason == ResultReason.RecognizedSpeech)
                {
                    Console.WriteLine($"RECOGNIZED: Text ={e.Result.Text}");
                }
                else if (e.Result.Reason == ResultReason.NoMatch)
                {
                    Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    Console.WriteLine($"NOMATCH: Detail ={NoMatchDetails.FromResult(e.Result).Reason}");
                    Console.WriteLine($"NOMATCH: Duration ={e.Result.Duration}");
                }
            };

            recognizer.Canceled += (s, e) =>
            {
                if (e.Reason == CancellationReason.Error)
                {
                    Console.WriteLine($"CANCELLED: ErrorCode ={e.ErrorCode}");
                    Console.WriteLine($"CANCELLED: ErrorDetails ={e.ErrorDetails}");
                    Console.WriteLine($"CANCELLED: Did you update your subscription info?");
                }

                stopRecognition.TrySetResult(0);
            };

            recognizer.SessionStopped += (s, e) =>
            {
                Console.WriteLine("\n   Session Stopped Event.");

                stopRecognition.TrySetResult(0);
            };
            #endregion

            // NAudio WaveInEvent Event-Based Handeling
            #region NAudio WaveInEvent Event-Based Handeling
            waveInEvent.DataAvailable += (s, a) =>
            {
                // Use callback to send recorded data for analysis via Recognizer
                //audioCallback.Read(a.Buffer, (uint)a.BytesRecorded);
                bufferedWaveProvider.AddSamples(a.Buffer, 0, a.BytesRecorded);

                // Then Write the data
                fileWriter.Write(a.Buffer, 0, a.BytesRecorded);

                // Force Stop Recording after 30 seconds
                //if (fileWriter.Position > waveInEvent.WaveFormat.AverageBytesPerSecond * 30)
                //{
                //    waveInEvent.StopRecording();
                //}
            };

            waveInEvent.RecordingStopped += (s, a) =>
            {
                fileWriter?.Dispose();
                fileWriter = null;
            };
            #endregion

            // Start Recording
            waveInEvent.StartRecording();

            Console.WriteLine("Begin Recording... ");
            Console.WriteLine("Press Any Key to Stop Recording.");

            // Start Continuous Recognition
            await recognizer.StartContinuousRecognitionAsync();

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

            Console.ReadKey();

            // Stops Recognition
            await recognizer.StopContinuousRecognitionAsync();

            // Stop Recording and dispose object
            waveInEvent.StopRecording();
            waveInEvent.Dispose();

            Console.WriteLine("Recording Stopped.");
        }