示例#1
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.");
            }
        }
示例#2
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?");
                            }
                        }
                    }
                }
        }
    }
示例#3
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);
 }
示例#4
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));
        }
示例#5
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);
        }
示例#6
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)));
        }
示例#7
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;
                }
            }
        }