@siteed/expo-audio-stream 1.8.0 → 1.9.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/CHANGELOG.md +36 -1
- package/build/ExpoAudioStream.types.d.ts +1 -14
- package/build/ExpoAudioStream.types.d.ts.map +1 -1
- package/build/ExpoAudioStream.types.js.map +1 -1
- package/build/ExpoAudioStream.web.d.ts +5 -3
- package/build/ExpoAudioStream.web.d.ts.map +1 -1
- package/build/ExpoAudioStream.web.js +52 -57
- package/build/ExpoAudioStream.web.js.map +1 -1
- package/build/WebRecorder.web.d.ts +7 -5
- package/build/WebRecorder.web.d.ts.map +1 -1
- package/build/WebRecorder.web.js +150 -197
- package/build/WebRecorder.web.js.map +1 -1
- package/build/useAudioRecorder.d.ts +2 -2
- package/build/useAudioRecorder.d.ts.map +1 -1
- package/build/useAudioRecorder.js.map +1 -1
- package/build/workers/inlineAudioWebWorker.web.d.ts +1 -1
- package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -1
- package/build/workers/inlineAudioWebWorker.web.js +65 -160
- package/build/workers/inlineAudioWebWorker.web.js.map +1 -1
- package/ios/AudioStreamManager.swift +39 -18
- package/package.json +1 -1
- package/src/ExpoAudioStream.types.ts +1 -15
- package/src/ExpoAudioStream.web.ts +55 -58
- package/src/WebRecorder.web.ts +182 -228
- package/src/useAudioRecorder.tsx +1 -2
- package/src/workers/inlineAudioWebWorker.web.tsx +65 -160
package/build/WebRecorder.web.js
CHANGED
|
@@ -10,6 +10,13 @@ const DEFAULT_WEB_INTERVAL = 500;
|
|
|
10
10
|
const DEFAULT_WEB_NUMBER_OF_CHANNELS = 1;
|
|
11
11
|
const DEFAULT_ALGORITHM = 'rms';
|
|
12
12
|
const TAG = 'WebRecorder';
|
|
13
|
+
const STOP_PERFORMANCE_MARKS = {
|
|
14
|
+
STOP_INITIATED: 'stopInitiated',
|
|
15
|
+
COMPRESSED_RECORDING_STOP: 'compressedRecordingStop',
|
|
16
|
+
AUDIO_WORKLET_STOP: 'audioWorkletStop',
|
|
17
|
+
CLEANUP: 'cleanup',
|
|
18
|
+
TOTAL_STOP_TIME: 'totalStopTime',
|
|
19
|
+
};
|
|
13
20
|
export class WebRecorder {
|
|
14
21
|
audioContext;
|
|
15
22
|
audioWorkletNode;
|
|
@@ -19,12 +26,10 @@ export class WebRecorder {
|
|
|
19
26
|
emitAudioEventCallback;
|
|
20
27
|
emitAudioAnalysisCallback;
|
|
21
28
|
config;
|
|
22
|
-
position
|
|
29
|
+
position = 0;
|
|
23
30
|
numberOfChannels; // Number of audio channels
|
|
24
31
|
bitDepth; // Bit depth of the audio
|
|
25
32
|
exportBitDepth; // Bit depth of the audio
|
|
26
|
-
audioBuffer; // Single buffer to store the audio data
|
|
27
|
-
audioBufferSize; // Keep track of the buffer size
|
|
28
33
|
audioAnalysisData; // Keep updating the full audio analysis data with latest events
|
|
29
34
|
packetCount = 0;
|
|
30
35
|
logger;
|
|
@@ -32,7 +37,7 @@ export class WebRecorder {
|
|
|
32
37
|
compressedChunks = [];
|
|
33
38
|
compressedSize = 0;
|
|
34
39
|
pendingCompressedChunk = null;
|
|
35
|
-
|
|
40
|
+
wavMimeType = 'audio/wav';
|
|
36
41
|
constructor({ audioContext, source, recordingConfig, audioWorkletUrl, emitAudioEventCallback, emitAudioAnalysisCallback, logger, }) {
|
|
37
42
|
this.audioContext = audioContext;
|
|
38
43
|
this.source = source;
|
|
@@ -40,7 +45,6 @@ export class WebRecorder {
|
|
|
40
45
|
this.emitAudioEventCallback = emitAudioEventCallback;
|
|
41
46
|
this.emitAudioAnalysisCallback = emitAudioAnalysisCallback;
|
|
42
47
|
this.config = recordingConfig;
|
|
43
|
-
this.position = 0;
|
|
44
48
|
this.logger = logger;
|
|
45
49
|
const audioContextFormat = this.checkAudioContextFormat({
|
|
46
50
|
sampleRate: this.audioContext.sampleRate,
|
|
@@ -60,9 +64,6 @@ export class WebRecorder {
|
|
|
60
64
|
}) ||
|
|
61
65
|
audioContextFormat.bitDepth ||
|
|
62
66
|
DEFAULT_WEB_BITDEPTH;
|
|
63
|
-
// Initialize the audio buffer separately
|
|
64
|
-
this.audioBuffer = new Float32Array(0);
|
|
65
|
-
this.audioBufferSize = 0;
|
|
66
67
|
this.audioAnalysisData = {
|
|
67
68
|
amplitudeRange: { min: 0, max: 0 },
|
|
68
69
|
dataPoints: [],
|
|
@@ -98,75 +99,61 @@ export class WebRecorder {
|
|
|
98
99
|
this.audioWorkletNode = new AudioWorkletNode(this.audioContext, 'recorder-processor');
|
|
99
100
|
this.audioWorkletNode.port.onmessage = async (event) => {
|
|
100
101
|
const command = event.data.command;
|
|
101
|
-
if (command !== 'newData')
|
|
102
|
+
if (command !== 'newData')
|
|
102
103
|
return;
|
|
103
|
-
}
|
|
104
104
|
const pcmBufferFloat = event.data.recordedData;
|
|
105
105
|
if (!pcmBufferFloat) {
|
|
106
106
|
this.logger?.warn('Received empty audio buffer', event);
|
|
107
107
|
return;
|
|
108
108
|
}
|
|
109
|
-
//
|
|
110
|
-
this.
|
|
111
|
-
this.audioBufferSize += pcmBufferFloat.length;
|
|
109
|
+
// Process data in smaller chunks and emit immediately
|
|
110
|
+
const chunkSize = this.audioContext.sampleRate * 2; // Reduce to 2 seconds chunks
|
|
112
111
|
const sampleRate = event.data.sampleRate ?? this.audioContext.sampleRate;
|
|
113
|
-
const duration = pcmBufferFloat.length / sampleRate;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
112
|
+
const duration = pcmBufferFloat.length / sampleRate;
|
|
113
|
+
// Emit chunks without storing them
|
|
114
|
+
for (let i = 0; i < pcmBufferFloat.length; i += chunkSize) {
|
|
115
|
+
const chunk = pcmBufferFloat.slice(i, i + chunkSize);
|
|
116
|
+
const chunkPosition = this.position + i / sampleRate;
|
|
117
|
+
// Process features if enabled
|
|
118
|
+
if (this.config.enableProcessing &&
|
|
119
|
+
this.featureExtractorWorker) {
|
|
120
|
+
this.featureExtractorWorker.postMessage({
|
|
121
|
+
command: 'process',
|
|
122
|
+
channelData: chunk,
|
|
123
|
+
sampleRate,
|
|
124
|
+
pointsPerSecond: this.config.pointsPerSecond ||
|
|
125
|
+
DEFAULT_WEB_POINTS_PER_SECOND,
|
|
126
|
+
algorithm: this.config.algorithm || 'rms',
|
|
127
|
+
bitDepth: this.bitDepth,
|
|
128
|
+
fullAudioDurationMs: this.position * 1000,
|
|
129
|
+
numberOfChannels: this.numberOfChannels,
|
|
130
|
+
features: this.config.features,
|
|
131
|
+
}, []);
|
|
132
|
+
}
|
|
133
|
+
// Emit chunk immediately
|
|
134
|
+
this.emitAudioEventCallback({
|
|
135
|
+
data: chunk,
|
|
136
|
+
position: chunkPosition,
|
|
137
|
+
compression: this.pendingCompressedChunk
|
|
138
|
+
? {
|
|
139
|
+
data: this.pendingCompressedChunk,
|
|
140
|
+
size: this.pendingCompressedChunk.size,
|
|
141
|
+
totalSize: this.compressedSize,
|
|
142
|
+
mimeType: 'audio/webm',
|
|
143
|
+
format: 'opus',
|
|
144
|
+
bitrate: this.config.compression?.bitrate ??
|
|
145
|
+
128000,
|
|
146
|
+
}
|
|
147
|
+
: undefined,
|
|
121
148
|
});
|
|
122
|
-
// For the first packet, combine WAV header with audio data
|
|
123
|
-
const headerFloatArray = new Float32Array(wavHeaderBuffer);
|
|
124
|
-
data = new Float32Array(headerFloatArray.length + this.audioBuffer.length);
|
|
125
|
-
data.set(headerFloatArray, 0);
|
|
126
|
-
data.set(this.audioBuffer, headerFloatArray.length);
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
// For subsequent packets, just send the new audio data
|
|
130
|
-
data = pcmBufferFloat;
|
|
131
|
-
}
|
|
132
|
-
// Track the number of packets
|
|
133
|
-
this.packetCount += 1;
|
|
134
|
-
// Prepare compression data if available
|
|
135
|
-
let compressionData;
|
|
136
|
-
if (this.pendingCompressedChunk) {
|
|
137
|
-
compressionData = {
|
|
138
|
-
data: this.pendingCompressedChunk,
|
|
139
|
-
size: this.pendingCompressedChunk.size,
|
|
140
|
-
totalSize: this.compressedSize,
|
|
141
|
-
mimeType: 'audio/webm',
|
|
142
|
-
format: 'opus',
|
|
143
|
-
bitrate: this.config.compression?.bitrate ?? 128000,
|
|
144
|
-
};
|
|
145
|
-
this.pendingCompressedChunk = null;
|
|
146
149
|
}
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
position: this.position,
|
|
150
|
-
compression: compressionData,
|
|
151
|
-
});
|
|
152
|
-
this.position += duration; // Update position
|
|
153
|
-
this.featureExtractorWorker?.postMessage({
|
|
154
|
-
command: 'process',
|
|
155
|
-
channelData: pcmBufferFloat,
|
|
156
|
-
sampleRate,
|
|
157
|
-
pointsPerSecond: this.config.pointsPerSecond ||
|
|
158
|
-
DEFAULT_WEB_POINTS_PER_SECOND,
|
|
159
|
-
algorithm: this.config.algorithm || 'rms',
|
|
160
|
-
bitDepth: this.bitDepth,
|
|
161
|
-
fullAudioDurationMs: this.position * 1000,
|
|
162
|
-
numberOfChannels: this.numberOfChannels,
|
|
163
|
-
features: this.config.features,
|
|
164
|
-
}, []);
|
|
150
|
+
this.position += duration;
|
|
151
|
+
this.pendingCompressedChunk = null;
|
|
165
152
|
};
|
|
166
153
|
this.logger?.debug(`WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`, this.config);
|
|
167
154
|
this.audioWorkletNode.port.postMessage({
|
|
168
155
|
command: 'init',
|
|
169
|
-
recordSampleRate: this.audioContext.sampleRate,
|
|
156
|
+
recordSampleRate: this.audioContext.sampleRate,
|
|
170
157
|
exportSampleRate: this.config.sampleRate ?? this.audioContext.sampleRate,
|
|
171
158
|
bitDepth: this.bitDepth,
|
|
172
159
|
exportBitDepth: this.exportBitDepth,
|
|
@@ -251,142 +238,108 @@ export class WebRecorder {
|
|
|
251
238
|
this.compressedMediaRecorder.start(this.config.interval ?? 1000);
|
|
252
239
|
}
|
|
253
240
|
}
|
|
254
|
-
async stop(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
241
|
+
async stop() {
|
|
242
|
+
try {
|
|
243
|
+
if (this.compressedMediaRecorder) {
|
|
244
|
+
this.compressedMediaRecorder.stop();
|
|
245
|
+
return {
|
|
246
|
+
pcmData: new Float32Array(), // Return empty array since we're streaming
|
|
247
|
+
compressedBlob: new Blob(this.compressedChunks, {
|
|
248
|
+
type: 'audio/webm;codecs=opus',
|
|
249
|
+
}),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
return { pcmData: new Float32Array() };
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
this.cleanup();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
cleanup() {
|
|
259
|
+
if (this.audioContext) {
|
|
260
|
+
this.audioContext.close();
|
|
261
|
+
}
|
|
262
|
+
if (this.audioWorkletNode) {
|
|
263
|
+
this.audioWorkletNode.disconnect();
|
|
264
|
+
}
|
|
265
|
+
if (this.source) {
|
|
266
|
+
this.source.disconnect();
|
|
267
|
+
}
|
|
268
|
+
this.stopMediaStreamTracks();
|
|
269
|
+
}
|
|
270
|
+
// Helper method to process recording stop
|
|
271
|
+
async processRecordingStop() {
|
|
272
|
+
const processStartTime = performance.now();
|
|
273
|
+
this.logger?.debug('[Performance] Starting recording stop process');
|
|
274
|
+
const [compressedData, workletData] = await Promise.all([
|
|
275
|
+
this.stopCompressedRecording(),
|
|
276
|
+
this.stopAudioWorklet(),
|
|
277
|
+
]);
|
|
278
|
+
this.logger?.debug(`[Performance] Recording stop process completed in ${performance.now() - processStartTime}ms`);
|
|
279
|
+
return {
|
|
280
|
+
pcmData: workletData ??
|
|
281
|
+
new Float32Array(this.audioAnalysisData.dataPoints.length),
|
|
282
|
+
compressedBlob: compressedData,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
// Helper method to stop compressed recording
|
|
286
|
+
stopCompressedRecording() {
|
|
287
|
+
const startTime = performance.now();
|
|
288
|
+
this.logger?.debug(`[Performance][${STOP_PERFORMANCE_MARKS.COMPRESSED_RECORDING_STOP}] Starting compressed recording stop`);
|
|
289
|
+
if (!this.compressedMediaRecorder) {
|
|
290
|
+
this.logger?.debug('[Performance] No compressed recorder to stop');
|
|
291
|
+
return Promise.resolve(undefined);
|
|
292
|
+
}
|
|
293
|
+
return new Promise((resolve) => {
|
|
294
|
+
this.compressedMediaRecorder.onstop = () => {
|
|
295
|
+
const blob = new Blob(this.compressedChunks, {
|
|
296
|
+
type: 'audio/webm;codecs=opus',
|
|
297
|
+
});
|
|
298
|
+
this.logger?.debug(`[Performance][${STOP_PERFORMANCE_MARKS.COMPRESSED_RECORDING_STOP}] Compressed recording stopped in ${performance.now() - startTime}ms, size: ${blob.size}`);
|
|
299
|
+
resolve(blob);
|
|
271
300
|
};
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
this.logger?.debug(`
|
|
303
|
-
|
|
304
|
-
const endIndex = Math.min(chunkIndex + CHUNKS_PER_BATCH, this.audioChunks.length);
|
|
305
|
-
// Process a batch of chunks
|
|
306
|
-
for (let i = chunkIndex; i < endIndex; i++) {
|
|
307
|
-
const chunk = this.audioChunks[i];
|
|
308
|
-
combinedBuffer.set(chunk, offset);
|
|
309
|
-
offset += chunk.length;
|
|
310
|
-
}
|
|
311
|
-
chunkIndex = endIndex;
|
|
312
|
-
if (chunkIndex < this.audioChunks.length) {
|
|
313
|
-
// Process next batch in next frame
|
|
314
|
-
requestAnimationFrame(processNextBatch);
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
// All chunks processed
|
|
318
|
-
this.audioBuffer = combinedBuffer;
|
|
319
|
-
queueMicrotask(() => {
|
|
320
|
-
cleanup();
|
|
321
|
-
resolve({
|
|
322
|
-
pcmData: this.audioBuffer,
|
|
323
|
-
compressedBlob,
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
};
|
|
328
|
-
processNextBatch();
|
|
301
|
+
this.compressedMediaRecorder.stop();
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
// Helper method to stop audio worklet
|
|
305
|
+
stopAudioWorklet() {
|
|
306
|
+
const startTime = performance.now();
|
|
307
|
+
this.logger?.debug(`[Performance][${STOP_PERFORMANCE_MARKS.AUDIO_WORKLET_STOP}] Starting audio worklet stop`);
|
|
308
|
+
if (!this.audioWorkletNode) {
|
|
309
|
+
this.logger?.debug('[Performance] No audio worklet to stop');
|
|
310
|
+
return Promise.resolve(undefined);
|
|
311
|
+
}
|
|
312
|
+
return new Promise((resolve) => {
|
|
313
|
+
const onMessage = (event) => {
|
|
314
|
+
if (event.data.command === 'recordedData') {
|
|
315
|
+
this.audioWorkletNode?.port.removeEventListener('message', onMessage);
|
|
316
|
+
const rawPCMDataFull = event.data.recordedData?.slice(0);
|
|
317
|
+
if (!rawPCMDataFull) {
|
|
318
|
+
this.logger?.debug('[Performance] No PCM data received');
|
|
319
|
+
resolve(undefined);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (this.exportBitDepth !== this.bitDepth) {
|
|
323
|
+
const conversionStart = performance.now();
|
|
324
|
+
convertPCMToFloat32({
|
|
325
|
+
buffer: rawPCMDataFull.buffer,
|
|
326
|
+
bitDepth: this.exportBitDepth,
|
|
327
|
+
skipWavHeader: true,
|
|
328
|
+
logger: this.logger,
|
|
329
|
+
}).then(({ pcmValues }) => {
|
|
330
|
+
this.logger?.debug(`[Performance] PCM conversion completed in ${performance.now() - conversionStart}ms`);
|
|
331
|
+
this.logger?.debug(`[Performance][${STOP_PERFORMANCE_MARKS.AUDIO_WORKLET_STOP}] Audio worklet stopped in ${performance.now() - startTime}ms`);
|
|
332
|
+
resolve(pcmValues);
|
|
329
333
|
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
const onMessage = (event) => {
|
|
336
|
-
if (event.data.command === 'recordedData') {
|
|
337
|
-
this.audioWorkletNode?.port.removeEventListener('message', onMessage);
|
|
338
|
-
queueMicrotask(async () => {
|
|
339
|
-
try {
|
|
340
|
-
const rawPCMDataFull = event.data.recordedData?.slice(0);
|
|
341
|
-
if (!rawPCMDataFull) {
|
|
342
|
-
cleanup();
|
|
343
|
-
reject(new Error('Failed to get recorded data'));
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
// Convert PCM data if needed based on bit depth
|
|
347
|
-
if (this.exportBitDepth !== this.bitDepth) {
|
|
348
|
-
const convertedData = await convertPCMToFloat32({
|
|
349
|
-
buffer: rawPCMDataFull.buffer,
|
|
350
|
-
bitDepth: this.exportBitDepth,
|
|
351
|
-
skipWavHeader: true,
|
|
352
|
-
logger: this.logger,
|
|
353
|
-
});
|
|
354
|
-
this.audioBuffer =
|
|
355
|
-
convertedData.pcmValues;
|
|
356
|
-
}
|
|
357
|
-
else {
|
|
358
|
-
this.audioBuffer = rawPCMDataFull;
|
|
359
|
-
}
|
|
360
|
-
if (!this.compressedMediaRecorder) {
|
|
361
|
-
cleanup();
|
|
362
|
-
resolve({ pcmData: this.audioBuffer });
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
catch (error) {
|
|
366
|
-
cleanup();
|
|
367
|
-
reject(error);
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
};
|
|
372
|
-
this.audioWorkletNode.port.addEventListener('message', onMessage);
|
|
373
|
-
this.audioWorkletNode.port.postMessage({ command: 'stop' });
|
|
374
|
-
// Safety timeout
|
|
375
|
-
setTimeout(() => {
|
|
376
|
-
this.audioWorkletNode?.port.removeEventListener('message', onMessage);
|
|
377
|
-
cleanup();
|
|
378
|
-
reject(new Error('Timeout while stopping recording'));
|
|
379
|
-
}, 5000);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
this.logger?.debug(`[Performance][${STOP_PERFORMANCE_MARKS.AUDIO_WORKLET_STOP}] Audio worklet stopped in ${performance.now() - startTime}ms`);
|
|
337
|
+
resolve(rawPCMDataFull);
|
|
338
|
+
}
|
|
380
339
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
catch (error) {
|
|
387
|
-
cleanup();
|
|
388
|
-
reject(error);
|
|
389
|
-
}
|
|
340
|
+
};
|
|
341
|
+
this.audioWorkletNode.port.addEventListener('message', onMessage);
|
|
342
|
+
this.audioWorkletNode.port.postMessage({ command: 'stop' });
|
|
390
343
|
});
|
|
391
344
|
}
|
|
392
345
|
pause() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebRecorder.web.js","sourceRoot":"","sources":["../src/WebRecorder.web.ts"],"names":[],"mappings":"AAAA,qBAAqB;AAQrB,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AACvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAA;AAC/E,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAA;AAiBzE,MAAM,oBAAoB,GA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CACJ","sourcesContent":["// src/WebRecorder.ts\n\nimport { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'\nimport { ConsoleLike, RecordingConfig, WebRecordingOptions } from './ExpoAudioStream.types'\nimport {\n EmitAudioAnalysisFunction,\n EmitAudioEventFunction,\n} from './ExpoAudioStream.web'\nimport { convertPCMToFloat32 } from './utils/convertPCMToFloat32'\nimport { encodingToBitDepth } from './utils/encodingToBitDepth'\nimport { writeWavHeader } from './utils/writeWavHeader'\nimport { InlineFeaturesExtractor } from './workers/InlineFeaturesExtractor.web'\nimport { InlineAudioWebWorker } from './workers/inlineAudioWebWorker.web'\n\ninterface AudioWorkletEvent {\n data: {\n command: string\n recordedData?: Float32Array\n sampleRate?: number\n }\n}\n\ninterface AudioFeaturesEvent {\n data: {\n command: string\n result: AudioAnalysis\n }\n}\n\nconst DEFAULT_WEB_BITDEPTH = 32\nconst DEFAULT_WEB_POINTS_PER_SECOND = 10\nconst DEFAULT_WEB_INTERVAL = 500\nconst DEFAULT_WEB_NUMBER_OF_CHANNELS = 1\nconst DEFAULT_ALGORITHM = 'rms'\n\nconst TAG = 'WebRecorder'\n\nexport class WebRecorder {\n private audioContext: AudioContext\n private audioWorkletNode!: AudioWorkletNode\n private featureExtractorWorker?: Worker\n private source: MediaStreamAudioSourceNode\n private audioWorkletUrl: string\n private emitAudioEventCallback: EmitAudioEventFunction\n private emitAudioAnalysisCallback: EmitAudioAnalysisFunction\n private config: RecordingConfig\n private position: number // Track the cumulative position\n private numberOfChannels: number // Number of audio channels\n private bitDepth: number // Bit depth of the audio\n private exportBitDepth: number // Bit depth of the audio\n private audioBuffer: Float32Array // Single buffer to store the audio data\n private audioBufferSize: number // Keep track of the buffer size\n private audioAnalysisData: AudioAnalysis // Keep updating the full audio analysis data with latest events\n private packetCount: number = 0\n private logger?: ConsoleLike\n private compressedMediaRecorder: MediaRecorder | null = null\n private compressedChunks: Blob[] = []\n private compressedSize: number = 0\n private pendingCompressedChunk: Blob | null = null\n private audioChunks: Float32Array[] = []\n\n constructor({\n audioContext,\n source,\n recordingConfig,\n audioWorkletUrl,\n emitAudioEventCallback,\n emitAudioAnalysisCallback,\n logger,\n }: {\n audioContext: AudioContext\n source: MediaStreamAudioSourceNode\n recordingConfig: RecordingConfig\n audioWorkletUrl: string\n emitAudioEventCallback: EmitAudioEventFunction\n emitAudioAnalysisCallback: EmitAudioAnalysisFunction\n logger?: ConsoleLike\n }) {\n this.audioContext = audioContext\n this.source = source\n this.audioWorkletUrl = audioWorkletUrl\n this.emitAudioEventCallback = emitAudioEventCallback\n this.emitAudioAnalysisCallback = emitAudioAnalysisCallback\n this.config = recordingConfig\n this.position = 0\n this.logger = logger\n\n const audioContextFormat = this.checkAudioContextFormat({\n sampleRate: this.audioContext.sampleRate,\n })\n this.logger?.debug('Initialized WebRecorder with config:', {\n sampleRate: audioContextFormat.sampleRate,\n bitDepth: audioContextFormat.bitDepth,\n numberOfChannels: audioContextFormat.numberOfChannels,\n })\n\n this.bitDepth = audioContextFormat.bitDepth\n this.numberOfChannels =\n audioContextFormat.numberOfChannels ||\n DEFAULT_WEB_NUMBER_OF_CHANNELS // Default to 1 if not available\n this.exportBitDepth =\n encodingToBitDepth({\n encoding: recordingConfig.encoding ?? 'pcm_32bit',\n }) ||\n audioContextFormat.bitDepth ||\n DEFAULT_WEB_BITDEPTH\n\n // Initialize the audio buffer separately\n this.audioBuffer = new Float32Array(0)\n this.audioBufferSize = 0\n\n this.audioAnalysisData = {\n amplitudeRange: { min: 0, max: 0 },\n dataPoints: [],\n durationMs: 0,\n samples: 0,\n amplitudeAlgorithm: recordingConfig.algorithm || DEFAULT_ALGORITHM,\n bitDepth: this.bitDepth,\n numberOfChannels: this.numberOfChannels,\n sampleRate: this.config.sampleRate || this.audioContext.sampleRate,\n pointsPerSecond:\n this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,\n speakerChanges: [],\n }\n\n if (recordingConfig.enableProcessing) {\n this.initFeatureExtractorWorker()\n }\n\n // Initialize compressed recording if enabled\n if (recordingConfig.compression?.enabled) {\n this.initializeCompressedRecorder()\n }\n }\n\n async init() {\n try {\n if (!this.audioWorkletUrl) {\n const blob = new Blob([InlineAudioWebWorker], {\n type: 'application/javascript',\n })\n const url = URL.createObjectURL(blob)\n await this.audioContext.audioWorklet.addModule(url)\n } else {\n await this.audioContext.audioWorklet.addModule(\n this.audioWorkletUrl\n )\n }\n this.audioWorkletNode = new AudioWorkletNode(\n this.audioContext,\n 'recorder-processor'\n )\n\n this.audioWorkletNode.port.onmessage = async (\n event: AudioWorkletEvent\n ) => {\n const command = event.data.command\n if (command !== 'newData') {\n return\n }\n const pcmBufferFloat = event.data.recordedData\n\n if (!pcmBufferFloat) {\n this.logger?.warn('Received empty audio buffer', event)\n return\n }\n\n // Store chunks instead of concatenating\n this.audioChunks.push(pcmBufferFloat)\n this.audioBufferSize += pcmBufferFloat.length\n\n const sampleRate =\n event.data.sampleRate ?? this.audioContext.sampleRate\n const duration = pcmBufferFloat.length / sampleRate // Calculate duration of the current buffer\n\n let data: Float32Array\n if (this.packetCount === 0) {\n // Initialize WAV header\n const wavHeaderBuffer = writeWavHeader({\n sampleRate: this.audioContext.sampleRate,\n numChannels: this.numberOfChannels,\n bitDepth: this.exportBitDepth,\n })\n\n // For the first packet, combine WAV header with audio data\n const headerFloatArray = new Float32Array(wavHeaderBuffer)\n data = new Float32Array(\n headerFloatArray.length + this.audioBuffer.length\n )\n data.set(headerFloatArray, 0)\n data.set(this.audioBuffer, headerFloatArray.length)\n } else {\n // For subsequent packets, just send the new audio data\n data = pcmBufferFloat\n }\n\n // Track the number of packets\n this.packetCount += 1\n\n // Prepare compression data if available\n let compressionData\n if (this.pendingCompressedChunk) {\n compressionData = {\n data: this.pendingCompressedChunk,\n size: this.pendingCompressedChunk.size,\n totalSize: this.compressedSize,\n mimeType: 'audio/webm',\n format: 'opus',\n bitrate: this.config.compression?.bitrate ?? 128000,\n }\n this.pendingCompressedChunk = null\n }\n\n this.emitAudioEventCallback({\n data: pcmBufferFloat,\n position: this.position,\n compression: compressionData,\n })\n this.position += duration // Update position\n\n this.featureExtractorWorker?.postMessage(\n {\n command: 'process',\n channelData: pcmBufferFloat,\n sampleRate,\n pointsPerSecond:\n this.config.pointsPerSecond ||\n DEFAULT_WEB_POINTS_PER_SECOND,\n algorithm: this.config.algorithm || 'rms',\n bitDepth: this.bitDepth,\n fullAudioDurationMs: this.position * 1000,\n numberOfChannels: this.numberOfChannels,\n features: this.config.features,\n },\n []\n )\n }\n\n this.logger?.debug(\n `WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`,\n this.config\n )\n this.audioWorkletNode.port.postMessage({\n command: 'init',\n recordSampleRate: this.audioContext.sampleRate, // Pass the original sample rate\n exportSampleRate:\n this.config.sampleRate ?? this.audioContext.sampleRate,\n bitDepth: this.bitDepth,\n exportBitDepth: this.exportBitDepth,\n channels: this.numberOfChannels,\n interval: this.config.interval ?? DEFAULT_WEB_INTERVAL,\n })\n\n // Connect the source to the AudioWorkletNode and start recording\n this.source.connect(this.audioWorkletNode)\n this.audioWorkletNode.connect(this.audioContext.destination)\n } catch (error) {\n console.error(`[${TAG}] Failed to initialize WebRecorder`, error)\n }\n }\n\n initFeatureExtractorWorker(featuresExtratorUrl?: string) {\n try {\n if (featuresExtratorUrl) {\n // Initialize the feature extractor worker\n //TODO: create audio feature extractor from a Blob instead of url since we cannot include the url directly in the library\n // We keep the url during dev and use the blob in production.\n this.featureExtractorWorker = new Worker(\n new URL(featuresExtratorUrl, window.location.href)\n )\n this.featureExtractorWorker.onmessage =\n this.handleFeatureExtractorMessage.bind(this)\n this.featureExtractorWorker.onerror =\n this.handleWorkerError.bind(this)\n } else {\n // Fallback to the inline worker if the URL is not provided\n this.initFallbackWorker()\n }\n } catch (error) {\n console.error(\n `[${TAG}] Failed to initialize feature extractor worker`,\n error\n )\n this.initFallbackWorker()\n }\n }\n\n initFallbackWorker() {\n try {\n const blob = new Blob([InlineFeaturesExtractor], {\n type: 'application/javascript',\n })\n const url = URL.createObjectURL(blob)\n this.featureExtractorWorker = new Worker(url)\n this.featureExtractorWorker.onmessage =\n this.handleFeatureExtractorMessage.bind(this)\n this.featureExtractorWorker.onerror = (error) => {\n console.error(`[${TAG}] Default Inline worker failed`, error)\n }\n this.logger?.log('Inline worker initialized successfully')\n } catch (error) {\n console.error(\n `[${TAG}] Failed to initialize Inline Feature Extractor worker`,\n error\n )\n }\n }\n\n handleWorkerError(error: ErrorEvent) {\n console.error(`[${TAG}] Feature extractor worker error:`, error)\n }\n\n handleFeatureExtractorMessage(event: AudioFeaturesEvent) {\n if (event.data.command === 'features') {\n const segmentResult = event.data.result\n\n // Merge the segment result with the full audio analysis data\n this.audioAnalysisData.dataPoints.push(...segmentResult.dataPoints)\n this.audioAnalysisData.speakerChanges?.push(\n ...(segmentResult.speakerChanges ?? [])\n )\n this.audioAnalysisData.durationMs = segmentResult.durationMs\n if (segmentResult.amplitudeRange) {\n this.audioAnalysisData.amplitudeRange = {\n min: Math.min(\n this.audioAnalysisData.amplitudeRange.min,\n segmentResult.amplitudeRange.min\n ),\n max: Math.max(\n this.audioAnalysisData.amplitudeRange.max,\n segmentResult.amplitudeRange.max\n ),\n }\n }\n // Handle the extracted features (e.g., emit an event or log them)\n this.logger?.debug('features event segmentResult', segmentResult)\n this.logger?.debug(\n `features event audioAnalysisData duration=${this.audioAnalysisData.durationMs}`,\n this.audioAnalysisData\n )\n this.emitAudioAnalysisCallback(segmentResult)\n }\n }\n\n start() {\n this.source.connect(this.audioWorkletNode)\n this.audioWorkletNode.connect(this.audioContext.destination)\n this.packetCount = 0\n\n if (this.compressedMediaRecorder) {\n this.compressedMediaRecorder.start(this.config.interval ?? 1000)\n }\n }\n\n async stop(options?: WebRecordingOptions): Promise<{ pcmData: Float32Array; compressedBlob?: Blob }> {\n return new Promise((resolve, reject) => {\n let isCleanupDone = false\n\n const cleanup = () => {\n if (isCleanupDone) return\n isCleanupDone = true\n\n if (this.audioContext) {\n this.audioContext.close()\n }\n if (this.audioWorkletNode) {\n this.audioWorkletNode.disconnect()\n }\n if (this.source) {\n this.source.disconnect()\n }\n\n this.stopMediaStreamTracks()\n }\n try {\n this.logger?.debug(`Stopping WebRecorder ${this.audioChunks.length} chunks - this.compressedChunks.length=${this.compressedChunks.length}`)\n\n // If skipFinalConsolidation is true and we have compressed data, return early\n if (options?.skipFinalConsolidation && this.compressedMediaRecorder) {\n this.compressedMediaRecorder.onstop = () => {\n const compressedBlob = new Blob(this.compressedChunks, {\n type: 'audio/webm;codecs=opus',\n })\n cleanup()\n // Return the last chunk as pcmData to maintain interface compatibility\n resolve({\n pcmData: this.audioChunks[this.audioChunks.length - 1] || new Float32Array(),\n compressedBlob,\n })\n }\n this.compressedMediaRecorder.stop()\n return\n }\n\n // Handle compressed recording first\n if (this.compressedMediaRecorder) {\n this.compressedMediaRecorder.onstop = () => {\n const compressedBlob = new Blob(this.compressedChunks, {\n type: 'audio/webm;codecs=opus',\n })\n\n // Process chunks in batches\n requestAnimationFrame(() => {\n const combinedBuffer = new Float32Array(this.audioBufferSize)\n let offset = 0\n let chunkIndex = 0\n const CHUNKS_PER_BATCH = 10 // Process 5 seconds worth of chunks at a time (10 * 500ms = 5s)\n\n this.logger?.debug(\n `Processing ${this.audioChunks.length} chunks in batches of ${CHUNKS_PER_BATCH}`\n )\n\n const processNextBatch = () => {\n const endIndex = Math.min(chunkIndex + CHUNKS_PER_BATCH, this.audioChunks.length)\n \n // Process a batch of chunks\n for (let i = chunkIndex; i < endIndex; i++) {\n const chunk = this.audioChunks[i]\n combinedBuffer.set(chunk, offset)\n offset += chunk.length\n }\n\n chunkIndex = endIndex\n\n if (chunkIndex < this.audioChunks.length) {\n // Process next batch in next frame\n requestAnimationFrame(processNextBatch)\n } else {\n // All chunks processed\n this.audioBuffer = combinedBuffer\n queueMicrotask(() => {\n cleanup()\n resolve({\n pcmData: this.audioBuffer,\n compressedBlob,\n })\n })\n }\n }\n\n processNextBatch()\n })\n }\n this.compressedMediaRecorder.stop()\n }\n\n // Handle audio worklet data\n if (this.audioWorkletNode) {\n const onMessage = (event: AudioWorkletEvent) => {\n if (event.data.command === 'recordedData') {\n this.audioWorkletNode?.port.removeEventListener(\n 'message',\n onMessage\n )\n\n queueMicrotask(async () => {\n try {\n const rawPCMDataFull =\n event.data.recordedData?.slice(0)\n if (!rawPCMDataFull) {\n cleanup()\n reject(\n new Error(\n 'Failed to get recorded data'\n )\n )\n return\n }\n\n // Convert PCM data if needed based on bit depth\n if (this.exportBitDepth !== this.bitDepth) {\n const convertedData =\n await convertPCMToFloat32({\n buffer: rawPCMDataFull.buffer,\n bitDepth: this.exportBitDepth,\n skipWavHeader: true,\n logger: this.logger,\n })\n this.audioBuffer =\n convertedData.pcmValues\n } else {\n this.audioBuffer = rawPCMDataFull\n }\n\n if (!this.compressedMediaRecorder) {\n cleanup()\n resolve({ pcmData: this.audioBuffer })\n }\n } catch (error) {\n cleanup()\n reject(error)\n }\n })\n }\n }\n\n this.audioWorkletNode.port.addEventListener(\n 'message',\n onMessage\n )\n this.audioWorkletNode.port.postMessage({ command: 'stop' })\n\n // Safety timeout\n setTimeout(() => {\n this.audioWorkletNode?.port.removeEventListener(\n 'message',\n onMessage\n )\n cleanup()\n reject(new Error('Timeout while stopping recording'))\n }, 5000)\n } else if (!this.compressedMediaRecorder) {\n cleanup()\n resolve({ pcmData: this.audioBuffer })\n }\n } catch (error) {\n cleanup()\n reject(error)\n }\n })\n }\n\n pause() {\n this.source.disconnect(this.audioWorkletNode) // Disconnect the source from the AudioWorkletNode\n this.audioWorkletNode.disconnect(this.audioContext.destination) // Disconnect the AudioWorkletNode from the destination\n this.audioWorkletNode.port.postMessage({ command: 'pause' })\n this.compressedMediaRecorder?.pause()\n }\n\n stopMediaStreamTracks() {\n // Stop all audio tracks to stop the recording icon\n const tracks = this.source.mediaStream.getTracks()\n tracks.forEach((track) => track.stop())\n }\n\n async playRecordedData({\n recordedData,\n }: {\n recordedData: ArrayBuffer\n mimeType?: string\n }) {\n try {\n // Create a WAV blob with proper headers\n const wavHeaderBuffer = writeWavHeader({\n buffer: recordedData,\n sampleRate: this.audioContext.sampleRate,\n numChannels: this.numberOfChannels,\n bitDepth: this.exportBitDepth,\n })\n\n const blob = new Blob([wavHeaderBuffer], { type: 'audio/wav' })\n const url = URL.createObjectURL(blob)\n const response = await fetch(url)\n const arrayBuffer = await response.arrayBuffer()\n\n // Decode the audio data\n const audioBuffer =\n await this.audioContext.decodeAudioData(arrayBuffer)\n\n // Create a buffer source node and play the audio\n const bufferSource = this.audioContext.createBufferSource()\n bufferSource.buffer = audioBuffer\n bufferSource.connect(this.audioContext.destination)\n bufferSource.start()\n this.logger?.debug('Playing recorded data', recordedData)\n\n // Clean up\n URL.revokeObjectURL(url)\n } catch (error) {\n console.error(`[${TAG}] Failed to play recorded data:`, error)\n }\n }\n\n private checkAudioContextFormat({ sampleRate }: { sampleRate: number }) {\n // Create a silent AudioBuffer\n const frameCount = sampleRate * 1.0 // 1 second buffer\n const audioBuffer = this.audioContext.createBuffer(\n 1,\n frameCount,\n sampleRate\n )\n\n // Check the format\n const channelData = audioBuffer.getChannelData(0)\n const bitDepth = channelData.BYTES_PER_ELEMENT * 8 // 4 bytes per element means 32-bit\n\n return {\n sampleRate: audioBuffer.sampleRate,\n bitDepth,\n numberOfChannels: audioBuffer.numberOfChannels,\n }\n }\n\n resume() {\n this.source.connect(this.audioWorkletNode)\n this.audioWorkletNode.connect(this.audioContext.destination)\n this.audioWorkletNode.port.postMessage({ command: 'resume' })\n this.compressedMediaRecorder?.resume()\n }\n\n private initializeCompressedRecorder() {\n try {\n const mimeType = 'audio/webm;codecs=opus'\n if (!MediaRecorder.isTypeSupported(mimeType)) {\n this.logger?.warn(\n 'Opus compression not supported in this browser'\n )\n return\n }\n\n this.compressedMediaRecorder = new MediaRecorder(\n this.source.mediaStream,\n {\n mimeType,\n audioBitsPerSecond:\n this.config.compression?.bitrate ?? 128000,\n }\n )\n\n this.compressedMediaRecorder.ondataavailable = (event) => {\n if (event.data.size > 0) {\n this.compressedChunks.push(event.data)\n this.compressedSize += event.data.size\n this.pendingCompressedChunk = event.data\n }\n }\n } catch (error) {\n this.logger?.error(\n 'Failed to initialize compressed recorder:',\n error\n )\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"WebRecorder.web.js","sourceRoot":"","sources":["../src/WebRecorder.web.ts"],"names":[],"mappings":"AAAA,qBAAqB;AAQrB,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AACvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAA;AAC/E,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAA;AAiBzE,MAAM,oBAAoB,GAAG,EAAE,CAAA;AAC/B,MAAM,6BAA6B,GAAG,EAAE,CAAA;AACxC,MAAM,oBAAoB,GAAG,GAAG,CAAA;AAChC,MAAM,8BAA8B,GAAG,CAAC,CAAA;AACxC,MAAM,iBAAiB,GAAG,KAAK,CAAA;AAE/B,MAAM,GAAG,GAAG,aAAa,CAAA;AAEzB,MAAM,sBAAsB,GAAG;IAC3B,cAAc,EAAE,eAAe;IAC/B,yBAAyB,EAAE,yBAAyB;IACpD,kBAAkB,EAAE,kBAAkB;IACtC,OAAO,EAAE,SAAS;IAClB,eAAe,EAAE,eAAe;CAC1B,CAAA;AAEV,MAAM,OAAO,WAAW;IACZ,YAAY,CAAc;IAC1B,gBAAgB,CAAmB;IACnC,sBAAsB,CAAS;IAC/B,MAAM,CAA4B;IAClC,eAAe,CAAQ;IACvB,sBAAsB,CAAwB;IAC9C,yBAAyB,CAA2B;IACpD,MAAM,CAAiB;IACvB,QAAQ,GAAW,CAAC,CAAA;IACpB,gBAAgB,CAAQ,CAAC,2BAA2B;IACpD,QAAQ,CAAQ,CAAC,yBAAyB;IAC1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,EAAE;gBACrD,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;oBACtB,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;oBACtC,IAAI,CAAC,cAAc,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAA;oBACtC,IAAI,CAAC,sBAAsB,GAAG,KAAK,CAAC,IAAI,CAAA;gBAC5C,CAAC;YACL,CAAC,CAAA;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,EAAE,KAAK,CACd,2CAA2C,EAC3C,KAAK,CACR,CAAA;QACL,CAAC;IACL,CAAC;CACJ","sourcesContent":["// src/WebRecorder.ts\n\nimport { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types'\nimport { ConsoleLike, RecordingConfig } from './ExpoAudioStream.types'\nimport {\n EmitAudioAnalysisFunction,\n EmitAudioEventFunction,\n} from './ExpoAudioStream.web'\nimport { convertPCMToFloat32 } from './utils/convertPCMToFloat32'\nimport { encodingToBitDepth } from './utils/encodingToBitDepth'\nimport { writeWavHeader } from './utils/writeWavHeader'\nimport { InlineFeaturesExtractor } from './workers/InlineFeaturesExtractor.web'\nimport { InlineAudioWebWorker } from './workers/inlineAudioWebWorker.web'\n\ninterface AudioWorkletEvent {\n data: {\n command: string\n recordedData?: Float32Array\n sampleRate?: number\n }\n}\n\ninterface AudioFeaturesEvent {\n data: {\n command: string\n result: AudioAnalysis\n }\n}\n\nconst DEFAULT_WEB_BITDEPTH = 32\nconst DEFAULT_WEB_POINTS_PER_SECOND = 10\nconst DEFAULT_WEB_INTERVAL = 500\nconst DEFAULT_WEB_NUMBER_OF_CHANNELS = 1\nconst DEFAULT_ALGORITHM = 'rms'\n\nconst TAG = 'WebRecorder'\n\nconst STOP_PERFORMANCE_MARKS = {\n STOP_INITIATED: 'stopInitiated',\n COMPRESSED_RECORDING_STOP: 'compressedRecordingStop',\n AUDIO_WORKLET_STOP: 'audioWorkletStop',\n CLEANUP: 'cleanup',\n TOTAL_STOP_TIME: 'totalStopTime',\n} as const\n\nexport class WebRecorder {\n private audioContext: AudioContext\n private audioWorkletNode!: AudioWorkletNode\n private featureExtractorWorker?: Worker\n private source: MediaStreamAudioSourceNode\n private audioWorkletUrl: string\n private emitAudioEventCallback: EmitAudioEventFunction\n private emitAudioAnalysisCallback: EmitAudioAnalysisFunction\n private config: RecordingConfig\n private position: number = 0\n private numberOfChannels: number // Number of audio channels\n private bitDepth: number // Bit depth of the audio\n private exportBitDepth: number // Bit depth of the audio\n private audioAnalysisData: AudioAnalysis // Keep updating the full audio analysis data with latest events\n private packetCount: number = 0\n private logger?: ConsoleLike\n private compressedMediaRecorder: MediaRecorder | null = null\n private compressedChunks: Blob[] = []\n private compressedSize: number = 0\n private pendingCompressedChunk: Blob | null = null\n private readonly wavMimeType = 'audio/wav'\n\n constructor({\n audioContext,\n source,\n recordingConfig,\n audioWorkletUrl,\n emitAudioEventCallback,\n emitAudioAnalysisCallback,\n logger,\n }: {\n audioContext: AudioContext\n source: MediaStreamAudioSourceNode\n recordingConfig: RecordingConfig\n audioWorkletUrl: string\n emitAudioEventCallback: EmitAudioEventFunction\n emitAudioAnalysisCallback: EmitAudioAnalysisFunction\n logger?: ConsoleLike\n }) {\n this.audioContext = audioContext\n this.source = source\n this.audioWorkletUrl = audioWorkletUrl\n this.emitAudioEventCallback = emitAudioEventCallback\n this.emitAudioAnalysisCallback = emitAudioAnalysisCallback\n this.config = recordingConfig\n this.logger = logger\n\n const audioContextFormat = this.checkAudioContextFormat({\n sampleRate: this.audioContext.sampleRate,\n })\n this.logger?.debug('Initialized WebRecorder with config:', {\n sampleRate: audioContextFormat.sampleRate,\n bitDepth: audioContextFormat.bitDepth,\n numberOfChannels: audioContextFormat.numberOfChannels,\n })\n\n this.bitDepth = audioContextFormat.bitDepth\n this.numberOfChannels =\n audioContextFormat.numberOfChannels ||\n DEFAULT_WEB_NUMBER_OF_CHANNELS // Default to 1 if not available\n this.exportBitDepth =\n encodingToBitDepth({\n encoding: recordingConfig.encoding ?? 'pcm_32bit',\n }) ||\n audioContextFormat.bitDepth ||\n DEFAULT_WEB_BITDEPTH\n\n this.audioAnalysisData = {\n amplitudeRange: { min: 0, max: 0 },\n dataPoints: [],\n durationMs: 0,\n samples: 0,\n amplitudeAlgorithm: recordingConfig.algorithm || DEFAULT_ALGORITHM,\n bitDepth: this.bitDepth,\n numberOfChannels: this.numberOfChannels,\n sampleRate: this.config.sampleRate || this.audioContext.sampleRate,\n pointsPerSecond:\n this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,\n speakerChanges: [],\n }\n\n if (recordingConfig.enableProcessing) {\n this.initFeatureExtractorWorker()\n }\n\n // Initialize compressed recording if enabled\n if (recordingConfig.compression?.enabled) {\n this.initializeCompressedRecorder()\n }\n }\n\n async init() {\n try {\n if (!this.audioWorkletUrl) {\n const blob = new Blob([InlineAudioWebWorker], {\n type: 'application/javascript',\n })\n const url = URL.createObjectURL(blob)\n await this.audioContext.audioWorklet.addModule(url)\n } else {\n await this.audioContext.audioWorklet.addModule(\n this.audioWorkletUrl\n )\n }\n this.audioWorkletNode = new AudioWorkletNode(\n this.audioContext,\n 'recorder-processor'\n )\n\n this.audioWorkletNode.port.onmessage = async (\n event: AudioWorkletEvent\n ) => {\n const command = event.data.command\n if (command !== 'newData') return\n\n const pcmBufferFloat = event.data.recordedData\n if (!pcmBufferFloat) {\n this.logger?.warn('Received empty audio buffer', event)\n return\n }\n\n // Process data in smaller chunks and emit immediately\n const chunkSize = this.audioContext.sampleRate * 2 // Reduce to 2 seconds chunks\n const sampleRate =\n event.data.sampleRate ?? this.audioContext.sampleRate\n const duration = pcmBufferFloat.length / sampleRate\n\n // Emit chunks without storing them\n for (let i = 0; i < pcmBufferFloat.length; i += chunkSize) {\n const chunk = pcmBufferFloat.slice(i, i + chunkSize)\n const chunkPosition = this.position + i / sampleRate\n\n // Process features if enabled\n if (\n this.config.enableProcessing &&\n this.featureExtractorWorker\n ) {\n this.featureExtractorWorker.postMessage(\n {\n command: 'process',\n channelData: chunk,\n sampleRate,\n pointsPerSecond:\n this.config.pointsPerSecond ||\n DEFAULT_WEB_POINTS_PER_SECOND,\n algorithm: this.config.algorithm || 'rms',\n bitDepth: this.bitDepth,\n fullAudioDurationMs: this.position * 1000,\n numberOfChannels: this.numberOfChannels,\n features: this.config.features,\n },\n []\n )\n }\n\n // Emit chunk immediately\n this.emitAudioEventCallback({\n data: chunk,\n position: chunkPosition,\n compression: this.pendingCompressedChunk\n ? {\n data: this.pendingCompressedChunk,\n size: this.pendingCompressedChunk.size,\n totalSize: this.compressedSize,\n mimeType: 'audio/webm',\n format: 'opus',\n bitrate:\n this.config.compression?.bitrate ??\n 128000,\n }\n : undefined,\n })\n }\n\n this.position += duration\n this.pendingCompressedChunk = null\n }\n\n this.logger?.debug(\n `WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`,\n this.config\n )\n this.audioWorkletNode.port.postMessage({\n command: 'init',\n recordSampleRate: this.audioContext.sampleRate,\n exportSampleRate:\n this.config.sampleRate ?? this.audioContext.sampleRate,\n bitDepth: this.bitDepth,\n exportBitDepth: this.exportBitDepth,\n channels: this.numberOfChannels,\n interval: this.config.interval ?? DEFAULT_WEB_INTERVAL,\n })\n\n // Connect the source to the AudioWorkletNode and start recording\n this.source.connect(this.audioWorkletNode)\n this.audioWorkletNode.connect(this.audioContext.destination)\n } catch (error) {\n console.error(`[${TAG}] Failed to initialize WebRecorder`, error)\n }\n }\n\n initFeatureExtractorWorker(featuresExtratorUrl?: string) {\n try {\n if (featuresExtratorUrl) {\n // Initialize the feature extractor worker\n //TODO: create audio feature extractor from a Blob instead of url since we cannot include the url directly in the library\n // We keep the url during dev and use the blob in production.\n this.featureExtractorWorker = new Worker(\n new URL(featuresExtratorUrl, window.location.href)\n )\n this.featureExtractorWorker.onmessage =\n this.handleFeatureExtractorMessage.bind(this)\n this.featureExtractorWorker.onerror =\n this.handleWorkerError.bind(this)\n } else {\n // Fallback to the inline worker if the URL is not provided\n this.initFallbackWorker()\n }\n } catch (error) {\n console.error(\n `[${TAG}] Failed to initialize feature extractor worker`,\n error\n )\n this.initFallbackWorker()\n }\n }\n\n initFallbackWorker() {\n try {\n const blob = new Blob([InlineFeaturesExtractor], {\n type: 'application/javascript',\n })\n const url = URL.createObjectURL(blob)\n this.featureExtractorWorker = new Worker(url)\n this.featureExtractorWorker.onmessage =\n this.handleFeatureExtractorMessage.bind(this)\n this.featureExtractorWorker.onerror = (error) => {\n console.error(`[${TAG}] Default Inline worker failed`, error)\n }\n this.logger?.log('Inline worker initialized successfully')\n } catch (error) {\n console.error(\n `[${TAG}] Failed to initialize Inline Feature Extractor worker`,\n error\n )\n }\n }\n\n handleWorkerError(error: ErrorEvent) {\n console.error(`[${TAG}] Feature extractor worker error:`, error)\n }\n\n handleFeatureExtractorMessage(event: AudioFeaturesEvent) {\n if (event.data.command === 'features') {\n const segmentResult = event.data.result\n\n // Merge the segment result with the full audio analysis data\n this.audioAnalysisData.dataPoints.push(...segmentResult.dataPoints)\n this.audioAnalysisData.speakerChanges?.push(\n ...(segmentResult.speakerChanges ?? [])\n )\n this.audioAnalysisData.durationMs = segmentResult.durationMs\n if (segmentResult.amplitudeRange) {\n this.audioAnalysisData.amplitudeRange = {\n min: Math.min(\n this.audioAnalysisData.amplitudeRange.min,\n segmentResult.amplitudeRange.min\n ),\n max: Math.max(\n this.audioAnalysisData.amplitudeRange.max,\n segmentResult.amplitudeRange.max\n ),\n }\n }\n // Handle the extracted features (e.g., emit an event or log them)\n this.logger?.debug('features event segmentResult', segmentResult)\n this.logger?.debug(\n `features event audioAnalysisData duration=${this.audioAnalysisData.durationMs}`,\n this.audioAnalysisData\n )\n this.emitAudioAnalysisCallback(segmentResult)\n }\n }\n\n start() {\n this.source.connect(this.audioWorkletNode)\n this.audioWorkletNode.connect(this.audioContext.destination)\n this.packetCount = 0\n\n if (this.compressedMediaRecorder) {\n this.compressedMediaRecorder.start(this.config.interval ?? 1000)\n }\n }\n\n async stop(): Promise<{ pcmData: Float32Array; compressedBlob?: Blob }> {\n try {\n if (this.compressedMediaRecorder) {\n this.compressedMediaRecorder.stop()\n return {\n pcmData: new Float32Array(), // Return empty array since we're streaming\n compressedBlob: new Blob(this.compressedChunks, {\n type: 'audio/webm;codecs=opus',\n }),\n }\n }\n return { pcmData: new Float32Array() }\n } finally {\n this.cleanup()\n }\n }\n\n private cleanup() {\n if (this.audioContext) {\n this.audioContext.close()\n }\n if (this.audioWorkletNode) {\n this.audioWorkletNode.disconnect()\n }\n if (this.source) {\n this.source.disconnect()\n }\n this.stopMediaStreamTracks()\n }\n\n // Helper method to process recording stop\n private async processRecordingStop(): Promise<{\n pcmData: Float32Array\n compressedBlob?: Blob\n }> {\n const processStartTime = performance.now()\n this.logger?.debug('[Performance] Starting recording stop process')\n\n const [compressedData, workletData] = await Promise.all([\n this.stopCompressedRecording(),\n this.stopAudioWorklet(),\n ])\n\n this.logger?.debug(\n `[Performance] Recording stop process completed in ${performance.now() - processStartTime}ms`\n )\n return {\n pcmData:\n workletData ??\n new Float32Array(this.audioAnalysisData.dataPoints.length),\n compressedBlob: compressedData,\n }\n }\n\n // Helper method to stop compressed recording\n private stopCompressedRecording(): Promise<Blob | undefined> {\n const startTime = performance.now()\n this.logger?.debug(\n `[Performance][${STOP_PERFORMANCE_MARKS.COMPRESSED_RECORDING_STOP}] Starting compressed recording stop`\n )\n\n if (!this.compressedMediaRecorder) {\n this.logger?.debug('[Performance] No compressed recorder to stop')\n return Promise.resolve(undefined)\n }\n\n return new Promise((resolve) => {\n this.compressedMediaRecorder!.onstop = () => {\n const blob = new Blob(this.compressedChunks, {\n type: 'audio/webm;codecs=opus',\n })\n this.logger?.debug(\n `[Performance][${STOP_PERFORMANCE_MARKS.COMPRESSED_RECORDING_STOP}] Compressed recording stopped in ${performance.now() - startTime}ms, size: ${blob.size}`\n )\n resolve(blob)\n }\n this.compressedMediaRecorder!.stop()\n })\n }\n\n // Helper method to stop audio worklet\n private stopAudioWorklet(): Promise<Float32Array | undefined> {\n const startTime = performance.now()\n this.logger?.debug(\n `[Performance][${STOP_PERFORMANCE_MARKS.AUDIO_WORKLET_STOP}] Starting audio worklet stop`\n )\n\n if (!this.audioWorkletNode) {\n this.logger?.debug('[Performance] No audio worklet to stop')\n return Promise.resolve(undefined)\n }\n\n return new Promise((resolve) => {\n const onMessage = (event: AudioWorkletEvent) => {\n if (event.data.command === 'recordedData') {\n this.audioWorkletNode?.port.removeEventListener(\n 'message',\n onMessage\n )\n const rawPCMDataFull = event.data.recordedData?.slice(0)\n\n if (!rawPCMDataFull) {\n this.logger?.debug('[Performance] No PCM data received')\n resolve(undefined)\n return\n }\n\n if (this.exportBitDepth !== this.bitDepth) {\n const conversionStart = performance.now()\n convertPCMToFloat32({\n buffer: rawPCMDataFull.buffer,\n bitDepth: this.exportBitDepth,\n skipWavHeader: true,\n logger: this.logger,\n }).then(({ pcmValues }) => {\n this.logger?.debug(\n `[Performance] PCM conversion completed in ${performance.now() - conversionStart}ms`\n )\n this.logger?.debug(\n `[Performance][${STOP_PERFORMANCE_MARKS.AUDIO_WORKLET_STOP}] Audio worklet stopped in ${performance.now() - startTime}ms`\n )\n resolve(pcmValues)\n })\n } else {\n this.logger?.debug(\n `[Performance][${STOP_PERFORMANCE_MARKS.AUDIO_WORKLET_STOP}] Audio worklet stopped in ${performance.now() - startTime}ms`\n )\n resolve(rawPCMDataFull)\n }\n }\n }\n\n this.audioWorkletNode.port.addEventListener('message', onMessage)\n this.audioWorkletNode.port.postMessage({ command: 'stop' })\n })\n }\n\n pause() {\n this.source.disconnect(this.audioWorkletNode) // Disconnect the source from the AudioWorkletNode\n this.audioWorkletNode.disconnect(this.audioContext.destination) // Disconnect the AudioWorkletNode from the destination\n this.audioWorkletNode.port.postMessage({ command: 'pause' })\n this.compressedMediaRecorder?.pause()\n }\n\n stopMediaStreamTracks() {\n // Stop all audio tracks to stop the recording icon\n const tracks = this.source.mediaStream.getTracks()\n tracks.forEach((track) => track.stop())\n }\n\n async playRecordedData({\n recordedData,\n }: {\n recordedData: ArrayBuffer\n mimeType?: string\n }) {\n try {\n // Create a WAV blob with proper headers\n const wavHeaderBuffer = writeWavHeader({\n buffer: recordedData,\n sampleRate: this.audioContext.sampleRate,\n numChannels: this.numberOfChannels,\n bitDepth: this.exportBitDepth,\n })\n\n const blob = new Blob([wavHeaderBuffer], { type: 'audio/wav' })\n const url = URL.createObjectURL(blob)\n const response = await fetch(url)\n const arrayBuffer = await response.arrayBuffer()\n\n // Decode the audio data\n const audioBuffer =\n await this.audioContext.decodeAudioData(arrayBuffer)\n\n // Create a buffer source node and play the audio\n const bufferSource = this.audioContext.createBufferSource()\n bufferSource.buffer = audioBuffer\n bufferSource.connect(this.audioContext.destination)\n bufferSource.start()\n this.logger?.debug('Playing recorded data', recordedData)\n\n // Clean up\n URL.revokeObjectURL(url)\n } catch (error) {\n console.error(`[${TAG}] Failed to play recorded data:`, error)\n }\n }\n\n private checkAudioContextFormat({ sampleRate }: { sampleRate: number }) {\n // Create a silent AudioBuffer\n const frameCount = sampleRate * 1.0 // 1 second buffer\n const audioBuffer = this.audioContext.createBuffer(\n 1,\n frameCount,\n sampleRate\n )\n\n // Check the format\n const channelData = audioBuffer.getChannelData(0)\n const bitDepth = channelData.BYTES_PER_ELEMENT * 8 // 4 bytes per element means 32-bit\n\n return {\n sampleRate: audioBuffer.sampleRate,\n bitDepth,\n numberOfChannels: audioBuffer.numberOfChannels,\n }\n }\n\n resume() {\n this.source.connect(this.audioWorkletNode)\n this.audioWorkletNode.connect(this.audioContext.destination)\n this.audioWorkletNode.port.postMessage({ command: 'resume' })\n this.compressedMediaRecorder?.resume()\n }\n\n private initializeCompressedRecorder() {\n try {\n const mimeType = 'audio/webm;codecs=opus'\n if (!MediaRecorder.isTypeSupported(mimeType)) {\n this.logger?.warn(\n 'Opus compression not supported in this browser'\n )\n return\n }\n\n this.compressedMediaRecorder = new MediaRecorder(\n this.source.mediaStream,\n {\n mimeType,\n audioBitsPerSecond:\n this.config.compression?.bitrate ?? 128000,\n }\n )\n\n this.compressedMediaRecorder.ondataavailable = (event) => {\n if (event.data.size > 0) {\n this.compressedChunks.push(event.data)\n this.compressedSize += event.data.size\n this.pendingCompressedChunk = event.data\n }\n }\n } catch (error) {\n this.logger?.error(\n 'Failed to initialize compressed recorder:',\n error\n )\n }\n }\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AudioAnalysis } from './AudioAnalysis/AudioAnalysis.types';
|
|
2
|
-
import { AudioRecording, CompressionInfo, ConsoleLike, RecordingConfig, StartRecordingResult
|
|
2
|
+
import { AudioRecording, CompressionInfo, ConsoleLike, RecordingConfig, StartRecordingResult } from './ExpoAudioStream.types';
|
|
3
3
|
export interface UseAudioRecorderProps {
|
|
4
4
|
logger?: ConsoleLike;
|
|
5
5
|
audioWorkletUrl?: string;
|
|
@@ -7,7 +7,7 @@ export interface UseAudioRecorderProps {
|
|
|
7
7
|
}
|
|
8
8
|
export interface UseAudioRecorderState {
|
|
9
9
|
startRecording: (_: RecordingConfig) => Promise<StartRecordingResult>;
|
|
10
|
-
stopRecording: (
|
|
10
|
+
stopRecording: () => Promise<AudioRecording>;
|
|
11
11
|
pauseRecording: () => Promise<void>;
|
|
12
12
|
resumeRecording: () => Promise<void>;
|
|
13
13
|
isRecording: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAudioRecorder.d.ts","sourceRoot":"","sources":["../src/useAudioRecorder.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAA;AACnE,OAAO,EAEH,cAAc,EAEd,eAAe,EACf,WAAW,EACX,eAAe,EACf,oBAAoB,
|
|
1
|
+
{"version":3,"file":"useAudioRecorder.d.ts","sourceRoot":"","sources":["../src/useAudioRecorder.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAA;AACnE,OAAO,EAEH,cAAc,EAEd,eAAe,EACf,WAAW,EACX,eAAe,EACf,oBAAoB,EACvB,MAAM,yBAAyB,CAAA;AAQhC,MAAM,WAAW,qBAAqB;IAClC,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED,MAAM,WAAW,qBAAqB;IAClC,cAAc,EAAE,CAAC,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,oBAAoB,CAAC,CAAA;IACrE,aAAa,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,CAAA;IAC5C,cAAc,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACnC,eAAe,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACpC,WAAW,EAAE,OAAO,CAAA;IACpB,QAAQ,EAAE,OAAO,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,eAAe,CAAA;IAC7B,YAAY,CAAC,EAAE,aAAa,CAAA;CAC/B;AAwGD,wBAAgB,gBAAgB,CAAC,EAC7B,MAAM,EACN,eAAe,EACf,mBAAmB,GACtB,GAAE,qBAA0B,GAAG,qBAAqB,CAiYpD"}
|