@micdrop/web 1.0.0 → 1.1.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 CHANGED
@@ -40,19 +40,6 @@ Micdrop.on('StateChange', (state) => {
40
40
  })
41
41
  ```
42
42
 
43
- ## Coming from @micdrop/client v2
44
-
45
- The browser code moved here so the same call can run on a phone. Change the import and the names below, everything else is unchanged. The full guide is on the website: [v2 to v3](https://micdrop.dev/docs/migration/v2-to-v3).
46
-
47
- ```diff
48
- -import { Micdrop } from '@micdrop/client'
49
- +import { Micdrop } from '@micdrop/web'
50
- ```
51
-
52
- - Devices are `{ id, label, kind }` rather than `MediaDeviceInfo`, so `device.deviceId` becomes `device.id`
53
- - `Speaker.playAudio()` takes PCM16 or an `ArrayBuffer` rather than a `Blob`
54
- - `Mic.analyser` and `Speaker.analyser` are gone, the level comes from `Mic.on('Volume')` and `Speaker.on('Volume')`. For a raw `AnalyserNode`, connect one to `getSpeakerOutput()`.
55
-
56
43
  ## Browser specifics
57
44
 
58
45
  - `import '@micdrop/web/silero'` adds the Silero voice detection, and only then does the ONNX runtime enter your bundle
package/dist/silero.d.mts CHANGED
@@ -26,6 +26,12 @@ declare class OnnxSileroModel implements SileroModel {
26
26
  release(): Promise<void>;
27
27
  }
28
28
 
29
+ /**
30
+ * Waits for the models ahead in the queue, then runs
31
+ * @param run - The inference to run once the runtime is free
32
+ */
33
+ declare function queueOnnxRun<T>(run: () => Promise<T>): Promise<T>;
34
+
29
35
  /**
30
36
  * Says where the Silero model comes from, to serve it from your own domain
31
37
  * rather than from a CDN, or to hand over its bytes directly.
@@ -33,4 +39,4 @@ declare class OnnxSileroModel implements SileroModel {
33
39
  */
34
40
  declare function setSileroOptions(next: OnnxSileroOptions): void;
35
41
 
36
- export { DEFAULT_SILERO_MODEL_URL, OnnxSileroModel, type OnnxSileroOptions, setSileroOptions };
42
+ export { DEFAULT_SILERO_MODEL_URL, OnnxSileroModel, type OnnxSileroOptions, queueOnnxRun, setSileroOptions };
package/dist/silero.d.ts CHANGED
@@ -26,6 +26,12 @@ declare class OnnxSileroModel implements SileroModel {
26
26
  release(): Promise<void>;
27
27
  }
28
28
 
29
+ /**
30
+ * Waits for the models ahead in the queue, then runs
31
+ * @param run - The inference to run once the runtime is free
32
+ */
33
+ declare function queueOnnxRun<T>(run: () => Promise<T>): Promise<T>;
34
+
29
35
  /**
30
36
  * Says where the Silero model comes from, to serve it from your own domain
31
37
  * rather than from a CDN, or to hand over its bytes directly.
@@ -33,4 +39,4 @@ declare class OnnxSileroModel implements SileroModel {
33
39
  */
34
40
  declare function setSileroOptions(next: OnnxSileroOptions): void;
35
41
 
36
- export { DEFAULT_SILERO_MODEL_URL, OnnxSileroModel, type OnnxSileroOptions, setSileroOptions };
42
+ export { DEFAULT_SILERO_MODEL_URL, OnnxSileroModel, type OnnxSileroOptions, queueOnnxRun, setSileroOptions };
package/dist/silero.js CHANGED
@@ -32,6 +32,7 @@ var silero_exports = {};
32
32
  __export(silero_exports, {
33
33
  DEFAULT_SILERO_MODEL_URL: () => DEFAULT_SILERO_MODEL_URL,
34
34
  OnnxSileroModel: () => OnnxSileroModel,
35
+ queueOnnxRun: () => queueOnnxRun,
35
36
  setSileroOptions: () => setSileroOptions
36
37
  });
37
38
  module.exports = __toCommonJS(silero_exports);
@@ -40,6 +41,19 @@ var import_client2 = require("@micdrop/client");
40
41
  // src/vad/OnnxSileroModel.ts
41
42
  var import_client = require("@micdrop/client");
42
43
  var ort = __toESM(require("onnxruntime-web"));
44
+
45
+ // src/vad/onnxQueue.ts
46
+ var globalScope = globalThis;
47
+ function queueOnnxRun(run) {
48
+ const started = (globalScope.micdropOnnxQueue ?? Promise.resolve()).then(
49
+ run,
50
+ run
51
+ );
52
+ globalScope.micdropOnnxQueue = started.catch(() => void 0);
53
+ return started;
54
+ }
55
+
56
+ // src/vad/OnnxSileroModel.ts
43
57
  var DEFAULT_SILERO_MODEL_URL = "https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx";
44
58
  var DEFAULT_WASM_PATH = "https://unpkg.com/onnxruntime-web@1.23.2/dist/";
45
59
  var loading;
@@ -64,11 +78,13 @@ var OnnxSileroModel = class {
64
78
  }
65
79
  async process(frame) {
66
80
  if (!this.session) await this.load();
67
- const output = await this.session.run({
68
- input: new ort.Tensor("float32", frame, [1, frame.length]),
69
- state: this.state,
70
- sr: this.sampleRate
71
- });
81
+ const output = await queueOnnxRun(
82
+ () => this.session.run({
83
+ input: new ort.Tensor("float32", frame, [1, frame.length]),
84
+ state: this.state,
85
+ sr: this.sampleRate
86
+ })
87
+ );
72
88
  this.state = output.stateN;
73
89
  return output.output.data[0];
74
90
  }
@@ -98,6 +114,7 @@ function setSileroOptions(next) {
98
114
  0 && (module.exports = {
99
115
  DEFAULT_SILERO_MODEL_URL,
100
116
  OnnxSileroModel,
117
+ queueOnnxRun,
101
118
  setSileroOptions
102
119
  });
103
120
  //# sourceMappingURL=silero.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/silero.ts","../src/vad/OnnxSileroModel.ts"],"sourcesContent":["import { setSileroModelLoader } from '@micdrop/client'\nimport { OnnxSileroModel, OnnxSileroOptions } from './vad/OnnxSileroModel'\n\nexport * from './vad/OnnxSileroModel'\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'\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 /** Where the ONNX runtime loads its WebAssembly files from */\n wasmPath?: string\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 ort.env.wasm.wasmPaths = this.options.wasmPath ?? DEFAULT_WASM_PATH\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 this.session!.run({\n input: new ort.Tensor('float32', frame, [1, frame.length]),\n state: this.state,\n sr: this.sampleRate,\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAAqC;;;ACArC,oBAAgD;AAChD,UAAqB;AAGd,IAAM,2BACX;AAKF,IAAM,oBAAoB;AAS1B,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,IAAI,QAAI,KAAK,YAAY,KAAK,QAAQ,YAAY;AAElD,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,KAAK,QAAS,IAAI;AAAA,MACrC,OAAO,IAAQ,WAAO,WAAW,OAAO,CAAC,GAAG,MAAM,MAAM,CAAC;AAAA,MACzD,OAAO,KAAK;AAAA,MACZ,IAAI,KAAK;AAAA,IACX,CAAC;AAED,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;;;AD7EA,IAAI,UAA6B,CAAC;AAO3B,SAAS,iBAAiB,MAAyB;AACxD,YAAU;AACZ;AAAA,IAIA,qCAAqB,YAAY;AAC/B,QAAM,QAAQ,IAAI,gBAAgB,OAAO;AACzC,QAAM,MAAM,KAAK;AACjB,SAAO;AACT,CAAC;","names":["import_client","options"]}
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 /** Where the ONNX runtime loads its WebAssembly files from */\n wasmPath?: string\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 ort.env.wasm.wasmPaths = this.options.wasmPath ?? DEFAULT_WASM_PATH\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;AAS1B,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,IAAI,QAAI,KAAK,YAAY,KAAK,QAAQ,YAAY;AAElD,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;;;AD/EA,IAAI,UAA6B,CAAC;AAO3B,SAAS,iBAAiB,MAAyB;AACxD,YAAU;AACZ;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
@@ -4,6 +4,19 @@ import { setSileroModelLoader } from "@micdrop/client";
4
4
  // src/vad/OnnxSileroModel.ts
5
5
  import { SILERO_SAMPLE_RATE } from "@micdrop/client";
6
6
  import * as ort from "onnxruntime-web";
7
+
8
+ // src/vad/onnxQueue.ts
9
+ var globalScope = globalThis;
10
+ function queueOnnxRun(run) {
11
+ const started = (globalScope.micdropOnnxQueue ?? Promise.resolve()).then(
12
+ run,
13
+ run
14
+ );
15
+ globalScope.micdropOnnxQueue = started.catch(() => void 0);
16
+ return started;
17
+ }
18
+
19
+ // src/vad/OnnxSileroModel.ts
7
20
  var DEFAULT_SILERO_MODEL_URL = "https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.30/dist/silero_vad_v5.onnx";
8
21
  var DEFAULT_WASM_PATH = "https://unpkg.com/onnxruntime-web@1.23.2/dist/";
9
22
  var loading;
@@ -28,11 +41,13 @@ var OnnxSileroModel = class {
28
41
  }
29
42
  async process(frame) {
30
43
  if (!this.session) await this.load();
31
- const output = await this.session.run({
32
- input: new ort.Tensor("float32", frame, [1, frame.length]),
33
- state: this.state,
34
- sr: this.sampleRate
35
- });
44
+ const output = await queueOnnxRun(
45
+ () => this.session.run({
46
+ input: new ort.Tensor("float32", frame, [1, frame.length]),
47
+ state: this.state,
48
+ sr: this.sampleRate
49
+ })
50
+ );
36
51
  this.state = output.stateN;
37
52
  return output.output.data[0];
38
53
  }
@@ -61,6 +76,7 @@ setSileroModelLoader(async () => {
61
76
  export {
62
77
  DEFAULT_SILERO_MODEL_URL,
63
78
  OnnxSileroModel,
79
+ queueOnnxRun,
64
80
  setSileroOptions
65
81
  };
66
82
  //# sourceMappingURL=silero.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/silero.ts","../src/vad/OnnxSileroModel.ts"],"sourcesContent":["import { setSileroModelLoader } from '@micdrop/client'\nimport { OnnxSileroModel, OnnxSileroOptions } from './vad/OnnxSileroModel'\n\nexport * from './vad/OnnxSileroModel'\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'\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 /** Where the ONNX runtime loads its WebAssembly files from */\n wasmPath?: string\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 ort.env.wasm.wasmPaths = this.options.wasmPath ?? DEFAULT_WASM_PATH\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 this.session!.run({\n input: new ort.Tensor('float32', frame, [1, frame.length]),\n state: this.state,\n sr: this.sampleRate,\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"],"mappings":";AAAA,SAAS,4BAA4B;;;ACArC,SAAsB,0BAA0B;AAChD,YAAY,SAAS;AAGd,IAAM,2BACX;AAKF,IAAM,oBAAoB;AAS1B,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,IAAI,QAAI,KAAK,YAAY,KAAK,QAAQ,YAAY;AAElD,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,KAAK,QAAS,IAAI;AAAA,MACrC,OAAO,IAAQ,WAAO,WAAW,OAAO,CAAC,GAAG,MAAM,MAAM,CAAC;AAAA,MACzD,OAAO,KAAK;AAAA,MACZ,IAAI,KAAK;AAAA,IACX,CAAC;AAED,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;;;AD7EA,IAAI,UAA6B,CAAC;AAO3B,SAAS,iBAAiB,MAAyB;AACxD,YAAU;AACZ;AAIA,qBAAqB,YAAY;AAC/B,QAAM,QAAQ,IAAI,gBAAgB,OAAO;AACzC,QAAM,MAAM,KAAK;AACjB,SAAO;AACT,CAAC;","names":["options"]}
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 /** Where the ONNX runtime loads its WebAssembly files from */\n wasmPath?: string\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 ort.env.wasm.wasmPaths = this.options.wasmPath ?? DEFAULT_WASM_PATH\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;AAS1B,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,IAAI,QAAI,KAAK,YAAY,KAAK,QAAQ,YAAY;AAElD,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;;;AD/EA,IAAI,UAA6B,CAAC;AAO3B,SAAS,iBAAiB,MAAyB;AACxD,YAAU;AACZ;AAIA,qBAAqB,YAAY;AAC/B,QAAM,QAAQ,IAAI,gBAAgB,OAAO;AACzC,QAAM,MAAM,KAAK;AACjB,SAAO;AACT,CAAC;","names":["options"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micdrop/web",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "\ud83d\udd90\ufe0f\ud83c\udfa4 Micdrop: real-time voice conversations with AI, in the browser",
5
5
  "author": "Godefroy de Compreignac",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "conversation"
34
34
  ],
35
35
  "dependencies": {
36
- "@micdrop/client": "^3.0.0",
36
+ "@micdrop/client": "^3.1.0",
37
37
  "onnxruntime-web": "^1.23.2"
38
38
  },
39
39
  "devDependencies": {