在 Azure 上调用 SpeechAPI 进行文本转语音

Calling SpeechAPI for text to speech on Azure

我的本地服务器上有以下非常基本的 TTS 代码 运行

using System.Speech.Synthesis;
...
SpeechSynthesizer reader = new SpeechSynthesizer();
reader.Speak("This is a test");

此代码依赖于 System.Speech,我已在我的 VS 2015 项目中为其添加了引用。 工作正常,但根据我所阅读和尝试的内容,我知道当代码托管在 Azure 上时,这将不起作用。 我已经阅读了一些关于 SO 的帖子,询问是否真的可以在 Azure 上进行 TTS。当然,在 2 年前,这似乎是不可能的。 How to get System.Speech on windows azure websites?

条条大路通微软演讲API https://azure.microsoft.com/en-gb/marketplace/partners/speechapis/speechapis/ 我已经注册并获得了用于调用此 API 的私钥和安全密钥。 但是我的问题是这样的。我实际上如何调用 SpeechAPI?在上面的简单代码示例中,我必须更改什么才能在 运行 on azure 时正常工作?

您在 Azure 市场上提到的演讲 API 是一个名为 ProjectOxford 的 AI Microsoft 项目的一部分,该项目为计算机视觉、语音和语言提供了一系列 API .

这些都是 RESTful API,这意味着您将构建 HTTP 请求以发送到云中的托管在线服务。 语音转文本文档可用 here and you can find sample code for various clients on github. Specifically for C# you can see some code in this sample project.

请注意,ProjectOxford 仍处于预览阶段(测试版)。可以在 ProjectOxford MSDN forum.

上找到对使用这些 API 的额外支持

但只是为了让您了解您的程序的外观(取自上述 github 上的代码示例):

        AccessTokenInfo token;

        // Note: Sign up at http://www.projectoxford.ai for the client credentials.
        Authentication auth = new Authentication("Your ClientId goes here", "Your Client Secret goes here");

        ... 

        token = auth.GetAccessToken();

        ...

        string requestUri = "https://speech.platform.bing.com/synthesize";

        var cortana = new Synthesize(new Synthesize.InputOptions()
        {
            RequestUri = new Uri(requestUri),
            // Text to be spoken.
            Text = "Hi, how are you doing?",
            VoiceType = Gender.Female,
            // Refer to the documentation for complete list of supported locales.
            Locale = "en-US",
            // You can also customize the output voice. Refer to the documentation to view the different
            // voices that the TTS service can output.
            VoiceName = "Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)",
            // Service can return audio in different output format. 
            OutputFormat = AudioOutputFormat.Riff16Khz16BitMonoPcm,
            AuthorizationToken = "Bearer " + token.access_token,
        });

        cortana.OnAudioAvailable += PlayAudio;
        cortana.OnError += ErrorHandler;
        cortana.Speak(CancellationToken.None).Wait();