Do text to speech (Cognitive Services) from a REST API hosted in Azure App Service

Hans-Henrik Møller (HAMR) 10 Reputation points
2023-03-14T14:32:51.28+00:00

I am trying to do Text-to-speech via Cognitive Services and package it up into an API and publish it to an Azure App Service. Running the code locally works fine, but when I publish it to Azure and call my API endpoint, I get an error when I try and initialize a SpeechSynthesizer. The error being: Exception with an error code: 0x38 (SPXERR_AUDIO_SYS_LIBRARY_NOT_FOUND)

The problem occurs in the method call when setting SpeechSynthesisVoiceName ... here the constructor on SpeechSynthesizer fails with the above mentioned error code. But again, all works fine when running the API locally.

My method:

        [HttpGet(Name = "GetSpokenText")]
        public async Task
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,393 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hans-Henrik Møller (HAMR) 10 Reputation points
    2023-03-15T15:07:54.6566667+00:00

    So, after digging, with the clues here [https://learn.microsoft.com/en-us/answers/questions/1184428/azure-text-to-speech-error-code-0x38-(spxerr-audio](https://learn.microsoft.com/en-us/answers/questions/1184428/azure-text-to-speech-error-code-0x38-(spxerr-audio), I found the solution..

    Just have to add an AudioConfig to the constructor pointing to anything but speaker or microphone (they do not exist on Azure machines).

            public static async Task<VoiceInfo> GetVoice(this SpeechConfig config, string localeid)
            {
                var audio_config = AudioConfig.FromStreamOutput(new PullAudioOutputStream());
    
                using (var synthesizer = new SpeechSynthesizer(config, audio_config))
                {
                    using (var voices = await synthesizer.GetVoicesAsync())
                    {
                        var voice = voices.Voices.FirstOrDefault(voice => voice.Locale == localeid) ??
                                    voices.Voices.First(voice => voice.Locale == defaultLocale);
    
                        return voice;
                    }
                }
            }
    
    1 person found this answer helpful.
    0 comments No comments