async function talaSpeechSetup(locale, voice, region) { return await talaSpeechSetupInternal(locale, voice, region); } async function talaSpeechSetupInternal(locale, voice, region) { try { const urlLocale = locale ? locale : "en_US"; const offerId = '844d0955-d52c-44d8-bdd4-7d26599b258f'; // Hard coded Sanity Offer ID provided by Jose in DevOps Issue #764 var endpoint = `https://production-handler.function.cloud.pratb.art/interact/${offerId}?non_neural=1&locale=${urlLocale}&ndg=unknown&style=a Teacher`; await window.TalaSpeech.send({ type: "SETUP", value: { deviceID: "talaspeech-devcube", endpoint: endpoint /** required */, azureRegion: region, azureCredentials: region == "northeurope" ? "https://tala.pratb.art/gettoken.php" : "https://tala.pratb.art/gettoken-swedencentral.php" /** required, */, locale: locale ? locale.replaceAll("_", "-") : "en-US" /** default */, asrDefaultCompleteTimeout: 0 /** default */, asrDefaultNoInputTimeout: 5000 /** default */, ttsDefaultVoice: voice, ttsLexicon: locale == "sv_SE" ? "https://cvoiceprodneu.blob.core.windows.net/acc-public-files/33867cc7d3ce43b9a040dc16212a025e/9f6c85e1-4fe9-4268-b337-24015d30b46c.xml" : undefined, azureLanguageCredentials: undefined /** default, not supported by TDM*/, speechRecognitionEndpointId: undefined /** default */, }, }); } catch (error) { return "ERROR talaSpeechSetup"; } } async function talaSpeechPrepare() { try { await window.TalaSpeech.send({ type: "PREPARE" }); } catch (error) { return "ERROR talaSpeechPrepare"; } } async function talaSpeechSpeak(utterance, voice) { try { await window.TalaSpeech.getSnapshot().context.spstRef.send({ type: "SPEAK", value: { utterance: utterance, voice: voice } }); } catch (error) { return "ERROR talaSpeechStart"; } } async function talaSpeechSpeakIncludingSetupAndPrepare(locale, utterance, voice, region) { await talaSpeechSetupInternal(locale, voice, region); if (window.TalaSpeechUIState !== 'ready') { await until((_) => window.TalaSpeechUIState === 'before-prepare'); await talaSpeechPrepare(); } await until((_) => window.TalaSpeechUIState === 'ready'); await talaSpeechSpeak(utterance, voice); // TODO: Replace this 3000ms setTimeout with a proper await for the SPEAK command await new Promise(resolve => setTimeout(resolve, 3000)); return 'DONE'; } const until = async (fn, time = 1000, wait = 10000) => { const startTime = new Date().getTime() for (;;) { try { if (await fn()) { return true } } catch (e) { throw e } if (new Date().getTime() - startTime > wait) { throw new Error('Max wait reached') } else { await new Promise((resolve) => setTimeout(resolve, time)) } } } function reloadWindow() { window.location.reload(); } function consoleLog(msg) { window.console.log(msg); } function historyBack() { window.history.back(); }