Leon
Leon

Reputation: 339

Azure Cognitive Service - TTS

I just signed for free Azure account. I checked and rechecked that my Subscription ID is correct.

This code fails and the reason is:

"WebSocket upgrade failed: Authentication error (401). Please check subscription information and region name. USP state: Sending. Received audio size: 0 bytes."

Can anybody shed the light on what's going on?

     public static async Task Start()

     {

         var subscriptionKey = "my subscription ID is here";

         var region = "westus2"; // I live in San Diego ,CA


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

         config.SpeechSynthesisVoiceName = "en-US-AriaNeural";


        using (var audioConfig = AudioConfig.FromWavFileOutput("output.wav"))
        {
            using (var synthesizer = new SpeechSynthesizer(config, audioConfig))
            {
                string inputText = "Hello, this is a sample text to be converted to speech. It is not long, but hopefully it is of a great quality" +

                                   "since it is generating using Azure SDK. Have a nice day!";

                try
                {
                    using (var result = await synthesizer.SpeakTextAsync(inputText))
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            Console.WriteLine($"The text has been converted to speech and saved as output.wav");
                        }
                        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?");
                            }
                        }
                   }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
        }
     }

Upvotes: 0

Views: 343

Answers (1)

Sampath
Sampath

Reputation: 3614

using Speech service in Azure I converted text-to-speech and saved as output.wav.

enter image description here

using System;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;

namespace TextToSpeech
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string subscriptionKey = "Your Speech service subscriptionKey";
            string region = "your Speech service Location/Region";
            var config = SpeechConfig.FromSubscription(subscriptionKey, region);
            config.SpeechSynthesisVoiceName = "en-US-AriaNeural";
            string inputText = "Hello, here is a sample text that will be said aloud.";
            using (var synthesizer = new SpeechSynthesizer(config))
            {
                using (var result = await synthesizer.SpeakTextAsync(inputText))
                {
                    if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                    {
                      using (var audioDataStream = AudioDataStream.FromResult(result))
                        {
                            await audioDataStream.SaveToWaveFileAsync("output.wav");
                        }
                        Console.WriteLine($"After being read out, the text was saved as output. wav");
                    }
                    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.ReadLine();
        }
    }
}

enter image description here

enter image description here

Upvotes: 0

Related Questions