gpt4 book ai didi

javascript - Azure 文本转语音 : how can I change language and voice for output?

转载 作者:行者123 更新时间:2023-12-02 23:13:44 24 4
gpt4 key购买 nike

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

最佳答案

为了进行快速测试,您可以在 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 示例:(

关于javascript - Azure 文本转语音 : how can I change language and voice for output?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64836184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com