示例#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 pull audio output stream.
        public static async Task SynthesisToPullAudioOutputStreamAsync()
        {
            // 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 audio out stream.
            using (var stream = AudioOutputStream.CreatePullStream())
            {
                // 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 pull 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?");
                                    }
                                }
                            }
                        }
                    }

                // Reads(pulls) data from the stream
                byte[] buffer     = new byte[32000];
                uint   filledSize = 0;
                uint   totalSize  = 0;
                while ((filledSize = stream.Read(buffer)) > 0)
                {
                    Console.WriteLine($"{filledSize} bytes received.");
                    totalSize += filledSize;
                }

                Console.WriteLine($"Totally {totalSize} bytes received.");
            }
        }
示例#3
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Text to PCM Console:");

            if (args == null || args.Length < 3)
            {
                Console.WriteLine("Usage: texttopcm <azure subscription key> <azure region> <text>");
                Console.WriteLine("e.g. texttopcm cb965... westeurope  \"Hello World\"");
                Console.WriteLine("e.g. dotnet run -- cb965... westeurope  \"Hello World\"");
            }
            else
            {
                var speechConfig = SpeechConfig.FromSubscription(args[0], args[1]);

                string text = args[2];

                TextToSpeechStream ttsOutStream      = new TextToSpeechStream();
                AudioConfig        audioTtsConfig    = AudioConfig.FromStreamOutput(ttsOutStream);
                SpeechSynthesizer  speechSynthesizer = new SpeechSynthesizer(speechConfig, audioTtsConfig);

                using (var result = await speechSynthesizer.SpeakTextAsync(text))
                {
                    if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                    {
                        Console.WriteLine($"Speech synthesized to speaker for text [{text}]");

                        var    buffer       = ttsOutStream.GetPcmBuffer();
                        string saveFilename = DateTime.Now.Ticks.ToString() + ".pcm16k";

                        using (StreamWriter sw = new StreamWriter(saveFilename))
                        {
                            for (int i = 0; i < buffer.Length; i++)
                            {
                                sw.BaseStream.Write(BitConverter.GetBytes(buffer[i]));
                            }
                        }

                        Console.WriteLine($"Result saved to {saveFilename}.");
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                        Console.WriteLine($"Speech synthesizer failed was cancelled, reason={cancellation.Reason}");

                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"Speech synthesizer cancelled: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"Speech synthesizer cancelled: ErrorDetails=[{cancellation.ErrorDetails}]");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Speech synthesizer failed with result {result.Reason} for text [{text}].");
                    }
                }
            }
        }
示例#4
0
    // Speech synthesis to pull audio output stream.
    public async Task SpeakWithSDKPlugin(string message)
    {
        Synthesize cortana = new Synthesize();

        // 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(SpeechServiceAPIKey, SpeechServiceRegion);

        config.SpeechSynthesisLanguage  = cortana.GetVoiceLocale(voiceName);
        config.SpeechSynthesisVoiceName = cortana.ConvertVoiceNametoString(voiceName);

        // Creates an audio out stream.
        using (var stream = AudioOutputStream.CreatePullStream())
        {
            // Creates a speech synthesizer using audio stream output.
            using (var streamConfig = AudioConfig.FromStreamOutput(stream))
                using (var synthesizer = new SpeechSynthesizer(config, streamConfig))
                {
                    using (var result = await synthesizer.SpeakTextAsync(message))
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            var audiodata = result.AudioData;
                            Debug.Log($"Speech synthesized for text and the audio was written to output stream.");

                            int sampleCount = 0;
                            int frequency   = 16000;
                            var unityData   = FixedRAWAudioToUnityAudio(audiodata, 1, 16, out sampleCount);

                            // Convert data to a Unity audio clip
                            Debug.Log($"Converting audio data of size {unityData.Length} to Unity audio clip with {sampleCount} samples at frequency {frequency}.");
                            var clip = ToClip("Speech", unityData, sampleCount, frequency);

                            // Set the source on the audio clip
                            audioSource.clip = clip;

                            Debug.Log($"Trigger playback of audio clip on AudioSource.");
                            // Play audio
                            audioSource.Play();
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                            Debug.Log($"CANCELED: Reason={cancellation.Reason}");

                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                Debug.Log($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                Debug.Log($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                                Debug.Log($"CANCELED: Did you update the subscription info?");
                            }
                        }
                    }
                }
        }
    }
示例#5
0
 public TranslationEngine(IConfiguration config, IHubContext <TranslationHub> hub)
 {
     _hub               = hub;
     _config            = config;
     _translationConfig = SpeechTranslationConfig.FromSubscription(_config["SUBSCRIPTION_KEY"], _config["REGION"]);
     _speechConfig      = SpeechTranslationConfig.FromSubscription(_config["SUBSCRIPTION_KEY"], _config["REGION"]);
     _audioInput        = AudioConfig.FromStreamInput(_inputStream);
     _audioOutputStream = AudioOutputStream.CreatePullStream();
     _output            = AudioConfig.FromStreamOutput(_audioOutputStream);
 }
示例#6
0
        private SpeechSynthesizer BuildAzureSpeechSynthesizer()
        {
            // Create an audio config to tell Azure Speech SDK to return speech output as a memory stream
            // using its default output format (16kHz, 16bit, mono).
            var audioConfig =
                AudioConfig.FromStreamOutput(
                    AudioOutputStream.CreatePullStream(AudioStreamFormat.GetDefaultOutputFormat()));

            // Create an instance of the Azure Speech SDK speech synthesizer
            return(new SpeechSynthesizer(SpeechConfig, audioConfig));
        }
示例#7
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.");
            }
        }
示例#8
0
        public async Task <Stream> RenderSpeechAsync(string content)
        {
            var audioStream = AudioOutputStream.CreatePullStream();
            var audioConfig = AudioConfig.FromStreamOutput(audioStream);

            using var _synthesizer = new SpeechSynthesizer(_configuration, audioConfig);
            using var result       = await _synthesizer.SpeakTextAsync(content);

            if (result.Reason == ResultReason.SynthesizingAudioCompleted)
            {
                var stream = new MemoryStream();
                stream.Write(result.AudioData, 0, result.AudioData.Length);
                return(stream);
            }

            var details = SpeechSynthesisCancellationDetails.FromResult(result);

            throw new RenderSpeechException(details.ErrorDetails);
        }
        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}");
        }
示例#10
0
        public async Task <IAudioClip> Synthesize(string text)
        {
            var stream = AudioOutputStream.CreatePullStream();

            //Generate voice data into stream
            using (var streamConfig = AudioConfig.FromStreamOutput(stream))
                using (var synthesizer = new SpeechSynthesizer(_config, streamConfig))
                {
                    using var result = await synthesizer.SpeakTextAsync(text);

                    if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                        throw new TaskCanceledException($"{cancellation.Reason}: {cancellation.ErrorDetails}");
                    }
                }

            //Create a clip which consumes this audio data
            return(new AudioOutputStreamClip($"TTS:`{text}`", stream, new WaveFormat(16000, 16, 1)));
        }
示例#11
0
        public async Task Speak(string text, BufferedWaveProvider waveProvider, int rate)
        {
            var fmt = new System.Speech.AudioFormat.SpeechAudioFormatInfo(waveProvider.WaveFormat.SampleRate, (System.Speech.AudioFormat.AudioBitsPerSample)waveProvider.WaveFormat.BitsPerSample, (System.Speech.AudioFormat.AudioChannel)waveProvider.WaveFormat.Channels);

            // 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(Key, Region);

            config.SpeechSynthesisLanguage  = Language;
            config.SpeechSynthesisVoiceName = Voice;

            // Creates an audio out stream.
            using (var stream = AudioOutputStream.CreatePullStream(AudioStreamFormat.GetWaveFormatPCM((uint)waveProvider.WaveFormat.SampleRate, (byte)waveProvider.WaveFormat.BitsPerSample, (byte)waveProvider.WaveFormat.Channels)))
            {
                // Creates a speech synthesizer using audio stream output.
                using (var streamConfig = AudioConfig.FromStreamOutput(stream))
                    using (var synthesizer = new SpeechSynthesizer(config, streamConfig))
                    {
                        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);
                                OnLog?.Invoke($"CANCELED: Reason={cancellation.Reason}");

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

/*
 *              using (var reader = new WaveFileReader(new PullStream(stream)))
 *              {
 *                  var newFormat = new WaveFormat(waveProvider.WaveFormat.SampleRate, waveProvider.WaveFormat.BitsPerSample, waveProvider.WaveFormat.Channels);
 *                  using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
 *                  {
 *                      //WaveFileWriter.CreateWaveFile("output.wav", conversionStream);
 *                      byte[] buffer = new byte[32000];
 *                      int filledSize = 0;
 *                      int totalSize = 0;
 *                      while ((filledSize = conversionStream.Read(buffer, 0, buffer.Length)) > 0)
 *                      {
 *                          waveProvider.AddSamples(buffer, 0, (int)filledSize);
 *                          //Console.WriteLine($"{filledSize} bytes received.");
 *                          totalSize += filledSize;
 *                      }
 *                  }
 *              }*/


                // Reads(pulls) data from the stream
                byte[] buffer     = new byte[32000];
                uint   filledSize = 0;
                uint   totalSize  = 0;
                while ((filledSize = stream.Read(buffer)) > 0)
                {
                    waveProvider.AddSamples(buffer, 0, (int)filledSize);
                    //Console.WriteLine($"{filledSize} bytes received.");
                    totalSize += filledSize;
                }
            }
        }