@micdrop/client 3.3.1 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -43,7 +43,7 @@ Mic.setDriver(new MyMic())
43
43
  Speaker.setDriver(new MySpeaker())
44
44
  ```
45
45
 
46
- `Pcm16AudioStream` does the playback scheduling for you if the platform offers something like Web Audio: give it an `AudioSink`, which is the small slice of it that Micdrop needs.
46
+ `Pcm16AudioStream` does the playback scheduling for you if the platform offers something like Web Audio: give it an `AudioSink`, which is the small slice of it that Micdrop needs, and call its `destroy()` when the driver stops. When the output needs a moment once the microphone opens, as with Firefox and a Bluetooth headset switching to its call profile, call `warmUp(duration)`: the stream plays silence that long and holds the first answer until it is over.
47
47
 
48
48
  For Silero, provide the inference and the state machine comes from here:
49
49
 
package/dist/index.d.mts CHANGED
@@ -522,6 +522,9 @@ declare class Pcm16AudioStream extends EventEmitter<Pcm16AudioStreamEvents> {
522
522
  private endTimer?;
523
523
  private volumeTimers;
524
524
  private meter;
525
+ private holdUntil;
526
+ private holdTimer?;
527
+ private warmUpAudio?;
525
528
  constructor(sink: AudioSink);
526
529
  /**
527
530
  * Queues samples for playback
@@ -529,6 +532,18 @@ declare class Pcm16AudioStream extends EventEmitter<Pcm16AudioStreamEvents> {
529
532
  * @param sampleRate - Sample rate of `pcm`, in Hz
530
533
  */
531
534
  playAudio(pcm: Int16Array, sampleRate: number): void;
535
+ /**
536
+ * Plays silence for a while, and holds the next utterance until it is over.
537
+ *
538
+ * Firefox mangles what it plays while a Bluetooth headset switches to its
539
+ * call profile, which the headset does as soon as its microphone opens. The
540
+ * first answer then comes out all at once. Silence played through that switch
541
+ * goes unheard.
542
+ * @param duration - How long to play silence, in milliseconds
543
+ */
544
+ warmUp(duration: number): void;
545
+ /** Stops playing, for a stream no longer used */
546
+ destroy(): void;
532
547
  /** Drops everything queued and stops playing immediately */
533
548
  stopAudio(): void;
534
549
  private handleSamples;
package/dist/index.d.ts CHANGED
@@ -522,6 +522,9 @@ declare class Pcm16AudioStream extends EventEmitter<Pcm16AudioStreamEvents> {
522
522
  private endTimer?;
523
523
  private volumeTimers;
524
524
  private meter;
525
+ private holdUntil;
526
+ private holdTimer?;
527
+ private warmUpAudio?;
525
528
  constructor(sink: AudioSink);
526
529
  /**
527
530
  * Queues samples for playback
@@ -529,6 +532,18 @@ declare class Pcm16AudioStream extends EventEmitter<Pcm16AudioStreamEvents> {
529
532
  * @param sampleRate - Sample rate of `pcm`, in Hz
530
533
  */
531
534
  playAudio(pcm: Int16Array, sampleRate: number): void;
535
+ /**
536
+ * Plays silence for a while, and holds the next utterance until it is over.
537
+ *
538
+ * Firefox mangles what it plays while a Bluetooth headset switches to its
539
+ * call profile, which the headset does as soon as its microphone opens. The
540
+ * first answer then comes out all at once. Silence played through that switch
541
+ * goes unheard.
542
+ * @param duration - How long to play silence, in milliseconds
543
+ */
544
+ warmUp(duration: number): void;
545
+ /** Stops playing, for a stream no longer used */
546
+ destroy(): void;
532
547
  /** Drops everything queued and stops playing immediately */
533
548
  stopAudio(): void;
534
549
  private handleSamples;
package/dist/index.js CHANGED
@@ -1289,6 +1289,8 @@ var Pcm16AudioStream = class extends import_eventemitter35.EventEmitter {
1289
1289
  this.prebufferDuration = 0;
1290
1290
  this.volumeTimers = [];
1291
1291
  this.meter = new VolumeMeter();
1292
+ // Wall time before which no utterance starts, see warmUp()
1293
+ this.holdUntil = 0;
1292
1294
  }
1293
1295
  /**
1294
1296
  * Queues samples for playback
@@ -1304,6 +1306,34 @@ var Pcm16AudioStream = class extends import_eventemitter35.EventEmitter {
1304
1306
  );
1305
1307
  this.handleSamples(samples);
1306
1308
  }
1309
+ /**
1310
+ * Plays silence for a while, and holds the next utterance until it is over.
1311
+ *
1312
+ * Firefox mangles what it plays while a Bluetooth headset switches to its
1313
+ * call profile, which the headset does as soon as its microphone opens. The
1314
+ * first answer then comes out all at once. Silence played through that switch
1315
+ * goes unheard.
1316
+ * @param duration - How long to play silence, in milliseconds
1317
+ */
1318
+ warmUp(duration) {
1319
+ this.holdUntil = Date.now() + duration;
1320
+ const samples = new Float32Array(
1321
+ Math.round(duration / 1e3 * this.sink.sampleRate)
1322
+ );
1323
+ this.warmUpAudio?.stop();
1324
+ this.warmUpAudio = this.sink.schedule(
1325
+ samples,
1326
+ this.sink.currentTime,
1327
+ () => this.warmUpAudio = void 0
1328
+ );
1329
+ }
1330
+ /** Stops playing, for a stream no longer used */
1331
+ destroy() {
1332
+ this.stopAudio();
1333
+ this.warmUpAudio?.stop();
1334
+ this.warmUpAudio = void 0;
1335
+ this.removeAllListeners();
1336
+ }
1307
1337
  /** Drops everything queued and stops playing immediately */
1308
1338
  stopAudio() {
1309
1339
  for (const source of this.sources) {
@@ -1343,6 +1373,14 @@ var Pcm16AudioStream = class extends import_eventemitter35.EventEmitter {
1343
1373
  clearTimeout(this.quietTimer);
1344
1374
  this.quietTimer = void 0;
1345
1375
  }
1376
+ const hold = this.holdUntil - Date.now();
1377
+ if (hold > 0) {
1378
+ this.holdTimer ?? (this.holdTimer = setTimeout(() => {
1379
+ this.holdTimer = void 0;
1380
+ this.flushPrebuffer();
1381
+ }, hold));
1382
+ return;
1383
+ }
1346
1384
  this.prebuffering = false;
1347
1385
  const buffers = this.prebuffer;
1348
1386
  this.prebuffer = [];
@@ -1358,6 +1396,10 @@ var Pcm16AudioStream = class extends import_eventemitter35.EventEmitter {
1358
1396
  clearTimeout(this.endTimer);
1359
1397
  this.endTimer = void 0;
1360
1398
  }
1399
+ if (this.holdTimer !== void 0) {
1400
+ clearTimeout(this.holdTimer);
1401
+ this.holdTimer = void 0;
1402
+ }
1361
1403
  this.prebuffering = true;
1362
1404
  this.prebuffer = [];
1363
1405
  this.prebufferDuration = 0;