This commit is contained in:
Zeeshaun
2026-04-22 19:46:53 +00:00
parent 197f224630
commit 75ad412ed8
2 changed files with 125 additions and 27 deletions
@@ -214,12 +214,20 @@
return; return;
} }
await JSRuntime.InvokeVoidAsync( try
"synthesizeSpeech", {
spitConfiguration.SpeechApiKey, await JSRuntime.InvokeVoidAsync(
spitConfiguration.Region, "synthesizeSpeech",
spitConfiguration.SpeechEndpoint, spitConfiguration.SpeechApiKey,
_textToSpeech.Ssml); spitConfiguration.Region,
spitConfiguration.SpeechEndpoint,
_textToSpeech.Ssml);
}
catch (JSException ex)
{
Log.Error(ex, "Play sound failed.");
toastService.ShowError($"Unable to play sound. {ex.Message}");
}
} }
public void OpenDirectory(string fileDestination) public void OpenDirectory(string fileDestination)
@@ -1,26 +1,116 @@
function synthesizeSpeech(subscriptionKey, region, endpoint, ssml) { function synthesizeSpeech(subscriptionKey, region, endpoint, ssml) {
var SpeechSDK; return new Promise(function (resolve, reject) {
if (!!window.SpeechSDK) { const SpeechSDK = window.SpeechSDK;
SpeechSDK = window.SpeechSDK;
if (!SpeechSDK) {
reject(new Error("Speech SDK is not loaded."));
return;
}
let speechConfig;
try {
speechConfig = createSpeechConfig(SpeechSDK, subscriptionKey, region, endpoint);
} catch (error) {
reject(error);
return;
}
const audioConfig = SpeechSDK.AudioConfig.fromDefaultSpeakerOutput();
let synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakSsmlAsync(
ssml,
function (result) {
try {
if (result.reason !== SpeechSDK.ResultReason.SynthesizingAudioCompleted) {
reject(new Error(buildSynthesisErrorMessage(SpeechSDK, result, region, endpoint)));
return;
}
resolve();
} finally {
synthesizer.close();
synthesizer = undefined;
}
},
function (error) {
try {
reject(new Error(normalizeSpeechError(error, region, endpoint)));
} finally {
synthesizer.close();
synthesizer = undefined;
}
});
});
}
function createSpeechConfig(SpeechSDK, subscriptionKey, region, endpoint) {
const trimmedRegion = typeof region === "string" ? region.trim() : "";
const trimmedEndpoint = typeof endpoint === "string" ? endpoint.trim() : "";
if (trimmedRegion) {
return SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey, trimmedRegion);
} }
const speechConfig = endpoint if (trimmedEndpoint) {
? SpeechSDK.SpeechConfig.fromEndpoint(new URL(endpoint), subscriptionKey) return SpeechSDK.SpeechConfig.fromEndpoint(new URL(trimmedEndpoint), subscriptionKey);
: SpeechSDK.SpeechConfig.fromSubscription(subscriptionKey, region); }
const audioConfig = SpeechSDK.AudioConfig.fromDefaultSpeakerOutput();
let synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakSsmlAsync( throw new Error("Speech configuration is missing both region and endpoint.");
ssml, }
function (result) {
window.console.log(result); function buildSynthesisErrorMessage(SpeechSDK, result, region, endpoint) {
synthesizer.close(); const cancellation = SpeechSDK.CancellationDetails.fromResult(result);
synthesizer = undefined; const parts = [
}, "Speech synthesis failed.",
function (err) { "Reason: " + result.reason
window.console.log(err); ];
synthesizer.close(); if (cancellation) {
synthesizer = undefined; if (cancellation.reason !== undefined) {
}); parts.push("CancellationReason: " + cancellation.reason);
}
if (cancellation.errorCode !== undefined) {
parts.push("CancellationErrorCode: " + cancellation.errorCode);
}
if (cancellation.errorDetails) {
parts.push("ErrorDetails: " + cancellation.errorDetails);
}
}
appendConnectionDetails(parts, region, endpoint);
return parts.join(" ");
}
function normalizeSpeechError(error, region, endpoint) {
const parts = ["Speech synthesis connection failed."];
if (error instanceof Error && error.message) {
parts.push(error.message);
} else if (typeof error === "string" && error.trim()) {
parts.push(error.trim());
} else if (error && error.toString) {
parts.push(error.toString());
} else {
parts.push("No error details were provided.");
}
appendConnectionDetails(parts, region, endpoint);
return parts.join(" ");
}
function appendConnectionDetails(parts, region, endpoint) {
const trimmedRegion = typeof region === "string" ? region.trim() : "";
const trimmedEndpoint = typeof endpoint === "string" ? endpoint.trim() : "";
if (trimmedRegion) {
parts.push("Region: " + trimmedRegion);
}
if (trimmedEndpoint) {
parts.push("Endpoint: " + trimmedEndpoint);
}
} }