javascript Azure文本到语音:如何更改输出语言和声音?

wfypjpf4  于 2023-04-04  发布在  Java
关注(0)|答案(2)|浏览(169)

我需要以下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>
yuvru6vn

yuvru6vn1#

要进行快速测试,您可以在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>

你可以从第130行找到这个页面上的所有声音名称。这不是你的错,似乎没有官方的js示例:(

b1payxdu

b1payxdu2#

将给定的文本保存为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";

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

相关问题