Azure 文本转语音:如何更改输出的语言和语音?

问题描述 投票:0回答:2

我需要以下JavaScript的帮助,希望有人能帮助我。文本以英语语音读出。

如何在以下工作代码中更改语言和语音?我在网上进行了大量搜索,但由于我的 java 技能很差,找不到合适的解决方案。

所以,不幸的是我的编程技能不够好,所以我需要一些具体代码行的帮助。谢谢。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Microsoft Cognitive Services Speech SDK JavaScript Quickstart</title>
  <meta charset="utf-8" />
</head>
<body>
  
  <button id="startSpeakTextAsyncButton">speak</button>
  
  <!-- Speech SDK reference sdk. -->
  <script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>

  <!--   Speech SDK Authorization token  -->
  <script>
  var authorizationEndpoint = "token.php";

  function RequestAuthorizationToken() {
    if (authorizationEndpoint) {
      var a = new XMLHttpRequest();
      a.open("GET", authorizationEndpoint);
      a.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      a.send("");
      a.onload = function() {
        var token = JSON.parse(atob(this.responseText.split(".")[1]));
        serviceRegion.value = token.region;
        authorizationToken = this.responseText;
        subscriptionKey.disabled = true;
      }
    }
  }
  </script>

  <!-- Speech SDK USAGE -->
  <script>
    var startSpeakTextAsyncButton;
    var serviceRegion = "westeurope"; 
    // for testing:
    // var voiceName = "HeddaRUS";
    // var voiceLanguage ="de-DE";

    var subscriptionKey;
    var authorizationToken;
    var SpeechSDK;
    var synthesizer;    

    document.addEventListener("DOMContentLoaded", function () {
      startSpeakTextAsyncButton = document.getElementById("startSpeakTextAsyncButton");
      subscriptionKey = document.getElementById("subscriptionKey");
      
        startSpeakTextAsyncButton.addEventListener("click", function () {
        startSpeakTextAsyncButton.disabled = true;
        
        speechConfig = SpeechSDK.SpeechConfig.fromAuthorizationToken(authorizationToken, serviceRegion);  
            
        // I don't know how the code should looke like:
        // speechConfig = SpeechSDK.SpeechConfig.setSpeechSynthesisLanguage(voiceLanguage);
        // speechConfig = SpeechSDK.SpeechConfig.setSpeechSynthesisVoiceName(voiceName);
            
        synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig);

        // output should be in German language:    
        let inputText = "Ich möchte es auf deutsche Sprache setzen, weiß aber nicht wie!";
                
        synthesizer.speakTextAsync(
          inputText,
          function (result) {
            startSpeakTextAsyncButton.disabled = false;
            window.console.log(result);
            synthesizer.close();
            synthesizer = undefined;
          });
      });

      if (!!window.SpeechSDK) {
        SpeechSDK = window.SpeechSDK;
        startSpeakTextAsyncButton.disabled = false;
        if (typeof RequestAuthorizationToken === "function") {RequestAuthorizationToken();}
      }
    });
  
  </script>
</body>
</html>
javascript azure text-to-speech voice
2个回答
5
投票

为了快速测试,您可以在

speechConfig
中指定您的语言和语音,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Microsoft Cognitive Services Speech SDK JavaScript Quickstart</title>
  <meta charset="utf-8" />
</head>
<body>
  
  <button id="startSpeakTextAsyncButton" onclick="synthesizeSpeech()">speak</button>
  
  <!-- Speech SDK reference sdk. -->
  <script src="microsoft.cognitiveservices.speech.sdk.bundle.js"></script>
 
  <script>
    function synthesizeSpeech() {
        
        var speechConfig = SpeechSDK.SpeechConfig.fromSubscription("<your subscription key,you can find it on azure portal=> Kesy and Endpoints blade>", "<your region>");
        speechConfig.speechSynthesisVoiceName = "Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)";
        speechConfig.speechSynthesisLanguage = "de-DE";
        
        
        var synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig);
        let inputText = "Ich möchte es auf deutsche Sprache setzen, weiß aber nicht wie!";
                
        synthesizer.speakTextAsync(
          inputText,
          function (result) {
            startSpeakTextAsyncButton.disabled = false;
            window.console.log(result);
            synthesizer.close();
            synthesizer = undefined;
        });
    }
  
  </script>
</body>
</html>

你可以在 this page 从第 130 行找到所有语音名称。这不是你的错,似乎没有官方的 js 示例:(


0
投票

将给定文本保存为

mp3
文件的功能,欢迎使用!

const textToSpeech = async (key, region, text, filename)=> {
    // convert callback function to promise
    return new Promise((resolve, reject) => {

        const speechConfig = sdk.SpeechConfig.fromSubscription(key, region);
        speechConfig.speechSynthesisOutputFormat = 5; // mp3
   
        speechConfig.speechSynthesisVoiceName = "es-MX-LibertoNeural";
        speechConfig.speechSynthesisLanguage = "es-ES";

        let audioConfig = sdk.AudioConfig.fromAudioFileOutput(filename);

        const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);

        synthesizer.speakTextAsync(
            text,
            result => {
                const { audioData } = result;

                synthesizer.close();

                // return stream from file
                const audioFile = fs.createReadStream(filename);
                resolve(audioFile);                
            },
            error => {
                console.log(error);
                synthesizer.close();
                reject(error);
            });
    });
};

module.exports = {
    textToSpeech
};

// example of use - saves the generated audio in the oof3.mp3 file
(async () => {
    await textToSpeech('yourkeygoeshere', 'youregiongoeshere', 'El gato y el perro', 'oof3.mp3');
})();

附言

speechConfig.speechSynthesisVoiceName = "es-MX-LibertoNeural";

足以使输出成为西班牙语。添加

speechConfig.speechSynthesisLanguage = "es-ES";

不改变输出。

当你只设置

speechConfig.speechSynthesisLanguage = "es-ES";

输出是西班牙语,但语音不同(我假设是默认语音)

© www.soinside.com 2019 - 2024. All rights reserved.