@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.mjs CHANGED
@@ -74,7 +74,8 @@ if (window.micdropAudioContext) {
74
74
  var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
75
75
  LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
76
76
  LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
77
- LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
77
+ LocalStorageKeys2["VolumeVADOptions"] = "micdrop.VolumeVADOptions";
78
+ LocalStorageKeys2["SileroVADOptions"] = "micdrop.SileroVADOptions";
78
79
  return LocalStorageKeys2;
79
80
  })(LocalStorageKeys || {});
80
81
 
@@ -85,10 +86,8 @@ function stopStream(stream) {
85
86
 
86
87
  // src/audio/mic.ts
87
88
  var mic;
88
- var defaultMicThreshold = -50;
89
89
  var Mic = class {
90
90
  constructor() {
91
- this.defaultThreshold = defaultMicThreshold;
92
91
  this.analyser = new AudioAnalyser(audioContext);
93
92
  }
94
93
  getMicConstraints(deviceId) {
@@ -172,38 +171,109 @@ function createDelayedStream(stream, delay) {
172
171
  return audioDestination.stream;
173
172
  }
174
173
 
175
- // src/audio/vad/SileroVAD.ts
176
- import { MicVAD } from "@ricky0123/vad-web/src";
177
-
178
174
  // src/audio/vad/VAD.ts
179
175
  import EventEmitter2 from "eventemitter3";
180
176
  var VAD = class extends EventEmitter2 {
181
- /**
182
- * Constructor for the VAD class
183
- * @param threshold - The threshold to use for the VAD
184
- */
185
- constructor(threshold = 0) {
186
- super();
177
+ constructor() {
178
+ super(...arguments);
187
179
  // Max delay to detect speech, can be used to record delayed stream
188
180
  this.delay = 100;
189
- this.threshold = threshold;
181
+ }
182
+ };
183
+
184
+ // src/audio/vad/MultipleVAD.ts
185
+ var MultipleVAD = class extends VAD {
186
+ constructor(vads) {
187
+ super();
188
+ this.startSpeakingCount = 0;
189
+ this.confirmSpeakingCount = 0;
190
+ this.lastEventName = "StopSpeaking";
191
+ this.vads = vads.map((vad) => getVAD(vad));
192
+ }
193
+ emit(eventName, ..._) {
194
+ this.lastEventName = eventName;
195
+ return super.emit(eventName);
196
+ }
197
+ get isStarted() {
198
+ return this.vads.some((vad) => vad.isStarted);
199
+ }
200
+ async start(stream, threshold) {
201
+ for (const vad of this.vads) {
202
+ if (vad.isStarted) {
203
+ continue;
204
+ }
205
+ vad.on("StartSpeaking", () => {
206
+ this.startSpeakingCount++;
207
+ if ((this.lastEventName === "StopSpeaking" || this.lastEventName === "CancelSpeaking") && this.startSpeakingCount === this.vads.length) {
208
+ this.emit("StartSpeaking");
209
+ }
210
+ if (this.startSpeakingCount > this.vads.length) {
211
+ this.startSpeakingCount = 0;
212
+ }
213
+ });
214
+ vad.on("ConfirmSpeaking", () => {
215
+ this.confirmSpeakingCount++;
216
+ if (this.lastEventName === "StartSpeaking" && this.confirmSpeakingCount === this.vads.length) {
217
+ this.emit("ConfirmSpeaking");
218
+ }
219
+ if (this.confirmSpeakingCount > this.vads.length) {
220
+ this.confirmSpeakingCount = 0;
221
+ }
222
+ });
223
+ vad.on("CancelSpeaking", () => {
224
+ this.startSpeakingCount--;
225
+ if (this.lastEventName === "StartSpeaking") {
226
+ this.emit("CancelSpeaking");
227
+ }
228
+ });
229
+ vad.on("StopSpeaking", () => {
230
+ this.startSpeakingCount--;
231
+ this.confirmSpeakingCount--;
232
+ if (this.lastEventName === "ConfirmSpeaking") {
233
+ this.emit("StopSpeaking");
234
+ }
235
+ });
236
+ await vad.start(stream);
237
+ }
238
+ }
239
+ async stop() {
240
+ for (const vad of this.vads) {
241
+ await vad.stop();
242
+ vad.removeAllListeners();
243
+ }
190
244
  }
191
245
  };
192
246
 
193
247
  // src/audio/vad/SileroVAD.ts
248
+ import { MicVAD } from "@ricky0123/vad-web/src";
249
+ var defaultOptions = {
250
+ positiveSpeechThreshold: 0.18,
251
+ negativeSpeechThreshold: 0.11,
252
+ minSpeechFrames: 8,
253
+ redemptionFrames: 20
254
+ };
194
255
  var SileroVAD = class extends VAD {
256
+ constructor(options) {
257
+ super();
258
+ this.options = defaultOptions;
259
+ if (options) {
260
+ this.setOptions(options);
261
+ } else {
262
+ const savedOptions = localStorage.getItem(
263
+ "micdrop.SileroVADOptions" /* SileroVADOptions */
264
+ );
265
+ try {
266
+ this.setOptions(savedOptions ? JSON.parse(savedOptions) : {});
267
+ } catch (error) {
268
+ this.setOptions({});
269
+ }
270
+ }
271
+ }
195
272
  get isStarted() {
196
273
  return !!this.vad;
197
274
  }
198
- async start(stream, threshold) {
275
+ async start(stream) {
199
276
  if (this.vad) return;
200
- if (threshold !== void 0) {
201
- this.threshold = threshold;
202
- }
203
- const speechThreshold = Math.min(
204
- Math.max((this.threshold + 100) / 100, 0),
205
- 1
206
- );
207
277
  this.vad = await MicVAD.new({
208
278
  stream,
209
279
  model: "v5",
@@ -211,10 +281,7 @@ var SileroVAD = class extends VAD {
211
281
  onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
212
282
  onVADMisfire: () => this.emit("CancelSpeaking"),
213
283
  onSpeechEnd: () => this.emit("StopSpeaking"),
214
- positiveSpeechThreshold: speechThreshold,
215
- negativeSpeechThreshold: speechThreshold * 0.7,
216
- minSpeechFrames: 3,
217
- redemptionFrames: 9
284
+ ...this.options
218
285
  });
219
286
  this.vad.start();
220
287
  }
@@ -223,27 +290,43 @@ var SileroVAD = class extends VAD {
223
290
  this.vad.destroy();
224
291
  this.vad = void 0;
225
292
  }
226
- async setThreshold(threshold) {
227
- if (threshold === this.threshold) return;
228
- if (this.vad) {
229
- const stream = this.vad["stream"];
230
- await this.stop();
231
- await this.start(stream, threshold);
232
- }
293
+ setOptions(options) {
294
+ this.options = { ...this.options, ...options };
295
+ this.vad?.setOptions(this.options);
296
+ localStorage.setItem(
297
+ "micdrop.SileroVADOptions" /* SileroVADOptions */,
298
+ JSON.stringify(this.options)
299
+ );
300
+ }
301
+ resetOptions() {
302
+ this.setOptions(defaultOptions);
233
303
  }
234
304
  };
235
305
 
236
306
  // src/audio/vad/VolumeVAD.ts
307
+ var defaultOptions2 = {
308
+ history: 5,
309
+ threshold: -55
310
+ };
237
311
  var VolumeVAD = class extends VAD {
238
- constructor() {
312
+ constructor(options) {
239
313
  super();
314
+ this.options = defaultOptions2;
240
315
  this.running = false;
241
316
  this.speaking = false;
242
317
  this.attemptSpeaking = false;
243
- this.history = 10;
244
318
  this.speakingHistory = [];
245
- for (let i = 0; i < this.history; i++) {
246
- this.speakingHistory.push(0);
319
+ if (options) {
320
+ this.setOptions(options);
321
+ } else {
322
+ const savedOptions = localStorage.getItem(
323
+ "micdrop.VolumeVADOptions" /* VolumeVADOptions */
324
+ );
325
+ try {
326
+ this.setOptions(savedOptions ? JSON.parse(savedOptions) : {});
327
+ } catch (error) {
328
+ this.setOptions({});
329
+ }
247
330
  }
248
331
  }
249
332
  getMaxVolume() {
@@ -260,11 +343,8 @@ var VolumeVAD = class extends VAD {
260
343
  get isStarted() {
261
344
  return this.running;
262
345
  }
263
- async start(stream, threshold) {
346
+ async start(stream) {
264
347
  if (this.running) return;
265
- if (threshold !== void 0) {
266
- this.threshold = threshold;
267
- }
268
348
  this.analyser = audioContext.createAnalyser();
269
349
  this.sourceNode = audioContext.createMediaStreamSource(stream);
270
350
  this.analyser.fftSize = 512;
@@ -277,7 +357,7 @@ var VolumeVAD = class extends VAD {
277
357
  startLoop() {
278
358
  if (!this.running) return;
279
359
  const currentVolume = this.getMaxVolume();
280
- if (currentVolume > this.threshold) {
360
+ if (currentVolume > this.options.threshold) {
281
361
  if (!this.speaking) {
282
362
  let recentHistory = 0;
283
363
  for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
@@ -309,7 +389,7 @@ var VolumeVAD = class extends VAD {
309
389
  }
310
390
  }
311
391
  this.speakingHistory.shift();
312
- this.speakingHistory.push(Number(currentVolume > this.threshold));
392
+ this.speakingHistory.push(Number(currentVolume > this.options.threshold));
313
393
  setTimeout(() => this.startLoop(), this.delay);
314
394
  }
315
395
  async stop() {
@@ -326,25 +406,49 @@ var VolumeVAD = class extends VAD {
326
406
  }
327
407
  this.fftBins = void 0;
328
408
  }
329
- async setThreshold(threshold) {
330
- if (threshold === this.threshold) return;
331
- this.threshold = threshold;
409
+ setOptions(options) {
410
+ if (typeof options.history === "number" && options.history < 3) {
411
+ throw new Error("History must be at least 3");
412
+ }
413
+ this.options = { ...this.options, ...options };
414
+ while (this.speakingHistory.length > this.options.history) {
415
+ this.speakingHistory.shift();
416
+ }
417
+ while (this.speakingHistory.length < this.options.history) {
418
+ this.speakingHistory.push(0);
419
+ }
420
+ localStorage.setItem(
421
+ "micdrop.VolumeVADOptions" /* VolumeVADOptions */,
422
+ JSON.stringify(this.options)
423
+ );
424
+ }
425
+ resetOptions() {
426
+ this.setOptions(defaultOptions2);
332
427
  }
333
428
  };
334
429
 
430
+ // src/audio/vad/getVAD.ts
431
+ function getVAD(vad) {
432
+ if (!vad) return new VolumeVAD();
433
+ if (vad === "volume") return new VolumeVAD();
434
+ if (vad === "silero") return new SileroVAD();
435
+ if (Array.isArray(vad)) {
436
+ return new MultipleVAD(vad);
437
+ }
438
+ return vad;
439
+ }
440
+
335
441
  // src/audio/MicRecorder.ts
336
442
  var timeSlice = 100;
337
443
  var defaultMicRecorderState = {
338
444
  isStarting: false,
339
445
  isStarted: false,
340
446
  isMuted: false,
341
- isSpeaking: false,
342
- threshold: defaultMicThreshold
447
+ isSpeaking: false
343
448
  };
344
449
  var MicRecorder = class extends EventEmitter3 {
345
450
  constructor(vadConfig) {
346
451
  super();
347
- this.vadConfig = vadConfig;
348
452
  this.audioInfo = this.getAudioInfo();
349
453
  this.speakingConfirmed = false;
350
454
  this.queuedChunks = [];
@@ -383,8 +487,8 @@ var MicRecorder = class extends EventEmitter3 {
383
487
  this.emit("StopSpeaking");
384
488
  this.speakingConfirmed = false;
385
489
  }).bind(this);
386
- const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
387
- this.state = { ...defaultMicRecorderState, threshold };
490
+ this.state = defaultMicRecorderState;
491
+ this.vad = getVAD(vadConfig);
388
492
  }
389
493
  async start(stream) {
390
494
  if (this.state.isStarted) {
@@ -396,9 +500,6 @@ var MicRecorder = class extends EventEmitter3 {
396
500
  isMuted: false,
397
501
  isSpeaking: false
398
502
  });
399
- if (!this.vad) {
400
- this.vad = this.initVAD(this.vadConfig);
401
- }
402
503
  const delayedStream = createDelayedStream(
403
504
  stream,
404
505
  this.vad.delay / 1e3 + 0.05
@@ -450,17 +551,6 @@ var MicRecorder = class extends EventEmitter3 {
450
551
  console.error(err);
451
552
  }
452
553
  }
453
- setThreshold(threshold) {
454
- this.changeState({ threshold });
455
- localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
456
- this.vad?.setThreshold(threshold);
457
- }
458
- initVAD(vad) {
459
- if (!vad) return new VolumeVAD();
460
- if (vad === "volume") return new VolumeVAD();
461
- if (vad === "silero") return new SileroVAD();
462
- return vad;
463
- }
464
554
  async startSpeakingDetection(stream) {
465
555
  if (!this.vad) {
466
556
  throw new Error("VAD not initialized");
@@ -472,7 +562,7 @@ var MicRecorder = class extends EventEmitter3 {
472
562
  this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
473
563
  this.vad.on("CancelSpeaking", this.onCancelSpeaking);
474
564
  this.vad.on("StopSpeaking", this.onStopSpeaking);
475
- await this.vad.start(stream, this.state.threshold);
565
+ await this.vad.start(stream);
476
566
  }
477
567
  async stopSpeakingDetection() {
478
568
  if (!this.vad) {
@@ -885,6 +975,7 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
885
975
  this.debug = false;
886
976
  this.startTime = 0;
887
977
  this._isProcessing = false;
978
+ this.vad = getVAD(this.options?.vad);
888
979
  }
889
980
  static getInstance(options) {
890
981
  if (!window.micdropCallHandler) {
@@ -913,9 +1004,6 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
913
1004
  get isMicSpeaking() {
914
1005
  return this.micRecorder?.state.isSpeaking ?? false;
915
1006
  }
916
- get micThreshold() {
917
- return this.micRecorder?.state.threshold ?? mic.defaultThreshold;
918
- }
919
1007
  get isProcessing() {
920
1008
  return this._isProcessing;
921
1009
  }
@@ -957,7 +1045,7 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
957
1045
  async startMic(deviceId, record = true) {
958
1046
  try {
959
1047
  if (!this.micRecorder) {
960
- this.micRecorder = new MicRecorder(this.options?.vad);
1048
+ this.micRecorder = new MicRecorder(this.vad);
961
1049
  this.micRecorder.on("StateChange", () => {
962
1050
  this.notifyStateChange();
963
1051
  });
@@ -1122,6 +1210,7 @@ export {
1122
1210
  LocalStorageKeys,
1123
1211
  mic as Mic,
1124
1212
  MicRecorder,
1213
+ MultipleVAD,
1125
1214
  SileroVAD,
1126
1215
  speaker as Speaker,
1127
1216
  VAD,