@micdrop/web 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,8 @@ export default defineConfig({
44
44
 
45
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
46
 
47
+ The runtime is the WebAssembly build, half the size of the full one, unless `@micdrop/smart-turn` is installed, which runs on the graphics card. `micdrop({ webgpu: true })` keeps the full build in any case.
48
+
47
49
  ## Usage
48
50
 
49
51
  ```ts
package/dist/index.d.mts CHANGED
@@ -55,6 +55,7 @@ declare class WebSpeaker extends SpeakerDriver {
55
55
  private stream;
56
56
  private _deviceId;
57
57
  private element;
58
+ private isEchoFallback;
58
59
  get isPlaying(): boolean;
59
60
  get deviceId(): string | undefined;
60
61
  /** The node the sound comes out of, to analyse or process it */
@@ -63,6 +64,12 @@ declare class WebSpeaker extends SpeakerDriver {
63
64
  play(pcm: Int16Array, sampleRate: number): void;
64
65
  stopAudio(): void;
65
66
  stop(): Promise<void>;
67
+ /**
68
+ * Moves the sound to an audio element, while it plays. The audio context
69
+ * keeps playing until the element is known to be allowed to.
70
+ */
71
+ useEchoFallback(): Promise<boolean>;
72
+ resetEchoFallback(): void;
66
73
  changeDevice(deviceId: string): Promise<void>;
67
74
  getDevices(): Promise<MicdropDevice[]>;
68
75
  }
package/dist/index.d.ts CHANGED
@@ -55,6 +55,7 @@ declare class WebSpeaker extends SpeakerDriver {
55
55
  private stream;
56
56
  private _deviceId;
57
57
  private element;
58
+ private isEchoFallback;
58
59
  get isPlaying(): boolean;
59
60
  get deviceId(): string | undefined;
60
61
  /** The node the sound comes out of, to analyse or process it */
@@ -63,6 +64,12 @@ declare class WebSpeaker extends SpeakerDriver {
63
64
  play(pcm: Int16Array, sampleRate: number): void;
64
65
  stopAudio(): void;
65
66
  stop(): Promise<void>;
67
+ /**
68
+ * Moves the sound to an audio element, while it plays. The audio context
69
+ * keeps playing until the element is known to be allowed to.
70
+ */
71
+ useEchoFallback(): Promise<boolean>;
72
+ resetEchoFallback(): void;
66
73
  changeDevice(deviceId: string): Promise<void>;
67
74
  getDevices(): Promise<MicdropDevice[]>;
68
75
  }
package/dist/index.js CHANGED
@@ -248,6 +248,10 @@ var WebAudioSink = class {
248
248
  // src/audio/WebSpeaker.ts
249
249
  var FIREFOX_WARM_UP = 2e3;
250
250
  var WebSpeaker = class extends import_client2.SpeakerDriver {
251
+ constructor() {
252
+ super(...arguments);
253
+ this.isEchoFallback = false;
254
+ }
251
255
  get isPlaying() {
252
256
  return this.stream?.isPlaying ?? false;
253
257
  }
@@ -281,12 +285,50 @@ var WebSpeaker = class extends import_client2.SpeakerDriver {
281
285
  this.stream = void 0;
282
286
  this.sink?.output.disconnect();
283
287
  this.sink = void 0;
288
+ this.isEchoFallback = false;
284
289
  if (this.element) {
285
290
  this.element.pause();
286
291
  this.element.srcObject = null;
287
292
  this.element = void 0;
288
293
  }
289
294
  }
295
+ /**
296
+ * Moves the sound to an audio element, while it plays. The audio context
297
+ * keeps playing until the element is known to be allowed to.
298
+ */
299
+ async useEchoFallback() {
300
+ const sink = this.sink;
301
+ if (!sink || this.element) return false;
302
+ const destination = audioContext.createMediaStreamDestination();
303
+ const element = new Audio();
304
+ element.srcObject = destination.stream;
305
+ try {
306
+ await element.play();
307
+ } catch (error) {
308
+ console.error("[Micdrop] Playing through an element failed", error);
309
+ element.srcObject = null;
310
+ return false;
311
+ }
312
+ if (this.sink !== sink || this.element) {
313
+ element.pause();
314
+ element.srcObject = null;
315
+ return false;
316
+ }
317
+ sink.output.disconnect(audioContext.destination);
318
+ sink.output.connect(destination);
319
+ this.element = element;
320
+ this.isEchoFallback = true;
321
+ return true;
322
+ }
323
+ resetEchoFallback() {
324
+ if (!this.isEchoFallback || !this.sink || !this.element) return;
325
+ this.isEchoFallback = false;
326
+ this.element.pause();
327
+ this.element.srcObject = null;
328
+ this.element = void 0;
329
+ this.sink.output.disconnect();
330
+ this.sink.output.connect(audioContext.destination);
331
+ }
290
332
  async changeDevice(deviceId) {
291
333
  const sink = this.sink;
292
334
  if (!sink) {
@@ -295,7 +337,7 @@ var WebSpeaker = class extends import_client2.SpeakerDriver {
295
337
  }
296
338
  const devices = await this.getDevices();
297
339
  const isDefault = devices.length === 0 || devices[0].id === deviceId;
298
- if (isDefault || "setSinkId" in audioContext) {
340
+ if (!this.isEchoFallback && (isDefault || "setSinkId" in audioContext)) {
299
341
  await audioContext.setSinkId?.(isDefault ? "" : deviceId);
300
342
  sink.output.disconnect();
301
343
  sink.output.connect(audioContext.destination);
@@ -313,7 +355,7 @@ var WebSpeaker = class extends import_client2.SpeakerDriver {
313
355
  this.element.autoplay = true;
314
356
  this.element.srcObject = destination.stream;
315
357
  }
316
- await this.element.setSinkId(deviceId);
358
+ await this.element.setSinkId?.(deviceId);
317
359
  }
318
360
  this._deviceId = deviceId;
319
361
  }
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// 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;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,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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"]}
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. iOS reports echo\n // cancellation on and still hears an audio context, but not an element.\n private element: HTMLAudioElement | undefined\n private isEchoFallback = false\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 this.isEchoFallback = false\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n }\n\n /**\n * Moves the sound to an audio element, while it plays. The audio context\n * keeps playing until the element is known to be allowed to.\n */\n async useEchoFallback(): Promise<boolean> {\n const sink = this.sink\n if (!sink || this.element) return false\n\n const destination = audioContext.createMediaStreamDestination()\n const element = new Audio()\n element.srcObject = destination.stream\n try {\n await element.play()\n } catch (error) {\n console.error('[Micdrop] Playing through an element failed', error)\n element.srcObject = null\n return false\n }\n\n // The call ended in the meantime\n if (this.sink !== sink || this.element) {\n element.pause()\n element.srcObject = null\n return false\n }\n sink.output.disconnect(audioContext.destination)\n sink.output.connect(destination)\n this.element = element\n this.isEchoFallback = true\n return true\n }\n\n resetEchoFallback(): void {\n if (!this.isEchoFallback || !this.sink || !this.element) return\n this.isEchoFallback = false\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n this.sink.output.disconnect()\n this.sink.output.connect(audioContext.destination)\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 (!this.isEchoFallback && (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,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,EAAE,OAAO;AAE5D,QAAI,CAAC,KAAK,mBAAmB,aAAa,eAAe,eAAe;AAGtE,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,YAAY,QAAQ;AAAA,IACzC;AAEA,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,MAAM,aAAuC;AAC3C,WAAO,YAAY,aAAa;AAAA,EAClC;AACF;;;AP9IA,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
@@ -216,6 +216,10 @@ var WebAudioSink = class {
216
216
  // src/audio/WebSpeaker.ts
217
217
  var FIREFOX_WARM_UP = 2e3;
218
218
  var WebSpeaker = class extends SpeakerDriver {
219
+ constructor() {
220
+ super(...arguments);
221
+ this.isEchoFallback = false;
222
+ }
219
223
  get isPlaying() {
220
224
  return this.stream?.isPlaying ?? false;
221
225
  }
@@ -249,12 +253,50 @@ var WebSpeaker = class extends SpeakerDriver {
249
253
  this.stream = void 0;
250
254
  this.sink?.output.disconnect();
251
255
  this.sink = void 0;
256
+ this.isEchoFallback = false;
252
257
  if (this.element) {
253
258
  this.element.pause();
254
259
  this.element.srcObject = null;
255
260
  this.element = void 0;
256
261
  }
257
262
  }
263
+ /**
264
+ * Moves the sound to an audio element, while it plays. The audio context
265
+ * keeps playing until the element is known to be allowed to.
266
+ */
267
+ async useEchoFallback() {
268
+ const sink = this.sink;
269
+ if (!sink || this.element) return false;
270
+ const destination = audioContext.createMediaStreamDestination();
271
+ const element = new Audio();
272
+ element.srcObject = destination.stream;
273
+ try {
274
+ await element.play();
275
+ } catch (error) {
276
+ console.error("[Micdrop] Playing through an element failed", error);
277
+ element.srcObject = null;
278
+ return false;
279
+ }
280
+ if (this.sink !== sink || this.element) {
281
+ element.pause();
282
+ element.srcObject = null;
283
+ return false;
284
+ }
285
+ sink.output.disconnect(audioContext.destination);
286
+ sink.output.connect(destination);
287
+ this.element = element;
288
+ this.isEchoFallback = true;
289
+ return true;
290
+ }
291
+ resetEchoFallback() {
292
+ if (!this.isEchoFallback || !this.sink || !this.element) return;
293
+ this.isEchoFallback = false;
294
+ this.element.pause();
295
+ this.element.srcObject = null;
296
+ this.element = void 0;
297
+ this.sink.output.disconnect();
298
+ this.sink.output.connect(audioContext.destination);
299
+ }
258
300
  async changeDevice(deviceId) {
259
301
  const sink = this.sink;
260
302
  if (!sink) {
@@ -263,7 +305,7 @@ var WebSpeaker = class extends SpeakerDriver {
263
305
  }
264
306
  const devices = await this.getDevices();
265
307
  const isDefault = devices.length === 0 || devices[0].id === deviceId;
266
- if (isDefault || "setSinkId" in audioContext) {
308
+ if (!this.isEchoFallback && (isDefault || "setSinkId" in audioContext)) {
267
309
  await audioContext.setSinkId?.(isDefault ? "" : deviceId);
268
310
  sink.output.disconnect();
269
311
  sink.output.connect(audioContext.destination);
@@ -281,7 +323,7 @@ var WebSpeaker = class extends SpeakerDriver {
281
323
  this.element.autoplay = true;
282
324
  this.element.srcObject = destination.stream;
283
325
  }
284
- await this.element.setSinkId(deviceId);
326
+ await this.element.setSinkId?.(deviceId);
285
327
  }
286
328
  this._deviceId = deviceId;
287
329
  }
@@ -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// 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;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;A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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. iOS reports echo\n // cancellation on and still hears an audio context, but not an element.\n private element: HTMLAudioElement | undefined\n private isEchoFallback = false\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 this.isEchoFallback = false\n if (this.element) {\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n }\n }\n\n /**\n * Moves the sound to an audio element, while it plays. The audio context\n * keeps playing until the element is known to be allowed to.\n */\n async useEchoFallback(): Promise<boolean> {\n const sink = this.sink\n if (!sink || this.element) return false\n\n const destination = audioContext.createMediaStreamDestination()\n const element = new Audio()\n element.srcObject = destination.stream\n try {\n await element.play()\n } catch (error) {\n console.error('[Micdrop] Playing through an element failed', error)\n element.srcObject = null\n return false\n }\n\n // The call ended in the meantime\n if (this.sink !== sink || this.element) {\n element.pause()\n element.srcObject = null\n return false\n }\n sink.output.disconnect(audioContext.destination)\n sink.output.connect(destination)\n this.element = element\n this.isEchoFallback = true\n return true\n }\n\n resetEchoFallback(): void {\n if (!this.isEchoFallback || !this.sink || !this.element) return\n this.isEchoFallback = false\n this.element.pause()\n this.element.srcObject = null\n this.element = undefined\n this.sink.output.disconnect()\n this.sink.output.connect(audioContext.destination)\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 (!this.isEchoFallback && (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;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AS,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"]}
package/dist/vite.d.mts CHANGED
@@ -11,11 +11,25 @@ import { Plugin } from 'vite';
11
11
  * `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where
12
12
  * `setSmartTurnOptions` points.
13
13
  *
14
+ * Silero only runs on WebAssembly, so the runtime is the WebAssembly build,
15
+ * half the size of the full one. Smart Turn runs on the graphics card when
16
+ * the browser offers it, so the full build is kept when `@micdrop/smart-turn`
17
+ * is installed. Every import of the runtime gets the same build, so an app
18
+ * never loads two of them.
19
+ *
14
20
  * @example
15
21
  * import { micdrop } from '@micdrop/web/vite'
16
22
  *
17
23
  * export default defineConfig({ plugins: [micdrop()] })
18
24
  */
19
- declare function micdrop(): Plugin;
25
+ declare function micdrop(options?: MicdropViteOptions): Plugin;
26
+ interface MicdropViteOptions {
27
+ /**
28
+ * Keeps the build of the ONNX runtime that runs on the graphics card, twice
29
+ * the size of the WebAssembly one. Only Smart Turn uses it, so it defaults
30
+ * to whether `@micdrop/smart-turn` is installed.
31
+ */
32
+ webgpu?: boolean;
33
+ }
20
34
 
21
- export { micdrop };
35
+ export { type MicdropViteOptions, micdrop };
package/dist/vite.d.ts CHANGED
@@ -11,11 +11,25 @@ import { Plugin } from 'vite';
11
11
  * `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where
12
12
  * `setSmartTurnOptions` points.
13
13
  *
14
+ * Silero only runs on WebAssembly, so the runtime is the WebAssembly build,
15
+ * half the size of the full one. Smart Turn runs on the graphics card when
16
+ * the browser offers it, so the full build is kept when `@micdrop/smart-turn`
17
+ * is installed. Every import of the runtime gets the same build, so an app
18
+ * never loads two of them.
19
+ *
14
20
  * @example
15
21
  * import { micdrop } from '@micdrop/web/vite'
16
22
  *
17
23
  * export default defineConfig({ plugins: [micdrop()] })
18
24
  */
19
- declare function micdrop(): Plugin;
25
+ declare function micdrop(options?: MicdropViteOptions): Plugin;
26
+ interface MicdropViteOptions {
27
+ /**
28
+ * Keeps the build of the ONNX runtime that runs on the graphics card, twice
29
+ * the size of the WebAssembly one. Only Smart Turn uses it, so it defaults
30
+ * to whether `@micdrop/smart-turn` is installed.
31
+ */
32
+ webgpu?: boolean;
33
+ }
20
34
 
21
- export { micdrop };
35
+ export { type MicdropViteOptions, micdrop };
package/dist/vite.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/vite.ts
@@ -24,29 +34,44 @@ __export(vite_exports, {
24
34
  });
25
35
  module.exports = __toCommonJS(vite_exports);
26
36
 
27
- // ../../node_modules/.pnpm/tsup@8.5.1_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js
37
+ // ../../node_modules/.pnpm/tsup@8.5.1_tsx@4.23.15_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js
28
38
  var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
29
39
  var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
30
40
 
31
41
  // src/vite.ts
42
+ var import_node_module = require("module");
43
+ var import_node_path = __toESM(require("path"));
44
+ var import_node_process = __toESM(require("process"));
32
45
  var import_node_url = require("url");
33
46
  var SILERO_ID = "@micdrop/web/silero";
34
47
  var SMART_TURN_ID = "@micdrop/smart-turn/web";
35
48
  var VIRTUAL_SILERO = "\0micdrop:silero";
36
49
  var VIRTUAL_SMART_TURN = "\0micdrop:smart-turn";
37
- function micdrop() {
50
+ function micdrop(options = {}) {
38
51
  const sileroModel = (0, import_node_url.fileURLToPath)(
39
52
  new URL("../assets/silero_vad_v5.onnx", importMetaUrl)
40
53
  ).replace(/\\/g, "/");
41
54
  return {
42
55
  name: "micdrop",
43
56
  enforce: "pre",
44
- config() {
57
+ config(config) {
58
+ const webgpu = options.webgpu ?? isInstalled(
59
+ "@micdrop/smart-turn",
60
+ import_node_path.default.resolve(config.root ?? import_node_process.default.cwd())
61
+ );
45
62
  return {
63
+ resolve: {
64
+ alias: webgpu ? [] : [
65
+ {
66
+ find: /^onnxruntime-web$/,
67
+ replacement: "onnxruntime-web/wasm"
68
+ }
69
+ ]
70
+ },
46
71
  optimizeDeps: {
47
72
  // Pre-bundling moves the runtime away from its WebAssembly files,
48
73
  // which it looks up relative to its own address
49
- exclude: ["onnxruntime-web"]
74
+ exclude: ["onnxruntime-web", "onnxruntime-web/wasm"]
50
75
  }
51
76
  };
52
77
  },
@@ -80,6 +105,14 @@ function micdrop() {
80
105
  }
81
106
  };
82
107
  }
108
+ function isInstalled(name, root) {
109
+ try {
110
+ (0, import_node_module.createRequire)(import_node_path.default.join(root, "package.json")).resolve(name);
111
+ return true;
112
+ } catch {
113
+ return false;
114
+ }
115
+ }
83
116
  // Annotate the CommonJS export names for ESM import in node:
84
117
  0 && (module.exports = {
85
118
  micdrop
package/dist/vite.js.map CHANGED
@@ -1 +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":[]}
1
+ {"version":3,"sources":["../src/vite.ts","../../../node_modules/.pnpm/tsup@8.5.1_tsx@4.23.15_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["import { createRequire } from 'node:module'\nimport path from 'node:path'\nimport process from 'node:process'\nimport { fileURLToPath } from 'node:url'\nimport type { Plugin } from 'vite'\n\nconst SILERO_ID = '@micdrop/web/silero'\nconst SMART_TURN_ID = '@micdrop/smart-turn/web'\nconst VIRTUAL_SILERO = '\\0micdrop:silero'\nconst VIRTUAL_SMART_TURN = '\\0micdrop:smart-turn'\n\n/**\n * Bundles the files the voice models load at runtime, so a call never depends\n * on a public CDN being reachable.\n *\n * Without it, `@micdrop/web/silero` fetches the Silero model from jsDelivr and\n * the ONNX runtime from unpkg, which an outage, a firewall or an ad blocker\n * can keep from loading. With it, the model ships with the app and the ONNX\n * runtime finds its WebAssembly next to itself, both emitted by Vite.\n * `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where\n * `setSmartTurnOptions` points.\n *\n * Silero only runs on WebAssembly, so the runtime is the WebAssembly build,\n * half the size of the full one. Smart Turn runs on the graphics card when\n * the browser offers it, so the full build is kept when `@micdrop/smart-turn`\n * is installed. Every import of the runtime gets the same build, so an app\n * never loads two of them.\n *\n * @example\n * import { micdrop } from '@micdrop/web/vite'\n *\n * export default defineConfig({ plugins: [micdrop()] })\n */\nexport function micdrop(options: MicdropViteOptions = {}): Plugin {\n const sileroModel = fileURLToPath(\n new URL('../assets/silero_vad_v5.onnx', import.meta.url)\n ).replace(/\\\\/g, '/')\n\n return {\n name: 'micdrop',\n enforce: 'pre',\n\n config(config) {\n const webgpu =\n options.webgpu ??\n isInstalled(\n '@micdrop/smart-turn',\n path.resolve(config.root ?? process.cwd())\n )\n return {\n resolve: {\n alias: webgpu\n ? []\n : [\n {\n find: /^onnxruntime-web$/,\n replacement: 'onnxruntime-web/wasm',\n },\n ],\n },\n optimizeDeps: {\n // Pre-bundling moves the runtime away from its WebAssembly files,\n // which it looks up relative to its own address\n exclude: ['onnxruntime-web', 'onnxruntime-web/wasm'],\n },\n }\n },\n\n // The app keeps importing `@micdrop/web/silero`, and gets a module that\n // sets the bundled options before handing over the real one. Imports from\n // dependencies and from the wrappers themselves go through untouched.\n resolveId(id, importer) {\n if (!importer || importer.startsWith('\\0')) return null\n if (importer.includes('/node_modules/')) return null\n if (id === SILERO_ID) return VIRTUAL_SILERO\n if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN\n return null\n },\n\n load(id) {\n if (id === VIRTUAL_SILERO) {\n return [\n `import { setSileroOptions } from '${SILERO_ID}'`,\n `import model from ${JSON.stringify(`${sileroModel}?url`)}`,\n `setSileroOptions({ model, wasmPath: false })`,\n `export * from '${SILERO_ID}'`,\n ].join('\\n')\n }\n if (id === VIRTUAL_SMART_TURN) {\n return [\n `import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,\n `setSmartTurnOptions({ wasmPath: false })`,\n `export * from '${SMART_TURN_ID}'`,\n ].join('\\n')\n }\n return null\n },\n }\n}\n\nexport interface MicdropViteOptions {\n /**\n * Keeps the build of the ONNX runtime that runs on the graphics card, twice\n * the size of the WebAssembly one. Only Smart Turn uses it, so it defaults\n * to whether `@micdrop/smart-turn` is installed.\n */\n webgpu?: boolean\n}\n\n/** Whether the app at `root` can import the package */\nfunction isInstalled(name: string, root: string): boolean {\n try {\n createRequire(path.join(root, 'package.json')).resolve(name)\n return true\n } catch {\n return false\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () => \n typeof document === \"undefined\" \n ? new URL(`file:${__filename}`).href \n : (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') \n ? document.currentScript.src \n : new URL(\"main.js\", document.baseURI).href;\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,QAAQ,YAAY,MAAM,WAC1E,SAAS,cAAc,MACvB,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEtC,IAAM,gBAAgC,iCAAiB;;;ADZ9D,yBAA8B;AAC9B,uBAAiB;AACjB,0BAAoB;AACpB,sBAA8B;AAG9B,IAAM,YAAY;AAClB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAwBpB,SAAS,QAAQ,UAA8B,CAAC,GAAW;AAChE,QAAM,kBAA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,cAAc,CAAC,EAAE,QAAQ,IAAI;AAC3D,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":["path","process"]}
package/dist/vite.mjs CHANGED
@@ -1,22 +1,37 @@
1
1
  // src/vite.ts
2
+ import { createRequire } from "module";
3
+ import path from "path";
4
+ import process from "process";
2
5
  import { fileURLToPath } from "url";
3
6
  var SILERO_ID = "@micdrop/web/silero";
4
7
  var SMART_TURN_ID = "@micdrop/smart-turn/web";
5
8
  var VIRTUAL_SILERO = "\0micdrop:silero";
6
9
  var VIRTUAL_SMART_TURN = "\0micdrop:smart-turn";
7
- function micdrop() {
10
+ function micdrop(options = {}) {
8
11
  const sileroModel = fileURLToPath(
9
12
  new URL("../assets/silero_vad_v5.onnx", import.meta.url)
10
13
  ).replace(/\\/g, "/");
11
14
  return {
12
15
  name: "micdrop",
13
16
  enforce: "pre",
14
- config() {
17
+ config(config) {
18
+ const webgpu = options.webgpu ?? isInstalled(
19
+ "@micdrop/smart-turn",
20
+ path.resolve(config.root ?? process.cwd())
21
+ );
15
22
  return {
23
+ resolve: {
24
+ alias: webgpu ? [] : [
25
+ {
26
+ find: /^onnxruntime-web$/,
27
+ replacement: "onnxruntime-web/wasm"
28
+ }
29
+ ]
30
+ },
16
31
  optimizeDeps: {
17
32
  // Pre-bundling moves the runtime away from its WebAssembly files,
18
33
  // which it looks up relative to its own address
19
- exclude: ["onnxruntime-web"]
34
+ exclude: ["onnxruntime-web", "onnxruntime-web/wasm"]
20
35
  }
21
36
  };
22
37
  },
@@ -50,6 +65,14 @@ function micdrop() {
50
65
  }
51
66
  };
52
67
  }
68
+ function isInstalled(name, root) {
69
+ try {
70
+ createRequire(path.join(root, "package.json")).resolve(name);
71
+ return true;
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
53
76
  export {
54
77
  micdrop
55
78
  };
package/dist/vite.mjs.map CHANGED
@@ -1 +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":[]}
1
+ {"version":3,"sources":["../src/vite.ts"],"sourcesContent":["import { createRequire } from 'node:module'\nimport path from 'node:path'\nimport process from 'node:process'\nimport { fileURLToPath } from 'node:url'\nimport type { Plugin } from 'vite'\n\nconst SILERO_ID = '@micdrop/web/silero'\nconst SMART_TURN_ID = '@micdrop/smart-turn/web'\nconst VIRTUAL_SILERO = '\\0micdrop:silero'\nconst VIRTUAL_SMART_TURN = '\\0micdrop:smart-turn'\n\n/**\n * Bundles the files the voice models load at runtime, so a call never depends\n * on a public CDN being reachable.\n *\n * Without it, `@micdrop/web/silero` fetches the Silero model from jsDelivr and\n * the ONNX runtime from unpkg, which an outage, a firewall or an ad blocker\n * can keep from loading. With it, the model ships with the app and the ONNX\n * runtime finds its WebAssembly next to itself, both emitted by Vite.\n * `@micdrop/smart-turn/web` gets the same runtime, its checkpoint stays where\n * `setSmartTurnOptions` points.\n *\n * Silero only runs on WebAssembly, so the runtime is the WebAssembly build,\n * half the size of the full one. Smart Turn runs on the graphics card when\n * the browser offers it, so the full build is kept when `@micdrop/smart-turn`\n * is installed. Every import of the runtime gets the same build, so an app\n * never loads two of them.\n *\n * @example\n * import { micdrop } from '@micdrop/web/vite'\n *\n * export default defineConfig({ plugins: [micdrop()] })\n */\nexport function micdrop(options: MicdropViteOptions = {}): Plugin {\n const sileroModel = fileURLToPath(\n new URL('../assets/silero_vad_v5.onnx', import.meta.url)\n ).replace(/\\\\/g, '/')\n\n return {\n name: 'micdrop',\n enforce: 'pre',\n\n config(config) {\n const webgpu =\n options.webgpu ??\n isInstalled(\n '@micdrop/smart-turn',\n path.resolve(config.root ?? process.cwd())\n )\n return {\n resolve: {\n alias: webgpu\n ? []\n : [\n {\n find: /^onnxruntime-web$/,\n replacement: 'onnxruntime-web/wasm',\n },\n ],\n },\n optimizeDeps: {\n // Pre-bundling moves the runtime away from its WebAssembly files,\n // which it looks up relative to its own address\n exclude: ['onnxruntime-web', 'onnxruntime-web/wasm'],\n },\n }\n },\n\n // The app keeps importing `@micdrop/web/silero`, and gets a module that\n // sets the bundled options before handing over the real one. Imports from\n // dependencies and from the wrappers themselves go through untouched.\n resolveId(id, importer) {\n if (!importer || importer.startsWith('\\0')) return null\n if (importer.includes('/node_modules/')) return null\n if (id === SILERO_ID) return VIRTUAL_SILERO\n if (id === SMART_TURN_ID) return VIRTUAL_SMART_TURN\n return null\n },\n\n load(id) {\n if (id === VIRTUAL_SILERO) {\n return [\n `import { setSileroOptions } from '${SILERO_ID}'`,\n `import model from ${JSON.stringify(`${sileroModel}?url`)}`,\n `setSileroOptions({ model, wasmPath: false })`,\n `export * from '${SILERO_ID}'`,\n ].join('\\n')\n }\n if (id === VIRTUAL_SMART_TURN) {\n return [\n `import { setSmartTurnOptions } from '${SMART_TURN_ID}'`,\n `setSmartTurnOptions({ wasmPath: false })`,\n `export * from '${SMART_TURN_ID}'`,\n ].join('\\n')\n }\n return null\n },\n }\n}\n\nexport interface MicdropViteOptions {\n /**\n * Keeps the build of the ONNX runtime that runs on the graphics card, twice\n * the size of the WebAssembly one. Only Smart Turn uses it, so it defaults\n * to whether `@micdrop/smart-turn` is installed.\n */\n webgpu?: boolean\n}\n\n/** Whether the app at `root` can import the package */\nfunction isInstalled(name: string, root: string): boolean {\n try {\n createRequire(path.join(root, 'package.json')).resolve(name)\n return true\n } catch {\n return false\n }\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAC9B,OAAO,UAAU;AACjB,OAAO,aAAa;AACpB,SAAS,qBAAqB;AAG9B,IAAM,YAAY;AAClB,IAAM,gBAAgB;AACtB,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAwBpB,SAAS,QAAQ,UAA8B,CAAC,GAAW;AAChE,QAAM,cAAc;AAAA,IAClB,IAAI,IAAI,gCAAgC,YAAY,GAAG;AAAA,EACzD,EAAE,QAAQ,OAAO,GAAG;AAEpB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,OAAO,QAAQ;AACb,YAAM,SACJ,QAAQ,UACR;AAAA,QACE;AAAA,QACA,KAAK,QAAQ,OAAO,QAAQ,QAAQ,IAAI,CAAC;AAAA,MAC3C;AACF,aAAO;AAAA,QACL,SAAS;AAAA,UACP,OAAO,SACH,CAAC,IACD;AAAA,YACE;AAAA,cACE,MAAM;AAAA,cACN,aAAa;AAAA,YACf;AAAA,UACF;AAAA,QACN;AAAA,QACA,cAAc;AAAA;AAAA;AAAA,UAGZ,SAAS,CAAC,mBAAmB,sBAAsB;AAAA,QACrD;AAAA,MACF;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAKA,UAAU,IAAI,UAAU;AACtB,UAAI,CAAC,YAAY,SAAS,WAAW,IAAI,EAAG,QAAO;AACnD,UAAI,SAAS,SAAS,gBAAgB,EAAG,QAAO;AAChD,UAAI,OAAO,UAAW,QAAO;AAC7B,UAAI,OAAO,cAAe,QAAO;AACjC,aAAO;AAAA,IACT;AAAA,IAEA,KAAK,IAAI;AACP,UAAI,OAAO,gBAAgB;AACzB,eAAO;AAAA,UACL,qCAAqC,SAAS;AAAA,UAC9C,qBAAqB,KAAK,UAAU,GAAG,WAAW,MAAM,CAAC;AAAA,UACzD;AAAA,UACA,kBAAkB,SAAS;AAAA,QAC7B,EAAE,KAAK,IAAI;AAAA,MACb;AACA,UAAI,OAAO,oBAAoB;AAC7B,eAAO;AAAA,UACL,wCAAwC,aAAa;AAAA,UACrD;AAAA,UACA,kBAAkB,aAAa;AAAA,QACjC,EAAE,KAAK,IAAI;AAAA,MACb;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAYA,SAAS,YAAY,MAAc,MAAuB;AACxD,MAAI;AACF,kBAAc,KAAK,KAAK,MAAM,cAAc,CAAC,EAAE,QAAQ,IAAI;AAC3D,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micdrop/web",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "🖐️🎤 Micdrop: real-time voice conversations with AI, in the browser",
5
5
  "author": "Godefroy de Compreignac",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@micdrop/client": "^3.4.0",
38
- "onnxruntime-web": "^1.23.2"
38
+ "onnxruntime-web": "^1.31.1"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "vite": ">=5.0.0"