【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音
  4AFg6g0R0HXv 2023年11月19日 28 0


【简介】

文本转语音可使用语音合成标记语言 (SSML) 将输入文本转换为类似人类的合成语音,本篇文档主要介绍了如何通过JavaScript 的语音SDK实现文本转换为语音的实践操作


【操作步骤】

一.配置语音 SDK集成环境

  1. 通过如下命令下载安装语音服务中的JavaScript SDK包
yarn add microsoft-cognitiveservices-speech-sdk

【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_语音合成


2.同时创建一个“yuyin“的目录以及一个”index.js"的文件(主要用于编写speech-sdk代码)

【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_microsoft_02


【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_Azure_03

二.配置调试语音服务SDK

1.将以下代码复制到我们创建的index.js当中:

(function() {

    "use strict";

    var sdk = require("microsoft-cognitiveservices-speech-sdk");
    var readline = require("readline");

    var key = "YourSubscriptionKey";
    var region = "YourServiceRegion";
    var audioFile = "YourAudioFile.wav";

    const speechConfig = sdk.SpeechConfig.fromSubscription(key, region);
    const audioConfig = sdk.AudioConfig.fromAudioFileOutput(audioFile);

    // The language of the voice that speaks.
    speechConfig.speechSynthesisVoiceName = "en-US-JennyNeural"; 

    // Create the speech synthesizer.
    var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);

    var rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
    });

    rl.question("Enter some text that you want to speak >\n> ", function (text) {
      rl.close();
      // Start the synthesizer and wait for a result.
      synthesizer.speakTextAsync(text,
          function (result) {
        if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
          console.log("synthesis finished.");
        } else {
          console.error("Speech synthesis canceled, " + result.errorDetails +
              "\nDid you set the speech resource key and region values?");
        }
        synthesizer.close();
        synthesizer = null;
      },
          function (err) {
        console.trace("err - " + err);
        synthesizer.close();
        synthesizer = null;
      });
      console.log("Now synthesizing to: " + audioFile);
    });
}());

2.并在index.js 中,将 YourSubscriptionKey 替换为语音资源密钥,将 YourServiceRegion 替换为语音资源区域。

(function() {

    "use strict";

    var sdk = require("microsoft-cognitiveservices-speech-sdk");
    var readline = require("readline");

    var key = "e68024063a4f74be08902ac71329082e6"; // 语音资源密钥
    var region = "EastUS";                        // 语音资源地域
    var audioFile = "YourAudioFile.wav";          // 语音资源文件

    const speechConfig = sdk.SpeechConfig.fromSubscription(key, region);
    const audioConfig = sdk.AudioConfig.fromAudioFileOutput(audioFile);

    // The language of the voice that speaks.
    speechConfig.speechSynthesisVoiceName = "en-US-JennyNeural";

    // Create the speech synthesizer.
    var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);

    var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.question("Enter some text that you want to speak >\n> ", function (text) {
        rl.close();
        // Start the synthesizer and wait for a result.
        synthesizer.speakTextAsync(text,
            function (result) {
                if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
                    console.log("synthesis finished.");
                } else {
                    console.error("Speech synthesis canceled, " + result.errorDetails +
                        "\nDid you set the speech resource key and region values?");
                }
                synthesizer.close();
                synthesizer = null;
            },
            function (err) {
                console.trace("err - " + err);
                synthesizer.close();
                synthesizer = null;
            });
        console.log("Now synthesizing to: " + audioFile);
    });
}());

替换完毕以后如图所示

【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_Azure_04

三.使用Node将语音合成到文件

1.在Vscode终端中,执行node程序,开始将语音合成到文件:

node.exe SpeechSynthesis.js

执行完毕后,如下图所示:

【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_javascript_05


2.我们按照如下步骤打开我们语音合成的文件

【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_javascript_06


3.如下图所示,我们就可以直接打开听一下我们语音合成的文件,至此,我们就成功使用语音合成的SDK成功合成了一个语音文件了

【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音_语音合成_07


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
  f18CFixvrKz8   2024年05月20日   90   0   0 JavaScript
  fxrR9b8fJ5Wh   2024年05月17日   52   0   0 JavaScript
  2xk0JyO908yA   2024年04月28日   40   0   0 JavaScript
4AFg6g0R0HXv