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

标签 c# azure microsoft-speech-api

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

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

此代码依赖于 System.Speech,我已在 VS 2015 项目中添加了引用。 工作正常,但从我读过的内容和尝试来看,我知道当代码托管在 Azure 上时,这将不起作用。 我读过几篇关于SO查询是否真的可以在azure上进行TTS的文章。当然,两年前这似乎是不可能的。 How to get System.Speech on windows azure websites?

条条大路似乎都通 Microsoft Speech API https://azure.microsoft.com/en-gb/marketplace/partners/speechapis/speechapis/ 我已经注册并获得了用于调用此 API 的私钥和安全 key 。 然而我的问题是这样的。我如何实际调用 SpeechAPI?我必须在上面的简单代码示例中更改哪些内容才能在 Azure 上运行时正常工作?

最佳答案

您在 Azure 市场中提到的语音 API 是名为 ProjectOxford 的 AI Microsoft 项目的一部分。它提供了一系列用于计算机视觉、语音和语言的 API。

这些都是 RESTful API,这意味着您将构建 HTTP 请求以发送到云中托管的在线服务。 语音转文本文档可用 here您可以在 github 上找到各种客户端的示例代码。特别是对于 C#,您可以在 this sample project 中看到一些代码。 .

请注意,ProjectOxford 仍处于预览阶段(测试版)。使用这些 API 的其他支持可以在 ProjectOxford MSDN forum 上找到。 .

但只是为了让您了解您的程序的外观(取自 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();

关于c# - 在 Azure 上调用 SpeechAPI 进行文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965060/

相关文章:

c# - 如何取二维数组的一行?

c# - 你能用命名空间声明和成员解释这个涉及 C# 'using' 关键字的边缘案例吗?

javascript - ASP.NET - jQuery (document).ready() 函数未被触发用于用户控制

sql-server - Azure SQL : Will Applying Index Advisor Changes Impact EF Migrations?

reactjs - 我如何在 azure 静态 Web 应用程序的生产中访问我的 React VITE ENV 变量?

c# - Azure 上的 HttpRuntime.AppDomainAppPath

azure - Azure 的语音转文本服务是否接受 Webm 音频并提供带时间戳的输出?

javascript - 将语音转文本模块添加到 C# 机器人

c# - C# 获取用户计算机的IP地址

azure - 将 Azure Bot 与 Azure 语音服务集成