From 90715e702bbebcf2c3cfd39628c931bbadda28b0 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 9 Apr 2024 19:19:33 +0200 Subject: Add project files --- projects/text_to_speech/index.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 projects/text_to_speech/index.js (limited to 'projects/text_to_speech/index.js') diff --git a/projects/text_to_speech/index.js b/projects/text_to_speech/index.js new file mode 100644 index 00000000..63059a8d --- /dev/null +++ b/projects/text_to_speech/index.js @@ -0,0 +1,38 @@ +const worker = {} +if (!worker.current) { + // Create the worker if it does not yet exist. + worker.current = new Worker(new URL('./worker.js', import.meta.url), { + type: 'module' + }); +} + +window.doSpeech = false; + +const onMessageReceived = (e) => { + switch (e.data.status) { + case 'error': + window.onSpeechResponse(null); + window.doSpeech = false; + break; + case 'complete': + const blobUrl = URL.createObjectURL(e.data.output); + window.onSpeechResponse(blobUrl); + window.doSpeech = false; + break; + } +}; +worker.current.addEventListener('message', onMessageReceived); + +import { DEFAULT_SPEAKER, SPEAKERS } from './constants'; + +const handleGenerateSpeech = (text, speaker_id=DEFAULT_SPEAKER) => { + window.doSpeech = true; + worker.current.postMessage({ + text, + speaker_id: speaker_id, + }); +}; + +window.SPEAKERS = SPEAKERS; +window.handleGenerateSpeech = handleGenerateSpeech; +window.onSpeechResponse = (url) => console.log(url); \ No newline at end of file -- cgit v1.2.3