Looking for some JavaScript assistance with changing language and voice settings in my code. I have tried searching online but haven't found a solution that fits my skill level.
If anyone could provide help with modifying the code, it would be greatly appreciated. Thank you!
<!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);
// Looking for guidance on how to modify the code to change language and voice:
// speechConfig = SpeechSDK.SpeechConfig.setSpeechSynthesisLanguage(voiceLanguage);
// speechConfig = SpeechSDK.SpeechConfig.setSpeechSynthesisVoiceName(voiceName);
synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig);
// Expecting output 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>