Skip to content

Commit cb27a68

Browse files
committed
added web worker example
1 parent 5da629f commit cb27a68

File tree

7 files changed

+49
-26
lines changed

7 files changed

+49
-26
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ const wav = await tts.predict({
2626
voiceId: 'en_US-hfc_female-medium',
2727
});
2828

29-
// available in Web Worker
30-
3129
const audio = new Audio();
3230
audio.src = URL.createObjectURL(wav);
3331
audio.play();
32+
33+
// as seen in /example with Web Worker
3434
```
3535

3636
With the initial run of the predict function you will download the model which will then be stored in your [Origin private file system](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system). You can also do this manually in advance *(recommended)*, as follows:

example/index.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as tts from '../src';
2+
import Worker from './worker.ts?worker';
3+
4+
// required for e2e
5+
Object.assign(window, { tts });
6+
7+
document.querySelector('#app')!.innerHTML = `
8+
<button id="btn" type="button">Predict</button>
9+
`
10+
11+
document.getElementById('btn')?.addEventListener('click', async () => {
12+
const worker = new Worker();
13+
14+
worker.postMessage({
15+
type: 'init',
16+
text: "As the waves crashed against the shore, they carried tales of distant lands and adventures untold.",
17+
voiceId: 'en_US-hfc_female-medium',
18+
});
19+
20+
worker.addEventListener('message', (event: MessageEvent<{ type: 'result', audio: Blob }>) => {
21+
if (event.data.type != 'result') return;
22+
23+
const audio = new Audio();
24+
audio.src = URL.createObjectURL(event.data.audio);
25+
audio.play();
26+
worker.terminate();
27+
});
28+
});

example/vite-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

example/worker.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as tts from '../src/index';
2+
3+
async function main(event: MessageEvent<tts.InferenceConfg & { type: 'init' }>) {
4+
if (event.data?.type != 'init') return;
5+
6+
const blob = await tts.predict({
7+
text: event.data.text,
8+
voiceId: event.data.voiceId,
9+
});
10+
11+
self.postMessage({ type: 'result', audio: blob })
12+
}
13+
14+
self.addEventListener('message', main);

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
</head>
99
<body>
1010
<div id="app"></div>
11-
<script type="module" src="/src/main.ts"></script>
11+
<script type="module" src="/example/index.ts"></script>
1212
</body>
1313
</html>

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@
4848
"test": "npx playwright test --project=chromium"
4949
},
5050
"devDependencies": {
51+
"@playwright/test": "^1.35.1",
5152
"typescript": "^5.2.2",
5253
"vite": "^5.3.1",
53-
"vite-plugin-dts": "^3.9.1",
54-
"@playwright/test": "^1.35.1"
54+
"vite-plugin-dts": "^3.9.1"
5555
},
5656
"peerDependencies": {
5757
"onnxruntime-web": "^1.18.0"
5858
}
59-
}
59+
}

src/main.ts

-20
This file was deleted.

0 commit comments

Comments
 (0)