Skip to content

Commit

Permalink
added feature for keeping imports in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
k9p5 committed Sep 9, 2024
1 parent 57264de commit bdf7f36
Show file tree
Hide file tree
Showing 17 changed files with 5,160 additions and 541 deletions.
18 changes: 18 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": false
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"jsxQuoteStyle": "double",
"trailingCommas": "all",
"semicolons": "always",
"lineWidth": 100
}
}
}
4 changes: 2 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ document.querySelector('#app')!.innerHTML = `
<button id="btn" type="button">Predict</button>
`

const worker = new Worker();

document.getElementById('btn')?.addEventListener('click', async () => {
const worker = new Worker();

worker.postMessage({
type: 'init',
Expand All @@ -23,6 +24,5 @@ document.getElementById('btn')?.addEventListener('click', async () => {
const audio = new Audio();
audio.src = URL.createObjectURL(event.data.audio);
audio.play();
worker.terminate();
});
});
2 changes: 2 additions & 0 deletions example/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import * as tts from '../src/index';
async function main(event: MessageEvent<tts.InferenceConfg & { type: 'init' }>) {
if (event.data?.type != 'init') return;

const start = performance.now();
const blob = await tts.predict({
text: event.data.text,
voiceId: event.data.voiceId,
});
console.log('Time taken:', performance.now() - start + ' ms');

self.postMessage({ type: 'result', audio: blob })
}
Expand Down
169 changes: 167 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@diffusionstudio/vits-web",
"private": false,
"version": "1.0.2",
"version": "1.0.3",
"description": "Web api for using VITS based models in the browser!",
"type": "module",
"files": [
Expand All @@ -22,12 +22,12 @@
},
"repository": {
"type": "git",
"url": "git@github.com:diffusion-studio/vits-web.git"
"url": "git@github.com:diffusionstudio/vits-web.git"
},
"bugs": {
"url": "https://github.com/diffusion-studio/vits-web/issues"
"url": "https://github.com/diffusionstudio/vits-web/issues"
},
"homepage": "https://github.com/diffusion-studio/vits-web#readme",
"homepage": "https://huggingface.co/spaces/diffusionstudio/vits-web",
"license": "MIT",
"keywords": [
"vits",
Expand All @@ -45,9 +45,11 @@
"dev": "vite",
"build": "rm -r -f ./dist && tsc && vite build",
"preview": "vite preview",
"format": "npx @biomejs/biome format --write ./src",
"test": "npx playwright test --project=chromium"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
"@playwright/test": "^1.35.1",
"typescript": "^5.2.2",
"vite": "^5.3.1",
Expand Down
53 changes: 25 additions & 28 deletions src/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@
* Convert a Float32Array (PCM) to an ArrayBuffer (WAV)
*/
export function pcm2wav(buffer: Float32Array, numChannels: number, sampleRate: number) {
const bufferLength = buffer.length;
const headerLength = 44;
const view = new DataView(new ArrayBuffer(bufferLength * numChannels * 2 + headerLength));
const bufferLength = buffer.length;
const headerLength = 44;
const view = new DataView(new ArrayBuffer(bufferLength * numChannels * 2 + headerLength));

view.setUint32(0, 0x46464952, true); // "RIFF"
view.setUint32(4, view.buffer.byteLength - 8, true); // RIFF size
view.setUint32(8, 0x45564157, true); // "WAVE"
view.setUint32(0, 0x46464952, true); // "RIFF"
view.setUint32(4, view.buffer.byteLength - 8, true); // RIFF size
view.setUint32(8, 0x45564157, true); // "WAVE"

view.setUint32(12, 0x20746d66, true); // Subchunk1ID ("fmt ")
view.setUint32(16, 0x10, true); // Subchunk1Size
view.setUint16(20, 0x0001, true); // AudioFormat
view.setUint16(22, numChannels, true); // NumChannels
view.setUint32(24, sampleRate, true); // SampleRate
view.setUint32(28, numChannels * 2 * sampleRate, true); // ByteRate
view.setUint16(32, numChannels * 2, true); // BlockAlign
view.setUint16(34, 16, true); // BitsPerSample
view.setUint32(12, 0x20746d66, true); // Subchunk1ID ("fmt ")
view.setUint32(16, 0x10, true); // Subchunk1Size
view.setUint16(20, 0x0001, true); // AudioFormat
view.setUint16(22, numChannels, true); // NumChannels
view.setUint32(24, sampleRate, true); // SampleRate
view.setUint32(28, numChannels * 2 * sampleRate, true); // ByteRate
view.setUint16(32, numChannels * 2, true); // BlockAlign
view.setUint16(34, 16, true); // BitsPerSample

view.setUint32(36, 0x61746164, true); // Subchunk2ID ("data")
view.setUint32(40, 2 * bufferLength, true); // Subchunk2Size
view.setUint32(36, 0x61746164, true); // Subchunk2ID ("data")
view.setUint32(40, 2 * bufferLength, true); // Subchunk2Size

let p = headerLength;
for (let i = 0; i < bufferLength; i++) {
const v = buffer[i];
if (v >= 1)
view.setInt16(p, 0x7fff, true);
else if (v <= -1)
view.setInt16(p, -0x8000, true);
else
view.setInt16(p, (v * 0x8000) | 0, true);
p += 2;
}
return view.buffer;
let p = headerLength;
for (let i = 0; i < bufferLength; i++) {
const v = buffer[i];
if (v >= 1) view.setInt16(p, 0x7fff, true);
else if (v <= -1) view.setInt16(p, -0x8000, true);
else view.setInt16(p, (v * 0x8000) | 0, true);
p += 2;
}
return view.buffer;
}
Loading

0 comments on commit bdf7f36

Please sign in to comment.