@micdrop/client 3.6.0 → 3.7.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
@@ -36,7 +36,7 @@ class MyMic extends MicDriver {
36
36
  }
37
37
 
38
38
  class MySpeaker extends SpeakerDriver {
39
- // play() queues 16 kHz PCM16
39
+ // play() queues PCM16 at the sample rate it is given
40
40
  }
41
41
 
42
42
  Mic.setDriver(new MyMic())
package/dist/index.d.mts CHANGED
@@ -181,8 +181,8 @@ interface SpeakerEvents {
181
181
  /**
182
182
  * The speaker, as a single object shared by the whole app.
183
183
  *
184
- * It wraps a {@link SpeakerDriver} and plays the 16 kHz PCM sent by the server
185
- * as it arrives.
184
+ * It wraps a {@link SpeakerDriver} and plays the PCM sent by the server as it
185
+ * arrives, at the rate the server announces.
186
186
  */
187
187
  declare class SpeakerController extends EventEmitter<SpeakerEvents> {
188
188
  private driver;
@@ -239,8 +239,14 @@ declare enum MicdropServerCommands {
239
239
  SkipAnswer = "SkipAnswer",
240
240
  EndCall = "EndCall",
241
241
  ToolCall = "ToolCall",
242
- Classification = "Classification"
242
+ Classification = "Classification",
243
+ SampleRate = "SampleRate"
243
244
  }
245
+ /**
246
+ * Rate of the audio the client records, and of the assistant voice until the
247
+ * server announces another one with a `SampleRate` command.
248
+ */
249
+ declare const DEFAULT_SAMPLE_RATE = 16000;
244
250
  /**
245
251
  * Hears whether a sentence has landed, where voice activity detection only
246
252
  * hears whether someone is speaking.
@@ -988,6 +994,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
988
994
  private startTime;
989
995
  private lastNotifiedState;
990
996
  private _isProcessing;
997
+ private assistantSampleRate;
991
998
  private _isMuted;
992
999
  private _isPaused;
993
1000
  private callStatus;
@@ -1123,4 +1130,4 @@ declare enum MicdropStorageKeys {
1123
1130
  declare function setMicdropStorage(storage: MicdropStorage): void;
1124
1131
  declare const storage: MicdropStorage;
1125
1132
 
1126
- export { type AudioSink, CONFIRM_MARGIN, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, LazySession, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropActivity, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStatus, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, type Releasable, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, createDownloadCache, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
1133
+ export { type AudioSink, CONFIRM_MARGIN, DEFAULT_SAMPLE_RATE, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, LazySession, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropActivity, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStatus, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, type Releasable, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, createDownloadCache, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
package/dist/index.d.ts CHANGED
@@ -181,8 +181,8 @@ interface SpeakerEvents {
181
181
  /**
182
182
  * The speaker, as a single object shared by the whole app.
183
183
  *
184
- * It wraps a {@link SpeakerDriver} and plays the 16 kHz PCM sent by the server
185
- * as it arrives.
184
+ * It wraps a {@link SpeakerDriver} and plays the PCM sent by the server as it
185
+ * arrives, at the rate the server announces.
186
186
  */
187
187
  declare class SpeakerController extends EventEmitter<SpeakerEvents> {
188
188
  private driver;
@@ -239,8 +239,14 @@ declare enum MicdropServerCommands {
239
239
  SkipAnswer = "SkipAnswer",
240
240
  EndCall = "EndCall",
241
241
  ToolCall = "ToolCall",
242
- Classification = "Classification"
242
+ Classification = "Classification",
243
+ SampleRate = "SampleRate"
243
244
  }
245
+ /**
246
+ * Rate of the audio the client records, and of the assistant voice until the
247
+ * server announces another one with a `SampleRate` command.
248
+ */
249
+ declare const DEFAULT_SAMPLE_RATE = 16000;
244
250
  /**
245
251
  * Hears whether a sentence has landed, where voice activity detection only
246
252
  * hears whether someone is speaking.
@@ -988,6 +994,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
988
994
  private startTime;
989
995
  private lastNotifiedState;
990
996
  private _isProcessing;
997
+ private assistantSampleRate;
991
998
  private _isMuted;
992
999
  private _isPaused;
993
1000
  private callStatus;
@@ -1123,4 +1130,4 @@ declare enum MicdropStorageKeys {
1123
1130
  declare function setMicdropStorage(storage: MicdropStorage): void;
1124
1131
  declare const storage: MicdropStorage;
1125
1132
 
1126
- export { type AudioSink, CONFIRM_MARGIN, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, LazySession, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropActivity, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStatus, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, type Releasable, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, createDownloadCache, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
1133
+ export { type AudioSink, CONFIRM_MARGIN, DEFAULT_SAMPLE_RATE, DEFAULT_TURN_MAX_WAIT, type DeepPartial, ECHO_TAIL, LazySession, Mic, MicController, MicDriver, type MicDriverEvents, type MicEvents, MicRecorder, type MicRecorderEvents, type MicRecorderState, type MicSource, Micdrop, type MicdropActivity, type MicdropAnswerMetadata, type MicdropCallSummary, type MicdropClassification, MicdropClient, MicdropClientCommands, MicdropClientError, MicdropClientErrorCode, type MicdropConversation, type MicdropConversationItem, type MicdropConversationMessage, type MicdropConversationToolCall, type MicdropConversationToolResult, type MicdropDevice, type MicdropEvents, type MicdropOptions, type MicdropReconnectOptions, MicdropServerCommands, type MicdropState, type MicdropStatus, type MicdropStorage, MicdropStorageKeys, type MicdropToolCall, MultipleVAD, Pcm16AudioStream, type Pcm16AudioStreamEvents, type Releasable, SILERO_FRAME_SAMPLES, SILERO_SAMPLE_RATE, type ScheduledAudio, type SileroModel, type SileroModelLoader, SileroVAD, type SileroVADOptions, Speaker, SpeakerController, SpeakerDriver, type SpeakerDriverEvents, type SpeakerEvents, type TurnDetector, VAD, type VADConfig, type VADConfigName, type VADEvents, VADStatus, VOLUME_SAMPLE_RATE, VolumeMeter, VolumeVAD, type VolumeVADOptions, type WSCloseEvent, concatFloat32, createDownloadCache, currentTurn, equalVADConfig, floatToPcm16, getClientErrorFromWSCloseEvent, getClientErrorFromWSCloseEventCode, getTurnClassification, getVAD, getVolume, isRecoverableError, pcm16ToArrayBuffer, pcm16ToFloat, resample, setMicdropStorage, setSileroModelLoader, storage };
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  CONFIRM_MARGIN: () => CONFIRM_MARGIN,
24
+ DEFAULT_SAMPLE_RATE: () => DEFAULT_SAMPLE_RATE,
24
25
  DEFAULT_TURN_MAX_WAIT: () => DEFAULT_TURN_MAX_WAIT,
25
26
  ECHO_TAIL: () => ECHO_TAIL,
26
27
  LazySession: () => LazySession,
@@ -405,6 +406,47 @@ var storage = {
405
406
  }
406
407
  };
407
408
 
409
+ // ../server/src/types.ts
410
+ var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
411
+ MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
412
+ MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
413
+ MicdropClientCommands2["Mute"] = "Mute";
414
+ return MicdropClientCommands2;
415
+ })(MicdropClientCommands || {});
416
+ var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
417
+ MicdropServerCommands2["Message"] = "Message";
418
+ MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
419
+ MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
420
+ MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
421
+ MicdropServerCommands2["EndCall"] = "EndCall";
422
+ MicdropServerCommands2["ToolCall"] = "ToolCall";
423
+ MicdropServerCommands2["Classification"] = "Classification";
424
+ MicdropServerCommands2["SampleRate"] = "SampleRate";
425
+ return MicdropServerCommands2;
426
+ })(MicdropServerCommands || {});
427
+ var DEFAULT_SAMPLE_RATE = 16e3;
428
+ function currentTurn(conversation) {
429
+ let start = conversation.length;
430
+ while (start > 0) {
431
+ const item = conversation[start - 1];
432
+ if (item.role !== "user" || item.metadata?.classification) break;
433
+ start--;
434
+ }
435
+ const messages = conversation.slice(start);
436
+ return {
437
+ transcript: messages.map((message) => message.content).join(" "),
438
+ messages,
439
+ before: conversation.slice(0, start)
440
+ };
441
+ }
442
+ function getTurnClassification(conversation) {
443
+ for (let index = conversation.length - 1; index >= 0; index--) {
444
+ const item = conversation[index];
445
+ if (item.role === "user") return item.metadata?.classification;
446
+ }
447
+ return void 0;
448
+ }
449
+
408
450
  // src/audio/Speaker.ts
409
451
  var SpeakerController = class extends import_eventemitter32.EventEmitter {
410
452
  constructor() {
@@ -469,7 +511,7 @@ var SpeakerController = class extends import_eventemitter32.EventEmitter {
469
511
  * @param pcm - Mono samples as 16 bits integers, or the bytes holding them
470
512
  * @param sampleRate - Sample rate of `pcm`, in Hz
471
513
  */
472
- playAudio(pcm, sampleRate = 16e3) {
514
+ playAudio(pcm, sampleRate = DEFAULT_SAMPLE_RATE) {
473
515
  try {
474
516
  const samples = pcm instanceof Int16Array ? pcm : new Int16Array(pcm);
475
517
  this.getDriver().play(samples, sampleRate);
@@ -1656,45 +1698,6 @@ var Speaker = globalScope.micdropSpeaker;
1656
1698
  // src/client/MicdropClient.ts
1657
1699
  var import_eventemitter37 = require("eventemitter3");
1658
1700
 
1659
- // ../server/src/types.ts
1660
- var MicdropClientCommands = /* @__PURE__ */ ((MicdropClientCommands2) => {
1661
- MicdropClientCommands2["StartSpeaking"] = "StartSpeaking";
1662
- MicdropClientCommands2["StopSpeaking"] = "StopSpeaking";
1663
- MicdropClientCommands2["Mute"] = "Mute";
1664
- return MicdropClientCommands2;
1665
- })(MicdropClientCommands || {});
1666
- var MicdropServerCommands = /* @__PURE__ */ ((MicdropServerCommands2) => {
1667
- MicdropServerCommands2["Message"] = "Message";
1668
- MicdropServerCommands2["PartialAssistantMessage"] = "PartialAssistantMessage";
1669
- MicdropServerCommands2["CancelLastUserMessage"] = "CancelLastUserMessage";
1670
- MicdropServerCommands2["SkipAnswer"] = "SkipAnswer";
1671
- MicdropServerCommands2["EndCall"] = "EndCall";
1672
- MicdropServerCommands2["ToolCall"] = "ToolCall";
1673
- MicdropServerCommands2["Classification"] = "Classification";
1674
- return MicdropServerCommands2;
1675
- })(MicdropServerCommands || {});
1676
- function currentTurn(conversation) {
1677
- let start = conversation.length;
1678
- while (start > 0) {
1679
- const item = conversation[start - 1];
1680
- if (item.role !== "user" || item.metadata?.classification) break;
1681
- start--;
1682
- }
1683
- const messages = conversation.slice(start);
1684
- return {
1685
- transcript: messages.map((message) => message.content).join(" "),
1686
- messages,
1687
- before: conversation.slice(0, start)
1688
- };
1689
- }
1690
- function getTurnClassification(conversation) {
1691
- for (let index = conversation.length - 1; index >= 0; index--) {
1692
- const item = conversation[index];
1693
- if (item.role === "user") return item.metadata?.classification;
1694
- }
1695
- return void 0;
1696
- }
1697
-
1698
1701
  // src/client/MicdropClientError.ts
1699
1702
  var MicdropClientErrorCode = /* @__PURE__ */ ((MicdropClientErrorCode2) => {
1700
1703
  MicdropClientErrorCode2["MissingUrl"] = "MissingUrl";
@@ -1801,6 +1804,8 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
1801
1804
  this.startTime = 0;
1802
1805
  this.lastNotifiedState = this.state;
1803
1806
  this._isProcessing = false;
1807
+ // Rate of the assistant voice, announced by the server whenever it changes
1808
+ this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
1804
1809
  this._isMuted = false;
1805
1810
  this._isPaused = false;
1806
1811
  // The call as start() and the connection leave it. The public status also
@@ -2053,7 +2058,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2053
2058
  if (data instanceof ArrayBuffer) {
2054
2059
  this.log("Received audio", data.byteLength);
2055
2060
  if (this.isPaused || this.isUserSpeaking) return;
2056
- Speaker.playAudio(new Int16Array(data));
2061
+ Speaker.playAudio(new Int16Array(data), this.assistantSampleRate);
2057
2062
  this._isProcessing = false;
2058
2063
  this.notifyStateChange();
2059
2064
  return;
@@ -2063,6 +2068,17 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2063
2068
  return;
2064
2069
  }
2065
2070
  this.log("Received message:", data);
2071
+ if (data.startsWith("SampleRate" /* SampleRate */)) {
2072
+ const sampleRate = Number(
2073
+ data.substring("SampleRate" /* SampleRate */.length + 1)
2074
+ );
2075
+ if (Number.isFinite(sampleRate) && sampleRate > 0) {
2076
+ this.assistantSampleRate = sampleRate;
2077
+ } else {
2078
+ console.error("[MicdropClient] Invalid sample rate:", data);
2079
+ }
2080
+ return;
2081
+ }
2066
2082
  if (data.startsWith("Message" /* Message */)) {
2067
2083
  try {
2068
2084
  const message = JSON.parse(
@@ -2319,6 +2335,7 @@ var MicdropClient = class extends import_eventemitter37.EventEmitter {
2319
2335
  const ws = new WebSocket(this.options.url);
2320
2336
  ws.binaryType = "arraybuffer";
2321
2337
  this.ws = ws;
2338
+ this.assistantSampleRate = DEFAULT_SAMPLE_RATE;
2322
2339
  this.notifyStateChange();
2323
2340
  const isCurrent = () => this.ws === ws;
2324
2341
  ws.onopen = () => isCurrent() && this.onWSOpen();
@@ -2438,6 +2455,7 @@ var Micdrop = globalScope2.micdropClient;
2438
2455
  // Annotate the CommonJS export names for ESM import in node:
2439
2456
  0 && (module.exports = {
2440
2457
  CONFIRM_MARGIN,
2458
+ DEFAULT_SAMPLE_RATE,
2441
2459
  DEFAULT_TURN_MAX_WAIT,
2442
2460
  ECHO_TAIL,
2443
2461
  LazySession,