@micdrop/client 1.1.0 → 1.2.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/dist/index.d.mts CHANGED
@@ -6,6 +6,46 @@ declare enum LocalStorageKeys {
6
6
  MicThreshold = "micdrop.micThreshold"
7
7
  }
8
8
 
9
+ interface VADEvents {
10
+ StartSpeaking: void;
11
+ ConfirmSpeaking: void;
12
+ CancelSpeaking: void;
13
+ StopSpeaking: void;
14
+ }
15
+ /**
16
+ * Abstract VAD class
17
+ * VAD = Voice Activity Detection
18
+ */
19
+ declare abstract class VAD extends EventEmitter<VADEvents> {
20
+ delay: number;
21
+ protected threshold: number;
22
+ /**
23
+ * Constructor for the VAD class
24
+ * @param threshold - The threshold to use for the VAD
25
+ */
26
+ constructor(threshold?: number);
27
+ /**
28
+ * Checks if the VAD is started
29
+ * @returns True if the VAD is started, false otherwise
30
+ */
31
+ abstract get isStarted(): boolean;
32
+ /**
33
+ * Starts the VAD
34
+ * @param stream - The stream to start the VAD on
35
+ * @param threshold - The threshold to use for the VAD
36
+ */
37
+ abstract start(stream: MediaStream, threshold?: number): Promise<void>;
38
+ /**
39
+ * Stops the VAD
40
+ */
41
+ abstract stop(): Promise<void>;
42
+ /**
43
+ * Sets the threshold for the VAD
44
+ * @param threshold - The threshold to use for the VAD
45
+ */
46
+ abstract setThreshold(threshold: number): Promise<void>;
47
+ }
48
+
9
49
  interface MicRecorderState {
10
50
  isStarting: boolean;
11
51
  isStarted: boolean;
@@ -22,10 +62,12 @@ interface MicRecorderEvents {
22
62
  declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
23
63
  state: MicRecorderState;
24
64
  private audioInfo;
25
- private hark;
65
+ private vad;
26
66
  private recorder;
27
67
  private delayedStream;
28
- constructor();
68
+ private speakingConfirmed;
69
+ private queuedChunks;
70
+ constructor(vad: VAD);
29
71
  start(stream: MediaStream): Promise<void>;
30
72
  mute(): void;
31
73
  unmute(): void;
@@ -34,6 +76,8 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
34
76
  private startSpeakingDetection;
35
77
  private stopSpeakingDetection;
36
78
  private onStartSpeaking;
79
+ private onConfirmSpeaking;
80
+ private onCancelSpeaking;
37
81
  private onStopSpeaking;
38
82
  private onDataAvailable;
39
83
  private changeState;
@@ -55,29 +99,56 @@ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
55
99
  private getMaxVolume;
56
100
  }
57
101
 
102
+ declare let audioContext: AudioContext | undefined;
58
103
  declare let micAnalyser: AudioAnalyser | undefined;
59
104
  declare const defaultMicThreshold = -50;
60
- declare function getMicConstraints(deviceId?: string): MediaStreamConstraints;
105
+ /**
106
+ * Starts the microphone
107
+ * @param deviceId - The deviceId to use
108
+ * @returns The microphone stream
109
+ */
61
110
  declare function startMicrophone(deviceId?: string): Promise<MediaStream>;
111
+ /**
112
+ * Stops the microphone
113
+ */
62
114
  declare function stopMicrophone(): void;
63
- declare function createDelayedStream(stream: MediaStream): MediaStream;
64
115
 
65
- declare const microphone_createDelayedStream: typeof createDelayedStream;
116
+ declare const microphone_audioContext: typeof audioContext;
66
117
  declare const microphone_defaultMicThreshold: typeof defaultMicThreshold;
67
- declare const microphone_getMicConstraints: typeof getMicConstraints;
68
118
  declare const microphone_micAnalyser: typeof micAnalyser;
69
119
  declare const microphone_startMicrophone: typeof startMicrophone;
70
120
  declare const microphone_stopMicrophone: typeof stopMicrophone;
71
121
  declare namespace microphone {
72
- export { microphone_createDelayedStream as createDelayedStream, microphone_defaultMicThreshold as defaultMicThreshold, microphone_getMicConstraints as getMicConstraints, microphone_micAnalyser as micAnalyser, microphone_startMicrophone as startMicrophone, microphone_stopMicrophone as stopMicrophone };
122
+ export { microphone_audioContext as audioContext, microphone_defaultMicThreshold as defaultMicThreshold, microphone_micAnalyser as micAnalyser, microphone_startMicrophone as startMicrophone, microphone_stopMicrophone as stopMicrophone };
73
123
  }
74
124
 
75
125
  declare let speakerAnalyser: AudioAnalyser | undefined;
126
+ /**
127
+ * Checks if the speaker device can be changed
128
+ * @returns True if the speaker device can be changed, false otherwise
129
+ */
76
130
  declare function canChangeSpeakerDevice(): boolean;
131
+ /**
132
+ * Changes the speaker device
133
+ * @param speakerId - The speakerId to use
134
+ */
77
135
  declare function changeSpeakerDevice(speakerId: string): Promise<void>;
136
+ /**
137
+ * Pauses the audio
138
+ */
78
139
  declare function pauseAudio(): void;
140
+ /**
141
+ * Resumes the audio
142
+ */
79
143
  declare function resumeAudio(): void;
144
+ /**
145
+ * Plays the audio
146
+ * @param blob - The blob to play (adds to the queue if already playing)
147
+ */
80
148
  declare function playAudio(blob: Blob): Promise<void>;
149
+ /**
150
+ * Stops the audio
151
+ */
81
152
  declare function stopAudio(): void;
82
153
 
83
154
  declare const speaker_canChangeSpeakerDevice: typeof canChangeSpeakerDevice;
@@ -91,6 +162,41 @@ declare namespace speaker {
91
162
  export { speaker_canChangeSpeakerDevice as canChangeSpeakerDevice, speaker_changeSpeakerDevice as changeSpeakerDevice, speaker_pauseAudio as pauseAudio, speaker_playAudio as playAudio, speaker_resumeAudio as resumeAudio, speaker_speakerAnalyser as speakerAnalyser, speaker_stopAudio as stopAudio };
92
163
  }
93
164
 
165
+ /**
166
+ * Silero VAD
167
+ * @see https://github.com/ricky0123/vad
168
+ */
169
+ declare class SileroVAD extends VAD {
170
+ private vad;
171
+ get isStarted(): boolean;
172
+ start(stream: MediaStream, threshold?: number): Promise<void>;
173
+ stop(): Promise<void>;
174
+ setThreshold(threshold: number): Promise<void>;
175
+ }
176
+
177
+ /**
178
+ * Volume-based Voice Activity Detection
179
+ * Based on the hark library implementation
180
+ */
181
+ declare class VolumeVAD extends VAD {
182
+ private audioContext;
183
+ private analyser;
184
+ private sourceNode;
185
+ private fftBins;
186
+ private running;
187
+ private speaking;
188
+ private attemptSpeaking;
189
+ private history;
190
+ private speakingHistory;
191
+ constructor();
192
+ private getMaxVolume;
193
+ get isStarted(): boolean;
194
+ start(stream: MediaStream, threshold?: number): Promise<void>;
195
+ private startLoop;
196
+ stop(): Promise<void>;
197
+ setThreshold(threshold: number): Promise<void>;
198
+ }
199
+
94
200
  declare enum CallHandlerErrorCode {
95
201
  Mic = "Mic",
96
202
  Unauthorized = "Unauthorized",
@@ -106,9 +212,12 @@ interface CallHandlerEvents {
106
212
  Error: [CallHandlerError];
107
213
  StateChange: void;
108
214
  }
109
- declare class CallHandler<Params extends {}> extends EventEmitter<CallHandlerEvents> {
215
+ interface CallHandlerOptions {
216
+ vad?: VAD | 'volume' | 'silero';
217
+ }
218
+ declare class CallHandler<Params extends {}, Options extends CallHandlerOptions = CallHandlerOptions> extends EventEmitter<CallHandlerEvents> {
110
219
  private static instance;
111
- static getInstance<T extends {}>(): CallHandler<T>;
220
+ static getInstance<T extends {}, Options extends CallHandlerOptions = CallHandlerOptions>(options?: Options): CallHandler<T, Options>;
112
221
  url?: string;
113
222
  params?: Params;
114
223
  micRecorder: MicRecorder;
@@ -119,6 +228,7 @@ declare class CallHandler<Params extends {}> extends EventEmitter<CallHandlerEve
119
228
  private startTime;
120
229
  private lastAudioBlob?;
121
230
  private constructor();
231
+ private getVAD;
122
232
  get isStarted(): boolean;
123
233
  get isStarting(): boolean;
124
234
  get isWSStarted(): boolean;
@@ -170,4 +280,4 @@ interface ConversationMessage {
170
280
  content: string;
171
281
  }
172
282
 
173
- export { CallClientCommands, type CallConfig, CallHandler, CallHandlerError, CallHandlerErrorCode, type CallHandlerEvents, CallServerCommands, type CallSummary, type Conversation, type ConversationMessage, LocalStorageKeys, microphone as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, speaker as Speaker };
283
+ export { CallClientCommands, type CallConfig, CallHandler, CallHandlerError, CallHandlerErrorCode, type CallHandlerEvents, type CallHandlerOptions, CallServerCommands, type CallSummary, type Conversation, type ConversationMessage, LocalStorageKeys, microphone as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, SileroVAD, speaker as Speaker, VAD, VolumeVAD };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,46 @@ declare enum LocalStorageKeys {
6
6
  MicThreshold = "micdrop.micThreshold"
7
7
  }
8
8
 
9
+ interface VADEvents {
10
+ StartSpeaking: void;
11
+ ConfirmSpeaking: void;
12
+ CancelSpeaking: void;
13
+ StopSpeaking: void;
14
+ }
15
+ /**
16
+ * Abstract VAD class
17
+ * VAD = Voice Activity Detection
18
+ */
19
+ declare abstract class VAD extends EventEmitter<VADEvents> {
20
+ delay: number;
21
+ protected threshold: number;
22
+ /**
23
+ * Constructor for the VAD class
24
+ * @param threshold - The threshold to use for the VAD
25
+ */
26
+ constructor(threshold?: number);
27
+ /**
28
+ * Checks if the VAD is started
29
+ * @returns True if the VAD is started, false otherwise
30
+ */
31
+ abstract get isStarted(): boolean;
32
+ /**
33
+ * Starts the VAD
34
+ * @param stream - The stream to start the VAD on
35
+ * @param threshold - The threshold to use for the VAD
36
+ */
37
+ abstract start(stream: MediaStream, threshold?: number): Promise<void>;
38
+ /**
39
+ * Stops the VAD
40
+ */
41
+ abstract stop(): Promise<void>;
42
+ /**
43
+ * Sets the threshold for the VAD
44
+ * @param threshold - The threshold to use for the VAD
45
+ */
46
+ abstract setThreshold(threshold: number): Promise<void>;
47
+ }
48
+
9
49
  interface MicRecorderState {
10
50
  isStarting: boolean;
11
51
  isStarted: boolean;
@@ -22,10 +62,12 @@ interface MicRecorderEvents {
22
62
  declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
23
63
  state: MicRecorderState;
24
64
  private audioInfo;
25
- private hark;
65
+ private vad;
26
66
  private recorder;
27
67
  private delayedStream;
28
- constructor();
68
+ private speakingConfirmed;
69
+ private queuedChunks;
70
+ constructor(vad: VAD);
29
71
  start(stream: MediaStream): Promise<void>;
30
72
  mute(): void;
31
73
  unmute(): void;
@@ -34,6 +76,8 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
34
76
  private startSpeakingDetection;
35
77
  private stopSpeakingDetection;
36
78
  private onStartSpeaking;
79
+ private onConfirmSpeaking;
80
+ private onCancelSpeaking;
37
81
  private onStopSpeaking;
38
82
  private onDataAvailable;
39
83
  private changeState;
@@ -55,29 +99,56 @@ declare class AudioAnalyser extends EventEmitter<AudioAnalyserEvents> {
55
99
  private getMaxVolume;
56
100
  }
57
101
 
102
+ declare let audioContext: AudioContext | undefined;
58
103
  declare let micAnalyser: AudioAnalyser | undefined;
59
104
  declare const defaultMicThreshold = -50;
60
- declare function getMicConstraints(deviceId?: string): MediaStreamConstraints;
105
+ /**
106
+ * Starts the microphone
107
+ * @param deviceId - The deviceId to use
108
+ * @returns The microphone stream
109
+ */
61
110
  declare function startMicrophone(deviceId?: string): Promise<MediaStream>;
111
+ /**
112
+ * Stops the microphone
113
+ */
62
114
  declare function stopMicrophone(): void;
63
- declare function createDelayedStream(stream: MediaStream): MediaStream;
64
115
 
65
- declare const microphone_createDelayedStream: typeof createDelayedStream;
116
+ declare const microphone_audioContext: typeof audioContext;
66
117
  declare const microphone_defaultMicThreshold: typeof defaultMicThreshold;
67
- declare const microphone_getMicConstraints: typeof getMicConstraints;
68
118
  declare const microphone_micAnalyser: typeof micAnalyser;
69
119
  declare const microphone_startMicrophone: typeof startMicrophone;
70
120
  declare const microphone_stopMicrophone: typeof stopMicrophone;
71
121
  declare namespace microphone {
72
- export { microphone_createDelayedStream as createDelayedStream, microphone_defaultMicThreshold as defaultMicThreshold, microphone_getMicConstraints as getMicConstraints, microphone_micAnalyser as micAnalyser, microphone_startMicrophone as startMicrophone, microphone_stopMicrophone as stopMicrophone };
122
+ export { microphone_audioContext as audioContext, microphone_defaultMicThreshold as defaultMicThreshold, microphone_micAnalyser as micAnalyser, microphone_startMicrophone as startMicrophone, microphone_stopMicrophone as stopMicrophone };
73
123
  }
74
124
 
75
125
  declare let speakerAnalyser: AudioAnalyser | undefined;
126
+ /**
127
+ * Checks if the speaker device can be changed
128
+ * @returns True if the speaker device can be changed, false otherwise
129
+ */
76
130
  declare function canChangeSpeakerDevice(): boolean;
131
+ /**
132
+ * Changes the speaker device
133
+ * @param speakerId - The speakerId to use
134
+ */
77
135
  declare function changeSpeakerDevice(speakerId: string): Promise<void>;
136
+ /**
137
+ * Pauses the audio
138
+ */
78
139
  declare function pauseAudio(): void;
140
+ /**
141
+ * Resumes the audio
142
+ */
79
143
  declare function resumeAudio(): void;
144
+ /**
145
+ * Plays the audio
146
+ * @param blob - The blob to play (adds to the queue if already playing)
147
+ */
80
148
  declare function playAudio(blob: Blob): Promise<void>;
149
+ /**
150
+ * Stops the audio
151
+ */
81
152
  declare function stopAudio(): void;
82
153
 
83
154
  declare const speaker_canChangeSpeakerDevice: typeof canChangeSpeakerDevice;
@@ -91,6 +162,41 @@ declare namespace speaker {
91
162
  export { speaker_canChangeSpeakerDevice as canChangeSpeakerDevice, speaker_changeSpeakerDevice as changeSpeakerDevice, speaker_pauseAudio as pauseAudio, speaker_playAudio as playAudio, speaker_resumeAudio as resumeAudio, speaker_speakerAnalyser as speakerAnalyser, speaker_stopAudio as stopAudio };
92
163
  }
93
164
 
165
+ /**
166
+ * Silero VAD
167
+ * @see https://github.com/ricky0123/vad
168
+ */
169
+ declare class SileroVAD extends VAD {
170
+ private vad;
171
+ get isStarted(): boolean;
172
+ start(stream: MediaStream, threshold?: number): Promise<void>;
173
+ stop(): Promise<void>;
174
+ setThreshold(threshold: number): Promise<void>;
175
+ }
176
+
177
+ /**
178
+ * Volume-based Voice Activity Detection
179
+ * Based on the hark library implementation
180
+ */
181
+ declare class VolumeVAD extends VAD {
182
+ private audioContext;
183
+ private analyser;
184
+ private sourceNode;
185
+ private fftBins;
186
+ private running;
187
+ private speaking;
188
+ private attemptSpeaking;
189
+ private history;
190
+ private speakingHistory;
191
+ constructor();
192
+ private getMaxVolume;
193
+ get isStarted(): boolean;
194
+ start(stream: MediaStream, threshold?: number): Promise<void>;
195
+ private startLoop;
196
+ stop(): Promise<void>;
197
+ setThreshold(threshold: number): Promise<void>;
198
+ }
199
+
94
200
  declare enum CallHandlerErrorCode {
95
201
  Mic = "Mic",
96
202
  Unauthorized = "Unauthorized",
@@ -106,9 +212,12 @@ interface CallHandlerEvents {
106
212
  Error: [CallHandlerError];
107
213
  StateChange: void;
108
214
  }
109
- declare class CallHandler<Params extends {}> extends EventEmitter<CallHandlerEvents> {
215
+ interface CallHandlerOptions {
216
+ vad?: VAD | 'volume' | 'silero';
217
+ }
218
+ declare class CallHandler<Params extends {}, Options extends CallHandlerOptions = CallHandlerOptions> extends EventEmitter<CallHandlerEvents> {
110
219
  private static instance;
111
- static getInstance<T extends {}>(): CallHandler<T>;
220
+ static getInstance<T extends {}, Options extends CallHandlerOptions = CallHandlerOptions>(options?: Options): CallHandler<T, Options>;
112
221
  url?: string;
113
222
  params?: Params;
114
223
  micRecorder: MicRecorder;
@@ -119,6 +228,7 @@ declare class CallHandler<Params extends {}> extends EventEmitter<CallHandlerEve
119
228
  private startTime;
120
229
  private lastAudioBlob?;
121
230
  private constructor();
231
+ private getVAD;
122
232
  get isStarted(): boolean;
123
233
  get isStarting(): boolean;
124
234
  get isWSStarted(): boolean;
@@ -170,4 +280,4 @@ interface ConversationMessage {
170
280
  content: string;
171
281
  }
172
282
 
173
- export { CallClientCommands, type CallConfig, CallHandler, CallHandlerError, CallHandlerErrorCode, type CallHandlerEvents, CallServerCommands, type CallSummary, type Conversation, type ConversationMessage, LocalStorageKeys, microphone as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, speaker as Speaker };
283
+ export { CallClientCommands, type CallConfig, CallHandler, CallHandlerError, CallHandlerErrorCode, type CallHandlerEvents, type CallHandlerOptions, CallServerCommands, type CallSummary, type Conversation, type ConversationMessage, LocalStorageKeys, microphone as Mic, MicRecorder, type MicRecorderEvents, type MicRecorderState, SileroVAD, speaker as Speaker, VAD, VolumeVAD };