tzviya
tzviya

Reputation: 568

Audio retrieval Using Azure TTS API in C#, missing audio for certain words

I am using my C# code with "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1" to get audio for English words. It was working well until I suddenly noticed that for certain words, it returns empty audio. For example, for the word "black."

At first, I thought that the audio for some words was simply missing, but then I tried calling the same API from Python or directly from curl and I was able to get the audio for the words that were missing.

What is the explanation for this? Are they accessing different locations? How can I change the C# code so that it can successfully retrieve these words?

Upvotes: 0

Views: 58

Answers (1)

Dasari Kamali
Dasari Kamali

Reputation: 3649

I tried the code below to convert Azure text to speech and got the speech for all the words I placed in my text in the code.

Code :

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
 
class Program
{
    static async Task Main(string[] args)
    {
        string textToSynthesize = "Hello, this is a test of the Azure Cognitive Services text-to-speech API with black.";
        string outputAudioFilePath = "path to/<wavefile>.wav";
        await ConvertTextToSpeech(textToSynthesize, outputAudioFilePath);
    }

    static async Task ConvertTextToSpeech(string textToSynthesize, string outputAudioFilePath)
    {
        var speechConfig = SpeechConfig.FromSubscription("<speech_key>", "<speech_region>");
        speechConfig.SpeechSynthesisLanguage = "en-US";
        speechConfig.SpeechSynthesisVoiceName = "en-US-AvaMultilingualNeural"; 

        var audioConfig = AudioConfig.FromWavFileOutput(outputAudioFilePath);
        var synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);

        var result = await synthesizer.SpeakTextAsync(textToSynthesize);

        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
        {
            Console.WriteLine($"Speech synthesized to [{outputAudioFilePath}] for text: {textToSynthesize}");
        }
        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: ErrorDetails=[{cancellation.ErrorDetails}]");
            }
        }
    }
}

Output :

The following code ran successfully as below,

enter image description here

Upvotes: 1

Related Questions