@siteed/audio-studio 3.0.2 → 3.0.3

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.
Files changed (86) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/android/src/main/java/net/siteed/audiostudio/AudioStudioModule.kt +7 -1
  3. package/build/cjs/AudioAnalysis/AudioAnalysis.types.js.map +1 -1
  4. package/build/cjs/AudioAnalysis/audioFeaturesWasm.js +10 -7
  5. package/build/cjs/AudioAnalysis/audioFeaturesWasm.js.map +1 -1
  6. package/build/cjs/AudioAnalysis/audioFeaturesWasm.web.js +78 -97
  7. package/build/cjs/AudioAnalysis/audioFeaturesWasm.web.js.map +1 -1
  8. package/build/cjs/AudioAnalysis/extractAudioAnalysis.js +15 -12
  9. package/build/cjs/AudioAnalysis/extractAudioAnalysis.js.map +1 -1
  10. package/build/cjs/AudioAnalysis/extractAudioData.js +144 -2
  11. package/build/cjs/AudioAnalysis/extractAudioData.js.map +1 -1
  12. package/build/cjs/AudioAnalysis/melSpectrogramWasm.web.js +9 -56
  13. package/build/cjs/AudioAnalysis/melSpectrogramWasm.web.js.map +1 -1
  14. package/build/cjs/AudioAnalysis/wasmConfig.js +4 -4
  15. package/build/cjs/AudioAnalysis/wasmConfig.js.map +1 -1
  16. package/build/cjs/AudioAnalysis/wasmLoader.web.js +78 -0
  17. package/build/cjs/AudioAnalysis/wasmLoader.web.js.map +1 -0
  18. package/build/cjs/AudioStudioModule.js +4 -599
  19. package/build/cjs/AudioStudioModule.js.map +1 -1
  20. package/build/cjs/trimAudio.js +227 -0
  21. package/build/cjs/trimAudio.js.map +1 -1
  22. package/build/cjs/utils/encodeCompressedAudio.web.js +65 -0
  23. package/build/cjs/utils/encodeCompressedAudio.web.js.map +1 -0
  24. package/build/cjs/utils/resampleAudioBuffer.web.js +25 -0
  25. package/build/cjs/utils/resampleAudioBuffer.web.js.map +1 -0
  26. package/build/esm/AudioAnalysis/AudioAnalysis.types.js.map +1 -1
  27. package/build/esm/AudioAnalysis/audioFeaturesWasm.js +8 -5
  28. package/build/esm/AudioAnalysis/audioFeaturesWasm.js.map +1 -1
  29. package/build/esm/AudioAnalysis/audioFeaturesWasm.web.js +76 -62
  30. package/build/esm/AudioAnalysis/audioFeaturesWasm.web.js.map +1 -1
  31. package/build/esm/AudioAnalysis/extractAudioAnalysis.js +15 -12
  32. package/build/esm/AudioAnalysis/extractAudioAnalysis.js.map +1 -1
  33. package/build/esm/AudioAnalysis/extractAudioData.js +144 -2
  34. package/build/esm/AudioAnalysis/extractAudioData.js.map +1 -1
  35. package/build/esm/AudioAnalysis/melSpectrogramWasm.web.js +9 -23
  36. package/build/esm/AudioAnalysis/melSpectrogramWasm.web.js.map +1 -1
  37. package/build/esm/AudioAnalysis/wasmConfig.js +4 -4
  38. package/build/esm/AudioAnalysis/wasmConfig.js.map +1 -1
  39. package/build/esm/AudioAnalysis/wasmLoader.web.js +42 -0
  40. package/build/esm/AudioAnalysis/wasmLoader.web.js.map +1 -0
  41. package/build/esm/AudioStudioModule.js +4 -596
  42. package/build/esm/AudioStudioModule.js.map +1 -1
  43. package/build/esm/trimAudio.js +227 -0
  44. package/build/esm/trimAudio.js.map +1 -1
  45. package/build/esm/utils/encodeCompressedAudio.web.js +62 -0
  46. package/build/esm/utils/encodeCompressedAudio.web.js.map +1 -0
  47. package/build/esm/utils/resampleAudioBuffer.web.js +22 -0
  48. package/build/esm/utils/resampleAudioBuffer.web.js.map +1 -0
  49. package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts +11 -0
  50. package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -1
  51. package/build/types/AudioAnalysis/audioFeaturesWasm.d.ts +5 -9
  52. package/build/types/AudioAnalysis/audioFeaturesWasm.d.ts.map +1 -1
  53. package/build/types/AudioAnalysis/audioFeaturesWasm.web.d.ts +35 -16
  54. package/build/types/AudioAnalysis/audioFeaturesWasm.web.d.ts.map +1 -1
  55. package/build/types/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -1
  56. package/build/types/AudioAnalysis/extractAudioData.d.ts +2 -2
  57. package/build/types/AudioAnalysis/extractAudioData.d.ts.map +1 -1
  58. package/build/types/AudioAnalysis/melSpectrogramWasm.web.d.ts.map +1 -1
  59. package/build/types/AudioAnalysis/wasmLoader.web.d.ts +3 -0
  60. package/build/types/AudioAnalysis/wasmLoader.web.d.ts.map +1 -0
  61. package/build/types/AudioStudioModule.d.ts.map +1 -1
  62. package/build/types/trimAudio.d.ts.map +1 -1
  63. package/build/types/utils/encodeCompressedAudio.web.d.ts +10 -0
  64. package/build/types/utils/encodeCompressedAudio.web.d.ts.map +1 -0
  65. package/build/types/utils/resampleAudioBuffer.web.d.ts +2 -0
  66. package/build/types/utils/resampleAudioBuffer.web.d.ts.map +1 -0
  67. package/package.json +1 -1
  68. package/src/AudioAnalysis/AudioAnalysis.types.ts +12 -0
  69. package/src/AudioAnalysis/audioFeaturesWasm.ts +17 -22
  70. package/src/AudioAnalysis/audioFeaturesWasm.web.ts +102 -94
  71. package/src/AudioAnalysis/extractAudioAnalysis.ts +23 -20
  72. package/src/AudioAnalysis/extractAudioData.ts +186 -4
  73. package/src/AudioAnalysis/melSpectrogramWasm.web.ts +10 -27
  74. package/src/AudioAnalysis/wasmConfig.ts +4 -4
  75. package/src/AudioAnalysis/wasmLoader.web.ts +48 -0
  76. package/src/AudioStudioModule.ts +6 -854
  77. package/src/trimAudio.ts +337 -0
  78. package/src/utils/encodeCompressedAudio.web.ts +78 -0
  79. package/src/utils/resampleAudioBuffer.web.ts +39 -0
  80. package/build/cjs/AudioAnalysis/extractWaveform.js +0 -18
  81. package/build/cjs/AudioAnalysis/extractWaveform.js.map +0 -1
  82. package/build/esm/AudioAnalysis/extractWaveform.js +0 -11
  83. package/build/esm/AudioAnalysis/extractWaveform.js.map +0 -1
  84. package/build/types/AudioAnalysis/extractWaveform.d.ts +0 -8
  85. package/build/types/AudioAnalysis/extractWaveform.d.ts.map +0 -1
  86. package/src/AudioAnalysis/extractWaveform.ts +0 -22
@@ -1,17 +1,12 @@
1
1
  import { requireNativeModule } from 'expo-modules-core';
2
2
  import { Platform } from 'react-native';
3
3
  import { AudioStudioWeb } from './AudioStudio.web';
4
- import { processAudioBuffer } from './utils/audioProcessing';
5
- import crc32 from './utils/crc32';
6
- import { writeWavHeader } from './utils/writeWavHeader';
7
4
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
5
  let AudioStudioModule;
9
6
  if (Platform.OS === 'web') {
10
7
  let instance = null;
11
8
  AudioStudioModule = (webProps) => {
12
- if (!instance) {
13
- instance = new AudioStudioWeb(webProps);
14
- }
9
+ instance ??= new AudioStudioWeb(webProps);
15
10
  return instance;
16
11
  };
17
12
  AudioStudioModule.requestPermissionsAsync = async () => {
@@ -68,487 +63,12 @@ if (Platform.OS === 'web') {
68
63
  return await AudioStudioModule.requestPermissionsAsync();
69
64
  }
70
65
  };
71
- AudioStudioModule.extractAudioData = async (options) => {
72
- try {
73
- const { fileUri, position, length, startTimeMs, endTimeMs, decodingOptions, includeNormalizedData, includeBase64Data, includeWavHeader = false, logger, } = options;
74
- logger?.debug('EXTRACT AUDIO - Step 1: Initial request', {
75
- fileUri,
76
- extractionParams: {
77
- position,
78
- length,
79
- startTimeMs,
80
- endTimeMs,
81
- },
82
- decodingOptions: {
83
- targetSampleRate: decodingOptions?.targetSampleRate ?? 16000,
84
- targetChannels: decodingOptions?.targetChannels ?? 1,
85
- targetBitDepth: decodingOptions?.targetBitDepth ?? 16,
86
- normalizeAudio: decodingOptions?.normalizeAudio ?? false,
87
- },
88
- outputOptions: {
89
- includeNormalizedData,
90
- includeBase64Data,
91
- includeWavHeader,
92
- },
93
- });
94
- // Process the audio using shared helper function
95
- const processedBuffer = await processAudioBuffer({
96
- fileUri,
97
- targetSampleRate: decodingOptions?.targetSampleRate ?? 16000,
98
- targetChannels: decodingOptions?.targetChannels ?? 1,
99
- normalizeAudio: decodingOptions?.normalizeAudio ?? false,
100
- position,
101
- length,
102
- startTimeMs,
103
- endTimeMs,
104
- logger,
105
- });
106
- logger?.debug('EXTRACT AUDIO - Step 2: Audio processing complete', {
107
- processedData: {
108
- samples: processedBuffer.samples,
109
- sampleRate: processedBuffer.sampleRate,
110
- channels: processedBuffer.channels,
111
- durationMs: processedBuffer.durationMs,
112
- },
113
- });
114
- const channelData = processedBuffer.channelData;
115
- const bitDepth = (decodingOptions?.targetBitDepth ?? 16);
116
- const bytesPerSample = bitDepth / 8;
117
- const numSamples = processedBuffer.samples;
118
- logger?.debug('EXTRACT AUDIO - Step 3: PCM conversion setup', {
119
- channelData: {
120
- length: channelData.length,
121
- first: channelData[0],
122
- last: channelData[channelData.length - 1],
123
- },
124
- calculation: {
125
- bitDepth,
126
- bytesPerSample,
127
- numSamples,
128
- expectedBytes: numSamples * bytesPerSample,
129
- },
130
- });
131
- // Create PCM data with correct length based on original byte length
132
- const pcmData = new Uint8Array(numSamples * bytesPerSample);
133
- let offset = 0;
134
- // Convert Float32 samples to PCM format
135
- for (let i = 0; i < numSamples; i++) {
136
- const sample = channelData[i];
137
- const value = Math.max(-1, Math.min(1, sample));
138
- // Convert to 16-bit signed integer
139
- let intValue = Math.round(value * 32767);
140
- // Handle negative values correctly
141
- if (intValue < 0) {
142
- intValue = 65536 + intValue;
143
- }
144
- // Write as little-endian
145
- pcmData[offset++] = intValue & 255; // Low byte
146
- pcmData[offset++] = (intValue >> 8) & 255; // High byte
147
- }
148
- const durationMs = Math.round((numSamples / processedBuffer.sampleRate) * 1000);
149
- logger?.debug('EXTRACT AUDIO - Step 4: Final output', {
150
- pcmData: {
151
- length: pcmData.length,
152
- first: pcmData[0],
153
- last: pcmData[pcmData.length - 1],
154
- },
155
- timing: {
156
- numSamples,
157
- sampleRate: processedBuffer.sampleRate,
158
- durationMs,
159
- shouldBe3000ms: endTimeMs
160
- ? endTimeMs - (startTimeMs ?? 0) === 3000
161
- : undefined,
162
- },
163
- });
164
- const result = {
165
- pcmData: new Uint8Array(pcmData.buffer),
166
- sampleRate: processedBuffer.sampleRate,
167
- channels: processedBuffer.channels,
168
- bitDepth,
169
- durationMs,
170
- format: `pcm_${bitDepth}bit`,
171
- samples: numSamples,
172
- };
173
- // Add WAV header if requested
174
- if (includeWavHeader) {
175
- logger?.debug('EXTRACT AUDIO - Step 4: Adding WAV header', {
176
- originalLength: pcmData.length,
177
- newLength: result.pcmData.length,
178
- firstBytes: Array.from(result.pcmData.slice(0, 44)), // WAV header is 44 bytes
179
- });
180
- const wavBuffer = writeWavHeader({
181
- buffer: pcmData.buffer.slice(0, pcmData.length),
182
- sampleRate: processedBuffer.sampleRate,
183
- numChannels: processedBuffer.channels,
184
- bitDepth,
185
- });
186
- result.pcmData = new Uint8Array(wavBuffer);
187
- result.hasWavHeader = true;
188
- }
189
- if (includeNormalizedData) {
190
- // // Simple approach: Create normalized data directly from the PCM data
191
- // // Just convert to -1 to 1 range without any amplification
192
- // const normalizedData = new Float32Array(numSamples)
193
- // // Convert the PCM data to float values
194
- // for (let i = 0; i < numSamples; i++) {
195
- // // Get the 16-bit PCM value (little endian)
196
- // const lowByte = pcmData[i * 2]
197
- // const highByte = pcmData[i * 2 + 1]
198
- // const pcmValue = (highByte << 8) | lowByte
199
- // // Convert to signed 16-bit value
200
- // const signedValue =
201
- // pcmValue > 32767 ? pcmValue - 65536 : pcmValue
202
- // // Normalize to float between -1 and 1
203
- // normalizedData[i] = signedValue / 32768.0
204
- // }
205
- // Store the normalized data in the result
206
- result.normalizedData = channelData;
207
- }
208
- if (includeBase64Data) {
209
- // Convert the PCM data to a base64 string
210
- const binary = Array.from(new Uint8Array(pcmData.buffer))
211
- .map((b) => String.fromCharCode(b))
212
- .join('');
213
- result.base64Data = btoa(binary);
214
- }
215
- if (options.computeChecksum) {
216
- result.checksum = crc32.buf(pcmData);
217
- }
218
- logger?.debug('EXTRACT AUDIO - Step 3: PCM conversion complete', {
219
- pcmStats: {
220
- length: pcmData.length,
221
- bytesPerSample,
222
- totalSamples: numSamples,
223
- firstBytes: Array.from(pcmData.slice(0, 16)),
224
- lastBytes: Array.from(pcmData.slice(-16)),
225
- },
226
- });
227
- return result;
228
- }
229
- catch (error) {
230
- options.logger?.error('EXTRACT AUDIO - Error:', error);
231
- throw error;
232
- }
233
- };
234
- AudioStudioModule.trimAudio = async (options) => {
235
- try {
236
- const startTime = performance.now();
237
- const { fileUri, mode = 'single', startTimeMs, endTimeMs, ranges, outputFileName, outputFormat, } = options;
238
- // Validate inputs
239
- if (!fileUri) {
240
- throw new Error('fileUri is required');
241
- }
242
- if (mode === 'single' &&
243
- startTimeMs === undefined &&
244
- endTimeMs === undefined) {
245
- throw new Error('At least one of startTimeMs or endTimeMs must be provided in single mode');
246
- }
247
- if ((mode === 'keep' || mode === 'remove') &&
248
- (!ranges || ranges.length === 0)) {
249
- throw new Error('ranges must be provided and non-empty for keep or remove modes');
250
- }
251
- // Create AudioContext
252
- const audioContext = new (window.AudioContext ||
253
- window.webkitAudioContext)();
254
- // First, load the entire audio file to get its properties
255
- const response = await fetch(fileUri);
256
- const arrayBuffer = await response.arrayBuffer();
257
- const originalAudioBuffer = await audioContext.decodeAudioData(arrayBuffer);
258
- // Get original audio properties
259
- const originalSampleRate = originalAudioBuffer.sampleRate;
260
- const originalChannels = originalAudioBuffer.numberOfChannels;
261
- // Add more detailed logging
262
- console.log(`Original audio details:`, {
263
- sampleRate: originalSampleRate,
264
- channels: originalChannels,
265
- duration: originalAudioBuffer.duration,
266
- length: originalAudioBuffer.length,
267
- // Log a few samples to verify content
268
- firstSamples: Array.from(originalAudioBuffer.getChannelData(0).slice(0, 5)),
269
- });
270
- // Determine output format - use original values as defaults if not specified
271
- let format = outputFormat?.format || 'wav';
272
- const targetSampleRate = outputFormat?.sampleRate || originalSampleRate;
273
- const targetChannels = outputFormat?.channels || originalChannels;
274
- const targetBitDepth = outputFormat?.bitDepth || 16;
275
- // Get file info from the URL
276
- const filename = outputFileName ||
277
- fileUri.split('/').pop() ||
278
- 'trimmed-audio.wav';
279
- // Process based on mode
280
- let resultBuffer;
281
- // Report initial progress
282
- AudioStudioModule.sendEvent('TrimProgress', {
283
- progress: 10,
284
- });
285
- if (mode === 'single') {
286
- // Single mode: extract a single range
287
- // Use original sample rate and channels for extraction to preserve quality
288
- const { buffer } = await processAudioBuffer({
289
- fileUri,
290
- targetSampleRate, // Use the requested sample rate
291
- targetChannels,
292
- normalizeAudio: false,
293
- startTimeMs,
294
- endTimeMs,
295
- audioContext,
296
- });
297
- console.log(`Processed buffer details:`, {
298
- sampleRate: buffer.sampleRate,
299
- channels: buffer.numberOfChannels,
300
- duration: buffer.duration,
301
- length: buffer.length,
302
- // Log a few samples to verify content
303
- firstSamples: Array.from(buffer.getChannelData(0).slice(0, 5)),
304
- });
305
- resultBuffer = buffer;
306
- // If we need to change sample rate or channels, do it after extraction
307
- if (targetSampleRate !== originalSampleRate ||
308
- targetChannels !== originalChannels) {
309
- console.log(`Resampling from ${originalSampleRate}Hz to ${targetSampleRate}Hz`);
310
- resultBuffer = await resampleAudioBuffer(audioContext, buffer, targetSampleRate, targetChannels);
311
- }
312
- }
313
- else {
314
- // For keep or remove modes
315
- const fullDuration = originalAudioBuffer.duration * 1000; // in ms
316
- let segmentsToProcess = [];
317
- if (mode === 'keep') {
318
- // For keep mode, use the ranges directly
319
- segmentsToProcess = ranges;
320
- }
321
- else {
322
- // mode === 'remove'
323
- // For remove mode, invert the ranges
324
- const sortedRanges = [...ranges].sort((a, b) => a.startTimeMs - b.startTimeMs);
325
- // Add segment from start to first range if needed
326
- if (sortedRanges.length > 0 &&
327
- sortedRanges[0].startTimeMs > 0) {
328
- segmentsToProcess.push({
329
- startTimeMs: 0,
330
- endTimeMs: sortedRanges[0].startTimeMs,
331
- });
332
- }
333
- // Add segments between ranges
334
- for (let i = 0; i < sortedRanges.length - 1; i++) {
335
- segmentsToProcess.push({
336
- startTimeMs: sortedRanges[i].endTimeMs,
337
- endTimeMs: sortedRanges[i + 1].startTimeMs,
338
- });
339
- }
340
- // Add segment from last range to end if needed
341
- if (sortedRanges.length > 0 &&
342
- sortedRanges[sortedRanges.length - 1].endTimeMs <
343
- fullDuration) {
344
- segmentsToProcess.push({
345
- startTimeMs: sortedRanges[sortedRanges.length - 1].endTimeMs,
346
- endTimeMs: fullDuration,
347
- });
348
- }
349
- }
350
- // Filter out empty or invalid segments
351
- segmentsToProcess = segmentsToProcess.filter((segment) => segment.startTimeMs < segment.endTimeMs &&
352
- segment.endTimeMs - segment.startTimeMs > 1); // 1ms minimum
353
- if (segmentsToProcess.length === 0) {
354
- throw new Error('No valid segments to process after filtering ranges');
355
- }
356
- // Process each segment using original sample rate and channels
357
- const segmentBuffers = [];
358
- for (let i = 0; i < segmentsToProcess.length; i++) {
359
- const segment = segmentsToProcess[i];
360
- // Report progress for each segment
361
- AudioStudioModule.sendEvent('TrimProgress', {
362
- progress: 10 +
363
- Math.round((i / segmentsToProcess.length) * 40),
364
- });
365
- // Use processAudioBuffer to extract this segment
366
- const { buffer: segmentBuffer } = await processAudioBuffer({
367
- fileUri,
368
- targetSampleRate: originalSampleRate, // Use original sample rate
369
- targetChannels: originalChannels, // Use original channels
370
- normalizeAudio: false,
371
- startTimeMs: segment.startTimeMs,
372
- endTimeMs: segment.endTimeMs,
373
- audioContext,
374
- });
375
- segmentBuffers.push(segmentBuffer);
376
- }
377
- // Concatenate all segments
378
- const totalSamples = segmentBuffers.reduce((sum, buffer) => sum + buffer.length, 0);
379
- // Create buffer with original properties first
380
- const concatenatedBuffer = audioContext.createBuffer(originalChannels, totalSamples, originalSampleRate);
381
- let offset = 0;
382
- for (const segmentBuffer of segmentBuffers) {
383
- for (let channel = 0; channel < originalChannels; channel++) {
384
- const outputData = concatenatedBuffer.getChannelData(channel);
385
- const segmentData = segmentBuffer.getChannelData(channel);
386
- for (let i = 0; i < segmentBuffer.length; i++) {
387
- outputData[offset + i] = segmentData[i];
388
- }
389
- }
390
- offset += segmentBuffer.length;
391
- }
392
- resultBuffer = concatenatedBuffer;
393
- // If we need to change sample rate or channels, do it after concatenation
394
- if (targetSampleRate !== originalSampleRate ||
395
- targetChannels !== originalChannels) {
396
- console.log(`Resampling concatenated buffer from ${originalSampleRate}Hz to ${targetSampleRate}Hz`);
397
- resultBuffer = await resampleAudioBuffer(audioContext, concatenatedBuffer, targetSampleRate, targetChannels);
398
- }
399
- }
400
- // Report progress (50% - processing complete)
401
- AudioStudioModule.sendEvent('TrimProgress', {
402
- progress: 50,
403
- });
404
- // Encode the result based on the requested format
405
- let outputData;
406
- let outputMimeType;
407
- let compressionInfo = null;
408
- // Check if AAC was requested on web and show a warning
409
- if (format === 'aac' && Platform.OS === 'web') {
410
- console.warn('AAC format is not supported on web platforms. Falling back to OPUS format.');
411
- format = 'opus';
412
- }
413
- if (format === 'wav') {
414
- // Create a properly interleaved buffer for WAV format
415
- // For WAV, we need to convert Float32Array to Int16Array (for 16-bit audio)
416
- const numSamples = resultBuffer.length * resultBuffer.numberOfChannels;
417
- const interleavedData = new Int16Array(numSamples);
418
- // Log detailed information about the buffer before encoding
419
- console.log(`Creating WAV file:`, {
420
- bufferSampleRate: resultBuffer.sampleRate,
421
- bufferChannels: resultBuffer.numberOfChannels,
422
- bufferLength: resultBuffer.length,
423
- targetSampleRate,
424
- targetChannels,
425
- targetBitDepth,
426
- // Log a few samples to verify content
427
- firstSamples: Array.from(resultBuffer.getChannelData(0).slice(0, 5)),
428
- });
429
- // Interleave channels properly
430
- for (let i = 0; i < resultBuffer.length; i++) {
431
- for (let channel = 0; channel < resultBuffer.numberOfChannels; channel++) {
432
- // Convert float (-1.0 to 1.0) to int16 (-32768 to 32767)
433
- const floatSample = resultBuffer.getChannelData(channel)[i];
434
- // Clamp the value to -1.0 to 1.0
435
- const clampedSample = Math.max(-1.0, Math.min(1.0, floatSample));
436
- // Convert to int16
437
- const intSample = Math.round(clampedSample * 32767);
438
- // Store in interleaved buffer
439
- interleavedData[i * resultBuffer.numberOfChannels + channel] = intSample;
440
- }
441
- }
442
- // Convert Int16Array to ArrayBuffer for WAV header
443
- const rawBuffer = interleavedData.buffer;
444
- // IMPORTANT: Make sure we're using the ACTUAL sample rate of the buffer
445
- // not just what was requested in the options
446
- console.log(`Creating WAV with ${resultBuffer.numberOfChannels} channels at ${resultBuffer.sampleRate}Hz`);
447
- outputData = writeWavHeader({
448
- buffer: rawBuffer,
449
- sampleRate: resultBuffer.sampleRate, // Use the actual buffer's sample rate
450
- numChannels: resultBuffer.numberOfChannels,
451
- bitDepth: targetBitDepth,
452
- });
453
- outputMimeType = 'audio/wav';
454
- }
455
- else if (format === 'opus' || format === 'aac') {
456
- try {
457
- // Try to use MediaRecorder for compressed formats
458
- const { data, bitrate } = await encodeCompressedAudio(resultBuffer, format, outputFormat?.bitrate);
459
- outputData = data;
460
- outputMimeType =
461
- format === 'opus' ? 'audio/webm' : 'audio/aac';
462
- compressionInfo = {
463
- format,
464
- bitrate,
465
- size: data.byteLength,
466
- };
467
- }
468
- catch (error) {
469
- console.warn(`Failed to encode to ${format}, falling back to WAV: ${error}`);
470
- // Same WAV encoding as above
471
- const wavData = new Float32Array(resultBuffer.length * resultBuffer.numberOfChannels);
472
- for (let i = 0; i < resultBuffer.length; i++) {
473
- for (let channel = 0; channel < resultBuffer.numberOfChannels; channel++) {
474
- wavData[i * resultBuffer.numberOfChannels + channel] = resultBuffer.getChannelData(channel)[i];
475
- }
476
- }
477
- outputData = writeWavHeader({
478
- buffer: wavData.buffer,
479
- sampleRate: resultBuffer.sampleRate,
480
- numChannels: resultBuffer.numberOfChannels,
481
- bitDepth: targetBitDepth,
482
- });
483
- outputMimeType = 'audio/wav';
484
- }
485
- }
486
- else {
487
- // Default to WAV for unsupported formats
488
- console.warn(`Format ${format} not supported on web, using WAV instead`);
489
- // Same WAV encoding as above
490
- const wavData = new Float32Array(resultBuffer.length * resultBuffer.numberOfChannels);
491
- for (let i = 0; i < resultBuffer.length; i++) {
492
- for (let channel = 0; channel < resultBuffer.numberOfChannels; channel++) {
493
- wavData[i * resultBuffer.numberOfChannels + channel] =
494
- resultBuffer.getChannelData(channel)[i];
495
- }
496
- }
497
- outputData = writeWavHeader({
498
- buffer: wavData.buffer,
499
- sampleRate: resultBuffer.sampleRate,
500
- numChannels: resultBuffer.numberOfChannels,
501
- bitDepth: targetBitDepth,
502
- });
503
- outputMimeType = 'audio/wav';
504
- }
505
- // Report progress (90% - encoding complete)
506
- AudioStudioModule.sendEvent('TrimProgress', {
507
- progress: 90,
508
- });
509
- // Create a blob and URL for the result
510
- const blob = new Blob([outputData], { type: outputMimeType });
511
- const outputUri = URL.createObjectURL(blob);
512
- // Calculate processing time
513
- const processingTimeMs = performance.now() - startTime;
514
- // Report progress (100% - complete)
515
- AudioStudioModule.sendEvent('TrimProgress', {
516
- progress: 100,
517
- });
518
- // Create result object
519
- const result = {
520
- uri: outputUri,
521
- filename,
522
- durationMs: Math.round(resultBuffer.duration * 1000),
523
- size: outputData.byteLength,
524
- sampleRate: resultBuffer.sampleRate,
525
- channels: resultBuffer.numberOfChannels,
526
- bitDepth: targetBitDepth,
527
- mimeType: outputMimeType,
528
- processingInfo: {
529
- durationMs: processingTimeMs,
530
- },
531
- };
532
- // Add compression info if available
533
- if (compressionInfo) {
534
- result.compression = compressionInfo;
535
- }
536
- return result;
537
- }
538
- catch (error) {
539
- console.error('Error in trimAudio:', error);
540
- throw error;
541
- }
542
- };
543
66
  // Add a sendEvent method for web
544
67
  AudioStudioModule.sendEvent = (eventName, params) => {
545
68
  // This will be picked up by the LegacyEventEmitter in trimAudio.ts
546
- if (AudioStudioModule.listeners &&
547
- AudioStudioModule.listeners[eventName]) {
548
- AudioStudioModule.listeners[eventName].forEach((listener) => {
549
- listener(params);
550
- });
551
- }
69
+ AudioStudioModule.listeners?.[eventName]?.forEach((listener) => {
70
+ listener(params);
71
+ });
552
72
  };
553
73
  // Initialize listeners object
554
74
  AudioStudioModule.listeners = {};
@@ -594,118 +114,6 @@ if (Platform.OS === 'web') {
594
114
  }
595
115
  };
596
116
  }
597
- // Move the encodeCompressedAudio function outside the if block to fix the ESLint error
598
- async function encodeCompressedAudio(buffer, format, bitrate) {
599
- return new Promise((resolve, reject) => {
600
- try {
601
- // On web, always use opus if aac is requested
602
- const actualFormat = Platform.OS === 'web' && format === 'aac' ? 'opus' : format;
603
- // Check if MediaRecorder supports the requested format
604
- const mimeType = actualFormat === 'opus' ? 'audio/webm;codecs=opus' : 'audio/aac';
605
- if (!MediaRecorder.isTypeSupported(mimeType)) {
606
- throw new Error(`MediaRecorder does not support ${mimeType}`);
607
- }
608
- // Create a new AudioContext and source
609
- const ctx = new (window.AudioContext ||
610
- window.webkitAudioContext)();
611
- const source = ctx.createBufferSource();
612
- source.buffer = buffer;
613
- // Create a MediaStreamDestination to capture the audio
614
- const destination = ctx.createMediaStreamDestination();
615
- source.connect(destination);
616
- // Create a MediaRecorder with the requested format
617
- const recorder = new MediaRecorder(destination.stream, {
618
- mimeType,
619
- audioBitsPerSecond: bitrate || (actualFormat === 'opus' ? 32000 : 64000),
620
- });
621
- const chunks = [];
622
- recorder.ondataavailable = (e) => {
623
- if (e.data.size > 0) {
624
- chunks.push(e.data);
625
- }
626
- };
627
- recorder.onstop = async () => {
628
- try {
629
- const blob = new Blob(chunks, { type: mimeType });
630
- const arrayBuffer = await blob.arrayBuffer();
631
- // Get the actual bitrate used
632
- const actualBitrate = Math.round((arrayBuffer.byteLength * 8) / buffer.duration);
633
- resolve({
634
- data: arrayBuffer,
635
- bitrate: actualBitrate / 1000, // Convert to kbps
636
- });
637
- // Clean up
638
- ctx.close();
639
- }
640
- catch (error) {
641
- reject(error);
642
- }
643
- };
644
- // Start recording and playback
645
- recorder.start();
646
- source.start(0);
647
- // Stop recording when the buffer finishes playing
648
- setTimeout(() => {
649
- recorder.stop();
650
- source.stop();
651
- }, buffer.duration * 1000);
652
- }
653
- catch (error) {
654
- reject(error);
655
- }
656
- });
657
- }
658
- // Improved resampleAudioBuffer function
659
- async function resampleAudioBuffer(context, buffer, targetSampleRate, targetChannels) {
660
- // If no change needed, return the original buffer
661
- if (buffer.sampleRate === targetSampleRate &&
662
- buffer.numberOfChannels === targetChannels) {
663
- return buffer;
664
- }
665
- console.log(`Resampling: ${buffer.sampleRate}Hz → ${targetSampleRate}Hz, ${buffer.numberOfChannels} → ${targetChannels} channels`);
666
- // Calculate the new length based on the sample rate change
667
- const newLength = Math.round((buffer.length * targetSampleRate) / buffer.sampleRate);
668
- // Create an offline context for resampling
669
- const offlineContext = new OfflineAudioContext(targetChannels, newLength, targetSampleRate);
670
- // Create a source node
671
- const source = offlineContext.createBufferSource();
672
- source.buffer = buffer;
673
- // If we need to change channel count
674
- if (buffer.numberOfChannels !== targetChannels) {
675
- if (targetChannels === 1 && buffer.numberOfChannels > 1) {
676
- // Downmix to mono
677
- const merger = offlineContext.createChannelMerger(1);
678
- // Create a gain node to reduce volume when downmixing to prevent clipping
679
- const gainNode = offlineContext.createGain();
680
- gainNode.gain.value = 1.0 / buffer.numberOfChannels;
681
- source.connect(gainNode);
682
- gainNode.connect(merger);
683
- merger.connect(offlineContext.destination);
684
- }
685
- else if (targetChannels === 2 && buffer.numberOfChannels === 1) {
686
- // Upmix mono to stereo (duplicate the channel)
687
- const splitter = offlineContext.createChannelSplitter(1);
688
- const merger = offlineContext.createChannelMerger(2);
689
- source.connect(splitter);
690
- splitter.connect(merger, 0, 0);
691
- splitter.connect(merger, 0, 1);
692
- merger.connect(offlineContext.destination);
693
- }
694
- else {
695
- // For other cases, just connect and let the system handle it
696
- source.connect(offlineContext.destination);
697
- }
698
- }
699
- else {
700
- // No channel conversion needed
701
- source.connect(offlineContext.destination);
702
- }
703
- // Start rendering
704
- source.start(0);
705
- const resampledBuffer = await offlineContext.startRendering();
706
- console.log(`Resampling complete: ${resampledBuffer.length} samples at ${resampledBuffer.sampleRate}Hz`);
707
- return resampledBuffer;
708
- }
709
117
  if (Platform.OS !== 'web') {
710
118
  AudioStudioModule = requireNativeModule('AudioStudio');
711
119
  }