@micdrop/web 1.1.2 → 1.2.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 +18 -1
- package/assets/silero_vad_v5.onnx +0 -0
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -2
- package/dist/index.mjs.map +1 -1
- 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 +21 -0
- package/dist/vite.d.ts +21 -0
- package/dist/vite.js +87 -0
- package/dist/vite.js.map +1 -0
- package/dist/vite.mjs +56 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +21 -5
package/README.md
CHANGED
|
@@ -28,6 +28,22 @@ 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
|
+
|
|
31
47
|
## Usage
|
|
32
48
|
|
|
33
49
|
```ts
|
|
@@ -43,7 +59,8 @@ Micdrop.on('StateChange', (state) => {
|
|
|
43
59
|
## Browser specifics
|
|
44
60
|
|
|
45
61
|
- `import '@micdrop/web/silero'` adds the Silero voice detection, and only then does the ONNX runtime enter your bundle
|
|
46
|
-
- `
|
|
62
|
+
- `@micdrop/web/vite` bundles the Silero model and the ONNX runtime files with your app, see above
|
|
63
|
+
- `setSileroOptions({ model, wasmPath })` serves the Silero model and the ONNX runtime from your own domain with another bundler
|
|
47
64
|
- `getSpeakerOutput()` gives the node the assistant voice comes out of, to analyse or process it
|
|
48
65
|
- `audioContext` is the shared `AudioContext`
|
|
49
66
|
|
|
Binary file
|
package/dist/index.js
CHANGED
|
@@ -246,6 +246,7 @@ var WebAudioSink = class {
|
|
|
246
246
|
};
|
|
247
247
|
|
|
248
248
|
// src/audio/WebSpeaker.ts
|
|
249
|
+
var FIREFOX_WARM_UP = 2e3;
|
|
249
250
|
var WebSpeaker = class extends import_client2.SpeakerDriver {
|
|
250
251
|
get isPlaying() {
|
|
251
252
|
return this.stream?.isPlaying ?? false;
|
|
@@ -261,6 +262,9 @@ var WebSpeaker = class extends import_client2.SpeakerDriver {
|
|
|
261
262
|
if (this.stream) return;
|
|
262
263
|
this.sink = new WebAudioSink();
|
|
263
264
|
this.stream = new import_client2.Pcm16AudioStream(this.sink);
|
|
265
|
+
if (navigator.userAgent.includes("Firefox")) {
|
|
266
|
+
this.stream.warmUp(FIREFOX_WARM_UP);
|
|
267
|
+
}
|
|
264
268
|
this.stream.on("StartPlaying", () => this.emit("StartPlaying"));
|
|
265
269
|
this.stream.on("StopPlaying", () => this.emit("StopPlaying"));
|
|
266
270
|
this.stream.on("Volume", (volume) => this.emit("Volume", volume));
|
|
@@ -273,8 +277,7 @@ var WebSpeaker = class extends import_client2.SpeakerDriver {
|
|
|
273
277
|
this.stream?.stopAudio();
|
|
274
278
|
}
|
|
275
279
|
async stop() {
|
|
276
|
-
this.stream?.
|
|
277
|
-
this.stream?.removeAllListeners();
|
|
280
|
+
this.stream?.destroy();
|
|
278
281
|
this.stream = void 0;
|
|
279
282
|
this.sink?.output.disconnect();
|
|
280
283
|
this.sink = void 0;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/audio/WebMic.ts","../src/audio/unlock.ts","../src/audio/audioContext.ts","raw:./pcm-processor-worklet","../src/audio/pcm-processor.ts","../src/audio/stopStream.ts","../src/audio/WebSpeaker.ts","../src/audio/WebAudioSink.ts"],"sourcesContent":["import { Mic, setMicdropStorage, Speaker } from '@micdrop/client'\nimport { WebMic } from './audio/WebMic'\nimport { WebSpeaker } from './audio/WebSpeaker'\n\n/**\n * The node the assistant voice comes out of, to analyse or process it.\n *\n * Connect an AnalyserNode to it to read the envelope on your own frames, which\n * is finer than the ten levels a second the `Volume` event reports.\n * @returns The node, once the call has started\n */\nexport function getSpeakerOutput(): AudioNode | undefined {\n const driver = Speaker.getDriver()\n return driver instanceof WebSpeaker ? driver.output : undefined\n}\n\nexport * from '@micdrop/client'\nexport * from './audio/audioContext'\nexport * from './audio/WebAudioSink'\nexport * from './audio/WebMic'\nexport * from './audio/WebSpeaker'\n\n/**\n * Records and plays with the Web Audio API, and keeps settings in\n * `localStorage`. Called when the package is imported, so a page has nothing\n * to wire.\n */\nexport function registerWebAudio() {\n Mic.setDriver(new WebMic())\n Speaker.setDriver(new WebSpeaker())\n setMicdropStorage(localStorage)\n}\n\nregisterWebAudio()\n","import { MicDriver, MicdropDevice } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { initPcmProcessor } from './pcm-processor'\nimport { stopStream } from './stopStream'\n\n/**\n * How long Firefox needs to let go of a microphone it released.\n *\n * A microphone requested again before that comes back without its processing:\n * no echo cancellation, no noise suppression and a raw gain some 30 dB higher,\n * while its settings still report them on. The room then sounds like speech,\n * and the assistant hears its own voice. No event tells when the device is\n * free, so a new stream waits for this delay to pass.\n */\nconst FIREFOX_RELEASE_DELAY = 15000 // ms\n\nconst isFirefox =\n typeof navigator !== 'undefined' && /Firefox\\//.test(navigator.userAgent)\n\n// The last stream released, outside the class: the browser holds the device\n// whichever WebMic opened it\nlet lastRelease: { at: number; deviceId: string | undefined } | undefined\n\n/**\n * Records the microphone with the Web Audio API.\n *\n * An audio worklet delivers the samples at a steady pace, and everything that\n * happens to them afterwards lives in `@micdrop/client`.\n */\nexport class WebMic extends MicDriver {\n private stream: MediaStream | undefined\n private source: MediaStreamAudioSourceNode | undefined\n private worklet: AudioWorkletNode | undefined\n private _deviceId: string | undefined\n\n get isStarted(): boolean {\n return !!this.stream\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n // Asking for echo cancellation is not getting it: some browsers and devices\n // do without, and the settings of the track say so\n get echoCancellation(): boolean | undefined {\n const track = this.stream?.getAudioTracks()[0]\n const setting = track?.getSettings?.().echoCancellation as unknown\n if (setting === undefined) return undefined\n return setting !== false\n }\n\n async start(deviceId?: string): Promise<void> {\n if (this.stream) {\n // Same device, keep recording\n if (this._deviceId === deviceId) return\n await this.stop()\n }\n\n await waitForRelease(deviceId)\n\n this.stream = await navigator.mediaDevices.getUserMedia({\n audio: {\n deviceId: { ideal: deviceId },\n sampleRate: 16000, // not working, it will follow device settings, usually 44.1kHz\n sampleSize: 16,\n channelCount: 1,\n echoCancellation: true,\n noiseSuppression: true,\n },\n })\n\n // Remember which device the browser settled on\n const track = this.stream.getTracks()[0]\n const settled = track?.getCapabilities?.().deviceId\n this._deviceId =\n settled && settled !== 'default' ? settled : (deviceId ?? settled)\n\n await initPcmProcessor()\n this.worklet = new AudioWorkletNode(audioContext, 'pcm-processor')\n this.worklet.port.onmessage = this.onWorkletMessage\n\n this.source = audioContext.createMediaStreamSource(this.stream)\n this.source.connect(this.worklet)\n\n // A browser only reveals the device labels once a stream is running, and\n // Firefox lists nothing at all before that\n navigator.mediaDevices.addEventListener('devicechange', this.onDeviceChange)\n }\n\n async stop(): Promise<void> {\n navigator.mediaDevices.removeEventListener(\n 'devicechange',\n this.onDeviceChange\n )\n if (this.worklet) {\n this.worklet.port.onmessage = null\n this.worklet.disconnect()\n this.worklet = undefined\n }\n if (this.source) {\n this.source.disconnect()\n this.source = undefined\n }\n if (this.stream) {\n stopStream(this.stream)\n this.stream = undefined\n lastRelease = { at: Date.now(), deviceId: this._deviceId }\n }\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audioinput')\n }\n\n private onWorkletMessage = (event: MessageEvent) => {\n const { type, frames, sampleRate } = event.data\n if (type !== 'frames') return\n this.emit('Frames', frames as Float32Array, sampleRate as number)\n }\n\n private onDeviceChange = () => {\n this.emit('DeviceChange')\n }\n}\n\n/**\n * Waits until Firefox let go of the microphone, see FIREFOX_RELEASE_DELAY\n * @param deviceId - The device about to be opened\n */\nasync function waitForRelease(deviceId?: string) {\n if (!isFirefox || !lastRelease) return\n if (deviceId && lastRelease.deviceId && deviceId !== lastRelease.deviceId) {\n return\n }\n const remaining = lastRelease.at + FIREFOX_RELEASE_DELAY - Date.now()\n if (remaining > 0) {\n await new Promise((resolve) => setTimeout(resolve, remaining))\n }\n}\n\n/**\n * Lists the devices of one kind, the default one first\n * @param kind - Which devices to list\n */\nexport async function listDevices(\n kind: 'audioinput' | 'audiooutput'\n): Promise<MicdropDevice[]> {\n const all = await navigator.mediaDevices.enumerateDevices()\n if (all.length === 0) return []\n\n // A browser lists the default device twice, once under the id \"default\" and\n // once under its real id. Keep the real one, and put it first.\n const defaults = all.filter((device) => device.deviceId === 'default')\n const devices = all.filter(\n (device) => device.deviceId !== 'default' && device.kind === kind\n )\n for (const fallback of defaults) {\n const index = devices.findIndex(\n (device) => device.groupId === fallback.groupId\n )\n if (index > 0) devices.unshift(...devices.splice(index, 1))\n }\n\n return devices.map((device) => ({\n id: device.deviceId,\n label: device.label || (kind === 'audioinput' ? 'Microphone' : 'Speaker'),\n kind,\n }))\n}\n","// Inspired https://www.mattmontag.com/web/unlock-web-audio-in-safari-for-ios-and-macos\nexport function unlock(audioContext: AudioContext): void {\n const events = ['touchstart', 'touchend', 'mousedown', 'keydown']\n\n const unlock = async () => {\n unlockSafari(audioContext)\n if (audioContext.state === 'suspended') {\n await audioContext.resume()\n }\n // Clean events\n events.forEach((e) => document.body.removeEventListener(e, unlock))\n }\n\n // Add events\n events.forEach((e) => document.body.addEventListener(e, unlock, false))\n}\n\n// safari/ios hack - inspired by https://codepen.io/kslstn/pen/pagLqL\n// unlock audioContext - call when interactive (button click)\nfunction unlockSafari(audioContext: AudioContext): void {\n // create empty buffer and play it to unlock audioContext when interactive\n const source = audioContext.createBufferSource()\n source.buffer = audioContext.createBuffer(1, 1, 22050)\n source.connect(audioContext.destination)\n const onEnded = () => {\n source.disconnect()\n source.buffer = null\n source.removeEventListener('ended', onEnded)\n }\n source.addEventListener('ended', onEnded)\n source.start()\n}\n","import { unlock } from './unlock'\n\nexport let audioContext: AudioContext\n\nexport function createAudioContext() {\n return new (window.AudioContext || (window as any).webkitAudioContext)()\n}\n\ndeclare global {\n interface Window {\n micdropAudioContext: AudioContext\n }\n}\n\nif (window.micdropAudioContext) {\n audioContext = window.micdropAudioContext\n} else {\n window.micdropAudioContext = audioContext = createAudioContext()\n\n // Hack to unlock audio context on some browsers\n unlock(audioContext)\n}\n","// Hands the microphone signal to the main thread, continuously.\n//\n// Deciding whether someone is speaking, keeping a reserve of audio and cutting\n// it into chunks all happen in @micdrop/client, so this only has to deliver the\n// samples at a steady pace.\n\nconst INTERVAL_MS = 20\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.buffers = []\n this.length = 0\n // `sampleRate` is provided by the worklet scope\n this.frameLength = Math.round((sampleRate * INTERVAL_MS) / 1000)\n }\n\n process(inputs) {\n const input = inputs[0]\n if (!input || input.length === 0) return true\n\n this.buffers.push(new Float32Array(input[0]))\n this.length += input[0].length\n if (this.length < this.frameLength) return true\n\n const frames = new Float32Array(this.length)\n let offset = 0\n for (const buffer of this.buffers) {\n frames.set(buffer, offset)\n offset += buffer.length\n }\n this.buffers.length = 0\n this.length = 0\n\n this.port.postMessage({ type: 'frames', frames, sampleRate }, [\n frames.buffer,\n ])\n return true\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n","import pcmProcessorWorklet from './pcm-processor-worklet?raw'\nimport { audioContext } from './audioContext'\n\ndeclare global {\n interface Window {\n micdropPcmProcessor: boolean\n }\n}\n\nexport async function initPcmProcessor() {\n if (window.micdropPcmProcessor) return\n window.micdropPcmProcessor = true\n\n // Init worklet URL\n const workletBlob = new Blob([pcmProcessorWorklet], {\n type: 'application/javascript',\n })\n const workletUrl = URL.createObjectURL(workletBlob)\n\n // Load the worklet module\n await audioContext.audioWorklet.addModule(workletUrl)\n}\n","/**\n * Stops a stream\n * @param stream - The stream to stop\n */\nexport function stopStream(stream?: MediaStream) {\n stream?.getTracks().forEach((track) => track.stop())\n}\n","import { MicdropDevice, Pcm16AudioStream, SpeakerDriver } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { listDevices } from './WebMic'\nimport { WebAudioSink } from './WebAudioSink'\n\n/**\n * Plays the assistant voice with the Web Audio API.\n */\nexport class WebSpeaker extends SpeakerDriver {\n private sink: WebAudioSink | undefined\n private stream: Pcm16AudioStream | undefined\n private _deviceId: string | undefined\n // Firefox cannot pick an output on an audio context, so a non default device\n // is played back through an audio element instead\n private element: HTMLAudioElement | undefined\n\n get isPlaying(): boolean {\n return this.stream?.isPlaying ?? false\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n /** The node the sound comes out of, to analyse or process it */\n get output(): AudioNode | undefined {\n return this.sink?.output\n }\n\n async start(): Promise<void> {\n if (this.stream) return\n\n this.sink = new WebAudioSink()\n this.stream = new Pcm16AudioStream(this.sink)\n this.stream.on('StartPlaying', () => this.emit('StartPlaying'))\n this.stream.on('StopPlaying', () => this.emit('StopPlaying'))\n this.stream.on('Volume', (volume) => this.emit('Volume', volume))\n\n this.sink.output.connect(audioContext.destination)\n }\n\n play(pcm: Int16Array, sampleRate: number): void {\n this.stream?.playAudio(pcm, sampleRate)\n }\n\n stopAudio(): void {\n this.stream?.stopAudio()\n }\n\n async stop(): Promise<void> {\n this.stream?.stopAudio()\n this.stream?.removeAllListeners()\n this.stream = undefined\n this.sink?.output.disconnect()\n this.sink = undefined\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n }\n\n async changeDevice(deviceId: string): Promise<void> {\n const sink = this.sink\n if (!sink) {\n this._deviceId = deviceId\n return\n }\n\n const devices = await this.getDevices()\n const isDefault = devices.length === 0 || devices[0].id === deviceId\n\n if (isDefault || 'setSinkId' in audioContext) {\n // Playing through the audio context is the preferred way, it is the only\n // one that keeps echo cancellation working\n await (audioContext as any).setSinkId?.(isDefault ? '' : deviceId)\n sink.output.disconnect()\n sink.output.connect(audioContext.destination)\n\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n } else {\n // Firefox: stream the sound back into an audio element, which does accept\n // an output device\n if (!this.element) {\n const destination = audioContext.createMediaStreamDestination()\n sink.output.disconnect()\n sink.output.connect(destination)\n\n this.element = new Audio()\n this.element.autoplay = true\n this.element.srcObject = destination.stream\n }\n await this.element.setSinkId(deviceId)\n }\n\n this._deviceId = deviceId\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audiooutput')\n }\n}\n","import { AudioSink, ScheduledAudio } from '@micdrop/client'\nimport { audioContext } from './audioContext'\n\n/**\n * Plays samples through the Web Audio graph, on the timeline given by the\n * caller so consecutive chunks are heard without a gap.\n */\nexport class WebAudioSink implements AudioSink {\n public readonly output: GainNode\n\n constructor() {\n this.output = audioContext.createGain()\n }\n\n get sampleRate(): number {\n return audioContext.sampleRate\n }\n\n get currentTime(): number {\n return audioContext.currentTime\n }\n\n schedule(\n samples: Float32Array,\n when: number,\n onEnded: () => void\n ): ScheduledAudio {\n const buffer = audioContext.createBuffer(\n 1,\n samples.length,\n audioContext.sampleRate\n )\n buffer.copyToChannel(samples as Float32Array<ArrayBuffer>, 0)\n\n const source = audioContext.createBufferSource()\n source.buffer = buffer\n source.connect(this.output)\n source.onended = onEnded\n source.start(when)\n\n let stopped = false\n return {\n stop() {\n if (stopped) return\n stopped = true\n source.onended = null\n try {\n source.stop()\n } catch {\n // Already finished\n }\n source.disconnect()\n },\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAAgD;;;ACAhD,oBAAyC;;;ACClC,SAAS,OAAOC,eAAkC;AACvD,QAAM,SAAS,CAAC,cAAc,YAAY,aAAa,SAAS;AAEhE,QAAMC,UAAS,YAAY;AACzB,iBAAaD,aAAY;AACzB,QAAIA,cAAa,UAAU,aAAa;AACtC,YAAMA,cAAa,OAAO;AAAA,IAC5B;AAEA,WAAO,QAAQ,CAAC,MAAM,SAAS,KAAK,oBAAoB,GAAGC,OAAM,CAAC;AAAA,EACpE;AAGA,SAAO,QAAQ,CAAC,MAAM,SAAS,KAAK,iBAAiB,GAAGA,SAAQ,KAAK,CAAC;AACxE;AAIA,SAAS,aAAaD,eAAkC;AAEtD,QAAM,SAASA,cAAa,mBAAmB;AAC/C,SAAO,SAASA,cAAa,aAAa,GAAG,GAAG,KAAK;AACrD,SAAO,QAAQA,cAAa,WAAW;AACvC,QAAM,UAAU,MAAM;AACpB,WAAO,WAAW;AAClB,WAAO,SAAS;AAChB,WAAO,oBAAoB,SAAS,OAAO;AAAA,EAC7C;AACA,SAAO,iBAAiB,SAAS,OAAO;AACxC,SAAO,MAAM;AACf;;;AC7BO,IAAI;AAEJ,SAAS,qBAAqB;AACnC,SAAO,KAAK,OAAO,gBAAiB,OAAe,oBAAoB;AACzE;AAQA,IAAI,OAAO,qBAAqB;AAC9B,iBAAe,OAAO;AACxB,OAAO;AACL,SAAO,sBAAsB,eAAe,mBAAmB;AAG/D,SAAO,YAAY;AACrB;;;ACrBA;;;ACSA,eAAsB,mBAAmB;AACvC,MAAI,OAAO,oBAAqB;AAChC,SAAO,sBAAsB;AAG7B,QAAM,cAAc,IAAI,KAAK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AK,WAAW;AACtC,UAAM,YAAY,QAAQ,WAAW,KAAK,QAAQ,CAAC,EAAE,OAAO;AAE5D,QAAI,aAAa,eAAe,cAAc;AAG5C,YAAO,aAAqB,YAAY,YAAY,KAAK,QAAQ;AACjE,WAAK,OAAO,WAAW;AACvB,WAAK,OAAO,QAAQ,aAAa,WAAW;AAE5C,UAAI,KAAK,SAAS;AAChB,aAAK,QAAQ,MAAM;AACnB,aAAK,QAAQ,YAAY;AACzB,aAAK,UAAU;AAAA,MACjB;AAAA,IACF,OAAO;AAGL,UAAI,CAAC,KAAK,SAAS;AACjB,cAAM,cAAc,aAAa,6BAA6B;AAC9D,aAAK,OAAO,WAAW;AACvB,aAAK,OAAO,QAAQ,WAAW;AAE/B,aAAK,UAAU,IAAI,MAAM;AACzB,aAAK,QAAQ,WAAW;AACxB,aAAK,QAAQ,YAAY,YAAY;AAAA,MACvC;AACA,YAAM,KAAK,QAAQ,UAAU,QAAQ;AAAA,IACvC;AAEA,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,MAAM,aAAuC;AAC3C,WAAO,YAAY,aAAa;AAAA,EAClC;AACF;;;APzFA,0BAAc,4BAhBd;AAWO,SAAS,mBAA0C;AACxD,QAAM,SAAS,uBAAQ,UAAU;AACjC,SAAO,kBAAkB,aAAa,OAAO,SAAS;AACxD;AAaO,SAAS,mBAAmB;AACjC,qBAAI,UAAU,IAAI,OAAO,CAAC;AAC1B,yBAAQ,UAAU,IAAI,WAAW,CAAC;AAClC,wCAAkB,YAAY;AAChC;AAEA,iBAAiB;","names":["import_client","audioContext","unlock","import_client"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/audio/WebMic.ts","../src/audio/unlock.ts","../src/audio/audioContext.ts","raw:./pcm-processor-worklet","../src/audio/pcm-processor.ts","../src/audio/stopStream.ts","../src/audio/WebSpeaker.ts","../src/audio/WebAudioSink.ts"],"sourcesContent":["import { Mic, setMicdropStorage, Speaker } from '@micdrop/client'\nimport { WebMic } from './audio/WebMic'\nimport { WebSpeaker } from './audio/WebSpeaker'\n\n/**\n * The node the assistant voice comes out of, to analyse or process it.\n *\n * Connect an AnalyserNode to it to read the envelope on your own frames, which\n * is finer than the ten levels a second the `Volume` event reports.\n * @returns The node, once the call has started\n */\nexport function getSpeakerOutput(): AudioNode | undefined {\n const driver = Speaker.getDriver()\n return driver instanceof WebSpeaker ? driver.output : undefined\n}\n\nexport * from '@micdrop/client'\nexport * from './audio/audioContext'\nexport * from './audio/WebAudioSink'\nexport * from './audio/WebMic'\nexport * from './audio/WebSpeaker'\n\n/**\n * Records and plays with the Web Audio API, and keeps settings in\n * `localStorage`. Called when the package is imported, so a page has nothing\n * to wire.\n */\nexport function registerWebAudio() {\n Mic.setDriver(new WebMic())\n Speaker.setDriver(new WebSpeaker())\n setMicdropStorage(localStorage)\n}\n\nregisterWebAudio()\n","import { MicDriver, MicdropDevice } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { initPcmProcessor } from './pcm-processor'\nimport { stopStream } from './stopStream'\n\n/**\n * How long Firefox needs to let go of a microphone it released.\n *\n * A microphone requested again before that comes back without its processing:\n * no echo cancellation, no noise suppression and a raw gain some 30 dB higher,\n * while its settings still report them on. The room then sounds like speech,\n * and the assistant hears its own voice. No event tells when the device is\n * free, so a new stream waits for this delay to pass.\n */\nconst FIREFOX_RELEASE_DELAY = 15000 // ms\n\nconst isFirefox =\n typeof navigator !== 'undefined' && /Firefox\\//.test(navigator.userAgent)\n\n// The last stream released, outside the class: the browser holds the device\n// whichever WebMic opened it\nlet lastRelease: { at: number; deviceId: string | undefined } | undefined\n\n/**\n * Records the microphone with the Web Audio API.\n *\n * An audio worklet delivers the samples at a steady pace, and everything that\n * happens to them afterwards lives in `@micdrop/client`.\n */\nexport class WebMic extends MicDriver {\n private stream: MediaStream | undefined\n private source: MediaStreamAudioSourceNode | undefined\n private worklet: AudioWorkletNode | undefined\n private _deviceId: string | undefined\n\n get isStarted(): boolean {\n return !!this.stream\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n // Asking for echo cancellation is not getting it: some browsers and devices\n // do without, and the settings of the track say so\n get echoCancellation(): boolean | undefined {\n const track = this.stream?.getAudioTracks()[0]\n const setting = track?.getSettings?.().echoCancellation as unknown\n if (setting === undefined) return undefined\n return setting !== false\n }\n\n async start(deviceId?: string): Promise<void> {\n if (this.stream) {\n // Same device, keep recording\n if (this._deviceId === deviceId) return\n await this.stop()\n }\n\n await waitForRelease(deviceId)\n\n this.stream = await navigator.mediaDevices.getUserMedia({\n audio: {\n deviceId: { ideal: deviceId },\n sampleRate: 16000, // not working, it will follow device settings, usually 44.1kHz\n sampleSize: 16,\n channelCount: 1,\n echoCancellation: true,\n noiseSuppression: true,\n },\n })\n\n // Remember which device the browser settled on\n const track = this.stream.getTracks()[0]\n const settled = track?.getCapabilities?.().deviceId\n this._deviceId =\n settled && settled !== 'default' ? settled : (deviceId ?? settled)\n\n await initPcmProcessor()\n this.worklet = new AudioWorkletNode(audioContext, 'pcm-processor')\n this.worklet.port.onmessage = this.onWorkletMessage\n\n this.source = audioContext.createMediaStreamSource(this.stream)\n this.source.connect(this.worklet)\n\n // A browser only reveals the device labels once a stream is running, and\n // Firefox lists nothing at all before that\n navigator.mediaDevices.addEventListener('devicechange', this.onDeviceChange)\n }\n\n async stop(): Promise<void> {\n navigator.mediaDevices.removeEventListener(\n 'devicechange',\n this.onDeviceChange\n )\n if (this.worklet) {\n this.worklet.port.onmessage = null\n this.worklet.disconnect()\n this.worklet = undefined\n }\n if (this.source) {\n this.source.disconnect()\n this.source = undefined\n }\n if (this.stream) {\n stopStream(this.stream)\n this.stream = undefined\n lastRelease = { at: Date.now(), deviceId: this._deviceId }\n }\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audioinput')\n }\n\n private onWorkletMessage = (event: MessageEvent) => {\n const { type, frames, sampleRate } = event.data\n if (type !== 'frames') return\n this.emit('Frames', frames as Float32Array, sampleRate as number)\n }\n\n private onDeviceChange = () => {\n this.emit('DeviceChange')\n }\n}\n\n/**\n * Waits until Firefox let go of the microphone, see FIREFOX_RELEASE_DELAY\n * @param deviceId - The device about to be opened\n */\nasync function waitForRelease(deviceId?: string) {\n if (!isFirefox || !lastRelease) return\n if (deviceId && lastRelease.deviceId && deviceId !== lastRelease.deviceId) {\n return\n }\n const remaining = lastRelease.at + FIREFOX_RELEASE_DELAY - Date.now()\n if (remaining > 0) {\n await new Promise((resolve) => setTimeout(resolve, remaining))\n }\n}\n\n/**\n * Lists the devices of one kind, the default one first\n * @param kind - Which devices to list\n */\nexport async function listDevices(\n kind: 'audioinput' | 'audiooutput'\n): Promise<MicdropDevice[]> {\n const all = await navigator.mediaDevices.enumerateDevices()\n if (all.length === 0) return []\n\n // A browser lists the default device twice, once under the id \"default\" and\n // once under its real id. Keep the real one, and put it first.\n const defaults = all.filter((device) => device.deviceId === 'default')\n const devices = all.filter(\n (device) => device.deviceId !== 'default' && device.kind === kind\n )\n for (const fallback of defaults) {\n const index = devices.findIndex(\n (device) => device.groupId === fallback.groupId\n )\n if (index > 0) devices.unshift(...devices.splice(index, 1))\n }\n\n return devices.map((device) => ({\n id: device.deviceId,\n label: device.label || (kind === 'audioinput' ? 'Microphone' : 'Speaker'),\n kind,\n }))\n}\n","// Inspired https://www.mattmontag.com/web/unlock-web-audio-in-safari-for-ios-and-macos\nexport function unlock(audioContext: AudioContext): void {\n const events = ['touchstart', 'touchend', 'mousedown', 'keydown']\n\n const unlock = async () => {\n unlockSafari(audioContext)\n if (audioContext.state === 'suspended') {\n await audioContext.resume()\n }\n // Clean events\n events.forEach((e) => document.body.removeEventListener(e, unlock))\n }\n\n // Add events\n events.forEach((e) => document.body.addEventListener(e, unlock, false))\n}\n\n// safari/ios hack - inspired by https://codepen.io/kslstn/pen/pagLqL\n// unlock audioContext - call when interactive (button click)\nfunction unlockSafari(audioContext: AudioContext): void {\n // create empty buffer and play it to unlock audioContext when interactive\n const source = audioContext.createBufferSource()\n source.buffer = audioContext.createBuffer(1, 1, 22050)\n source.connect(audioContext.destination)\n const onEnded = () => {\n source.disconnect()\n source.buffer = null\n source.removeEventListener('ended', onEnded)\n }\n source.addEventListener('ended', onEnded)\n source.start()\n}\n","import { unlock } from './unlock'\n\nexport let audioContext: AudioContext\n\nexport function createAudioContext() {\n return new (window.AudioContext || (window as any).webkitAudioContext)()\n}\n\ndeclare global {\n interface Window {\n micdropAudioContext: AudioContext\n }\n}\n\nif (window.micdropAudioContext) {\n audioContext = window.micdropAudioContext\n} else {\n window.micdropAudioContext = audioContext = createAudioContext()\n\n // Hack to unlock audio context on some browsers\n unlock(audioContext)\n}\n","// Hands the microphone signal to the main thread, continuously.\n//\n// Deciding whether someone is speaking, keeping a reserve of audio and cutting\n// it into chunks all happen in @micdrop/client, so this only has to deliver the\n// samples at a steady pace.\n\nconst INTERVAL_MS = 20\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.buffers = []\n this.length = 0\n // `sampleRate` is provided by the worklet scope\n this.frameLength = Math.round((sampleRate * INTERVAL_MS) / 1000)\n }\n\n process(inputs) {\n const input = inputs[0]\n if (!input || input.length === 0) return true\n\n this.buffers.push(new Float32Array(input[0]))\n this.length += input[0].length\n if (this.length < this.frameLength) return true\n\n const frames = new Float32Array(this.length)\n let offset = 0\n for (const buffer of this.buffers) {\n frames.set(buffer, offset)\n offset += buffer.length\n }\n this.buffers.length = 0\n this.length = 0\n\n this.port.postMessage({ type: 'frames', frames, sampleRate }, [\n frames.buffer,\n ])\n return true\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n","import pcmProcessorWorklet from './pcm-processor-worklet?raw'\nimport { audioContext } from './audioContext'\n\ndeclare global {\n interface Window {\n micdropPcmProcessor: boolean\n }\n}\n\nexport async function initPcmProcessor() {\n if (window.micdropPcmProcessor) return\n window.micdropPcmProcessor = true\n\n // Init worklet URL\n const workletBlob = new Blob([pcmProcessorWorklet], {\n type: 'application/javascript',\n })\n const workletUrl = URL.createObjectURL(workletBlob)\n\n // Load the worklet module\n await audioContext.audioWorklet.addModule(workletUrl)\n}\n","/**\n * Stops a stream\n * @param stream - The stream to stop\n */\nexport function stopStream(stream?: MediaStream) {\n stream?.getTracks().forEach((track) => track.stop())\n}\n","import { MicdropDevice, Pcm16AudioStream, SpeakerDriver } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { listDevices } from './WebMic'\nimport { WebAudioSink } from './WebAudioSink'\n\n// A Bluetooth headset switches to its call profile as its microphone opens,\n// and Firefox mangles what it plays through that switch: the first answer of a\n// call comes out all at once. The speaker starts right after the microphone, so\n// it plays silence that long and holds the first answer until then.\nconst FIREFOX_WARM_UP = 2000 // ms\n\n/**\n * Plays the assistant voice with the Web Audio API.\n */\nexport class WebSpeaker extends SpeakerDriver {\n private sink: WebAudioSink | undefined\n private stream: Pcm16AudioStream | undefined\n private _deviceId: string | undefined\n // Firefox cannot pick an output on an audio context, so a non default device\n // is played back through an audio element instead\n private element: HTMLAudioElement | undefined\n\n get isPlaying(): boolean {\n return this.stream?.isPlaying ?? false\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n /** The node the sound comes out of, to analyse or process it */\n get output(): AudioNode | undefined {\n return this.sink?.output\n }\n\n async start(): Promise<void> {\n if (this.stream) return\n\n this.sink = new WebAudioSink()\n this.stream = new Pcm16AudioStream(this.sink)\n if (navigator.userAgent.includes('Firefox')) {\n this.stream.warmUp(FIREFOX_WARM_UP)\n }\n this.stream.on('StartPlaying', () => this.emit('StartPlaying'))\n this.stream.on('StopPlaying', () => this.emit('StopPlaying'))\n this.stream.on('Volume', (volume) => this.emit('Volume', volume))\n\n this.sink.output.connect(audioContext.destination)\n }\n\n play(pcm: Int16Array, sampleRate: number): void {\n this.stream?.playAudio(pcm, sampleRate)\n }\n\n stopAudio(): void {\n this.stream?.stopAudio()\n }\n\n async stop(): Promise<void> {\n this.stream?.destroy()\n this.stream = undefined\n this.sink?.output.disconnect()\n this.sink = undefined\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n }\n\n async changeDevice(deviceId: string): Promise<void> {\n const sink = this.sink\n if (!sink) {\n this._deviceId = deviceId\n return\n }\n\n const devices = await this.getDevices()\n const isDefault = devices.length === 0 || devices[0].id === deviceId\n\n if (isDefault || 'setSinkId' in audioContext) {\n // Playing through the audio context is the preferred way, it is the only\n // one that keeps echo cancellation working\n await (audioContext as any).setSinkId?.(isDefault ? '' : deviceId)\n sink.output.disconnect()\n sink.output.connect(audioContext.destination)\n\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n } else {\n // Firefox: stream the sound back into an audio element, which does accept\n // an output device\n if (!this.element) {\n const destination = audioContext.createMediaStreamDestination()\n sink.output.disconnect()\n sink.output.connect(destination)\n\n this.element = new Audio()\n this.element.autoplay = true\n this.element.srcObject = destination.stream\n }\n await this.element.setSinkId(deviceId)\n }\n\n this._deviceId = deviceId\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audiooutput')\n }\n}\n","import { AudioSink, ScheduledAudio } from '@micdrop/client'\nimport { audioContext } from './audioContext'\n\n/**\n * Plays samples through the Web Audio graph, on the timeline given by the\n * caller so consecutive chunks are heard without a gap.\n */\nexport class WebAudioSink implements AudioSink {\n public readonly output: GainNode\n\n constructor() {\n this.output = audioContext.createGain()\n }\n\n get sampleRate(): number {\n return audioContext.sampleRate\n }\n\n get currentTime(): number {\n return audioContext.currentTime\n }\n\n schedule(\n samples: Float32Array,\n when: number,\n onEnded: () => void\n ): ScheduledAudio {\n const buffer = audioContext.createBuffer(\n 1,\n samples.length,\n audioContext.sampleRate\n )\n buffer.copyToChannel(samples as Float32Array<ArrayBuffer>, 0)\n\n const source = audioContext.createBufferSource()\n source.buffer = buffer\n source.connect(this.output)\n source.onended = onEnded\n source.start(when)\n\n let stopped = false\n return {\n stop() {\n if (stopped) return\n stopped = true\n source.onended = null\n try {\n source.stop()\n } catch {\n // Already finished\n }\n source.disconnect()\n },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C,UAAM,YAAY,QAAQ,WAAW,KAAK,QAAQ,CAAC,EAAE,OAAO;AAE5D,QAAI,aAAa,eAAe,cAAc;AAG5C,YAAO,aAAqB,YAAY,YAAY,KAAK,QAAQ;AACjE,WAAK,OAAO,WAAW;AACvB,WAAK,OAAO,QAAQ,aAAa,WAAW;AAE5C,UAAI,KAAK,SAAS;AAChB,aAAK,QAAQ,MAAM;AACnB,aAAK,QAAQ,YAAY;AACzB,aAAK,UAAU;AAAA,MACjB;AAAA,IACF,OAAO;AAGL,UAAI,CAAC,KAAK,SAAS;AACjB,cAAM,cAAc,aAAa,6BAA6B;AAC9D,aAAK,OAAO,WAAW;AACvB,aAAK,OAAO,QAAQ,WAAW;AAE/B,aAAK,UAAU,IAAI,MAAM;AACzB,aAAK,QAAQ,WAAW;AACxB,aAAK,QAAQ,YAAY,YAAY;AAAA,MACvC;AACA,YAAM,KAAK,QAAQ,UAAU,QAAQ;AAAA,IACvC;AAEA,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,MAAM,aAAuC;AAC3C,WAAO,YAAY,aAAa;AAAA,EAClC;AACF;;;APjGA,0BAAc,4BAhBd;AAWO,SAAS,mBAA0C;AACxD,QAAM,SAAS,uBAAQ,UAAU;AACjC,SAAO,kBAAkB,aAAa,OAAO,SAAS;AACxD;AAaO,SAAS,mBAAmB;AACjC,qBAAI,UAAU,IAAI,OAAO,CAAC;AAC1B,yBAAQ,UAAU,IAAI,WAAW,CAAC;AAClC,wCAAkB,YAAY;AAChC;AAEA,iBAAiB;","names":["import_client","audioContext","unlock","import_client"]}
|
package/dist/index.mjs
CHANGED
|
@@ -214,6 +214,7 @@ var WebAudioSink = class {
|
|
|
214
214
|
};
|
|
215
215
|
|
|
216
216
|
// src/audio/WebSpeaker.ts
|
|
217
|
+
var FIREFOX_WARM_UP = 2e3;
|
|
217
218
|
var WebSpeaker = class extends SpeakerDriver {
|
|
218
219
|
get isPlaying() {
|
|
219
220
|
return this.stream?.isPlaying ?? false;
|
|
@@ -229,6 +230,9 @@ var WebSpeaker = class extends SpeakerDriver {
|
|
|
229
230
|
if (this.stream) return;
|
|
230
231
|
this.sink = new WebAudioSink();
|
|
231
232
|
this.stream = new Pcm16AudioStream(this.sink);
|
|
233
|
+
if (navigator.userAgent.includes("Firefox")) {
|
|
234
|
+
this.stream.warmUp(FIREFOX_WARM_UP);
|
|
235
|
+
}
|
|
232
236
|
this.stream.on("StartPlaying", () => this.emit("StartPlaying"));
|
|
233
237
|
this.stream.on("StopPlaying", () => this.emit("StopPlaying"));
|
|
234
238
|
this.stream.on("Volume", (volume) => this.emit("Volume", volume));
|
|
@@ -241,8 +245,7 @@ var WebSpeaker = class extends SpeakerDriver {
|
|
|
241
245
|
this.stream?.stopAudio();
|
|
242
246
|
}
|
|
243
247
|
async stop() {
|
|
244
|
-
this.stream?.
|
|
245
|
-
this.stream?.removeAllListeners();
|
|
248
|
+
this.stream?.destroy();
|
|
246
249
|
this.stream = void 0;
|
|
247
250
|
this.sink?.output.disconnect();
|
|
248
251
|
this.sink = void 0;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/audio/WebMic.ts","../src/audio/unlock.ts","../src/audio/audioContext.ts","raw:./pcm-processor-worklet","../src/audio/pcm-processor.ts","../src/audio/stopStream.ts","../src/audio/WebSpeaker.ts","../src/audio/WebAudioSink.ts"],"sourcesContent":["import { Mic, setMicdropStorage, Speaker } from '@micdrop/client'\nimport { WebMic } from './audio/WebMic'\nimport { WebSpeaker } from './audio/WebSpeaker'\n\n/**\n * The node the assistant voice comes out of, to analyse or process it.\n *\n * Connect an AnalyserNode to it to read the envelope on your own frames, which\n * is finer than the ten levels a second the `Volume` event reports.\n * @returns The node, once the call has started\n */\nexport function getSpeakerOutput(): AudioNode | undefined {\n const driver = Speaker.getDriver()\n return driver instanceof WebSpeaker ? driver.output : undefined\n}\n\nexport * from '@micdrop/client'\nexport * from './audio/audioContext'\nexport * from './audio/WebAudioSink'\nexport * from './audio/WebMic'\nexport * from './audio/WebSpeaker'\n\n/**\n * Records and plays with the Web Audio API, and keeps settings in\n * `localStorage`. Called when the package is imported, so a page has nothing\n * to wire.\n */\nexport function registerWebAudio() {\n Mic.setDriver(new WebMic())\n Speaker.setDriver(new WebSpeaker())\n setMicdropStorage(localStorage)\n}\n\nregisterWebAudio()\n","import { MicDriver, MicdropDevice } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { initPcmProcessor } from './pcm-processor'\nimport { stopStream } from './stopStream'\n\n/**\n * How long Firefox needs to let go of a microphone it released.\n *\n * A microphone requested again before that comes back without its processing:\n * no echo cancellation, no noise suppression and a raw gain some 30 dB higher,\n * while its settings still report them on. The room then sounds like speech,\n * and the assistant hears its own voice. No event tells when the device is\n * free, so a new stream waits for this delay to pass.\n */\nconst FIREFOX_RELEASE_DELAY = 15000 // ms\n\nconst isFirefox =\n typeof navigator !== 'undefined' && /Firefox\\//.test(navigator.userAgent)\n\n// The last stream released, outside the class: the browser holds the device\n// whichever WebMic opened it\nlet lastRelease: { at: number; deviceId: string | undefined } | undefined\n\n/**\n * Records the microphone with the Web Audio API.\n *\n * An audio worklet delivers the samples at a steady pace, and everything that\n * happens to them afterwards lives in `@micdrop/client`.\n */\nexport class WebMic extends MicDriver {\n private stream: MediaStream | undefined\n private source: MediaStreamAudioSourceNode | undefined\n private worklet: AudioWorkletNode | undefined\n private _deviceId: string | undefined\n\n get isStarted(): boolean {\n return !!this.stream\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n // Asking for echo cancellation is not getting it: some browsers and devices\n // do without, and the settings of the track say so\n get echoCancellation(): boolean | undefined {\n const track = this.stream?.getAudioTracks()[0]\n const setting = track?.getSettings?.().echoCancellation as unknown\n if (setting === undefined) return undefined\n return setting !== false\n }\n\n async start(deviceId?: string): Promise<void> {\n if (this.stream) {\n // Same device, keep recording\n if (this._deviceId === deviceId) return\n await this.stop()\n }\n\n await waitForRelease(deviceId)\n\n this.stream = await navigator.mediaDevices.getUserMedia({\n audio: {\n deviceId: { ideal: deviceId },\n sampleRate: 16000, // not working, it will follow device settings, usually 44.1kHz\n sampleSize: 16,\n channelCount: 1,\n echoCancellation: true,\n noiseSuppression: true,\n },\n })\n\n // Remember which device the browser settled on\n const track = this.stream.getTracks()[0]\n const settled = track?.getCapabilities?.().deviceId\n this._deviceId =\n settled && settled !== 'default' ? settled : (deviceId ?? settled)\n\n await initPcmProcessor()\n this.worklet = new AudioWorkletNode(audioContext, 'pcm-processor')\n this.worklet.port.onmessage = this.onWorkletMessage\n\n this.source = audioContext.createMediaStreamSource(this.stream)\n this.source.connect(this.worklet)\n\n // A browser only reveals the device labels once a stream is running, and\n // Firefox lists nothing at all before that\n navigator.mediaDevices.addEventListener('devicechange', this.onDeviceChange)\n }\n\n async stop(): Promise<void> {\n navigator.mediaDevices.removeEventListener(\n 'devicechange',\n this.onDeviceChange\n )\n if (this.worklet) {\n this.worklet.port.onmessage = null\n this.worklet.disconnect()\n this.worklet = undefined\n }\n if (this.source) {\n this.source.disconnect()\n this.source = undefined\n }\n if (this.stream) {\n stopStream(this.stream)\n this.stream = undefined\n lastRelease = { at: Date.now(), deviceId: this._deviceId }\n }\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audioinput')\n }\n\n private onWorkletMessage = (event: MessageEvent) => {\n const { type, frames, sampleRate } = event.data\n if (type !== 'frames') return\n this.emit('Frames', frames as Float32Array, sampleRate as number)\n }\n\n private onDeviceChange = () => {\n this.emit('DeviceChange')\n }\n}\n\n/**\n * Waits until Firefox let go of the microphone, see FIREFOX_RELEASE_DELAY\n * @param deviceId - The device about to be opened\n */\nasync function waitForRelease(deviceId?: string) {\n if (!isFirefox || !lastRelease) return\n if (deviceId && lastRelease.deviceId && deviceId !== lastRelease.deviceId) {\n return\n }\n const remaining = lastRelease.at + FIREFOX_RELEASE_DELAY - Date.now()\n if (remaining > 0) {\n await new Promise((resolve) => setTimeout(resolve, remaining))\n }\n}\n\n/**\n * Lists the devices of one kind, the default one first\n * @param kind - Which devices to list\n */\nexport async function listDevices(\n kind: 'audioinput' | 'audiooutput'\n): Promise<MicdropDevice[]> {\n const all = await navigator.mediaDevices.enumerateDevices()\n if (all.length === 0) return []\n\n // A browser lists the default device twice, once under the id \"default\" and\n // once under its real id. Keep the real one, and put it first.\n const defaults = all.filter((device) => device.deviceId === 'default')\n const devices = all.filter(\n (device) => device.deviceId !== 'default' && device.kind === kind\n )\n for (const fallback of defaults) {\n const index = devices.findIndex(\n (device) => device.groupId === fallback.groupId\n )\n if (index > 0) devices.unshift(...devices.splice(index, 1))\n }\n\n return devices.map((device) => ({\n id: device.deviceId,\n label: device.label || (kind === 'audioinput' ? 'Microphone' : 'Speaker'),\n kind,\n }))\n}\n","// Inspired https://www.mattmontag.com/web/unlock-web-audio-in-safari-for-ios-and-macos\nexport function unlock(audioContext: AudioContext): void {\n const events = ['touchstart', 'touchend', 'mousedown', 'keydown']\n\n const unlock = async () => {\n unlockSafari(audioContext)\n if (audioContext.state === 'suspended') {\n await audioContext.resume()\n }\n // Clean events\n events.forEach((e) => document.body.removeEventListener(e, unlock))\n }\n\n // Add events\n events.forEach((e) => document.body.addEventListener(e, unlock, false))\n}\n\n// safari/ios hack - inspired by https://codepen.io/kslstn/pen/pagLqL\n// unlock audioContext - call when interactive (button click)\nfunction unlockSafari(audioContext: AudioContext): void {\n // create empty buffer and play it to unlock audioContext when interactive\n const source = audioContext.createBufferSource()\n source.buffer = audioContext.createBuffer(1, 1, 22050)\n source.connect(audioContext.destination)\n const onEnded = () => {\n source.disconnect()\n source.buffer = null\n source.removeEventListener('ended', onEnded)\n }\n source.addEventListener('ended', onEnded)\n source.start()\n}\n","import { unlock } from './unlock'\n\nexport let audioContext: AudioContext\n\nexport function createAudioContext() {\n return new (window.AudioContext || (window as any).webkitAudioContext)()\n}\n\ndeclare global {\n interface Window {\n micdropAudioContext: AudioContext\n }\n}\n\nif (window.micdropAudioContext) {\n audioContext = window.micdropAudioContext\n} else {\n window.micdropAudioContext = audioContext = createAudioContext()\n\n // Hack to unlock audio context on some browsers\n unlock(audioContext)\n}\n","// Hands the microphone signal to the main thread, continuously.\n//\n// Deciding whether someone is speaking, keeping a reserve of audio and cutting\n// it into chunks all happen in @micdrop/client, so this only has to deliver the\n// samples at a steady pace.\n\nconst INTERVAL_MS = 20\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.buffers = []\n this.length = 0\n // `sampleRate` is provided by the worklet scope\n this.frameLength = Math.round((sampleRate * INTERVAL_MS) / 1000)\n }\n\n process(inputs) {\n const input = inputs[0]\n if (!input || input.length === 0) return true\n\n this.buffers.push(new Float32Array(input[0]))\n this.length += input[0].length\n if (this.length < this.frameLength) return true\n\n const frames = new Float32Array(this.length)\n let offset = 0\n for (const buffer of this.buffers) {\n frames.set(buffer, offset)\n offset += buffer.length\n }\n this.buffers.length = 0\n this.length = 0\n\n this.port.postMessage({ type: 'frames', frames, sampleRate }, [\n frames.buffer,\n ])\n return true\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n","import pcmProcessorWorklet from './pcm-processor-worklet?raw'\nimport { audioContext } from './audioContext'\n\ndeclare global {\n interface Window {\n micdropPcmProcessor: boolean\n }\n}\n\nexport async function initPcmProcessor() {\n if (window.micdropPcmProcessor) return\n window.micdropPcmProcessor = true\n\n // Init worklet URL\n const workletBlob = new Blob([pcmProcessorWorklet], {\n type: 'application/javascript',\n })\n const workletUrl = URL.createObjectURL(workletBlob)\n\n // Load the worklet module\n await audioContext.audioWorklet.addModule(workletUrl)\n}\n","/**\n * Stops a stream\n * @param stream - The stream to stop\n */\nexport function stopStream(stream?: MediaStream) {\n stream?.getTracks().forEach((track) => track.stop())\n}\n","import { MicdropDevice, Pcm16AudioStream, SpeakerDriver } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { listDevices } from './WebMic'\nimport { WebAudioSink } from './WebAudioSink'\n\n/**\n * Plays the assistant voice with the Web Audio API.\n */\nexport class WebSpeaker extends SpeakerDriver {\n private sink: WebAudioSink | undefined\n private stream: Pcm16AudioStream | undefined\n private _deviceId: string | undefined\n // Firefox cannot pick an output on an audio context, so a non default device\n // is played back through an audio element instead\n private element: HTMLAudioElement | undefined\n\n get isPlaying(): boolean {\n return this.stream?.isPlaying ?? false\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n /** The node the sound comes out of, to analyse or process it */\n get output(): AudioNode | undefined {\n return this.sink?.output\n }\n\n async start(): Promise<void> {\n if (this.stream) return\n\n this.sink = new WebAudioSink()\n this.stream = new Pcm16AudioStream(this.sink)\n this.stream.on('StartPlaying', () => this.emit('StartPlaying'))\n this.stream.on('StopPlaying', () => this.emit('StopPlaying'))\n this.stream.on('Volume', (volume) => this.emit('Volume', volume))\n\n this.sink.output.connect(audioContext.destination)\n }\n\n play(pcm: Int16Array, sampleRate: number): void {\n this.stream?.playAudio(pcm, sampleRate)\n }\n\n stopAudio(): void {\n this.stream?.stopAudio()\n }\n\n async stop(): Promise<void> {\n this.stream?.stopAudio()\n this.stream?.removeAllListeners()\n this.stream = undefined\n this.sink?.output.disconnect()\n this.sink = undefined\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n }\n\n async changeDevice(deviceId: string): Promise<void> {\n const sink = this.sink\n if (!sink) {\n this._deviceId = deviceId\n return\n }\n\n const devices = await this.getDevices()\n const isDefault = devices.length === 0 || devices[0].id === deviceId\n\n if (isDefault || 'setSinkId' in audioContext) {\n // Playing through the audio context is the preferred way, it is the only\n // one that keeps echo cancellation working\n await (audioContext as any).setSinkId?.(isDefault ? '' : deviceId)\n sink.output.disconnect()\n sink.output.connect(audioContext.destination)\n\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n } else {\n // Firefox: stream the sound back into an audio element, which does accept\n // an output device\n if (!this.element) {\n const destination = audioContext.createMediaStreamDestination()\n sink.output.disconnect()\n sink.output.connect(destination)\n\n this.element = new Audio()\n this.element.autoplay = true\n this.element.srcObject = destination.stream\n }\n await this.element.setSinkId(deviceId)\n }\n\n this._deviceId = deviceId\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audiooutput')\n }\n}\n","import { AudioSink, ScheduledAudio } from '@micdrop/client'\nimport { audioContext } from './audioContext'\n\n/**\n * Plays samples through the Web Audio graph, on the timeline given by the\n * caller so consecutive chunks are heard without a gap.\n */\nexport class WebAudioSink implements AudioSink {\n public readonly output: GainNode\n\n constructor() {\n this.output = audioContext.createGain()\n }\n\n get sampleRate(): number {\n return audioContext.sampleRate\n }\n\n get currentTime(): number {\n return audioContext.currentTime\n }\n\n schedule(\n samples: Float32Array,\n when: number,\n onEnded: () => void\n ): ScheduledAudio {\n const buffer = audioContext.createBuffer(\n 1,\n samples.length,\n audioContext.sampleRate\n )\n buffer.copyToChannel(samples as Float32Array<ArrayBuffer>, 0)\n\n const source = audioContext.createBufferSource()\n source.buffer = buffer\n source.connect(this.output)\n source.onended = onEnded\n source.start(when)\n\n let stopped = false\n return {\n stop() {\n if (stopped) return\n stopped = true\n source.onended = null\n try {\n source.stop()\n } catch {\n // Already finished\n }\n source.disconnect()\n },\n }\n }\n}\n"],"mappings":";AAAA,SAAS,KAAK,mBAAmB,eAAe;;;ACAhD,SAAS,iBAAgC;;;ACClC,SAAS,OAAOA,eAAkC;AACvD,QAAM,SAAS,CAAC,cAAc,YAAY,aAAa,SAAS;AAEhE,QAAMC,UAAS,YAAY;AACzB,iBAAaD,aAAY;AACzB,QAAIA,cAAa,UAAU,aAAa;AACtC,YAAMA,cAAa,OAAO;AAAA,IAC5B;AAEA,WAAO,QAAQ,CAAC,MAAM,SAAS,KAAK,oBAAoB,GAAGC,OAAM,CAAC;AAAA,EACpE;AAGA,SAAO,QAAQ,CAAC,MAAM,SAAS,KAAK,iBAAiB,GAAGA,SAAQ,KAAK,CAAC;AACxE;AAIA,SAAS,aAAaD,eAAkC;AAEtD,QAAM,SAASA,c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uC;AAC3C,WAAO,YAAY,aAAa;AAAA,EAClC;AACF;;;APzFA,cAAc;AALP,SAAS,mBAA0C;AACxD,QAAM,SAAS,QAAQ,UAAU;AACjC,SAAO,kBAAkB,aAAa,OAAO,SAAS;AACxD;AAaO,SAAS,mBAAmB;AACjC,MAAI,UAAU,IAAI,OAAO,CAAC;AAC1B,UAAQ,UAAU,IAAI,WAAW,CAAC;AAClC,oBAAkB,YAAY;AAChC;AAEA,iBAAiB;","names":["audioContext","unlock"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/audio/WebMic.ts","../src/audio/unlock.ts","../src/audio/audioContext.ts","raw:./pcm-processor-worklet","../src/audio/pcm-processor.ts","../src/audio/stopStream.ts","../src/audio/WebSpeaker.ts","../src/audio/WebAudioSink.ts"],"sourcesContent":["import { Mic, setMicdropStorage, Speaker } from '@micdrop/client'\nimport { WebMic } from './audio/WebMic'\nimport { WebSpeaker } from './audio/WebSpeaker'\n\n/**\n * The node the assistant voice comes out of, to analyse or process it.\n *\n * Connect an AnalyserNode to it to read the envelope on your own frames, which\n * is finer than the ten levels a second the `Volume` event reports.\n * @returns The node, once the call has started\n */\nexport function getSpeakerOutput(): AudioNode | undefined {\n const driver = Speaker.getDriver()\n return driver instanceof WebSpeaker ? driver.output : undefined\n}\n\nexport * from '@micdrop/client'\nexport * from './audio/audioContext'\nexport * from './audio/WebAudioSink'\nexport * from './audio/WebMic'\nexport * from './audio/WebSpeaker'\n\n/**\n * Records and plays with the Web Audio API, and keeps settings in\n * `localStorage`. Called when the package is imported, so a page has nothing\n * to wire.\n */\nexport function registerWebAudio() {\n Mic.setDriver(new WebMic())\n Speaker.setDriver(new WebSpeaker())\n setMicdropStorage(localStorage)\n}\n\nregisterWebAudio()\n","import { MicDriver, MicdropDevice } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { initPcmProcessor } from './pcm-processor'\nimport { stopStream } from './stopStream'\n\n/**\n * How long Firefox needs to let go of a microphone it released.\n *\n * A microphone requested again before that comes back without its processing:\n * no echo cancellation, no noise suppression and a raw gain some 30 dB higher,\n * while its settings still report them on. The room then sounds like speech,\n * and the assistant hears its own voice. No event tells when the device is\n * free, so a new stream waits for this delay to pass.\n */\nconst FIREFOX_RELEASE_DELAY = 15000 // ms\n\nconst isFirefox =\n typeof navigator !== 'undefined' && /Firefox\\//.test(navigator.userAgent)\n\n// The last stream released, outside the class: the browser holds the device\n// whichever WebMic opened it\nlet lastRelease: { at: number; deviceId: string | undefined } | undefined\n\n/**\n * Records the microphone with the Web Audio API.\n *\n * An audio worklet delivers the samples at a steady pace, and everything that\n * happens to them afterwards lives in `@micdrop/client`.\n */\nexport class WebMic extends MicDriver {\n private stream: MediaStream | undefined\n private source: MediaStreamAudioSourceNode | undefined\n private worklet: AudioWorkletNode | undefined\n private _deviceId: string | undefined\n\n get isStarted(): boolean {\n return !!this.stream\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n // Asking for echo cancellation is not getting it: some browsers and devices\n // do without, and the settings of the track say so\n get echoCancellation(): boolean | undefined {\n const track = this.stream?.getAudioTracks()[0]\n const setting = track?.getSettings?.().echoCancellation as unknown\n if (setting === undefined) return undefined\n return setting !== false\n }\n\n async start(deviceId?: string): Promise<void> {\n if (this.stream) {\n // Same device, keep recording\n if (this._deviceId === deviceId) return\n await this.stop()\n }\n\n await waitForRelease(deviceId)\n\n this.stream = await navigator.mediaDevices.getUserMedia({\n audio: {\n deviceId: { ideal: deviceId },\n sampleRate: 16000, // not working, it will follow device settings, usually 44.1kHz\n sampleSize: 16,\n channelCount: 1,\n echoCancellation: true,\n noiseSuppression: true,\n },\n })\n\n // Remember which device the browser settled on\n const track = this.stream.getTracks()[0]\n const settled = track?.getCapabilities?.().deviceId\n this._deviceId =\n settled && settled !== 'default' ? settled : (deviceId ?? settled)\n\n await initPcmProcessor()\n this.worklet = new AudioWorkletNode(audioContext, 'pcm-processor')\n this.worklet.port.onmessage = this.onWorkletMessage\n\n this.source = audioContext.createMediaStreamSource(this.stream)\n this.source.connect(this.worklet)\n\n // A browser only reveals the device labels once a stream is running, and\n // Firefox lists nothing at all before that\n navigator.mediaDevices.addEventListener('devicechange', this.onDeviceChange)\n }\n\n async stop(): Promise<void> {\n navigator.mediaDevices.removeEventListener(\n 'devicechange',\n this.onDeviceChange\n )\n if (this.worklet) {\n this.worklet.port.onmessage = null\n this.worklet.disconnect()\n this.worklet = undefined\n }\n if (this.source) {\n this.source.disconnect()\n this.source = undefined\n }\n if (this.stream) {\n stopStream(this.stream)\n this.stream = undefined\n lastRelease = { at: Date.now(), deviceId: this._deviceId }\n }\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audioinput')\n }\n\n private onWorkletMessage = (event: MessageEvent) => {\n const { type, frames, sampleRate } = event.data\n if (type !== 'frames') return\n this.emit('Frames', frames as Float32Array, sampleRate as number)\n }\n\n private onDeviceChange = () => {\n this.emit('DeviceChange')\n }\n}\n\n/**\n * Waits until Firefox let go of the microphone, see FIREFOX_RELEASE_DELAY\n * @param deviceId - The device about to be opened\n */\nasync function waitForRelease(deviceId?: string) {\n if (!isFirefox || !lastRelease) return\n if (deviceId && lastRelease.deviceId && deviceId !== lastRelease.deviceId) {\n return\n }\n const remaining = lastRelease.at + FIREFOX_RELEASE_DELAY - Date.now()\n if (remaining > 0) {\n await new Promise((resolve) => setTimeout(resolve, remaining))\n }\n}\n\n/**\n * Lists the devices of one kind, the default one first\n * @param kind - Which devices to list\n */\nexport async function listDevices(\n kind: 'audioinput' | 'audiooutput'\n): Promise<MicdropDevice[]> {\n const all = await navigator.mediaDevices.enumerateDevices()\n if (all.length === 0) return []\n\n // A browser lists the default device twice, once under the id \"default\" and\n // once under its real id. Keep the real one, and put it first.\n const defaults = all.filter((device) => device.deviceId === 'default')\n const devices = all.filter(\n (device) => device.deviceId !== 'default' && device.kind === kind\n )\n for (const fallback of defaults) {\n const index = devices.findIndex(\n (device) => device.groupId === fallback.groupId\n )\n if (index > 0) devices.unshift(...devices.splice(index, 1))\n }\n\n return devices.map((device) => ({\n id: device.deviceId,\n label: device.label || (kind === 'audioinput' ? 'Microphone' : 'Speaker'),\n kind,\n }))\n}\n","// Inspired https://www.mattmontag.com/web/unlock-web-audio-in-safari-for-ios-and-macos\nexport function unlock(audioContext: AudioContext): void {\n const events = ['touchstart', 'touchend', 'mousedown', 'keydown']\n\n const unlock = async () => {\n unlockSafari(audioContext)\n if (audioContext.state === 'suspended') {\n await audioContext.resume()\n }\n // Clean events\n events.forEach((e) => document.body.removeEventListener(e, unlock))\n }\n\n // Add events\n events.forEach((e) => document.body.addEventListener(e, unlock, false))\n}\n\n// safari/ios hack - inspired by https://codepen.io/kslstn/pen/pagLqL\n// unlock audioContext - call when interactive (button click)\nfunction unlockSafari(audioContext: AudioContext): void {\n // create empty buffer and play it to unlock audioContext when interactive\n const source = audioContext.createBufferSource()\n source.buffer = audioContext.createBuffer(1, 1, 22050)\n source.connect(audioContext.destination)\n const onEnded = () => {\n source.disconnect()\n source.buffer = null\n source.removeEventListener('ended', onEnded)\n }\n source.addEventListener('ended', onEnded)\n source.start()\n}\n","import { unlock } from './unlock'\n\nexport let audioContext: AudioContext\n\nexport function createAudioContext() {\n return new (window.AudioContext || (window as any).webkitAudioContext)()\n}\n\ndeclare global {\n interface Window {\n micdropAudioContext: AudioContext\n }\n}\n\nif (window.micdropAudioContext) {\n audioContext = window.micdropAudioContext\n} else {\n window.micdropAudioContext = audioContext = createAudioContext()\n\n // Hack to unlock audio context on some browsers\n unlock(audioContext)\n}\n","// Hands the microphone signal to the main thread, continuously.\n//\n// Deciding whether someone is speaking, keeping a reserve of audio and cutting\n// it into chunks all happen in @micdrop/client, so this only has to deliver the\n// samples at a steady pace.\n\nconst INTERVAL_MS = 20\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.buffers = []\n this.length = 0\n // `sampleRate` is provided by the worklet scope\n this.frameLength = Math.round((sampleRate * INTERVAL_MS) / 1000)\n }\n\n process(inputs) {\n const input = inputs[0]\n if (!input || input.length === 0) return true\n\n this.buffers.push(new Float32Array(input[0]))\n this.length += input[0].length\n if (this.length < this.frameLength) return true\n\n const frames = new Float32Array(this.length)\n let offset = 0\n for (const buffer of this.buffers) {\n frames.set(buffer, offset)\n offset += buffer.length\n }\n this.buffers.length = 0\n this.length = 0\n\n this.port.postMessage({ type: 'frames', frames, sampleRate }, [\n frames.buffer,\n ])\n return true\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n","import pcmProcessorWorklet from './pcm-processor-worklet?raw'\nimport { audioContext } from './audioContext'\n\ndeclare global {\n interface Window {\n micdropPcmProcessor: boolean\n }\n}\n\nexport async function initPcmProcessor() {\n if (window.micdropPcmProcessor) return\n window.micdropPcmProcessor = true\n\n // Init worklet URL\n const workletBlob = new Blob([pcmProcessorWorklet], {\n type: 'application/javascript',\n })\n const workletUrl = URL.createObjectURL(workletBlob)\n\n // Load the worklet module\n await audioContext.audioWorklet.addModule(workletUrl)\n}\n","/**\n * Stops a stream\n * @param stream - The stream to stop\n */\nexport function stopStream(stream?: MediaStream) {\n stream?.getTracks().forEach((track) => track.stop())\n}\n","import { MicdropDevice, Pcm16AudioStream, SpeakerDriver } from '@micdrop/client'\nimport { audioContext } from './audioContext'\nimport { listDevices } from './WebMic'\nimport { WebAudioSink } from './WebAudioSink'\n\n// A Bluetooth headset switches to its call profile as its microphone opens,\n// and Firefox mangles what it plays through that switch: the first answer of a\n// call comes out all at once. The speaker starts right after the microphone, so\n// it plays silence that long and holds the first answer until then.\nconst FIREFOX_WARM_UP = 2000 // ms\n\n/**\n * Plays the assistant voice with the Web Audio API.\n */\nexport class WebSpeaker extends SpeakerDriver {\n private sink: WebAudioSink | undefined\n private stream: Pcm16AudioStream | undefined\n private _deviceId: string | undefined\n // Firefox cannot pick an output on an audio context, so a non default device\n // is played back through an audio element instead\n private element: HTMLAudioElement | undefined\n\n get isPlaying(): boolean {\n return this.stream?.isPlaying ?? false\n }\n\n get deviceId(): string | undefined {\n return this._deviceId\n }\n\n /** The node the sound comes out of, to analyse or process it */\n get output(): AudioNode | undefined {\n return this.sink?.output\n }\n\n async start(): Promise<void> {\n if (this.stream) return\n\n this.sink = new WebAudioSink()\n this.stream = new Pcm16AudioStream(this.sink)\n if (navigator.userAgent.includes('Firefox')) {\n this.stream.warmUp(FIREFOX_WARM_UP)\n }\n this.stream.on('StartPlaying', () => this.emit('StartPlaying'))\n this.stream.on('StopPlaying', () => this.emit('StopPlaying'))\n this.stream.on('Volume', (volume) => this.emit('Volume', volume))\n\n this.sink.output.connect(audioContext.destination)\n }\n\n play(pcm: Int16Array, sampleRate: number): void {\n this.stream?.playAudio(pcm, sampleRate)\n }\n\n stopAudio(): void {\n this.stream?.stopAudio()\n }\n\n async stop(): Promise<void> {\n this.stream?.destroy()\n this.stream = undefined\n this.sink?.output.disconnect()\n this.sink = undefined\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n }\n\n async changeDevice(deviceId: string): Promise<void> {\n const sink = this.sink\n if (!sink) {\n this._deviceId = deviceId\n return\n }\n\n const devices = await this.getDevices()\n const isDefault = devices.length === 0 || devices[0].id === deviceId\n\n if (isDefault || 'setSinkId' in audioContext) {\n // Playing through the audio context is the preferred way, it is the only\n // one that keeps echo cancellation working\n await (audioContext as any).setSinkId?.(isDefault ? '' : deviceId)\n sink.output.disconnect()\n sink.output.connect(audioContext.destination)\n\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n } else {\n // Firefox: stream the sound back into an audio element, which does accept\n // an output device\n if (!this.element) {\n const destination = audioContext.createMediaStreamDestination()\n sink.output.disconnect()\n sink.output.connect(destination)\n\n this.element = new Audio()\n this.element.autoplay = true\n this.element.srcObject = destination.stream\n }\n await this.element.setSinkId(deviceId)\n }\n\n this._deviceId = deviceId\n }\n\n async getDevices(): Promise<MicdropDevice[]> {\n return listDevices('audiooutput')\n }\n}\n","import { AudioSink, ScheduledAudio } from '@micdrop/client'\nimport { audioContext } from './audioContext'\n\n/**\n * Plays samples through the Web Audio graph, on the timeline given by the\n * caller so consecutive chunks are heard without a gap.\n */\nexport class WebAudioSink implements AudioSink {\n public readonly output: GainNode\n\n constructor() {\n this.output = audioContext.createGain()\n }\n\n get sampleRate(): number {\n return audioContext.sampleRate\n }\n\n get currentTime(): number {\n return audioContext.currentTime\n }\n\n schedule(\n samples: Float32Array,\n when: number,\n onEnded: () => void\n ): ScheduledAudio {\n const buffer = audioContext.createBuffer(\n 1,\n samples.length,\n audioContext.sampleRate\n )\n buffer.copyToChannel(samples as Float32Array<ArrayBuffer>, 0)\n\n const source = audioContext.createBufferSource()\n source.buffer = buffer\n source.connect(this.output)\n source.onended = onEnded\n source.start(when)\n\n let stopped = false\n return {\n stop() {\n if (stopped) return\n stopped = true\n source.onended = null\n try {\n source.stop()\n } catch {\n // Already finished\n }\n source.disconnect()\n },\n }\n }\n}\n"],"mappings":";AAAA,SAAS,KAAK,mBAAmB,eAAe;;;ACAhD,SAAS,iBAAgC;;;ACClC,SAAS,OAAOA,eAAkC;AACvD,QAAM,SAAS,CAAC,cAAc,YAAY,aAAa,SAAS;AAEhE,QAAMC,UAAS,YAAY;AACzB,iBAAaD,aAAY;AACzB,QAAIA,cAAa,UAAU,aAAa;AACtC,YAAMA,cAAa,OAAO;AAAA,IAC5B;AAEA,WAAO,QAAQ,CAAC,MAAM,SAAS,KAAK,oBAAoB,GAAGC,OAAM,CAAC;AAAA,EACpE;AAGA,SAAO,QAAQ,CAAC,MAAM,SAAS,KAAK,iBAAiB,GAAGA,SAAQ,KAAK,CAAC;AACxE;AAIA,SAAS,aAAaD,eAAkC;AAEtD,QAAM,SAASA,cAAa,mBAAmB;AAC/C,SAAO,SAASA,cAAa,aAAa,GAAG,GAAG,KAAK;AACrD,SAAO,QAAQA,cAAa,WAAW;AACvC,QAAM,UAAU,MAAM;AACpB,WAAO,WAAW;AAClB,WAAO,SAAS;AAChB,WAAO,oBAAoB,SAAS,OAAO;AAA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AACjC,SAAO,kBAAkB,aAAa,OAAO,SAAS;AACxD;AAaO,SAAS,mBAAmB;AACjC,MAAI,UAAU,IAAI,OAAO,CAAC;AAC1B,UAAQ,UAAU,IAAI,WAAW,CAAC;AAClC,oBAAkB,YAAY;AAChC;AAEA,iBAAiB;","names":["audioContext","unlock"]}
|
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.micd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AAC,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"],"mapp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AK,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,21 @@
|
|
|
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
|
+
* @example
|
|
15
|
+
* import { micdrop } from '@micdrop/web/vite'
|
|
16
|
+
*
|
|
17
|
+
* export default defineConfig({ plugins: [micdrop()] })
|
|
18
|
+
*/
|
|
19
|
+
declare function micdrop(): Plugin;
|
|
20
|
+
|
|
21
|
+
export { micdrop };
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
* @example
|
|
15
|
+
* import { micdrop } from '@micdrop/web/vite'
|
|
16
|
+
*
|
|
17
|
+
* export default defineConfig({ plugins: [micdrop()] })
|
|
18
|
+
*/
|
|
19
|
+
declare function micdrop(): Plugin;
|
|
20
|
+
|
|
21
|
+
export { micdrop };
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/vite.ts
|
|
21
|
+
var vite_exports = {};
|
|
22
|
+
__export(vite_exports, {
|
|
23
|
+
micdrop: () => micdrop
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(vite_exports);
|
|
26
|
+
|
|
27
|
+
// ../../node_modules/.pnpm/tsup@8.5.1_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js
|
|
28
|
+
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;
|
|
29
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
30
|
+
|
|
31
|
+
// src/vite.ts
|
|
32
|
+
var import_node_url = require("url");
|
|
33
|
+
var SILERO_ID = "@micdrop/web/silero";
|
|
34
|
+
var SMART_TURN_ID = "@micdrop/smart-turn/web";
|
|
35
|
+
var VIRTUAL_SILERO = "\0micdrop:silero";
|
|
36
|
+
var VIRTUAL_SMART_TURN = "\0micdrop:smart-turn";
|
|
37
|
+
function micdrop() {
|
|
38
|
+
const sileroModel = (0, import_node_url.fileURLToPath)(
|
|
39
|
+
new URL("../assets/silero_vad_v5.onnx", importMetaUrl)
|
|
40
|
+
).replace(/\\/g, "/");
|
|
41
|
+
return {
|
|
42
|
+
name: "micdrop",
|
|
43
|
+
enforce: "pre",
|
|
44
|
+
config() {
|
|
45
|
+
return {
|
|
46
|
+
optimizeDeps: {
|
|
47
|
+
// Pre-bundling moves the runtime away from its WebAssembly files,
|
|
48
|
+
// which it looks up relative to its own address
|
|
49
|
+
exclude: ["onnxruntime-web"]
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
// The app keeps importing `@micdrop/web/silero`, and gets a module that
|
|
54
|
+
// sets the bundled options before handing over the real one. Imports from
|
|
55
|
+
// dependencies and from the wrappers themselves go through untouched.
|
|
56
|
+
resolveId(id, importer) {
|
|
57
|
+
if (!importer || importer.startsWith("\0")) return null;
|
|
58
|
+
if (importer.includes("/node_modules/")) return null;
|
|
59
|
+
if (id === SILERO_ID) return VIRTUAL_SILERO;
|
|
60
|
+
if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN;
|
|
61
|
+
return null;
|
|
62
|
+
},
|
|
63
|
+
load(id) {
|
|
64
|
+
if (id === VIRTUAL_SILERO) {
|
|
65
|
+
return [
|
|
66
|
+
`import { setSileroOptions } from '${SILERO_ID}'`,
|
|
67
|
+
`import model from ${JSON.stringify(`${sileroModel}?url`)}`,
|
|
68
|
+
`setSileroOptions({ model, wasmPath: false })`,
|
|
69
|
+
`export * from '${SILERO_ID}'`
|
|
70
|
+
].join("\n");
|
|
71
|
+
}
|
|
72
|
+
if (id === VIRTUAL_SMART_TURN) {
|
|
73
|
+
return [
|
|
74
|
+
`import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,
|
|
75
|
+
`setSmartTurnOptions({ wasmPath: false })`,
|
|
76
|
+
`export * from '${SMART_TURN_ID}'`
|
|
77
|
+
].join("\n");
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
84
|
+
0 && (module.exports = {
|
|
85
|
+
micdrop
|
|
86
|
+
});
|
|
87
|
+
//# 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 { 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 * @example\n * import { micdrop } from '@micdrop/web/vite'\n *\n * export default defineConfig({ plugins: [micdrop()] })\n */\nexport function micdrop(): 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() {\n return {\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'],\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","// 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,sBAA8B;AAG9B,IAAM,YAAY;AAClB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAkBpB,SAAS,UAAkB;AAChC,QAAM,kBAAc;AAAA,IAClB,IAAI,IAAI,gCAAgC,aAAe;AAAA,EACzD,EAAE,QAAQ,OAAO,GAAG;AAEpB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,SAAS;AACP,aAAO;AAAA,QACL,cAAc;AAAA;AAAA;AAAA,UAGZ,SAAS,CAAC,iBAAiB;AAAA,QAC7B;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;","names":[]}
|
package/dist/vite.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import { fileURLToPath } from "url";
|
|
3
|
+
var SILERO_ID = "@micdrop/web/silero";
|
|
4
|
+
var SMART_TURN_ID = "@micdrop/smart-turn/web";
|
|
5
|
+
var VIRTUAL_SILERO = "\0micdrop:silero";
|
|
6
|
+
var VIRTUAL_SMART_TURN = "\0micdrop:smart-turn";
|
|
7
|
+
function micdrop() {
|
|
8
|
+
const sileroModel = fileURLToPath(
|
|
9
|
+
new URL("../assets/silero_vad_v5.onnx", import.meta.url)
|
|
10
|
+
).replace(/\\/g, "/");
|
|
11
|
+
return {
|
|
12
|
+
name: "micdrop",
|
|
13
|
+
enforce: "pre",
|
|
14
|
+
config() {
|
|
15
|
+
return {
|
|
16
|
+
optimizeDeps: {
|
|
17
|
+
// Pre-bundling moves the runtime away from its WebAssembly files,
|
|
18
|
+
// which it looks up relative to its own address
|
|
19
|
+
exclude: ["onnxruntime-web"]
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
// The app keeps importing `@micdrop/web/silero`, and gets a module that
|
|
24
|
+
// sets the bundled options before handing over the real one. Imports from
|
|
25
|
+
// dependencies and from the wrappers themselves go through untouched.
|
|
26
|
+
resolveId(id, importer) {
|
|
27
|
+
if (!importer || importer.startsWith("\0")) return null;
|
|
28
|
+
if (importer.includes("/node_modules/")) return null;
|
|
29
|
+
if (id === SILERO_ID) return VIRTUAL_SILERO;
|
|
30
|
+
if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN;
|
|
31
|
+
return null;
|
|
32
|
+
},
|
|
33
|
+
load(id) {
|
|
34
|
+
if (id === VIRTUAL_SILERO) {
|
|
35
|
+
return [
|
|
36
|
+
`import { setSileroOptions } from '${SILERO_ID}'`,
|
|
37
|
+
`import model from ${JSON.stringify(`${sileroModel}?url`)}`,
|
|
38
|
+
`setSileroOptions({ model, wasmPath: false })`,
|
|
39
|
+
`export * from '${SILERO_ID}'`
|
|
40
|
+
].join("\n");
|
|
41
|
+
}
|
|
42
|
+
if (id === VIRTUAL_SMART_TURN) {
|
|
43
|
+
return [
|
|
44
|
+
`import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,
|
|
45
|
+
`setSmartTurnOptions({ wasmPath: false })`,
|
|
46
|
+
`export * from '${SMART_TURN_ID}'`
|
|
47
|
+
].join("\n");
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
micdrop
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=vite.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vite.ts"],"sourcesContent":["import { 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 * @example\n * import { micdrop } from '@micdrop/web/vite'\n *\n * export default defineConfig({ plugins: [micdrop()] })\n */\nexport function micdrop(): 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() {\n return {\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'],\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"],"mappings":";AAAA,SAAS,qBAAqB;AAG9B,IAAM,YAAY;AAClB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAkBpB,SAAS,UAAkB;AAChC,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,SAAS;AACP,aAAO;AAAA,QACL,cAAc;AAAA;AAAA;AAAA,UAGZ,SAAS,CAAC,iBAAiB;AAAA,QAC7B;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;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@micdrop/web",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.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",
|
|
@@ -33,13 +34,23 @@
|
|
|
33
34
|
"conversation"
|
|
34
35
|
],
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@micdrop/client": "^3.
|
|
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
|
}
|