示例#1
0
        private async Task <byte[]> SynthesisWithByteStreamToByteArrayAsync(string ssmlInput, SpeechConfig config)
        {
            var callback = new PushAudioOutputStreamSampleCallback();

            using var stream       = AudioOutputStream.CreatePushStream(callback);
            using var streamConfig = AudioConfig.FromStreamOutput(stream);
            using var synthesizer  = new SpeechSynthesizer(config, streamConfig);
            using var result       = await synthesizer.SpeakSsmlAsync(ssmlInput);

            if (result.Reason == ResultReason.SynthesizingAudioCompleted)
            {
                return(callback.GetAudioData());
            }
            else if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = SpeechSynthesisCancellationDetails.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);
        }
示例#2
0
        // Speech synthesis to push audio output stream.
        public static async Task SynthesisToPushAudioOutputStreamAsync()
        {
            // Creates an instance of a speech config with specified subscription key and service region.
            // Replace with your own subscription key and service region (e.g., "westus").
            var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");

            // Creates an instance of a customer class inherited from PushAudioOutputStreamCallback
            var callback = new PushAudioOutputStreamSampleCallback();

            // Creates an audio out stream from the callback.
            using (var stream = AudioOutputStream.CreatePushStream(callback))
            {
                // Creates a speech synthesizer using audio stream output.
                using (var streamConfig = AudioConfig.FromStreamOutput(stream))
                    using (var synthesizer = new SpeechSynthesizer(config, streamConfig))
                    {
                        while (true)
                        {
                            // Receives a text from console input and synthesize it to push audio output stream.
                            Console.WriteLine("Enter some text that you want to synthesize, or enter empty text to exit.");
                            Console.Write("> ");
                            string text = Console.ReadLine();
                            if (string.IsNullOrEmpty(text))
                            {
                                break;
                            }

                            using (var result = await synthesizer.SpeakTextAsync(text))
                            {
                                if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                                {
                                    Console.WriteLine($"Speech synthesized for text [{text}], and the audio was written to output stream.");
                                }
                                else if (result.Reason == ResultReason.Canceled)
                                {
                                    var cancellation = SpeechSynthesisCancellationDetails.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($"Totally {callback.GetAudioData().Length} bytes received.");
            }
        }
        private async Task <KeyValuePair <string, string>?> CreateAndUploadSpeech(int episodeId, SrStoredEpisode storedEpisode, string text, string language, string voice)
        {
            var speechConfig = _speechConfigFactory.Get();

            speechConfig.SpeechSynthesisLanguage  = language;
            speechConfig.SpeechSynthesisVoiceName = voice;
            speechConfig.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3);

            using var stream      = new MemoryStream();
            using var audioStream = AudioOutputStream.CreatePushStream(new AudioPushAudioOutputStreamCallback(stream));
            using var fileOutput  = AudioConfig.FromStreamOutput(audioStream);
            using var synthesizer = new SpeechSynthesizer(speechConfig, fileOutput);

            var result = await synthesizer.SpeakTextAsync(text);

            if (result.Reason == ResultReason.SynthesizingAudioCompleted)
            {
                _logger.LogInformation($"Created speech for episode {episodeId}");
                var uploadedBlob = await UploadSpeech(storedEpisode, stream, voice);

                _logger.LogInformation($"Uploaded speech for episode {episodeId}");

                return(uploadedBlob);
            }

            if (result.Reason == ResultReason.Canceled)
            {
                var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                _logger.LogError($"Error creating speech for episode {episodeId}: Reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)
                {
                    // Expect some texts to be to long etc
                    _logger.LogError(
                        $"Error creating speech for episode {episodeId}: ErrorCode={cancellation.ErrorCode}; ErrorDetails=[{cancellation.ErrorDetails}]");
                }

                return(null);
            }

            throw new Exception($"Unknown result status for speech: {result.Reason}");
        }