summaryrefslogtreecommitdiffstats
path: root/projects/text_to_speech/index.js
blob: 63059a8d94dc525c2fb872378bfa6de01ee6c15e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);