index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const worker = {}
  2. if (!worker.current) {
  3. // Create the worker if it does not yet exist.
  4. worker.current = new Worker(new URL('./worker.js', import.meta.url), {
  5. type: 'module'
  6. });
  7. }
  8. window.doSpeech = false;
  9. const onMessageReceived = (e) => {
  10. switch (e.data.status) {
  11. case 'error':
  12. window.onSpeechResponse(null);
  13. window.doSpeech = false;
  14. break;
  15. case 'complete':
  16. const blobUrl = URL.createObjectURL(e.data.output);
  17. window.onSpeechResponse(blobUrl);
  18. window.doSpeech = false;
  19. break;
  20. }
  21. };
  22. worker.current.addEventListener('message', onMessageReceived);
  23. import { DEFAULT_SPEAKER, SPEAKERS } from './constants';
  24. const handleGenerateSpeech = (text, speaker_id=DEFAULT_SPEAKER) => {
  25. window.doSpeech = true;
  26. worker.current.postMessage({
  27. text,
  28. speaker_id: speaker_id,
  29. });
  30. };
  31. window.SPEAKERS = SPEAKERS;
  32. window.handleGenerateSpeech = handleGenerateSpeech;
  33. window.onSpeechResponse = (url) => console.log(url);