@micdrop/web 1.1.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -1
- package/assets/silero_vad_v5.onnx +0 -0
- package/dist/silero.d.mts +8 -3
- package/dist/silero.d.ts +8 -3
- package/dist/silero.js +5 -2
- package/dist/silero.js.map +1 -1
- package/dist/silero.mjs +5 -2
- package/dist/silero.mjs.map +1 -1
- package/dist/vite.d.mts +35 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +120 -0
- package/dist/vite.js.map +1 -0
- package/dist/vite.mjs +79 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +20 -4
package/README.md
CHANGED
|
@@ -28,6 +28,24 @@ npm install @micdrop/web
|
|
|
28
28
|
|
|
29
29
|
That's it 🎉 No configuration, no asset to copy.
|
|
30
30
|
|
|
31
|
+
### Vite plugin (strongly recommended)
|
|
32
|
+
|
|
33
|
+
If your app is built with Vite and uses Silero VAD or turn detection, add the plugin:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
// vite.config.ts
|
|
37
|
+
import { micdrop } from '@micdrop/web/vite'
|
|
38
|
+
import { defineConfig } from 'vite'
|
|
39
|
+
|
|
40
|
+
export default defineConfig({
|
|
41
|
+
plugins: [micdrop()],
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
It ships the Silero model with your app and lets Vite emit the ONNX runtime WebAssembly from the version you installed. Without it, both are fetched from jsDelivr and unpkg when a call starts, and a CDN outage, a firewall or an ad blocker keeps voice detection from starting.
|
|
46
|
+
|
|
47
|
+
The runtime is the WebAssembly build, half the size of the full one, unless `@micdrop/smart-turn` is installed, which runs on the graphics card. `micdrop({ webgpu: true })` keeps the full build in any case.
|
|
48
|
+
|
|
31
49
|
## Usage
|
|
32
50
|
|
|
33
51
|
```ts
|
|
@@ -43,7 +61,8 @@ Micdrop.on('StateChange', (state) => {
|
|
|
43
61
|
## Browser specifics
|
|
44
62
|
|
|
45
63
|
- `import '@micdrop/web/silero'` adds the Silero voice detection, and only then does the ONNX runtime enter your bundle
|
|
46
|
-
- `
|
|
64
|
+
- `@micdrop/web/vite` bundles the Silero model and the ONNX runtime files with your app, see above
|
|
65
|
+
- `setSileroOptions({ model, wasmPath })` serves the Silero model and the ONNX runtime from your own domain with another bundler
|
|
47
66
|
- `getSpeakerOutput()` gives the node the assistant voice comes out of, to analyse or process it
|
|
48
67
|
- `audioContext` is the shared `AudioContext`
|
|
49
68
|
|
|
Binary file
|
package/dist/silero.d.mts
CHANGED
|
@@ -5,8 +5,12 @@ declare const DEFAULT_SILERO_MODEL_URL = "https://cdn.jsdelivr.net/npm/@ricky012
|
|
|
5
5
|
interface OnnxSileroOptions {
|
|
6
6
|
/** Address of the Silero v5 model, or the bytes of it */
|
|
7
7
|
model?: string | ArrayBuffer;
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Where the ONNX runtime loads its WebAssembly files from. `false` leaves
|
|
10
|
+
* them to the runtime, which finds them next to itself once a bundler has
|
|
11
|
+
* emitted them, as the `@micdrop/web/vite` plugin does.
|
|
12
|
+
*/
|
|
13
|
+
wasmPath?: string | false;
|
|
10
14
|
}
|
|
11
15
|
/**
|
|
12
16
|
* Runs the Silero model in the browser, on the ONNX WebAssembly runtime.
|
|
@@ -34,7 +38,8 @@ declare function queueOnnxRun<T>(run: () => Promise<T>): Promise<T>;
|
|
|
34
38
|
|
|
35
39
|
/**
|
|
36
40
|
* Says where the Silero model comes from, to serve it from your own domain
|
|
37
|
-
* rather than from a CDN, or to hand over its bytes directly.
|
|
41
|
+
* rather than from a CDN, or to hand over its bytes directly. Merged into the
|
|
42
|
+
* options already set, so it keeps what the `@micdrop/web/vite` plugin chose.
|
|
38
43
|
* @param next - Address or bytes of the model, and of the ONNX runtime
|
|
39
44
|
*/
|
|
40
45
|
declare function setSileroOptions(next: OnnxSileroOptions): void;
|
package/dist/silero.d.ts
CHANGED
|
@@ -5,8 +5,12 @@ declare const DEFAULT_SILERO_MODEL_URL = "https://cdn.jsdelivr.net/npm/@ricky012
|
|
|
5
5
|
interface OnnxSileroOptions {
|
|
6
6
|
/** Address of the Silero v5 model, or the bytes of it */
|
|
7
7
|
model?: string | ArrayBuffer;
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Where the ONNX runtime loads its WebAssembly files from. `false` leaves
|
|
10
|
+
* them to the runtime, which finds them next to itself once a bundler has
|
|
11
|
+
* emitted them, as the `@micdrop/web/vite` plugin does.
|
|
12
|
+
*/
|
|
13
|
+
wasmPath?: string | false;
|
|
10
14
|
}
|
|
11
15
|
/**
|
|
12
16
|
* Runs the Silero model in the browser, on the ONNX WebAssembly runtime.
|
|
@@ -34,7 +38,8 @@ declare function queueOnnxRun<T>(run: () => Promise<T>): Promise<T>;
|
|
|
34
38
|
|
|
35
39
|
/**
|
|
36
40
|
* Says where the Silero model comes from, to serve it from your own domain
|
|
37
|
-
* rather than from a CDN, or to hand over its bytes directly.
|
|
41
|
+
* rather than from a CDN, or to hand over its bytes directly. Merged into the
|
|
42
|
+
* options already set, so it keeps what the `@micdrop/web/vite` plugin chose.
|
|
38
43
|
* @param next - Address or bytes of the model, and of the ONNX runtime
|
|
39
44
|
*/
|
|
40
45
|
declare function setSileroOptions(next: OnnxSileroOptions): void;
|
package/dist/silero.js
CHANGED
|
@@ -65,7 +65,10 @@ var OnnxSileroModel = class {
|
|
|
65
65
|
}
|
|
66
66
|
async load() {
|
|
67
67
|
if (this.session) return;
|
|
68
|
-
|
|
68
|
+
const wasmPath = this.options.wasmPath ?? DEFAULT_WASM_PATH;
|
|
69
|
+
if (wasmPath !== false) {
|
|
70
|
+
ort.env.wasm.wasmPaths = wasmPath;
|
|
71
|
+
}
|
|
69
72
|
const source = this.options.model ?? DEFAULT_SILERO_MODEL_URL;
|
|
70
73
|
let bytes;
|
|
71
74
|
if (typeof source === "string") {
|
|
@@ -103,7 +106,7 @@ function newState() {
|
|
|
103
106
|
// src/silero.ts
|
|
104
107
|
var options = {};
|
|
105
108
|
function setSileroOptions(next) {
|
|
106
|
-
options = next;
|
|
109
|
+
options = { ...options, ...next };
|
|
107
110
|
}
|
|
108
111
|
(0, import_client2.setSileroModelLoader)(async () => {
|
|
109
112
|
const model = new OnnxSileroModel(options);
|
package/dist/silero.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/silero.ts","../src/vad/OnnxSileroModel.ts","../src/vad/onnxQueue.ts"],"sourcesContent":["import { setSileroModelLoader } from '@micdrop/client'\nimport { OnnxSileroModel, OnnxSileroOptions } from './vad/OnnxSileroModel'\n\nexport * from './vad/OnnxSileroModel'\nexport * from './vad/onnxQueue'\n\nlet options: OnnxSileroOptions = {}\n\n/**\n * Says where the Silero model comes from, to serve it from your own domain\n * rather than from a CDN, or to hand over its bytes directly.\n * @param next - Address or bytes of the model, and of the ONNX runtime\n */\nexport function setSileroOptions(next: OnnxSileroOptions) {\n options = next\n}\n\n// The ONNX runtime weighs more than the rest of Micdrop put together, so it\n// only enters the bundle of an app that imports this file\nsetSileroModelLoader(async () => {\n const model = new OnnxSileroModel(options)\n await model.load()\n return model\n})\n","import { SileroModel, SILERO_SAMPLE_RATE } from '@micdrop/client'\nimport * as ort from 'onnxruntime-web'\nimport { queueOnnxRun } from './onnxQueue'\n\n/** Where the model is fetched from, when the app does not provide it */\nexport const DEFAULT_SILERO_MODEL_URL =\n 'https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx'\n\n// onnxruntime-web@1.23 is not hosted on jsdelivr (its dist exceeds the CDN size\n// limit, so every file 404s). unpkg serves it, so the wasm backend comes from\n// there instead.\nconst DEFAULT_WASM_PATH = 'https://unpkg.com/onnxruntime-web@1.23.2/dist/'\n\nexport interface OnnxSileroOptions {\n /** Address of the Silero v5 model, or the bytes of it */\n model?: string | ArrayBuffer\n
|
|
1
|
+
{"version":3,"sources":["../src/silero.ts","../src/vad/OnnxSileroModel.ts","../src/vad/onnxQueue.ts"],"sourcesContent":["import { setSileroModelLoader } from '@micdrop/client'\nimport { OnnxSileroModel, OnnxSileroOptions } from './vad/OnnxSileroModel'\n\nexport * from './vad/OnnxSileroModel'\nexport * from './vad/onnxQueue'\n\nlet options: OnnxSileroOptions = {}\n\n/**\n * Says where the Silero model comes from, to serve it from your own domain\n * rather than from a CDN, or to hand over its bytes directly. Merged into the\n * options already set, so it keeps what the `@micdrop/web/vite` plugin chose.\n * @param next - Address or bytes of the model, and of the ONNX runtime\n */\nexport function setSileroOptions(next: OnnxSileroOptions) {\n options = { ...options, ...next }\n}\n\n// The ONNX runtime weighs more than the rest of Micdrop put together, so it\n// only enters the bundle of an app that imports this file\nsetSileroModelLoader(async () => {\n const model = new OnnxSileroModel(options)\n await model.load()\n return model\n})\n","import { SileroModel, SILERO_SAMPLE_RATE } from '@micdrop/client'\nimport * as ort from 'onnxruntime-web'\nimport { queueOnnxRun } from './onnxQueue'\n\n/** Where the model is fetched from, when the app does not provide it */\nexport const DEFAULT_SILERO_MODEL_URL =\n 'https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx'\n\n// onnxruntime-web@1.23 is not hosted on jsdelivr (its dist exceeds the CDN size\n// limit, so every file 404s). unpkg serves it, so the wasm backend comes from\n// there instead.\nconst DEFAULT_WASM_PATH = 'https://unpkg.com/onnxruntime-web@1.23.2/dist/'\n\nexport interface OnnxSileroOptions {\n /** Address of the Silero v5 model, or the bytes of it */\n model?: string | ArrayBuffer\n /**\n * Where the ONNX runtime loads its WebAssembly files from. `false` leaves\n * them to the runtime, which finds them next to itself once a bundler has\n * emitted them, as the `@micdrop/web/vite` plugin does.\n */\n wasmPath?: string | false\n}\n\nlet loading: Promise<ArrayBuffer> | undefined\n\n/**\n * Runs the Silero model in the browser, on the ONNX WebAssembly runtime.\n *\n * The state machine that turns its answers into turns lives in\n * `@micdrop/client`, this only scores windows of audio.\n */\nexport class OnnxSileroModel implements SileroModel {\n private session: ort.InferenceSession | undefined\n private state: ort.Tensor\n private sampleRate: ort.Tensor\n\n constructor(private options: OnnxSileroOptions = {}) {\n this.state = newState()\n this.sampleRate = new ort.Tensor('int64', [BigInt(SILERO_SAMPLE_RATE)])\n }\n\n async load() {\n if (this.session) return\n\n const wasmPath = this.options.wasmPath ?? DEFAULT_WASM_PATH\n if (wasmPath !== false) {\n ort.env.wasm.wasmPaths = wasmPath\n }\n\n const source = this.options.model ?? DEFAULT_SILERO_MODEL_URL\n let bytes: ArrayBuffer\n if (typeof source === 'string') {\n // Fetched once per page, the model is a couple of megabytes\n loading =\n loading ?? fetch(source).then((response) => response.arrayBuffer())\n bytes = await loading\n } else {\n bytes = source\n }\n\n this.session = await ort.InferenceSession.create(bytes)\n }\n\n async process(frame: Float32Array): Promise<number> {\n if (!this.session) await this.load()\n\n const output = await queueOnnxRun(() =>\n this.session!.run({\n input: new ort.Tensor('float32', frame, [1, frame.length]),\n state: this.state,\n sr: this.sampleRate,\n })\n )\n\n this.state = output.stateN as ort.Tensor\n return (output.output.data as Float32Array)[0]\n }\n\n reset() {\n this.state = newState()\n }\n\n async release() {\n await this.session?.release()\n this.session = undefined\n }\n}\n\n/** The memory the model carries from one window to the next, emptied */\nfunction newState(): ort.Tensor {\n return new ort.Tensor('float32', new Float32Array(2 * 128), [2, 1, 128])\n}\n","/**\n * Runs one inference at a time, whichever model asks.\n *\n * The ONNX runtime keeps a single active session for the whole WebAssembly\n * module, so two models cannot be in flight together. A model running on the\n * graphics card yields while it waits for the device, and a detector scoring\n * its next window on top of it makes both fail, one with `Session already\n * started` and the other with `Session mismatch`.\n *\n * The queue lives on the global scope so every Micdrop model takes the same\n * turns, `@micdrop/smart-turn` included, which ships separately and holds the\n * same few lines.\n */\nconst globalScope = globalThis as typeof globalThis & {\n micdropOnnxQueue?: Promise<unknown>\n}\n\n/**\n * Waits for the models ahead in the queue, then runs\n * @param run - The inference to run once the runtime is free\n */\nexport function queueOnnxRun<T>(run: () => Promise<T>): Promise<T> {\n const started = (globalScope.micdropOnnxQueue ?? Promise.resolve()).then(\n run,\n run\n )\n // A failed run still lets the next one through\n globalScope.micdropOnnxQueue = started.catch(() => undefined)\n return started\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAAqC;;;ACArC,oBAAgD;AAChD,UAAqB;;;ACYrB,IAAM,cAAc;AAQb,SAAS,aAAgB,KAAmC;AACjE,QAAM,WAAW,YAAY,oBAAoB,QAAQ,QAAQ,GAAG;AAAA,IAClE;AAAA,IACA;AAAA,EACF;AAEA,cAAY,mBAAmB,QAAQ,MAAM,MAAM,MAAS;AAC5D,SAAO;AACT;;;ADxBO,IAAM,2BACX;AAKF,IAAM,oBAAoB;AAa1B,IAAI;AAQG,IAAM,kBAAN,MAA6C;AAAA,EAKlD,YAAoBC,WAA6B,CAAC,GAAG;AAAjC,mBAAAA;AAClB,SAAK,QAAQ,SAAS;AACtB,SAAK,aAAa,IAAQ,WAAO,SAAS,CAAC,OAAO,gCAAkB,CAAC,CAAC;AAAA,EACxE;AAAA,EAEA,MAAM,OAAO;AACX,QAAI,KAAK,QAAS;AAElB,UAAM,WAAW,KAAK,QAAQ,YAAY;AAC1C,QAAI,aAAa,OAAO;AACtB,MAAI,QAAI,KAAK,YAAY;AAAA,IAC3B;AAEA,UAAM,SAAS,KAAK,QAAQ,SAAS;AACrC,QAAI;AACJ,QAAI,OAAO,WAAW,UAAU;AAE9B,gBACE,WAAW,MAAM,MAAM,EAAE,KAAK,CAAC,aAAa,SAAS,YAAY,CAAC;AACpE,cAAQ,MAAM;AAAA,IAChB,OAAO;AACL,cAAQ;AAAA,IACV;AAEA,SAAK,UAAU,MAAU,qBAAiB,OAAO,KAAK;AAAA,EACxD;AAAA,EAEA,MAAM,QAAQ,OAAsC;AAClD,QAAI,CAAC,KAAK,QAAS,OAAM,KAAK,KAAK;AAEnC,UAAM,SAAS,MAAM;AAAA,MAAa,MAChC,KAAK,QAAS,IAAI;AAAA,QAChB,OAAO,IAAQ,WAAO,WAAW,OAAO,CAAC,GAAG,MAAM,MAAM,CAAC;AAAA,QACzD,OAAO,KAAK;AAAA,QACZ,IAAI,KAAK;AAAA,MACX,CAAC;AAAA,IACH;AAEA,SAAK,QAAQ,OAAO;AACpB,WAAQ,OAAO,OAAO,KAAsB,CAAC;AAAA,EAC/C;AAAA,EAEA,QAAQ;AACN,SAAK,QAAQ,SAAS;AAAA,EACxB;AAAA,EAEA,MAAM,UAAU;AACd,UAAM,KAAK,SAAS,QAAQ;AAC5B,SAAK,UAAU;AAAA,EACjB;AACF;AAGA,SAAS,WAAuB;AAC9B,SAAO,IAAQ,WAAO,WAAW,IAAI,aAAa,IAAI,GAAG,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;AACzE;;;ADtFA,IAAI,UAA6B,CAAC;AAQ3B,SAAS,iBAAiB,MAAyB;AACxD,YAAU,EAAE,GAAG,SAAS,GAAG,KAAK;AAClC;AAAA,IAIA,qCAAqB,YAAY;AAC/B,QAAM,QAAQ,IAAI,gBAAgB,OAAO;AACzC,QAAM,MAAM,KAAK;AACjB,SAAO;AACT,CAAC;","names":["import_client","options"]}
|
package/dist/silero.mjs
CHANGED
|
@@ -28,7 +28,10 @@ var OnnxSileroModel = class {
|
|
|
28
28
|
}
|
|
29
29
|
async load() {
|
|
30
30
|
if (this.session) return;
|
|
31
|
-
|
|
31
|
+
const wasmPath = this.options.wasmPath ?? DEFAULT_WASM_PATH;
|
|
32
|
+
if (wasmPath !== false) {
|
|
33
|
+
ort.env.wasm.wasmPaths = wasmPath;
|
|
34
|
+
}
|
|
32
35
|
const source = this.options.model ?? DEFAULT_SILERO_MODEL_URL;
|
|
33
36
|
let bytes;
|
|
34
37
|
if (typeof source === "string") {
|
|
@@ -66,7 +69,7 @@ function newState() {
|
|
|
66
69
|
// src/silero.ts
|
|
67
70
|
var options = {};
|
|
68
71
|
function setSileroOptions(next) {
|
|
69
|
-
options = next;
|
|
72
|
+
options = { ...options, ...next };
|
|
70
73
|
}
|
|
71
74
|
setSileroModelLoader(async () => {
|
|
72
75
|
const model = new OnnxSileroModel(options);
|
package/dist/silero.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/silero.ts","../src/vad/OnnxSileroModel.ts","../src/vad/onnxQueue.ts"],"sourcesContent":["import { setSileroModelLoader } from '@micdrop/client'\nimport { OnnxSileroModel, OnnxSileroOptions } from './vad/OnnxSileroModel'\n\nexport * from './vad/OnnxSileroModel'\nexport * from './vad/onnxQueue'\n\nlet options: OnnxSileroOptions = {}\n\n/**\n * Says where the Silero model comes from, to serve it from your own domain\n * rather than from a CDN, or to hand over its bytes directly.\n * @param next - Address or bytes of the model, and of the ONNX runtime\n */\nexport function setSileroOptions(next: OnnxSileroOptions) {\n options = next\n}\n\n// The ONNX runtime weighs more than the rest of Micdrop put together, so it\n// only enters the bundle of an app that imports this file\nsetSileroModelLoader(async () => {\n const model = new OnnxSileroModel(options)\n await model.load()\n return model\n})\n","import { SileroModel, SILERO_SAMPLE_RATE } from '@micdrop/client'\nimport * as ort from 'onnxruntime-web'\nimport { queueOnnxRun } from './onnxQueue'\n\n/** Where the model is fetched from, when the app does not provide it */\nexport const DEFAULT_SILERO_MODEL_URL =\n 'https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx'\n\n// onnxruntime-web@1.23 is not hosted on jsdelivr (its dist exceeds the CDN size\n// limit, so every file 404s). unpkg serves it, so the wasm backend comes from\n// there instead.\nconst DEFAULT_WASM_PATH = 'https://unpkg.com/onnxruntime-web@1.23.2/dist/'\n\nexport interface OnnxSileroOptions {\n /** Address of the Silero v5 model, or the bytes of it */\n model?: string | ArrayBuffer\n
|
|
1
|
+
{"version":3,"sources":["../src/silero.ts","../src/vad/OnnxSileroModel.ts","../src/vad/onnxQueue.ts"],"sourcesContent":["import { setSileroModelLoader } from '@micdrop/client'\nimport { OnnxSileroModel, OnnxSileroOptions } from './vad/OnnxSileroModel'\n\nexport * from './vad/OnnxSileroModel'\nexport * from './vad/onnxQueue'\n\nlet options: OnnxSileroOptions = {}\n\n/**\n * Says where the Silero model comes from, to serve it from your own domain\n * rather than from a CDN, or to hand over its bytes directly. Merged into the\n * options already set, so it keeps what the `@micdrop/web/vite` plugin chose.\n * @param next - Address or bytes of the model, and of the ONNX runtime\n */\nexport function setSileroOptions(next: OnnxSileroOptions) {\n options = { ...options, ...next }\n}\n\n// The ONNX runtime weighs more than the rest of Micdrop put together, so it\n// only enters the bundle of an app that imports this file\nsetSileroModelLoader(async () => {\n const model = new OnnxSileroModel(options)\n await model.load()\n return model\n})\n","import { SileroModel, SILERO_SAMPLE_RATE } from '@micdrop/client'\nimport * as ort from 'onnxruntime-web'\nimport { queueOnnxRun } from './onnxQueue'\n\n/** Where the model is fetched from, when the app does not provide it */\nexport const DEFAULT_SILERO_MODEL_URL =\n 'https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx'\n\n// onnxruntime-web@1.23 is not hosted on jsdelivr (its dist exceeds the CDN size\n// limit, so every file 404s). unpkg serves it, so the wasm backend comes from\n// there instead.\nconst DEFAULT_WASM_PATH = 'https://unpkg.com/onnxruntime-web@1.23.2/dist/'\n\nexport interface OnnxSileroOptions {\n /** Address of the Silero v5 model, or the bytes of it */\n model?: string | ArrayBuffer\n /**\n * Where the ONNX runtime loads its WebAssembly files from. `false` leaves\n * them to the runtime, which finds them next to itself once a bundler has\n * emitted them, as the `@micdrop/web/vite` plugin does.\n */\n wasmPath?: string | false\n}\n\nlet loading: Promise<ArrayBuffer> | undefined\n\n/**\n * Runs the Silero model in the browser, on the ONNX WebAssembly runtime.\n *\n * The state machine that turns its answers into turns lives in\n * `@micdrop/client`, this only scores windows of audio.\n */\nexport class OnnxSileroModel implements SileroModel {\n private session: ort.InferenceSession | undefined\n private state: ort.Tensor\n private sampleRate: ort.Tensor\n\n constructor(private options: OnnxSileroOptions = {}) {\n this.state = newState()\n this.sampleRate = new ort.Tensor('int64', [BigInt(SILERO_SAMPLE_RATE)])\n }\n\n async load() {\n if (this.session) return\n\n const wasmPath = this.options.wasmPath ?? DEFAULT_WASM_PATH\n if (wasmPath !== false) {\n ort.env.wasm.wasmPaths = wasmPath\n }\n\n const source = this.options.model ?? DEFAULT_SILERO_MODEL_URL\n let bytes: ArrayBuffer\n if (typeof source === 'string') {\n // Fetched once per page, the model is a couple of megabytes\n loading =\n loading ?? fetch(source).then((response) => response.arrayBuffer())\n bytes = await loading\n } else {\n bytes = source\n }\n\n this.session = await ort.InferenceSession.create(bytes)\n }\n\n async process(frame: Float32Array): Promise<number> {\n if (!this.session) await this.load()\n\n const output = await queueOnnxRun(() =>\n this.session!.run({\n input: new ort.Tensor('float32', frame, [1, frame.length]),\n state: this.state,\n sr: this.sampleRate,\n })\n )\n\n this.state = output.stateN as ort.Tensor\n return (output.output.data as Float32Array)[0]\n }\n\n reset() {\n this.state = newState()\n }\n\n async release() {\n await this.session?.release()\n this.session = undefined\n }\n}\n\n/** The memory the model carries from one window to the next, emptied */\nfunction newState(): ort.Tensor {\n return new ort.Tensor('float32', new Float32Array(2 * 128), [2, 1, 128])\n}\n","/**\n * Runs one inference at a time, whichever model asks.\n *\n * The ONNX runtime keeps a single active session for the whole WebAssembly\n * module, so two models cannot be in flight together. A model running on the\n * graphics card yields while it waits for the device, and a detector scoring\n * its next window on top of it makes both fail, one with `Session already\n * started` and the other with `Session mismatch`.\n *\n * The queue lives on the global scope so every Micdrop model takes the same\n * turns, `@micdrop/smart-turn` included, which ships separately and holds the\n * same few lines.\n */\nconst globalScope = globalThis as typeof globalThis & {\n micdropOnnxQueue?: Promise<unknown>\n}\n\n/**\n * Waits for the models ahead in the queue, then runs\n * @param run - The inference to run once the runtime is free\n */\nexport function queueOnnxRun<T>(run: () => Promise<T>): Promise<T> {\n const started = (globalScope.micdropOnnxQueue ?? Promise.resolve()).then(\n run,\n run\n )\n // A failed run still lets the next one through\n globalScope.micdropOnnxQueue = started.catch(() => undefined)\n return started\n}\n"],"mappings":";AAAA,SAAS,4BAA4B;;;ACArC,SAAsB,0BAA0B;AAChD,YAAY,SAAS;;;ACYrB,IAAM,cAAc;AAQb,SAAS,aAAgB,KAAmC;AACjE,QAAM,WAAW,YAAY,oBAAoB,QAAQ,QAAQ,GAAG;AAAA,IAClE;AAAA,IACA;AAAA,EACF;AAEA,cAAY,mBAAmB,QAAQ,MAAM,MAAM,MAAS;AAC5D,SAAO;AACT;;;ADxBO,IAAM,2BACX;AAKF,IAAM,oBAAoB;AAa1B,IAAI;AAQG,IAAM,kBAAN,MAA6C;AAAA,EAKlD,YAAoBA,WAA6B,CAAC,GAAG;AAAjC,mBAAAA;AAClB,SAAK,QAAQ,SAAS;AACtB,SAAK,aAAa,IAAQ,WAAO,SAAS,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAAA,EACxE;AAAA,EAEA,MAAM,OAAO;AACX,QAAI,KAAK,QAAS;AAElB,UAAM,WAAW,KAAK,QAAQ,YAAY;AAC1C,QAAI,aAAa,OAAO;AACtB,MAAI,QAAI,KAAK,YAAY;AAAA,IAC3B;AAEA,UAAM,SAAS,KAAK,QAAQ,SAAS;AACrC,QAAI;AACJ,QAAI,OAAO,WAAW,UAAU;AAE9B,gBACE,WAAW,MAAM,MAAM,EAAE,KAAK,CAAC,aAAa,SAAS,YAAY,CAAC;AACpE,cAAQ,MAAM;AAAA,IAChB,OAAO;AACL,cAAQ;AAAA,IACV;AAEA,SAAK,UAAU,MAAU,qBAAiB,OAAO,KAAK;AAAA,EACxD;AAAA,EAEA,MAAM,QAAQ,OAAsC;AAClD,QAAI,CAAC,KAAK,QAAS,OAAM,KAAK,KAAK;AAEnC,UAAM,SAAS,MAAM;AAAA,MAAa,MAChC,KAAK,QAAS,IAAI;AAAA,QAChB,OAAO,IAAQ,WAAO,WAAW,OAAO,CAAC,GAAG,MAAM,MAAM,CAAC;AAAA,QACzD,OAAO,KAAK;AAAA,QACZ,IAAI,KAAK;AAAA,MACX,CAAC;AAAA,IACH;AAEA,SAAK,QAAQ,OAAO;AACpB,WAAQ,OAAO,OAAO,KAAsB,CAAC;AAAA,EAC/C;AAAA,EAEA,QAAQ;AACN,SAAK,QAAQ,SAAS;AAAA,EACxB;AAAA,EAEA,MAAM,UAAU;AACd,UAAM,KAAK,SAAS,QAAQ;AAC5B,SAAK,UAAU;AAAA,EACjB;AACF;AAGA,SAAS,WAAuB;AAC9B,SAAO,IAAQ,WAAO,WAAW,IAAI,aAAa,IAAI,GAAG,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;AACzE;;;ADtFA,IAAI,UAA6B,CAAC;AAQ3B,SAAS,iBAAiB,MAAyB;AACxD,YAAU,EAAE,GAAG,SAAS,GAAG,KAAK;AAClC;AAIA,qBAAqB,YAAY;AAC/B,QAAM,QAAQ,IAAI,gBAAgB,OAAO;AACzC,QAAM,MAAM,KAAK;AACjB,SAAO;AACT,CAAC;","names":["options"]}
|
package/dist/vite.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Bundles the files the voice models load at runtime, so a call never depends
|
|
5
|
+
* on a public CDN being reachable.
|
|
6
|
+
*
|
|
7
|
+
* Without it, `@micdrop/web/silero` fetches the Silero model from jsDelivr and
|
|
8
|
+
* the ONNX runtime from unpkg, which an outage, a firewall or an ad blocker
|
|
9
|
+
* can keep from loading. With it, the model ships with the app and the ONNX
|
|
10
|
+
* runtime finds its WebAssembly next to itself, both emitted by Vite.
|
|
11
|
+
* `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where
|
|
12
|
+
* `setSmartTurnOptions` points.
|
|
13
|
+
*
|
|
14
|
+
* Silero only runs on WebAssembly, so the runtime is the WebAssembly build,
|
|
15
|
+
* half the size of the full one. Smart Turn runs on the graphics card when
|
|
16
|
+
* the browser offers it, so the full build is kept when `@micdrop/smart-turn`
|
|
17
|
+
* is installed. Every import of the runtime gets the same build, so an app
|
|
18
|
+
* never loads two of them.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { micdrop } from '@micdrop/web/vite'
|
|
22
|
+
*
|
|
23
|
+
* export default defineConfig({ plugins: [micdrop()] })
|
|
24
|
+
*/
|
|
25
|
+
declare function micdrop(options?: MicdropViteOptions): Plugin;
|
|
26
|
+
interface MicdropViteOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Keeps the build of the ONNX runtime that runs on the graphics card, twice
|
|
29
|
+
* the size of the WebAssembly one. Only Smart Turn uses it, so it defaults
|
|
30
|
+
* to whether `@micdrop/smart-turn` is installed.
|
|
31
|
+
*/
|
|
32
|
+
webgpu?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { type MicdropViteOptions, micdrop };
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Bundles the files the voice models load at runtime, so a call never depends
|
|
5
|
+
* on a public CDN being reachable.
|
|
6
|
+
*
|
|
7
|
+
* Without it, `@micdrop/web/silero` fetches the Silero model from jsDelivr and
|
|
8
|
+
* the ONNX runtime from unpkg, which an outage, a firewall or an ad blocker
|
|
9
|
+
* can keep from loading. With it, the model ships with the app and the ONNX
|
|
10
|
+
* runtime finds its WebAssembly next to itself, both emitted by Vite.
|
|
11
|
+
* `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where
|
|
12
|
+
* `setSmartTurnOptions` points.
|
|
13
|
+
*
|
|
14
|
+
* Silero only runs on WebAssembly, so the runtime is the WebAssembly build,
|
|
15
|
+
* half the size of the full one. Smart Turn runs on the graphics card when
|
|
16
|
+
* the browser offers it, so the full build is kept when `@micdrop/smart-turn`
|
|
17
|
+
* is installed. Every import of the runtime gets the same build, so an app
|
|
18
|
+
* never loads two of them.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { micdrop } from '@micdrop/web/vite'
|
|
22
|
+
*
|
|
23
|
+
* export default defineConfig({ plugins: [micdrop()] })
|
|
24
|
+
*/
|
|
25
|
+
declare function micdrop(options?: MicdropViteOptions): Plugin;
|
|
26
|
+
interface MicdropViteOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Keeps the build of the ONNX runtime that runs on the graphics card, twice
|
|
29
|
+
* the size of the WebAssembly one. Only Smart Turn uses it, so it defaults
|
|
30
|
+
* to whether `@micdrop/smart-turn` is installed.
|
|
31
|
+
*/
|
|
32
|
+
webgpu?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { type MicdropViteOptions, micdrop };
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/vite.ts
|
|
31
|
+
var vite_exports = {};
|
|
32
|
+
__export(vite_exports, {
|
|
33
|
+
micdrop: () => micdrop
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(vite_exports);
|
|
36
|
+
|
|
37
|
+
// ../../node_modules/.pnpm/tsup@8.5.1_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js
|
|
38
|
+
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
|
|
39
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
40
|
+
|
|
41
|
+
// src/vite.ts
|
|
42
|
+
var import_node_module = require("module");
|
|
43
|
+
var import_node_path = __toESM(require("path"));
|
|
44
|
+
var import_node_process = __toESM(require("process"));
|
|
45
|
+
var import_node_url = require("url");
|
|
46
|
+
var SILERO_ID = "@micdrop/web/silero";
|
|
47
|
+
var SMART_TURN_ID = "@micdrop/smart-turn/web";
|
|
48
|
+
var VIRTUAL_SILERO = "\0micdrop:silero";
|
|
49
|
+
var VIRTUAL_SMART_TURN = "\0micdrop:smart-turn";
|
|
50
|
+
function micdrop(options = {}) {
|
|
51
|
+
const sileroModel = (0, import_node_url.fileURLToPath)(
|
|
52
|
+
new URL("../assets/silero_vad_v5.onnx", importMetaUrl)
|
|
53
|
+
).replace(/\\/g, "/");
|
|
54
|
+
return {
|
|
55
|
+
name: "micdrop",
|
|
56
|
+
enforce: "pre",
|
|
57
|
+
config(config) {
|
|
58
|
+
const webgpu = options.webgpu ?? isInstalled(
|
|
59
|
+
"@micdrop/smart-turn",
|
|
60
|
+
import_node_path.default.resolve(config.root ?? import_node_process.default.cwd())
|
|
61
|
+
);
|
|
62
|
+
return {
|
|
63
|
+
resolve: {
|
|
64
|
+
alias: webgpu ? [] : [
|
|
65
|
+
{
|
|
66
|
+
find: /^onnxruntime-web$/,
|
|
67
|
+
replacement: "onnxruntime-web/wasm"
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
optimizeDeps: {
|
|
72
|
+
// Pre-bundling moves the runtime away from its WebAssembly files,
|
|
73
|
+
// which it looks up relative to its own address
|
|
74
|
+
exclude: ["onnxruntime-web", "onnxruntime-web/wasm"]
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
// The app keeps importing `@micdrop/web/silero`, and gets a module that
|
|
79
|
+
// sets the bundled options before handing over the real one. Imports from
|
|
80
|
+
// dependencies and from the wrappers themselves go through untouched.
|
|
81
|
+
resolveId(id, importer) {
|
|
82
|
+
if (!importer || importer.startsWith("\0")) return null;
|
|
83
|
+
if (importer.includes("/node_modules/")) return null;
|
|
84
|
+
if (id === SILERO_ID) return VIRTUAL_SILERO;
|
|
85
|
+
if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN;
|
|
86
|
+
return null;
|
|
87
|
+
},
|
|
88
|
+
load(id) {
|
|
89
|
+
if (id === VIRTUAL_SILERO) {
|
|
90
|
+
return [
|
|
91
|
+
`import { setSileroOptions } from '${SILERO_ID}'`,
|
|
92
|
+
`import model from ${JSON.stringify(`${sileroModel}?url`)}`,
|
|
93
|
+
`setSileroOptions({ model, wasmPath: false })`,
|
|
94
|
+
`export * from '${SILERO_ID}'`
|
|
95
|
+
].join("\n");
|
|
96
|
+
}
|
|
97
|
+
if (id === VIRTUAL_SMART_TURN) {
|
|
98
|
+
return [
|
|
99
|
+
`import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,
|
|
100
|
+
`setSmartTurnOptions({ wasmPath: false })`,
|
|
101
|
+
`export * from '${SMART_TURN_ID}'`
|
|
102
|
+
].join("\n");
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function isInstalled(name, root) {
|
|
109
|
+
try {
|
|
110
|
+
(0, import_node_module.createRequire)(import_node_path.default.join(root, "package.json")).resolve(name);
|
|
111
|
+
return true;
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
117
|
+
0 && (module.exports = {
|
|
118
|
+
micdrop
|
|
119
|
+
});
|
|
120
|
+
//# sourceMappingURL=vite.js.map
|
package/dist/vite.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vite.ts","../../../node_modules/.pnpm/tsup@8.5.1_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["import { createRequire } from 'node:module'\nimport path from 'node:path'\nimport process from 'node:process'\nimport { fileURLToPath } from 'node:url'\nimport type { Plugin } from 'vite'\n\nconst SILERO_ID = '@micdrop/web/silero'\nconst SMART_TURN_ID = '@micdrop/smart-turn/web'\nconst VIRTUAL_SILERO = '\\0micdrop:silero'\nconst VIRTUAL_SMART_TURN = '\\0micdrop:smart-turn'\n\n/**\n * Bundles the files the voice models load at runtime, so a call never depends\n * on a public CDN being reachable.\n *\n * Without it, `@micdrop/web/silero` fetches the Silero model from jsDelivr and\n * the ONNX runtime from unpkg, which an outage, a firewall or an ad blocker\n * can keep from loading. With it, the model ships with the app and the ONNX\n * runtime finds its WebAssembly next to itself, both emitted by Vite.\n * `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where\n * `setSmartTurnOptions` points.\n *\n * Silero only runs on WebAssembly, so the runtime is the WebAssembly build,\n * half the size of the full one. Smart Turn runs on the graphics card when\n * the browser offers it, so the full build is kept when `@micdrop/smart-turn`\n * is installed. Every import of the runtime gets the same build, so an app\n * never loads two of them.\n *\n * @example\n * import { micdrop } from '@micdrop/web/vite'\n *\n * export default defineConfig({ plugins: [micdrop()] })\n */\nexport function micdrop(options: MicdropViteOptions = {}): Plugin {\n const sileroModel = fileURLToPath(\n new URL('../assets/silero_vad_v5.onnx', import.meta.url)\n ).replace(/\\\\/g, '/')\n\n return {\n name: 'micdrop',\n enforce: 'pre',\n\n config(config) {\n const webgpu =\n options.webgpu ??\n isInstalled(\n '@micdrop/smart-turn',\n path.resolve(config.root ?? process.cwd())\n )\n return {\n resolve: {\n alias: webgpu\n ? []\n : [\n {\n find: /^onnxruntime-web$/,\n replacement: 'onnxruntime-web/wasm',\n },\n ],\n },\n optimizeDeps: {\n // Pre-bundling moves the runtime away from its WebAssembly files,\n // which it looks up relative to its own address\n exclude: ['onnxruntime-web', 'onnxruntime-web/wasm'],\n },\n }\n },\n\n // The app keeps importing `@micdrop/web/silero`, and gets a module that\n // sets the bundled options before handing over the real one. Imports from\n // dependencies and from the wrappers themselves go through untouched.\n resolveId(id, importer) {\n if (!importer || importer.startsWith('\\0')) return null\n if (importer.includes('/node_modules/')) return null\n if (id === SILERO_ID) return VIRTUAL_SILERO\n if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN\n return null\n },\n\n load(id) {\n if (id === VIRTUAL_SILERO) {\n return [\n `import { setSileroOptions } from '${SILERO_ID}'`,\n `import model from ${JSON.stringify(`${sileroModel}?url`)}`,\n `setSileroOptions({ model, wasmPath: false })`,\n `export * from '${SILERO_ID}'`,\n ].join('\\n')\n }\n if (id === VIRTUAL_SMART_TURN) {\n return [\n `import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,\n `setSmartTurnOptions({ wasmPath: false })`,\n `export * from '${SMART_TURN_ID}'`,\n ].join('\\n')\n }\n return null\n },\n }\n}\n\nexport interface MicdropViteOptions {\n /**\n * Keeps the build of the ONNX runtime that runs on the graphics card, twice\n * the size of the WebAssembly one. Only Smart Turn uses it, so it defaults\n * to whether `@micdrop/smart-turn` is installed.\n */\n webgpu?: boolean\n}\n\n/** Whether the app at `root` can import the package */\nfunction isInstalled(name: string, root: string): boolean {\n try {\n createRequire(path.join(root, 'package.json')).resolve(name)\n return true\n } catch {\n return false\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () => \n typeof document === \"undefined\" \n ? new URL(`file:${__filename}`).href \n : (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') \n ? document.currentScript.src \n : new URL(\"main.js\", document.baseURI).href;\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,QAAQ,YAAY,MAAM,WAC1E,SAAS,cAAc,MACvB,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEtC,IAAM,gBAAgC,iCAAiB;;;ADZ9D,yBAA8B;AAC9B,uBAAiB;AACjB,0BAAoB;AACpB,sBAA8B;AAG9B,IAAM,YAAY;AAClB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAwBpB,SAAS,QAAQ,UAA8B,CAAC,GAAW;AAChE,QAAM,kBAAc;AAAA,IAClB,IAAI,IAAI,gCAAgC,aAAe;AAAA,EACzD,EAAE,QAAQ,OAAO,GAAG;AAEpB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,OAAO,QAAQ;AACb,YAAM,SACJ,QAAQ,UACR;AAAA,QACE;AAAA,QACA,iBAAAA,QAAK,QAAQ,OAAO,QAAQ,oBAAAC,QAAQ,IAAI,CAAC;AAAA,MAC3C;AACF,aAAO;AAAA,QACL,SAAS;AAAA,UACP,OAAO,SACH,CAAC,IACD;AAAA,YACE;AAAA,cACE,MAAM;AAAA,cACN,aAAa;AAAA,YACf;AAAA,UACF;AAAA,QACN;AAAA,QACA,cAAc;AAAA;AAAA;AAAA,UAGZ,SAAS,CAAC,mBAAmB,sBAAsB;AAAA,QACrD;AAAA,MACF;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAKA,UAAU,IAAI,UAAU;AACtB,UAAI,CAAC,YAAY,SAAS,WAAW,IAAI,EAAG,QAAO;AACnD,UAAI,SAAS,SAAS,gBAAgB,EAAG,QAAO;AAChD,UAAI,OAAO,UAAW,QAAO;AAC7B,UAAI,OAAO,cAAe,QAAO;AACjC,aAAO;AAAA,IACT;AAAA,IAEA,KAAK,IAAI;AACP,UAAI,OAAO,gBAAgB;AACzB,eAAO;AAAA,UACL,qCAAqC,SAAS;AAAA,UAC9C,qBAAqB,KAAK,UAAU,GAAG,WAAW,MAAM,CAAC;AAAA,UACzD;AAAA,UACA,kBAAkB,SAAS;AAAA,QAC7B,EAAE,KAAK,IAAI;AAAA,MACb;AACA,UAAI,OAAO,oBAAoB;AAC7B,eAAO;AAAA,UACL,wCAAwC,aAAa;AAAA,UACrD;AAAA,UACA,kBAAkB,aAAa;AAAA,QACjC,EAAE,KAAK,IAAI;AAAA,MACb;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAYA,SAAS,YAAY,MAAc,MAAuB;AACxD,MAAI;AACF,0CAAc,iBAAAD,QAAK,KAAK,MAAM,cAAc,CAAC,EAAE,QAAQ,IAAI;AAC3D,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":["path","process"]}
|
package/dist/vite.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import process from "process";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
var SILERO_ID = "@micdrop/web/silero";
|
|
7
|
+
var SMART_TURN_ID = "@micdrop/smart-turn/web";
|
|
8
|
+
var VIRTUAL_SILERO = "\0micdrop:silero";
|
|
9
|
+
var VIRTUAL_SMART_TURN = "\0micdrop:smart-turn";
|
|
10
|
+
function micdrop(options = {}) {
|
|
11
|
+
const sileroModel = fileURLToPath(
|
|
12
|
+
new URL("../assets/silero_vad_v5.onnx", import.meta.url)
|
|
13
|
+
).replace(/\\/g, "/");
|
|
14
|
+
return {
|
|
15
|
+
name: "micdrop",
|
|
16
|
+
enforce: "pre",
|
|
17
|
+
config(config) {
|
|
18
|
+
const webgpu = options.webgpu ?? isInstalled(
|
|
19
|
+
"@micdrop/smart-turn",
|
|
20
|
+
path.resolve(config.root ?? process.cwd())
|
|
21
|
+
);
|
|
22
|
+
return {
|
|
23
|
+
resolve: {
|
|
24
|
+
alias: webgpu ? [] : [
|
|
25
|
+
{
|
|
26
|
+
find: /^onnxruntime-web$/,
|
|
27
|
+
replacement: "onnxruntime-web/wasm"
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
optimizeDeps: {
|
|
32
|
+
// Pre-bundling moves the runtime away from its WebAssembly files,
|
|
33
|
+
// which it looks up relative to its own address
|
|
34
|
+
exclude: ["onnxruntime-web", "onnxruntime-web/wasm"]
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
// The app keeps importing `@micdrop/web/silero`, and gets a module that
|
|
39
|
+
// sets the bundled options before handing over the real one. Imports from
|
|
40
|
+
// dependencies and from the wrappers themselves go through untouched.
|
|
41
|
+
resolveId(id, importer) {
|
|
42
|
+
if (!importer || importer.startsWith("\0")) return null;
|
|
43
|
+
if (importer.includes("/node_modules/")) return null;
|
|
44
|
+
if (id === SILERO_ID) return VIRTUAL_SILERO;
|
|
45
|
+
if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN;
|
|
46
|
+
return null;
|
|
47
|
+
},
|
|
48
|
+
load(id) {
|
|
49
|
+
if (id === VIRTUAL_SILERO) {
|
|
50
|
+
return [
|
|
51
|
+
`import { setSileroOptions } from '${SILERO_ID}'`,
|
|
52
|
+
`import model from ${JSON.stringify(`${sileroModel}?url`)}`,
|
|
53
|
+
`setSileroOptions({ model, wasmPath: false })`,
|
|
54
|
+
`export * from '${SILERO_ID}'`
|
|
55
|
+
].join("\n");
|
|
56
|
+
}
|
|
57
|
+
if (id === VIRTUAL_SMART_TURN) {
|
|
58
|
+
return [
|
|
59
|
+
`import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,
|
|
60
|
+
`setSmartTurnOptions({ wasmPath: false })`,
|
|
61
|
+
`export * from '${SMART_TURN_ID}'`
|
|
62
|
+
].join("\n");
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function isInstalled(name, root) {
|
|
69
|
+
try {
|
|
70
|
+
createRequire(path.join(root, "package.json")).resolve(name);
|
|
71
|
+
return true;
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
micdrop
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=vite.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vite.ts"],"sourcesContent":["import { createRequire } from 'node:module'\nimport path from 'node:path'\nimport process from 'node:process'\nimport { fileURLToPath } from 'node:url'\nimport type { Plugin } from 'vite'\n\nconst SILERO_ID = '@micdrop/web/silero'\nconst SMART_TURN_ID = '@micdrop/smart-turn/web'\nconst VIRTUAL_SILERO = '\\0micdrop:silero'\nconst VIRTUAL_SMART_TURN = '\\0micdrop:smart-turn'\n\n/**\n * Bundles the files the voice models load at runtime, so a call never depends\n * on a public CDN being reachable.\n *\n * Without it, `@micdrop/web/silero` fetches the Silero model from jsDelivr and\n * the ONNX runtime from unpkg, which an outage, a firewall or an ad blocker\n * can keep from loading. With it, the model ships with the app and the ONNX\n * runtime finds its WebAssembly next to itself, both emitted by Vite.\n * `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where\n * `setSmartTurnOptions` points.\n *\n * Silero only runs on WebAssembly, so the runtime is the WebAssembly build,\n * half the size of the full one. Smart Turn runs on the graphics card when\n * the browser offers it, so the full build is kept when `@micdrop/smart-turn`\n * is installed. Every import of the runtime gets the same build, so an app\n * never loads two of them.\n *\n * @example\n * import { micdrop } from '@micdrop/web/vite'\n *\n * export default defineConfig({ plugins: [micdrop()] })\n */\nexport function micdrop(options: MicdropViteOptions = {}): Plugin {\n const sileroModel = fileURLToPath(\n new URL('../assets/silero_vad_v5.onnx', import.meta.url)\n ).replace(/\\\\/g, '/')\n\n return {\n name: 'micdrop',\n enforce: 'pre',\n\n config(config) {\n const webgpu =\n options.webgpu ??\n isInstalled(\n '@micdrop/smart-turn',\n path.resolve(config.root ?? process.cwd())\n )\n return {\n resolve: {\n alias: webgpu\n ? []\n : [\n {\n find: /^onnxruntime-web$/,\n replacement: 'onnxruntime-web/wasm',\n },\n ],\n },\n optimizeDeps: {\n // Pre-bundling moves the runtime away from its WebAssembly files,\n // which it looks up relative to its own address\n exclude: ['onnxruntime-web', 'onnxruntime-web/wasm'],\n },\n }\n },\n\n // The app keeps importing `@micdrop/web/silero`, and gets a module that\n // sets the bundled options before handing over the real one. Imports from\n // dependencies and from the wrappers themselves go through untouched.\n resolveId(id, importer) {\n if (!importer || importer.startsWith('\\0')) return null\n if (importer.includes('/node_modules/')) return null\n if (id === SILERO_ID) return VIRTUAL_SILERO\n if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN\n return null\n },\n\n load(id) {\n if (id === VIRTUAL_SILERO) {\n return [\n `import { setSileroOptions } from '${SILERO_ID}'`,\n `import model from ${JSON.stringify(`${sileroModel}?url`)}`,\n `setSileroOptions({ model, wasmPath: false })`,\n `export * from '${SILERO_ID}'`,\n ].join('\\n')\n }\n if (id === VIRTUAL_SMART_TURN) {\n return [\n `import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,\n `setSmartTurnOptions({ wasmPath: false })`,\n `export * from '${SMART_TURN_ID}'`,\n ].join('\\n')\n }\n return null\n },\n }\n}\n\nexport interface MicdropViteOptions {\n /**\n * Keeps the build of the ONNX runtime that runs on the graphics card, twice\n * the size of the WebAssembly one. Only Smart Turn uses it, so it defaults\n * to whether `@micdrop/smart-turn` is installed.\n */\n webgpu?: boolean\n}\n\n/** Whether the app at `root` can import the package */\nfunction isInstalled(name: string, root: string): boolean {\n try {\n createRequire(path.join(root, 'package.json')).resolve(name)\n return true\n } catch {\n return false\n }\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAC9B,OAAO,UAAU;AACjB,OAAO,aAAa;AACpB,SAAS,qBAAqB;AAG9B,IAAM,YAAY;AAClB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAwBpB,SAAS,QAAQ,UAA8B,CAAC,GAAW;AAChE,QAAM,cAAc;AAAA,IAClB,IAAI,IAAI,gCAAgC,YAAY,GAAG;AAAA,EACzD,EAAE,QAAQ,OAAO,GAAG;AAEpB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,OAAO,QAAQ;AACb,YAAM,SACJ,QAAQ,UACR;AAAA,QACE;AAAA,QACA,KAAK,QAAQ,OAAO,QAAQ,QAAQ,IAAI,CAAC;AAAA,MAC3C;AACF,aAAO;AAAA,QACL,SAAS;AAAA,UACP,OAAO,SACH,CAAC,IACD;AAAA,YACE;AAAA,cACE,MAAM;AAAA,cACN,aAAa;AAAA,YACf;AAAA,UACF;AAAA,QACN;AAAA,QACA,cAAc;AAAA;AAAA;AAAA,UAGZ,SAAS,CAAC,mBAAmB,sBAAsB;AAAA,QACrD;AAAA,MACF;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAKA,UAAU,IAAI,UAAU;AACtB,UAAI,CAAC,YAAY,SAAS,WAAW,IAAI,EAAG,QAAO;AACnD,UAAI,SAAS,SAAS,gBAAgB,EAAG,QAAO;AAChD,UAAI,OAAO,UAAW,QAAO;AAC7B,UAAI,OAAO,cAAe,QAAO;AACjC,aAAO;AAAA,IACT;AAAA,IAEA,KAAK,IAAI;AACP,UAAI,OAAO,gBAAgB;AACzB,eAAO;AAAA,UACL,qCAAqC,SAAS;AAAA,UAC9C,qBAAqB,KAAK,UAAU,GAAG,WAAW,MAAM,CAAC;AAAA,UACzD;AAAA,UACA,kBAAkB,SAAS;AAAA,QAC7B,EAAE,KAAK,IAAI;AAAA,MACb;AACA,UAAI,OAAO,oBAAoB;AAC7B,eAAO;AAAA,UACL,wCAAwC,aAAa;AAAA,UACrD;AAAA,UACA,kBAAkB,aAAa;AAAA,QACjC,EAAE,KAAK,IAAI;AAAA,MACb;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAYA,SAAS,YAAY,MAAc,MAAuB;AACxD,MAAI;AACF,kBAAc,KAAK,KAAK,MAAM,cAAc,CAAC,EAAE,QAAQ,IAAI;AAC3D,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@micdrop/web",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "🖐️🎤 Micdrop: real-time voice conversations with AI, in the browser",
|
|
5
5
|
"author": "Godefroy de Compreignac",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "dist/index.js",
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"prepublishOnly": "npm run build"
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
|
-
"dist"
|
|
18
|
+
"dist",
|
|
19
|
+
"assets"
|
|
19
20
|
],
|
|
20
21
|
"repository": {
|
|
21
22
|
"type": "git",
|
|
@@ -36,10 +37,20 @@
|
|
|
36
37
|
"@micdrop/client": "^3.4.0",
|
|
37
38
|
"onnxruntime-web": "^1.23.2"
|
|
38
39
|
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"vite": ">=5.0.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"vite": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
47
|
+
},
|
|
39
48
|
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.19.13",
|
|
40
50
|
"esbuild-raw-plugin": "^0.3.1",
|
|
41
51
|
"tsup": "^8.5.1",
|
|
42
|
-
"typescript": "^5.9.3"
|
|
52
|
+
"typescript": "^5.9.3",
|
|
53
|
+
"vite": "^7.3.1"
|
|
43
54
|
},
|
|
44
55
|
"exports": {
|
|
45
56
|
".": {
|
|
@@ -51,6 +62,11 @@
|
|
|
51
62
|
"types": "./dist/silero.d.ts",
|
|
52
63
|
"import": "./dist/silero.mjs",
|
|
53
64
|
"require": "./dist/silero.js"
|
|
65
|
+
},
|
|
66
|
+
"./vite": {
|
|
67
|
+
"types": "./dist/vite.d.ts",
|
|
68
|
+
"import": "./dist/vite.mjs",
|
|
69
|
+
"require": "./dist/vite.js"
|
|
54
70
|
}
|
|
55
71
|
}
|
|
56
72
|
}
|