@micdrop/client 1.4.0 → 1.5.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 +80 -86
- package/dist/index.d.ts +80 -86
- package/dist/index.js +423 -390
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +423 -396
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -36,27 +36,15 @@ __export(index_exports, {
|
|
|
36
36
|
CallHandlerErrorCode: () => CallHandlerErrorCode,
|
|
37
37
|
CallServerCommands: () => CallServerCommands,
|
|
38
38
|
LocalStorageKeys: () => LocalStorageKeys,
|
|
39
|
-
Mic: () =>
|
|
39
|
+
Mic: () => mic,
|
|
40
40
|
MicRecorder: () => MicRecorder,
|
|
41
41
|
SileroVAD: () => SileroVAD,
|
|
42
|
-
Speaker: () =>
|
|
42
|
+
Speaker: () => speaker,
|
|
43
43
|
VAD: () => VAD,
|
|
44
44
|
VolumeVAD: () => VolumeVAD
|
|
45
45
|
});
|
|
46
46
|
module.exports = __toCommonJS(index_exports);
|
|
47
47
|
|
|
48
|
-
// src/audio/MicRecorder.ts
|
|
49
|
-
var import_eventemitter32 = __toESM(require("eventemitter3"));
|
|
50
|
-
|
|
51
|
-
// src/audio/microphone.ts
|
|
52
|
-
var microphone_exports = {};
|
|
53
|
-
__export(microphone_exports, {
|
|
54
|
-
defaultMicThreshold: () => defaultMicThreshold,
|
|
55
|
-
micAnalyser: () => micAnalyser,
|
|
56
|
-
startMicrophone: () => startMicrophone,
|
|
57
|
-
stopMicrophone: () => stopMicrophone
|
|
58
|
-
});
|
|
59
|
-
|
|
60
48
|
// src/audio/utils/AudioAnalyser.ts
|
|
61
49
|
var import_eventemitter3 = __toESM(require("eventemitter3"));
|
|
62
50
|
var AudioAnalyser = class extends import_eventemitter3.default {
|
|
@@ -92,8 +80,42 @@ var AudioAnalyser = class extends import_eventemitter3.default {
|
|
|
92
80
|
}
|
|
93
81
|
};
|
|
94
82
|
|
|
83
|
+
// src/audio/utils/unlock.ts
|
|
84
|
+
function unlock(audioContext2) {
|
|
85
|
+
const events = ["touchstart", "touchend", "mousedown", "keydown"];
|
|
86
|
+
const unlock2 = async () => {
|
|
87
|
+
unlockSafari(audioContext2);
|
|
88
|
+
if (audioContext2.state === "suspended") {
|
|
89
|
+
await audioContext2.resume();
|
|
90
|
+
}
|
|
91
|
+
events.forEach((e) => document.body.removeEventListener(e, unlock2));
|
|
92
|
+
};
|
|
93
|
+
events.forEach((e) => document.body.addEventListener(e, unlock2, false));
|
|
94
|
+
}
|
|
95
|
+
function unlockSafari(audioContext2) {
|
|
96
|
+
const source = audioContext2.createBufferSource();
|
|
97
|
+
source.buffer = audioContext2.createBuffer(1, 1, 22050);
|
|
98
|
+
source.connect(audioContext2.destination);
|
|
99
|
+
const onEnded = () => {
|
|
100
|
+
source.disconnect();
|
|
101
|
+
source.buffer = null;
|
|
102
|
+
source.removeEventListener("ended", onEnded);
|
|
103
|
+
};
|
|
104
|
+
source.addEventListener("ended", onEnded);
|
|
105
|
+
source.start();
|
|
106
|
+
}
|
|
107
|
+
|
|
95
108
|
// src/audio/utils/audioContext.ts
|
|
96
|
-
var audioContext
|
|
109
|
+
var audioContext;
|
|
110
|
+
function createAudioContext() {
|
|
111
|
+
return new (window.AudioContext || window.webkitAudioContext)();
|
|
112
|
+
}
|
|
113
|
+
if (window.micdropAudioContext) {
|
|
114
|
+
audioContext = window.micdropAudioContext;
|
|
115
|
+
} else {
|
|
116
|
+
window.micdropAudioContext = audioContext = createAudioContext();
|
|
117
|
+
unlock(audioContext);
|
|
118
|
+
}
|
|
97
119
|
|
|
98
120
|
// src/audio/utils/localStorage.ts
|
|
99
121
|
var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
|
|
@@ -108,71 +130,86 @@ function stopStream(stream) {
|
|
|
108
130
|
stream?.getTracks().forEach((track) => track.stop());
|
|
109
131
|
}
|
|
110
132
|
|
|
111
|
-
// src/audio/
|
|
112
|
-
var
|
|
113
|
-
var audioStream;
|
|
114
|
-
var sourceNode;
|
|
115
|
-
var micAnalyser = new AudioAnalyser(audioContext);
|
|
133
|
+
// src/audio/mic.ts
|
|
134
|
+
var mic;
|
|
116
135
|
var defaultMicThreshold = -50;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (!deviceId) {
|
|
135
|
-
deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
|
|
136
|
+
var Mic = class {
|
|
137
|
+
constructor() {
|
|
138
|
+
this.defaultThreshold = defaultMicThreshold;
|
|
139
|
+
this.analyser = new AudioAnalyser(audioContext);
|
|
140
|
+
}
|
|
141
|
+
getMicConstraints(deviceId) {
|
|
142
|
+
return {
|
|
143
|
+
audio: {
|
|
144
|
+
deviceId: { ideal: deviceId },
|
|
145
|
+
sampleRate: 16e3,
|
|
146
|
+
// not working, it will follow device settings, usually 44.1kHz
|
|
147
|
+
sampleSize: 16,
|
|
148
|
+
channelCount: 1,
|
|
149
|
+
echoCancellation: true,
|
|
150
|
+
noiseSuppression: true
|
|
151
|
+
}
|
|
152
|
+
};
|
|
136
153
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Starts the microphone
|
|
156
|
+
* @param deviceId - The deviceId to use
|
|
157
|
+
* @returns The microphone stream
|
|
158
|
+
*/
|
|
159
|
+
async start(deviceId) {
|
|
160
|
+
if (!audioContext || !this.analyser) {
|
|
161
|
+
throw new Error("AudioContext not initialized");
|
|
140
162
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
sourceNode.disconnect();
|
|
163
|
-
sourceNode =
|
|
163
|
+
if (!deviceId) {
|
|
164
|
+
deviceId = localStorage.getItem("micdrop.micDevice" /* MicDevice */) || void 0;
|
|
165
|
+
}
|
|
166
|
+
if (this.audioStream) {
|
|
167
|
+
if (this.currentDeviceId === deviceId) {
|
|
168
|
+
return this.audioStream;
|
|
169
|
+
}
|
|
170
|
+
stopStream(this.audioStream);
|
|
171
|
+
}
|
|
172
|
+
this.audioStream = await navigator.mediaDevices.getUserMedia(
|
|
173
|
+
this.getMicConstraints(deviceId)
|
|
174
|
+
);
|
|
175
|
+
const track = this.audioStream.getTracks()[0];
|
|
176
|
+
const newDeviceId = track && track.getCapabilities && track.getCapabilities().deviceId;
|
|
177
|
+
if (newDeviceId) {
|
|
178
|
+
localStorage.setItem("micdrop.micDevice" /* MicDevice */, newDeviceId);
|
|
179
|
+
this.currentDeviceId = newDeviceId;
|
|
180
|
+
} else {
|
|
181
|
+
localStorage.removeItem("micdrop.micDevice" /* MicDevice */);
|
|
182
|
+
this.currentDeviceId = void 0;
|
|
183
|
+
}
|
|
184
|
+
if (this.sourceNode) this.sourceNode.disconnect();
|
|
185
|
+
this.sourceNode = audioContext.createMediaStreamSource(this.audioStream);
|
|
186
|
+
this.sourceNode.connect(this.analyser.node);
|
|
187
|
+
return this.audioStream;
|
|
164
188
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Stops the microphone
|
|
191
|
+
*/
|
|
192
|
+
stop() {
|
|
193
|
+
if (this.sourceNode) {
|
|
194
|
+
this.sourceNode.disconnect();
|
|
195
|
+
this.sourceNode = void 0;
|
|
196
|
+
}
|
|
197
|
+
if (this.audioStream) {
|
|
198
|
+
stopStream(this.audioStream);
|
|
199
|
+
this.audioStream = void 0;
|
|
200
|
+
}
|
|
168
201
|
}
|
|
202
|
+
};
|
|
203
|
+
if (!window.micdropMic) {
|
|
204
|
+
window.micdropMic = new Mic();
|
|
169
205
|
}
|
|
206
|
+
mic = window.micdropMic;
|
|
207
|
+
|
|
208
|
+
// src/audio/MicRecorder.ts
|
|
209
|
+
var import_eventemitter33 = __toESM(require("eventemitter3"));
|
|
170
210
|
|
|
171
|
-
// src/audio/utils/
|
|
211
|
+
// src/audio/utils/delayedStream.ts
|
|
172
212
|
function createDelayedStream(stream, delay) {
|
|
173
|
-
if (!audioContext) {
|
|
174
|
-
throw new Error("AudioContext not initialized");
|
|
175
|
-
}
|
|
176
213
|
const audioSource = audioContext.createMediaStreamSource(stream);
|
|
177
214
|
const delayNode = audioContext.createDelay(delay);
|
|
178
215
|
delayNode.delayTime.value = delay;
|
|
@@ -182,6 +219,166 @@ function createDelayedStream(stream, delay) {
|
|
|
182
219
|
return audioDestination.stream;
|
|
183
220
|
}
|
|
184
221
|
|
|
222
|
+
// src/audio/vad/SileroVAD.ts
|
|
223
|
+
var import_src = require("@ricky0123/vad-web/src");
|
|
224
|
+
|
|
225
|
+
// src/audio/vad/VAD.ts
|
|
226
|
+
var import_eventemitter32 = __toESM(require("eventemitter3"));
|
|
227
|
+
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();
|
|
234
|
+
// Max delay to detect speech, can be used to record delayed stream
|
|
235
|
+
this.delay = 100;
|
|
236
|
+
this.threshold = threshold;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// src/audio/vad/SileroVAD.ts
|
|
241
|
+
var SileroVAD = class extends VAD {
|
|
242
|
+
get isStarted() {
|
|
243
|
+
return !!this.vad;
|
|
244
|
+
}
|
|
245
|
+
async start(stream, threshold) {
|
|
246
|
+
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
|
+
this.vad = await import_src.MicVAD.new({
|
|
255
|
+
stream,
|
|
256
|
+
model: "v5",
|
|
257
|
+
onSpeechStart: () => this.emit("StartSpeaking"),
|
|
258
|
+
onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
|
|
259
|
+
onVADMisfire: () => this.emit("CancelSpeaking"),
|
|
260
|
+
onSpeechEnd: () => this.emit("StopSpeaking"),
|
|
261
|
+
positiveSpeechThreshold: speechThreshold,
|
|
262
|
+
negativeSpeechThreshold: speechThreshold * 0.7,
|
|
263
|
+
minSpeechFrames: 3,
|
|
264
|
+
redemptionFrames: 9
|
|
265
|
+
});
|
|
266
|
+
this.vad.start();
|
|
267
|
+
}
|
|
268
|
+
async stop() {
|
|
269
|
+
if (!this.vad) return;
|
|
270
|
+
this.vad.destroy();
|
|
271
|
+
this.vad = void 0;
|
|
272
|
+
}
|
|
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
|
+
}
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// src/audio/vad/VolumeVAD.ts
|
|
284
|
+
var VolumeVAD = class extends VAD {
|
|
285
|
+
constructor() {
|
|
286
|
+
super();
|
|
287
|
+
this.running = false;
|
|
288
|
+
this.speaking = false;
|
|
289
|
+
this.attemptSpeaking = false;
|
|
290
|
+
this.history = 10;
|
|
291
|
+
this.speakingHistory = [];
|
|
292
|
+
for (let i = 0; i < this.history; i++) {
|
|
293
|
+
this.speakingHistory.push(0);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
getMaxVolume() {
|
|
297
|
+
if (!this.analyser || !this.fftBins) return -Infinity;
|
|
298
|
+
let maxVolume = -Infinity;
|
|
299
|
+
this.analyser.getFloatFrequencyData(this.fftBins);
|
|
300
|
+
for (let i = 4; i < this.fftBins.length; i++) {
|
|
301
|
+
if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
|
|
302
|
+
maxVolume = this.fftBins[i];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return maxVolume;
|
|
306
|
+
}
|
|
307
|
+
get isStarted() {
|
|
308
|
+
return this.running;
|
|
309
|
+
}
|
|
310
|
+
async start(stream, threshold) {
|
|
311
|
+
if (this.running) return;
|
|
312
|
+
if (threshold !== void 0) {
|
|
313
|
+
this.threshold = threshold;
|
|
314
|
+
}
|
|
315
|
+
this.analyser = audioContext.createAnalyser();
|
|
316
|
+
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
317
|
+
this.analyser.fftSize = 512;
|
|
318
|
+
this.analyser.smoothingTimeConstant = 0.1;
|
|
319
|
+
this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
|
|
320
|
+
this.sourceNode.connect(this.analyser);
|
|
321
|
+
this.running = true;
|
|
322
|
+
this.startLoop();
|
|
323
|
+
}
|
|
324
|
+
startLoop() {
|
|
325
|
+
if (!this.running) return;
|
|
326
|
+
const currentVolume = this.getMaxVolume();
|
|
327
|
+
if (currentVolume > this.threshold) {
|
|
328
|
+
if (!this.speaking) {
|
|
329
|
+
let recentHistory = 0;
|
|
330
|
+
for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
|
|
331
|
+
recentHistory += this.speakingHistory[i];
|
|
332
|
+
}
|
|
333
|
+
if (recentHistory >= 2) {
|
|
334
|
+
this.speaking = true;
|
|
335
|
+
this.attemptSpeaking = false;
|
|
336
|
+
this.emit("ConfirmSpeaking");
|
|
337
|
+
} else if (recentHistory === 1) {
|
|
338
|
+
this.attemptSpeaking = true;
|
|
339
|
+
this.emit("StartSpeaking");
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} else if (this.speaking) {
|
|
343
|
+
let totalHistory = 0;
|
|
344
|
+
for (let i = 0; i < this.speakingHistory.length; i++) {
|
|
345
|
+
totalHistory += this.speakingHistory[i];
|
|
346
|
+
}
|
|
347
|
+
if (totalHistory === 0) {
|
|
348
|
+
this.speaking = false;
|
|
349
|
+
this.emit("StopSpeaking");
|
|
350
|
+
}
|
|
351
|
+
} else if (this.attemptSpeaking) {
|
|
352
|
+
let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
|
|
353
|
+
if (lastHistory === 0) {
|
|
354
|
+
this.attemptSpeaking = false;
|
|
355
|
+
this.emit("CancelSpeaking");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
this.speakingHistory.shift();
|
|
359
|
+
this.speakingHistory.push(Number(currentVolume > this.threshold));
|
|
360
|
+
setTimeout(() => this.startLoop(), this.delay);
|
|
361
|
+
}
|
|
362
|
+
async stop() {
|
|
363
|
+
if (!this.running) return;
|
|
364
|
+
this.running = false;
|
|
365
|
+
this.speaking = false;
|
|
366
|
+
if (this.sourceNode) {
|
|
367
|
+
this.sourceNode.disconnect();
|
|
368
|
+
this.sourceNode = void 0;
|
|
369
|
+
}
|
|
370
|
+
if (this.analyser) {
|
|
371
|
+
this.analyser.disconnect();
|
|
372
|
+
this.analyser = void 0;
|
|
373
|
+
}
|
|
374
|
+
this.fftBins = void 0;
|
|
375
|
+
}
|
|
376
|
+
async setThreshold(threshold) {
|
|
377
|
+
if (threshold === this.threshold) return;
|
|
378
|
+
this.threshold = threshold;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
185
382
|
// src/audio/MicRecorder.ts
|
|
186
383
|
var timeSlice = 100;
|
|
187
384
|
var defaultMicRecorderState = {
|
|
@@ -191,16 +388,21 @@ var defaultMicRecorderState = {
|
|
|
191
388
|
isSpeaking: false,
|
|
192
389
|
threshold: defaultMicThreshold
|
|
193
390
|
};
|
|
194
|
-
var MicRecorder = class extends
|
|
195
|
-
constructor(
|
|
391
|
+
var MicRecorder = class extends import_eventemitter33.default {
|
|
392
|
+
constructor(vadConfig) {
|
|
196
393
|
super();
|
|
394
|
+
this.vadConfig = vadConfig;
|
|
197
395
|
this.audioInfo = this.getAudioInfo();
|
|
198
396
|
this.speakingConfirmed = false;
|
|
199
397
|
this.queuedChunks = [];
|
|
200
398
|
this.onStartSpeaking = (async () => {
|
|
201
399
|
if (!this.recorder || this.state.isMuted) return;
|
|
202
|
-
|
|
203
|
-
|
|
400
|
+
try {
|
|
401
|
+
this.recorder.start(timeSlice);
|
|
402
|
+
this.queuedChunks.length = 0;
|
|
403
|
+
} catch (error) {
|
|
404
|
+
console.error(error);
|
|
405
|
+
}
|
|
204
406
|
}).bind(this);
|
|
205
407
|
this.onConfirmSpeaking = (async () => {
|
|
206
408
|
if (this.state.isMuted) return;
|
|
@@ -210,19 +412,26 @@ var MicRecorder = class extends import_eventemitter32.default {
|
|
|
210
412
|
}).bind(this);
|
|
211
413
|
this.onCancelSpeaking = (async () => {
|
|
212
414
|
if (!this.recorder) return;
|
|
213
|
-
|
|
415
|
+
try {
|
|
416
|
+
this.recorder.stop();
|
|
417
|
+
} catch (error) {
|
|
418
|
+
console.error(error);
|
|
419
|
+
}
|
|
214
420
|
this.queuedChunks.length = 0;
|
|
215
421
|
}).bind(this);
|
|
216
422
|
this.onStopSpeaking = (async () => {
|
|
217
423
|
if (!this.recorder) return;
|
|
218
|
-
|
|
424
|
+
try {
|
|
425
|
+
this.recorder.stop();
|
|
426
|
+
} catch (error) {
|
|
427
|
+
console.error(error);
|
|
428
|
+
}
|
|
219
429
|
this.changeState({ isSpeaking: false });
|
|
220
430
|
this.emit("StopSpeaking");
|
|
221
431
|
this.speakingConfirmed = false;
|
|
222
432
|
}).bind(this);
|
|
223
433
|
const threshold = parseFloat(localStorage.getItem("micdrop.micThreshold" /* MicThreshold */) || "0") || defaultMicThreshold;
|
|
224
434
|
this.state = { ...defaultMicRecorderState, threshold };
|
|
225
|
-
this.vad = vad;
|
|
226
435
|
}
|
|
227
436
|
async start(stream) {
|
|
228
437
|
if (this.state.isStarted) {
|
|
@@ -234,6 +443,9 @@ var MicRecorder = class extends import_eventemitter32.default {
|
|
|
234
443
|
isMuted: false,
|
|
235
444
|
isSpeaking: false
|
|
236
445
|
});
|
|
446
|
+
if (!this.vad) {
|
|
447
|
+
this.vad = this.initVAD(this.vadConfig);
|
|
448
|
+
}
|
|
237
449
|
const delayedStream = createDelayedStream(
|
|
238
450
|
stream,
|
|
239
451
|
this.vad.delay / 1e3 + 0.05
|
|
@@ -288,9 +500,18 @@ var MicRecorder = class extends import_eventemitter32.default {
|
|
|
288
500
|
setThreshold(threshold) {
|
|
289
501
|
this.changeState({ threshold });
|
|
290
502
|
localStorage.setItem("micdrop.micThreshold" /* MicThreshold */, threshold.toString());
|
|
291
|
-
this.vad
|
|
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;
|
|
292
510
|
}
|
|
293
511
|
async startSpeakingDetection(stream) {
|
|
512
|
+
if (!this.vad) {
|
|
513
|
+
throw new Error("VAD not initialized");
|
|
514
|
+
}
|
|
294
515
|
if (this.vad.isStarted) {
|
|
295
516
|
this.vad.stop();
|
|
296
517
|
}
|
|
@@ -301,6 +522,9 @@ var MicRecorder = class extends import_eventemitter32.default {
|
|
|
301
522
|
await this.vad.start(stream, this.state.threshold);
|
|
302
523
|
}
|
|
303
524
|
async stopSpeakingDetection() {
|
|
525
|
+
if (!this.vad) {
|
|
526
|
+
throw new Error("VAD not initialized");
|
|
527
|
+
}
|
|
304
528
|
this.vad.off("StartSpeaking", this.onStartSpeaking);
|
|
305
529
|
this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
306
530
|
this.vad.off("CancelSpeaking", this.onCancelSpeaking);
|
|
@@ -338,21 +562,6 @@ var MicRecorder = class extends import_eventemitter32.default {
|
|
|
338
562
|
}
|
|
339
563
|
};
|
|
340
564
|
|
|
341
|
-
// src/audio/speaker.ts
|
|
342
|
-
var speaker_exports = {};
|
|
343
|
-
__export(speaker_exports, {
|
|
344
|
-
canChangeSpeakerDevice: () => canChangeSpeakerDevice,
|
|
345
|
-
changeSpeakerDevice: () => changeSpeakerDevice,
|
|
346
|
-
disableStreaming: () => disableStreaming,
|
|
347
|
-
enableStreaming: () => enableStreaming,
|
|
348
|
-
isStreamingEnabled: () => isStreamingEnabled,
|
|
349
|
-
pauseAudio: () => pauseAudio,
|
|
350
|
-
playAudio: () => playAudio,
|
|
351
|
-
resumeAudio: () => resumeAudio,
|
|
352
|
-
speakerAnalyser: () => speakerAnalyser,
|
|
353
|
-
stopAudio: () => stopAudio
|
|
354
|
-
});
|
|
355
|
-
|
|
356
565
|
// src/audio/player/SpeakerPlayer.ts
|
|
357
566
|
var SpeakerPlayer = class {
|
|
358
567
|
constructor(outputNode) {
|
|
@@ -543,7 +752,7 @@ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
|
|
|
543
752
|
this.audioElement = void 0;
|
|
544
753
|
}
|
|
545
754
|
if (this.mediaStream) {
|
|
546
|
-
this.mediaStream
|
|
755
|
+
stopStream(this.mediaStream);
|
|
547
756
|
this.mediaStream = void 0;
|
|
548
757
|
}
|
|
549
758
|
this.mediaSource = void 0;
|
|
@@ -608,280 +817,94 @@ var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
|
608
817
|
}
|
|
609
818
|
};
|
|
610
819
|
|
|
611
|
-
// src/audio/
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
820
|
+
// src/audio/speaker.ts
|
|
821
|
+
var speaker;
|
|
822
|
+
var Speaker = class {
|
|
823
|
+
constructor() {
|
|
824
|
+
this.streamingEnabled = false;
|
|
825
|
+
this.analyser = new AudioAnalyser(audioContext);
|
|
826
|
+
this.audioElement = new Audio();
|
|
827
|
+
this.audioElement.autoplay = true;
|
|
828
|
+
const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
|
|
829
|
+
if (speakerId) {
|
|
830
|
+
this.setSinkId(speakerId).catch((error) => {
|
|
831
|
+
console.error("Failed to set sink ID:", error);
|
|
832
|
+
});
|
|
619
833
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
834
|
+
this.setupSpeakerPlayer();
|
|
835
|
+
const destinationNode = audioContext.createMediaStreamDestination();
|
|
836
|
+
this.analyser.node.connect(destinationNode);
|
|
837
|
+
this.audioElement.srcObject = destinationNode.stream;
|
|
624
838
|
}
|
|
625
|
-
|
|
626
|
-
function unlockSafari(audioContext2) {
|
|
627
|
-
const source = audioContext2.createBufferSource();
|
|
628
|
-
source.buffer = audioContext2.createBuffer(1, 1, 22050);
|
|
629
|
-
source.connect(audioContext2.destination);
|
|
630
|
-
const onEnded = () => {
|
|
631
|
-
source.disconnect();
|
|
632
|
-
source.buffer = null;
|
|
633
|
-
source.removeEventListener("ended", onEnded);
|
|
634
|
-
};
|
|
635
|
-
source.addEventListener("ended", onEnded);
|
|
636
|
-
source.start();
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
// src/audio/speaker.ts
|
|
640
|
-
var audioElement;
|
|
641
|
-
var player;
|
|
642
|
-
var streamingEnabled = false;
|
|
643
|
-
var speakerAnalyser = new AudioAnalyser(audioContext);
|
|
644
|
-
async function init() {
|
|
645
|
-
unlock(audioContext);
|
|
646
|
-
audioElement = new Audio();
|
|
647
|
-
audioElement.autoplay = true;
|
|
648
|
-
const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
|
|
649
|
-
if (speakerId) {
|
|
839
|
+
async setupSpeakerPlayer() {
|
|
650
840
|
try {
|
|
651
|
-
|
|
841
|
+
this.player?.destroy();
|
|
842
|
+
this.player = this.streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(this.analyser.node) : new SpeakerConcatPlayer(this.analyser.node) : new SpeakerSequencePlayer(this.analyser.node);
|
|
843
|
+
await this.player?.setup();
|
|
652
844
|
} catch (error) {
|
|
653
|
-
console.error("
|
|
845
|
+
console.error("Error setting up speaker player", error);
|
|
654
846
|
}
|
|
655
847
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
speakerAnalyser.node.connect(destinationNode);
|
|
659
|
-
audioElement.srcObject = destinationNode.stream;
|
|
660
|
-
}
|
|
661
|
-
init();
|
|
662
|
-
async function setupSpeakerPlayer() {
|
|
663
|
-
try {
|
|
664
|
-
player?.destroy();
|
|
665
|
-
player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
|
|
666
|
-
await player?.setup();
|
|
667
|
-
} catch (error) {
|
|
668
|
-
console.error("Error setting up speaker player", error);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
async function isStreamingEnabled() {
|
|
672
|
-
return streamingEnabled;
|
|
673
|
-
}
|
|
674
|
-
async function enableStreaming() {
|
|
675
|
-
if (streamingEnabled) return;
|
|
676
|
-
streamingEnabled = true;
|
|
677
|
-
await setupSpeakerPlayer();
|
|
678
|
-
}
|
|
679
|
-
async function disableStreaming() {
|
|
680
|
-
if (!streamingEnabled) return;
|
|
681
|
-
streamingEnabled = false;
|
|
682
|
-
await setupSpeakerPlayer();
|
|
683
|
-
}
|
|
684
|
-
function canChangeSpeakerDevice() {
|
|
685
|
-
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
686
|
-
}
|
|
687
|
-
async function setSinkId(speakerId) {
|
|
688
|
-
if (speakerId && canChangeSpeakerDevice() && audioElement) {
|
|
689
|
-
return audioElement.setSinkId(speakerId);
|
|
848
|
+
get isStreamingEnabled() {
|
|
849
|
+
return this.streamingEnabled;
|
|
690
850
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
await setSinkId(speakerId);
|
|
696
|
-
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
697
|
-
} catch (error) {
|
|
698
|
-
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
851
|
+
async enableStreaming() {
|
|
852
|
+
if (this.streamingEnabled) return;
|
|
853
|
+
this.streamingEnabled = true;
|
|
854
|
+
await this.setupSpeakerPlayer();
|
|
699
855
|
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
856
|
+
async disableStreaming() {
|
|
857
|
+
if (!this.streamingEnabled) return;
|
|
858
|
+
this.streamingEnabled = false;
|
|
859
|
+
await this.setupSpeakerPlayer();
|
|
704
860
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
if (audioElement) {
|
|
708
|
-
audioElement.play();
|
|
861
|
+
canChangeDevice() {
|
|
862
|
+
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
709
863
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
try {
|
|
714
|
-
player.addBlob(blob);
|
|
715
|
-
if (audioElement.paused) {
|
|
716
|
-
audioElement.play();
|
|
864
|
+
async setSinkId(speakerId) {
|
|
865
|
+
if (speakerId && this.canChangeDevice() && this.audioElement) {
|
|
866
|
+
return this.audioElement.setSinkId(speakerId);
|
|
717
867
|
}
|
|
718
|
-
} catch (error) {
|
|
719
|
-
console.error("Error playing audio blob", error, blob);
|
|
720
868
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
var import_src = require("@ricky0123/vad-web/src");
|
|
729
|
-
|
|
730
|
-
// src/audio/vad/VAD.ts
|
|
731
|
-
var import_eventemitter33 = __toESM(require("eventemitter3"));
|
|
732
|
-
var VAD = class extends import_eventemitter33.default {
|
|
733
|
-
/**
|
|
734
|
-
* Constructor for the VAD class
|
|
735
|
-
* @param threshold - The threshold to use for the VAD
|
|
736
|
-
*/
|
|
737
|
-
constructor(threshold = 0) {
|
|
738
|
-
super();
|
|
739
|
-
// Max delay to detect speech, can be used to record delayed stream
|
|
740
|
-
this.delay = 100;
|
|
741
|
-
this.threshold = threshold;
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
|
|
745
|
-
// src/audio/vad/SileroVAD.ts
|
|
746
|
-
var SileroVAD = class extends VAD {
|
|
747
|
-
get isStarted() {
|
|
748
|
-
return !!this.vad;
|
|
749
|
-
}
|
|
750
|
-
async start(stream, threshold) {
|
|
751
|
-
if (this.vad) return;
|
|
752
|
-
if (threshold !== void 0) {
|
|
753
|
-
this.threshold = threshold;
|
|
754
|
-
}
|
|
755
|
-
const speechThreshold = Math.min(
|
|
756
|
-
Math.max((this.threshold + 100) / 100, 0),
|
|
757
|
-
1
|
|
758
|
-
);
|
|
759
|
-
this.vad = await import_src.MicVAD.new({
|
|
760
|
-
stream,
|
|
761
|
-
onSpeechStart: () => this.emit("StartSpeaking"),
|
|
762
|
-
onSpeechRealStart: () => this.emit("ConfirmSpeaking"),
|
|
763
|
-
onVADMisfire: () => this.emit("CancelSpeaking"),
|
|
764
|
-
onSpeechEnd: () => this.emit("StopSpeaking"),
|
|
765
|
-
positiveSpeechThreshold: speechThreshold,
|
|
766
|
-
negativeSpeechThreshold: speechThreshold * 0.7,
|
|
767
|
-
// Adjust negative threshold proportionally
|
|
768
|
-
redemptionFrames: 5
|
|
769
|
-
});
|
|
770
|
-
this.vad.start();
|
|
771
|
-
}
|
|
772
|
-
async stop() {
|
|
773
|
-
if (!this.vad) return;
|
|
774
|
-
this.vad.pause();
|
|
775
|
-
this.vad = void 0;
|
|
776
|
-
}
|
|
777
|
-
async setThreshold(threshold) {
|
|
778
|
-
if (threshold === this.threshold) return;
|
|
779
|
-
if (this.vad) {
|
|
780
|
-
const stream = this.vad["stream"];
|
|
781
|
-
await this.stop();
|
|
782
|
-
await this.start(stream, threshold);
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
};
|
|
786
|
-
|
|
787
|
-
// src/audio/vad/VolumeVAD.ts
|
|
788
|
-
var VolumeVAD = class extends VAD {
|
|
789
|
-
constructor() {
|
|
790
|
-
super();
|
|
791
|
-
this.running = false;
|
|
792
|
-
this.speaking = false;
|
|
793
|
-
this.attemptSpeaking = false;
|
|
794
|
-
this.history = 10;
|
|
795
|
-
this.speakingHistory = [];
|
|
796
|
-
for (let i = 0; i < this.history; i++) {
|
|
797
|
-
this.speakingHistory.push(0);
|
|
869
|
+
async changeDevice(speakerId) {
|
|
870
|
+
if (!this.canChangeDevice()) return;
|
|
871
|
+
try {
|
|
872
|
+
await this.setSinkId(speakerId);
|
|
873
|
+
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
874
|
+
} catch (error) {
|
|
875
|
+
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
798
876
|
}
|
|
799
877
|
}
|
|
800
|
-
|
|
801
|
-
if (
|
|
802
|
-
|
|
803
|
-
this.analyser.getFloatFrequencyData(this.fftBins);
|
|
804
|
-
for (let i = 4; i < this.fftBins.length; i++) {
|
|
805
|
-
if (this.fftBins[i] > maxVolume && this.fftBins[i] < 0) {
|
|
806
|
-
maxVolume = this.fftBins[i];
|
|
807
|
-
}
|
|
878
|
+
pauseAudio() {
|
|
879
|
+
if (this.audioElement) {
|
|
880
|
+
this.audioElement.pause();
|
|
808
881
|
}
|
|
809
|
-
return maxVolume;
|
|
810
882
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
async start(stream, threshold) {
|
|
815
|
-
if (this.running) return;
|
|
816
|
-
if (threshold !== void 0) {
|
|
817
|
-
this.threshold = threshold;
|
|
883
|
+
resumeAudio() {
|
|
884
|
+
if (this.audioElement) {
|
|
885
|
+
this.audioElement.play();
|
|
818
886
|
}
|
|
819
|
-
this.analyser = audioContext.createAnalyser();
|
|
820
|
-
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
821
|
-
this.analyser.fftSize = 512;
|
|
822
|
-
this.analyser.smoothingTimeConstant = 0.1;
|
|
823
|
-
this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
|
|
824
|
-
this.sourceNode.connect(this.analyser);
|
|
825
|
-
this.running = true;
|
|
826
|
-
this.startLoop();
|
|
827
887
|
}
|
|
828
|
-
|
|
829
|
-
if (!this.
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
if (
|
|
833
|
-
|
|
834
|
-
for (let i = this.speakingHistory.length - 3; i < this.speakingHistory.length; i++) {
|
|
835
|
-
recentHistory += this.speakingHistory[i];
|
|
836
|
-
}
|
|
837
|
-
if (recentHistory >= 2) {
|
|
838
|
-
this.speaking = true;
|
|
839
|
-
this.attemptSpeaking = false;
|
|
840
|
-
this.emit("ConfirmSpeaking");
|
|
841
|
-
} else if (recentHistory === 1) {
|
|
842
|
-
this.attemptSpeaking = true;
|
|
843
|
-
this.emit("StartSpeaking");
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
} else if (this.speaking) {
|
|
847
|
-
let totalHistory = 0;
|
|
848
|
-
for (let i = 0; i < this.speakingHistory.length; i++) {
|
|
849
|
-
totalHistory += this.speakingHistory[i];
|
|
850
|
-
}
|
|
851
|
-
if (totalHistory === 0) {
|
|
852
|
-
this.speaking = false;
|
|
853
|
-
this.emit("StopSpeaking");
|
|
854
|
-
}
|
|
855
|
-
} else if (this.attemptSpeaking) {
|
|
856
|
-
let lastHistory = this.speakingHistory[this.speakingHistory.length - 1];
|
|
857
|
-
if (lastHistory === 0) {
|
|
858
|
-
this.attemptSpeaking = false;
|
|
859
|
-
this.emit("CancelSpeaking");
|
|
888
|
+
async playAudio(blob) {
|
|
889
|
+
if (!this.audioElement || !this.player) return;
|
|
890
|
+
try {
|
|
891
|
+
this.player.addBlob(blob);
|
|
892
|
+
if (this.audioElement.paused) {
|
|
893
|
+
this.audioElement.play();
|
|
860
894
|
}
|
|
895
|
+
} catch (error) {
|
|
896
|
+
console.error("Error playing audio blob", error, blob);
|
|
861
897
|
}
|
|
862
|
-
this.speakingHistory.shift();
|
|
863
|
-
this.speakingHistory.push(Number(currentVolume > this.threshold));
|
|
864
|
-
setTimeout(() => this.startLoop(), this.delay);
|
|
865
|
-
}
|
|
866
|
-
async stop() {
|
|
867
|
-
if (!this.running) return;
|
|
868
|
-
this.running = false;
|
|
869
|
-
this.speaking = false;
|
|
870
|
-
if (this.sourceNode) {
|
|
871
|
-
this.sourceNode.disconnect();
|
|
872
|
-
this.sourceNode = void 0;
|
|
873
|
-
}
|
|
874
|
-
if (this.analyser) {
|
|
875
|
-
this.analyser.disconnect();
|
|
876
|
-
this.analyser = void 0;
|
|
877
|
-
}
|
|
878
|
-
this.fftBins = void 0;
|
|
879
898
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
this.
|
|
899
|
+
stopAudio() {
|
|
900
|
+
this.audioElement?.pause();
|
|
901
|
+
this.player?.stop();
|
|
883
902
|
}
|
|
884
903
|
};
|
|
904
|
+
if (!window.micdropSpeaker) {
|
|
905
|
+
window.micdropSpeaker = new Speaker();
|
|
906
|
+
}
|
|
907
|
+
speaker = window.micdropSpeaker;
|
|
885
908
|
|
|
886
909
|
// src/call/CallHandler.ts
|
|
887
910
|
var import_eventemitter34 = __toESM(require("eventemitter3"));
|
|
@@ -904,49 +927,23 @@ var CallHandlerError = class extends Error {
|
|
|
904
927
|
var CallHandler = class _CallHandler extends import_eventemitter34.default {
|
|
905
928
|
constructor(options) {
|
|
906
929
|
super();
|
|
930
|
+
this.options = options;
|
|
907
931
|
this.conversation = [];
|
|
908
932
|
this.debug = false;
|
|
909
933
|
this.startTime = 0;
|
|
910
934
|
this._isProcessing = false;
|
|
911
|
-
this.micRecorder = new MicRecorder(this.getVAD(options?.vad));
|
|
912
|
-
this.micRecorder.on("StateChange", () => {
|
|
913
|
-
this.notifyStateChange();
|
|
914
|
-
});
|
|
915
|
-
this.micRecorder.on("Chunk", (blob) => {
|
|
916
|
-
this.log(`[Mic] Received chunk`, blob);
|
|
917
|
-
this.ws?.send(blob);
|
|
918
|
-
});
|
|
919
|
-
this.micRecorder.on("StartSpeaking", () => {
|
|
920
|
-
this.log("[Mic] Start speaking");
|
|
921
|
-
this.ws?.send("startSpeaking" /* StartSpeaking */);
|
|
922
|
-
this._isProcessing = false;
|
|
923
|
-
this.notifyStateChange();
|
|
924
|
-
stopAudio();
|
|
925
|
-
});
|
|
926
|
-
this.micRecorder.on("StopSpeaking", () => {
|
|
927
|
-
this.log("[Mic] Stop speaking");
|
|
928
|
-
this.ws?.send("stopSpeaking" /* StopSpeaking */);
|
|
929
|
-
this._isProcessing = true;
|
|
930
|
-
this.notifyStateChange();
|
|
931
|
-
});
|
|
932
935
|
}
|
|
933
936
|
static getInstance(options) {
|
|
934
|
-
if (!
|
|
935
|
-
|
|
937
|
+
if (!window.micdropCallHandler) {
|
|
938
|
+
window.micdropCallHandler = new _CallHandler(options);
|
|
936
939
|
}
|
|
937
|
-
return
|
|
938
|
-
}
|
|
939
|
-
getVAD(vad) {
|
|
940
|
-
if (!vad) return new VolumeVAD();
|
|
941
|
-
if (vad === "volume") return new VolumeVAD();
|
|
942
|
-
if (vad === "silero") return new SileroVAD();
|
|
943
|
-
return vad;
|
|
940
|
+
return window.micdropCallHandler;
|
|
944
941
|
}
|
|
945
942
|
get isStarted() {
|
|
946
|
-
return this.isWSStarted && this.micRecorder
|
|
943
|
+
return this.isWSStarted && this.micRecorder?.state.isStarted || false;
|
|
947
944
|
}
|
|
948
945
|
get isStarting() {
|
|
949
|
-
return this.isWSStarting || this.micRecorder
|
|
946
|
+
return this.isWSStarting || this.micRecorder?.state.isStarting || false;
|
|
950
947
|
}
|
|
951
948
|
get isWSStarted() {
|
|
952
949
|
return this.ws?.readyState === WebSocket.OPEN;
|
|
@@ -957,6 +954,15 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
|
|
|
957
954
|
get isMicStarted() {
|
|
958
955
|
return !!this.micStream;
|
|
959
956
|
}
|
|
957
|
+
get isMicMuted() {
|
|
958
|
+
return this.micRecorder?.state.isMuted ?? false;
|
|
959
|
+
}
|
|
960
|
+
get isMicSpeaking() {
|
|
961
|
+
return this.micRecorder?.state.isSpeaking ?? false;
|
|
962
|
+
}
|
|
963
|
+
get micThreshold() {
|
|
964
|
+
return this.micRecorder?.state.threshold ?? mic.defaultThreshold;
|
|
965
|
+
}
|
|
960
966
|
get isProcessing() {
|
|
961
967
|
return this._isProcessing;
|
|
962
968
|
}
|
|
@@ -974,32 +980,57 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
|
|
|
974
980
|
console.error("[CallHandler] stop WS", error);
|
|
975
981
|
}
|
|
976
982
|
try {
|
|
977
|
-
this.micRecorder
|
|
983
|
+
this.micRecorder?.stop();
|
|
978
984
|
this.stopMic();
|
|
979
985
|
} catch (error) {
|
|
980
986
|
console.error("[CallHandler] stop mic", error);
|
|
981
987
|
}
|
|
982
988
|
}
|
|
983
989
|
pause() {
|
|
984
|
-
this.micRecorder
|
|
990
|
+
this.micRecorder?.mute();
|
|
991
|
+
this._isProcessing = false;
|
|
985
992
|
this.notifyStateChange();
|
|
986
|
-
pauseAudio();
|
|
993
|
+
speaker.pauseAudio();
|
|
994
|
+
this.ws?.send("mute" /* Mute */);
|
|
987
995
|
}
|
|
988
996
|
resume() {
|
|
989
997
|
if (this.lastAudioBlob) {
|
|
990
|
-
playAudio(this.lastAudioBlob);
|
|
998
|
+
speaker.playAudio(this.lastAudioBlob);
|
|
991
999
|
}
|
|
992
|
-
resumeAudio();
|
|
993
|
-
this.micRecorder
|
|
1000
|
+
speaker.resumeAudio();
|
|
1001
|
+
this.micRecorder?.unmute();
|
|
994
1002
|
this.notifyStateChange();
|
|
995
1003
|
}
|
|
996
1004
|
async startMic(deviceId, record = true) {
|
|
997
1005
|
try {
|
|
1006
|
+
if (!this.micRecorder) {
|
|
1007
|
+
this.micRecorder = new MicRecorder(this.options?.vad);
|
|
1008
|
+
this.micRecorder.on("StateChange", () => {
|
|
1009
|
+
this.notifyStateChange();
|
|
1010
|
+
});
|
|
1011
|
+
this.micRecorder.on("Chunk", (blob) => {
|
|
1012
|
+
this.log(`[Mic] Received chunk`, blob);
|
|
1013
|
+
this.ws?.send(blob);
|
|
1014
|
+
});
|
|
1015
|
+
this.micRecorder.on("StartSpeaking", () => {
|
|
1016
|
+
this.log("[Mic] Start speaking");
|
|
1017
|
+
this.ws?.send("startSpeaking" /* StartSpeaking */);
|
|
1018
|
+
this._isProcessing = false;
|
|
1019
|
+
this.notifyStateChange();
|
|
1020
|
+
speaker.stopAudio();
|
|
1021
|
+
});
|
|
1022
|
+
this.micRecorder.on("StopSpeaking", () => {
|
|
1023
|
+
this.log("[Mic] Stop speaking");
|
|
1024
|
+
this.ws?.send("stopSpeaking" /* StopSpeaking */);
|
|
1025
|
+
this._isProcessing = true;
|
|
1026
|
+
this.notifyStateChange();
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
998
1029
|
const isRecorderStarted = this.micRecorder.state.isStarted;
|
|
999
1030
|
if (isRecorderStarted) {
|
|
1000
|
-
this.micRecorder
|
|
1031
|
+
this.micRecorder?.stop();
|
|
1001
1032
|
}
|
|
1002
|
-
this.micStream = await
|
|
1033
|
+
this.micStream = await mic.start(deviceId);
|
|
1003
1034
|
this.notifyStateChange();
|
|
1004
1035
|
if (isRecorderStarted || record) {
|
|
1005
1036
|
await this.micRecorder.start(this.micStream);
|
|
@@ -1011,7 +1042,9 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
|
|
|
1011
1042
|
}
|
|
1012
1043
|
}
|
|
1013
1044
|
stopMic() {
|
|
1014
|
-
|
|
1045
|
+
this.micRecorder?.stop();
|
|
1046
|
+
this.micRecorder = void 0;
|
|
1047
|
+
mic.stop();
|
|
1015
1048
|
this.micStream = void 0;
|
|
1016
1049
|
this.notifyStateChange();
|
|
1017
1050
|
}
|
|
@@ -1038,7 +1071,7 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
|
|
|
1038
1071
|
this.ws.onmessage = (event) => {
|
|
1039
1072
|
this.log("[WS]", event.data);
|
|
1040
1073
|
if (event.data instanceof Blob) {
|
|
1041
|
-
playAudio(event.data);
|
|
1074
|
+
speaker.playAudio(event.data);
|
|
1042
1075
|
this.lastAudioBlob = event.data;
|
|
1043
1076
|
this._isProcessing = false;
|
|
1044
1077
|
this.notifyStateChange();
|
|
@@ -1060,7 +1093,7 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
|
|
|
1060
1093
|
this.notifyStateChange();
|
|
1061
1094
|
}
|
|
1062
1095
|
} else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
|
|
1063
|
-
enableStreaming();
|
|
1096
|
+
speaker.enableStreaming();
|
|
1064
1097
|
}
|
|
1065
1098
|
};
|
|
1066
1099
|
this.ws.onclose = (event) => {
|