summaryrefslogtreecommitdiffstats
path: root/projects/text_to_speech/index.js
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-09 19:40:42 +0200
committerGitHub <noreply@github.com>2024-04-09 19:40:42 +0200
commit4c23b4cad4744e20da7ccffb303503ea627df7c2 (patch)
treea032e6ebd494136ba35e049b06aa4af45558b540 /projects/text_to_speech/index.js
parentMerge pull request #1809 from ochen1/patch-1 (diff)
parentAdd project files (diff)
downloadgpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.gz
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.bz2
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.lz
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.xz
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.zst
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.zip
Diffstat (limited to 'projects/text_to_speech/index.js')
-rw-r--r--projects/text_to_speech/index.js38
1 files changed, 38 insertions, 0 deletions
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