@micdrop/client 1.6.0 → 1.7.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/dist/index.js CHANGED
@@ -38,6 +38,7 @@ __export(index_exports, {
38
38
  LocalStorageKeys: () => LocalStorageKeys,
39
39
  Mic: () => mic,
40
40
  MicRecorder: () => MicRecorder,
41
+ MultipleVAD: () => MultipleVAD,
41
42
  SileroVAD: () => SileroVAD,
42
43
  Speaker: () => speaker,
43
44
  VAD: () => VAD,
@@ -121,7 +122,8 @@ if (window.micdropAudioContext) {
121
122
  var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
122
123
  LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
123
124
  LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
124
- LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
125
+ LocalStorageKeys2["VolumeVADOptions"] = "micdrop.VolumeVADOptions";
126
+ LocalStorageKeys2["SileroVADOptions"] = "micdrop.SileroVADOptions";
125
127
  return LocalStorageKeys2;
126
128
  })(LocalStorageKeys || {});
127
129
 
@@ -132,10 +134,8 @@ function stopStream(stream) {
132
134
 
133
135
  // src/audio/mic.ts
134
136
  var mic;
135
- var defaultMicThreshold = -50;
136
137
  var Mic = class {
137
138
  constructor() {
138
- this.defaultThreshold = defaultMicThreshold;
139
139
  this.analyser = new AudioAnalyser(audioContext);
140
140
  }
141
141
  getMicConstraints(deviceId) {
@@ -219,38 +219,109 @@ function createDelayedStream(stream, delay) {
219
219
  return audioDestination.stream;
220
220
  }
221
221
 
222
- // src/audio/vad/SileroVAD.ts
223
- var import_src = require("@ricky0123/vad-web/src");
224
-
225
222
  // src/audio/vad/VAD.ts
226
223
  var import_eventemitter32 = __toESM(require("eventemitter3"));
227
224
  var VAD = class extends import_eventemitter32.default {
228
- /**
229
- * Constructor for the VAD class
230
- * @param threshold - The threshold to use for the VAD
231
- */
232
- constructor(threshold = 0) {
233
- super();
225
+ constructor() {
226
+ super(...arguments);
234
227
  // Max delay to detect speech, can be used to record delayed stream
235
228
  this.delay = 100;
236
- this.threshold = threshold;
229
+ }
230
+ };
231
+
232
+ // src/audio/vad/MultipleVAD.ts
233
+ var MultipleVAD = class extends VAD {
234
+ constructor(vads) {
235
+ super();
236
+ this.startSpeakingCount = 0;
237
+ this.confirmSpeakingCount = 0;
238
+ this.lastEventName = "StopSpeaking";
239
+ this.vads = vads.map((vad) => getVAD(vad));
240
+ }
241
+ emit(eventName, ..._) {
242
+ this.lastEventName = eventName;
243
+ return super.emit(eventName);
244
+ }
245
+ get isStarted() {
246
+ return this.vads.some((vad) => vad.isStarted);
247
+ }
248
+ async start(stream, threshold) {
249
+ for (const vad of this.vads) {
250
+ if (vad.isStarted) {
251
+ continue;
252
+ }
253
+ vad.on("StartSpeaking", () => {
254
+ this.startSpeakingCount++;
255
+ if ((this.lastEventName === "StopSpeaking" || this.lastEventName === "CancelSpeaking") && this.startSpeakingCount === this.vads.length) {
256
+ this.emit("StartSpeaking");
257
+ }
258
+ if (this.startSpeakingCount > this.vads.length) {
259
+ this.startSpeakingCount = 0;
260
+ }
261
+ });
262
+ vad.on("ConfirmSpeaking", () => {
263
+ this.confirmSpeakingCount++;
264
+ if (this.lastEventName === "StartSpeaking" && this.confirmSpeakingCount === this.vads.length) {
265
+ this.emit("ConfirmSpeaking");
266
+ }
267
+ if (this.confirmSpeakingCount > this.vads.length) {
268
+ this.confirmSpeakingCount = 0;
269
+ }
270
+ });
271
+ vad.on("CancelSpeaking", () => {
272
+ this.startSpeakingCount--;
273
+ if (this.lastEventName === "StartSpeaking") {
274
+ this.emit("CancelSpeaking");
275
+ }
276
+ });
277
+ vad.on("StopSpeaking", () => {
278
+ this.startSpeakingCount--;
279
+ this.confirmSpeakingCount--;
280
+ if (this.lastEventName === "ConfirmSpeaking") {
281
+ this.emit("StopSpeaking");
282
+ }
283
+ });
284
+ await vad.start(stream);
285
+ }
286
+ }
287
+ async stop() {
288
+ for (const vad of this.vads) {
289
+ await vad.stop();
290
+ vad.removeAllListeners();
291
+ }
237
292
  }
238
293
  };
239
294
 
240
295
  // src/audio/vad/SileroVAD.ts
296
+ var import_src = require("@ricky0123/vad-web/src");
297
+ var defaultOptions = {
298
+ positiveSpeechThreshold: 0.18,
299
+ negativeSpeechThreshold: 0.11,
300
+ minSpeechFrames: 8,
301
+ redemptionFrames: 20
302
+ };
241
303
  var SileroVAD = class extends VAD {
304
+ constructor(options) {
305
+ super();
306
+ this.options = defaultOptions;
307
+ if (options) {
308
+ this.setOptions(options);
309
+ } else {
310
+ const savedOptions = localStorage.getItem(
311
+ "micdrop.SileroVADOptions" /* SileroVADOptions */
312
+ );
313
+ try {
314
+ this.setOptions(savedOptions ? JSON.parse(savedOptions) : {});
315
+ } catch (error) {
316
+ this.setOptions({});
317
+ }
318
+ }
319
+ }
242
320
  get isStarted() {
243
321
  return !!this.vad;
244
322
  }
245
- async start(stream, threshold) {
323
+ async start(stream) {
246
324
  if (this.vad) return;
247
- if (threshold !== void 0) {
248
- this.threshold = threshold;
249
- }
250
- const speechThreshold = Math.min(
251
- Math.max((this.threshold + 100) / 100, 0),
252
- 1
253
- );
254
325
  this.vad = await import_src.MicVAD.new({
255
326
  stream,
256
327
  model: "v5",
@@ -258,10 +329,7 @@ var SileroVAD = class extends VAD {
258
329
  onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
259
330
  onVADMisfire: () => this.emit("CancelSpeaking"),
260
331
  onSpeechEnd: () => this.emit("StopSpeaking"),
261
- positiveSpeechThreshold: speechThreshold,
262
- negativeSpeechThreshold: speechThreshold * 0.7,
263
- minSpeechFrames: 3,
264
- redemptionFrames: 9
332
+ ...this.options
265
333
  });
266
334
  this.vad.start();
267
335
  }
@@ -270,27 +338,43 @@ var SileroVAD = class extends VAD {
270
338
  this.vad.destroy();
271
339
  this.vad = void 0;
272
340
  }
273
- async setThreshold(threshold) {
274
- if (threshold === this.threshold) return;
275
- if (this.vad) {
276
- const stream = this.vad["stream"];
277
- await this.stop();
278
- await this.start(stream, threshold);
279
- }
341
+ setOptions(options) {
342
+ this.options = { ...this.options, ...options };
343
+ this.vad?.setOptions(this.options);
344
+ localStorage.setItem(
345
+ "micdrop.SileroVADOptions" /* SileroVADOptions */,
346
+ JSON.stringify(this.options)
347
+ );
348
+ }
349
+ resetOptions() {
350
+ this.setOptions(defaultOptions);
280
351
  }
281
352
  };
282
353
 
283
354
  // src/audio/vad/VolumeVAD.ts
355
+ var defaultOptions2 = {
356
+ history: 5,
357
+ threshold: -55
358
+ };
284
359
  var VolumeVAD = class extends VAD {
285
- constructor() {
360
+ constructor(options) {
286
361
  super();
362
+ this.options = defaultOptions2;
287
363
  this.running = false;
288
364
  this.speaking = false;
289
365
  this.attemptSpeaking = false;
290
- this.history = 10;
291
366
  this.speakingHistory = [];
292
- for (let i = 0; i < this.history; i++) {
293
- this.speakingHistory.push(0);
367
+ if (options) {
368
+ this.setOptions(options);
369
+ } else {
370
+ const savedOptions = localStorage.getItem(
371
+ "micdrop.VolumeVADOptions" /* VolumeVADOptions */
372
+ );
373
+ try {
374
+ this.setOptions(savedOptions ? JSON.parse(savedOptions) : {});
375
+ } catch (error) {
376
+ this.setOptions({});
377
+ }
294
378
  }
295
379
  }
296
380
  getMaxVolume() {
@@ -307,11 +391,8 @@ var VolumeVAD = class extends VAD {
307
391
  get isStarted() {
308
392
  return this.running;
309
393
  }
310
- async start(stream, threshold) {
394
+ async start(stream) {
311
395
  if (this.running) return;
312
- if (threshold !== void 0) {
313
- this.threshold = threshold;
314
- }
315
396
  this.analyser = audioContext.createAnalyser();
316
397
  this.sourceNode = audioContext.createMediaStreamSource(stream);
317
398
  this.analyser.fftSize = 512;
@@ -324,7 +405,7 @@ var VolumeVAD = class extends VAD {
324
405
  startLoop() {
325
406
  if (!this.running) return;
326
407
  const currentVolume = this.getMaxVolume();
327
- if (currentVolume > this.threshold) {
408
+ if (currentVolume > this.options.threshold) {
328
409
  if (!this.speaking) {
329
410
  let recentHistory = 0;
330
411
  for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
@@ -356,7 +437,7 @@ var VolumeVAD = class extends VAD {
356
437
  }
357
438
  }
358
439
  this.speakingHistory.shift();
359
- this.speakingHistory.push(Number(currentVolume > this.threshold));
440
+ this.speakingHistory.push(Number(currentVolume > this.options.threshold));
360
441
  setTimeout(() => this.startLoop(), this.delay);
361
442
  }
362
443
  async stop() {
@@ -373,25 +454,49 @@ var VolumeVAD = class extends VAD {
373
454
  }
374
455
  this.fftBins = void 0;
375
456
  }
376
- async setThreshold(threshold) {
377
- if (threshold === this.threshold) return;
378
- this.threshold = threshold;
457
+ setOptions(options) {
458
+ if (typeof options.history === "number" && options.history < 3) {
459
+ throw new Error("History must be at least 3");
460
+ }
461
+ this.options = { ...this.options, ...options };
462
+ while (this.speakingHistory.length > this.options.history) {
463
+ this.speakingHistory.shift();
464
+ }
465
+ while (this.speakingHistory.length < this.options.history) {
466
+ this.speakingHistory.push(0);
467
+ }
468
+ localStorage.setItem(
469
+ "micdrop.VolumeVADOptions" /* VolumeVADOptions */,
470
+ JSON.stringify(this.options)
471
+ );
472
+ }
473
+ resetOptions() {
474
+ this.setOptions(defaultOptions2);
379
475
  }
380
476
  };
381
477
 
478
+ // src/audio/vad/getVAD.ts
479
+ function getVAD(vad) {
480
+ if (!vad) return new VolumeVAD();
481
+ if (vad === "volume") return new VolumeVAD();
482
+ if (vad === "silero") return new SileroVAD();
483
+ if (Array.isArray(vad)) {
484
+ return new MultipleVAD(vad);
485
+ }
486
+ return vad;
487
+ }
488
+
382
489
  // src/audio/MicRecorder.ts
383
490
  var timeSlice = 100;
384
491
  var defaultMicRecorderState = {
385
492
  isStarting: false,
386
493
  isStarted: false,
387
494
  isMuted: false,
388
- isSpeaking: false,
389
- threshold: defaultMicThreshold
495
+ isSpeaking: false
390
496
  };
391
497
  var MicRecorder = class extends import_eventemitter33.default {
392
498
  constructor(vadConfig) {
393
499
  super();
394
- this.vadConfig = vadConfig;
395
500
  this.audioInfo = this.getAudioInfo();
396
501
  this.speakingConfirmed = false;
397
502
  this.queuedChunks = [];
@@ -430,8 +535,8 @@ var MicRecorder = class extends import_eventemitter33.default {
430
535
  this.emit("StopSpeaking");
431
536
  this.speakingConfirmed = false;
432
537
  }).bind(this);
433
- const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
434
- this.state = { ...defaultMicRecorderState, threshold };
538
+ this.state = defaultMicRecorderState;
539
+ this.vad = getVAD(vadConfig);
435
540
  }
436
541
  async start(stream) {
437
542
  if (this.state.isStarted) {
@@ -443,9 +548,6 @@ var MicRecorder = class extends import_eventemitter33.default {
443
548
  isMuted: false,
444
549
  isSpeaking: false
445
550
  });
446
- if (!this.vad) {
447
- this.vad = this.initVAD(this.vadConfig);
448
- }
449
551
  const delayedStream = createDelayedStream(
450
552
  stream,
451
553
  this.vad.delay / 1e3 + 0.05
@@ -497,17 +599,6 @@ var MicRecorder = class extends import_eventemitter33.default {
497
599
  console.error(err);
498
600
  }
499
601
  }
500
- setThreshold(threshold) {
501
- this.changeState({ threshold });
502
- localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
503
- this.vad?.setThreshold(threshold);
504
- }
505
- initVAD(vad) {
506
- if (!vad) return new VolumeVAD();
507
- if (vad === "volume") return new VolumeVAD();
508
- if (vad === "silero") return new SileroVAD();
509
- return vad;
510
- }
511
602
  async startSpeakingDetection(stream) {
512
603
  if (!this.vad) {
513
604
  throw new Error("VAD not initialized");
@@ -519,7 +610,7 @@ var MicRecorder = class extends import_eventemitter33.default {
519
610
  this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
520
611
  this.vad.on("CancelSpeaking", this.onCancelSpeaking);
521
612
  this.vad.on("StopSpeaking", this.onStopSpeaking);
522
- await this.vad.start(stream, this.state.threshold);
613
+ await this.vad.start(stream);
523
614
  }
524
615
  async stopSpeakingDetection() {
525
616
  if (!this.vad) {
@@ -932,6 +1023,7 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
932
1023
  this.debug = false;
933
1024
  this.startTime = 0;
934
1025
  this._isProcessing = false;
1026
+ this.vad = getVAD(this.options?.vad);
935
1027
  }
936
1028
  static getInstance(options) {
937
1029
  if (!window.micdropCallHandler) {
@@ -960,9 +1052,6 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
960
1052
  get isMicSpeaking() {
961
1053
  return this.micRecorder?.state.isSpeaking ?? false;
962
1054
  }
963
- get micThreshold() {
964
- return this.micRecorder?.state.threshold ?? mic.defaultThreshold;
965
- }
966
1055
  get isProcessing() {
967
1056
  return this._isProcessing;
968
1057
  }
@@ -1004,7 +1093,7 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
1004
1093
  async startMic(deviceId, record = true) {
1005
1094
  try {
1006
1095
  if (!this.micRecorder) {
1007
- this.micRecorder = new MicRecorder(this.options?.vad);
1096
+ this.micRecorder = new MicRecorder(this.vad);
1008
1097
  this.micRecorder.on("StateChange", () => {
1009
1098
  this.notifyStateChange();
1010
1099
  });
@@ -1170,6 +1259,7 @@ var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
1170
1259
  LocalStorageKeys,
1171
1260
  Mic,
1172
1261
  MicRecorder,
1262
+ MultipleVAD,
1173
1263
  SileroVAD,
1174
1264
  Speaker,
1175
1265
  VAD,