@siteed/audio-studio 3.0.2-beta.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 +16 -150
  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 -96
  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 -55
  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 -61
  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 -22
  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 +2 -2
  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 -92
  71. package/src/AudioAnalysis/extractAudioAnalysis.ts +23 -20
  72. package/src/AudioAnalysis/extractAudioData.ts +186 -4
  73. package/src/AudioAnalysis/melSpectrogramWasm.web.ts +10 -25
  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,58 +1,8 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.initAudioFeaturesWasm = initAudioFeaturesWasm;
37
- exports.computeAudioFeaturesFrameWasm = computeAudioFeaturesFrameWasm;
3
+ exports.AudioFeaturesStreamingSession = void 0;
38
4
  exports.computeAudioFeaturesWasm = computeAudioFeaturesWasm;
39
- const wasmConfig_1 = require("./wasmConfig");
40
- let modulePromise = null;
41
- function getModule() {
42
- if (!modulePromise) {
43
- modulePromise = (async () => {
44
- const url = (0, wasmConfig_1.getMelSpectrogramWasmUrl)();
45
- // webpackIgnore + @vite-ignore prevent bundlers from trying to resolve the URL
46
- const mod = await Promise.resolve(`${url}`).then(s => __importStar(require(s)));
47
- const factory = mod.default ?? mod;
48
- return factory();
49
- })().catch((err) => {
50
- modulePromise = null;
51
- throw err;
52
- });
53
- }
54
- return modulePromise;
55
- }
5
+ const wasmLoader_web_1 = require("./wasmLoader.web");
56
6
  // --- Struct layout for CAudioFeaturesResult (wasm32) ---
57
7
  // Offset 0: float spectralCentroid (4 bytes)
58
8
  // Offset 4: float spectralFlatness (4 bytes)
@@ -96,60 +46,92 @@ function readResult(Module, ptr) {
96
46
  };
97
47
  }
98
48
  // --- Streaming (per-frame) API ---
99
- let streamingModule = null;
100
- let streamingFramePtr = 0;
101
- let streamingFrameCapacity = 0;
102
- let streamingResultPtr = 0;
103
- /**
104
- * Initialise the WASM streaming audio features processor.
105
- * Call once before computeAudioFeaturesFrameWasm().
106
- */
107
- async function initAudioFeaturesWasm(sampleRate, fftLength = 1024, nMfcc = 13, nMelFilters = 26, computeMfcc = true, computeChroma = true) {
108
- const Module = await getModule();
109
- streamingModule = Module;
110
- Module._audio_features_init(sampleRate, fftLength, nMfcc, nMelFilters, computeMfcc ? 1 : 0, computeChroma ? 1 : 0);
111
- // Pre-allocate result struct on WASM heap
112
- if (streamingResultPtr)
113
- Module._free(streamingResultPtr);
114
- streamingResultPtr = Module._malloc(STRUCT_SIZE);
115
- // Zero-initialize to prevent freeing garbage pointers on first use
116
- Module.HEAPU8.fill(0, streamingResultPtr, streamingResultPtr + STRUCT_SIZE);
117
- // Frame input buffer allocated on demand
118
- streamingFrameCapacity = 0;
119
- streamingFramePtr = 0;
120
- }
121
49
  /**
122
- * Compute audio features for a single frame via WASM C++.
123
- * Returns null if not initialised or on error.
50
+ * Encapsulates a single WASM streaming audio features session.
51
+ * Each instance owns its own WASM heap allocations; multiple sessions
52
+ * can exist concurrently without interfering with each other.
53
+ *
54
+ * Usage:
55
+ * const session = await AudioFeaturesStreamingSession.create(sampleRate)
56
+ * try {
57
+ * for (const frame of frames) {
58
+ * const result = session.computeFrame(frame)
59
+ * }
60
+ * } finally {
61
+ * session.dispose()
62
+ * }
124
63
  */
125
- function computeAudioFeaturesFrameWasm(samples) {
126
- if (!streamingModule || !streamingResultPtr)
127
- return null;
128
- const Module = streamingModule;
129
- // (Re-)allocate frame input buffer if needed
130
- if (samples.length > streamingFrameCapacity) {
131
- if (streamingFramePtr)
132
- Module._free(streamingFramePtr);
133
- streamingFramePtr = Module._malloc(samples.length * 4);
134
- streamingFrameCapacity = samples.length;
64
+ class AudioFeaturesStreamingSession {
65
+ module;
66
+ framePtr = 0;
67
+ frameCapacity = 0;
68
+ resultPtr = 0;
69
+ constructor(module) {
70
+ this.module = module;
71
+ }
72
+ /**
73
+ * Initialise a new streaming session. Loads the WASM module if needed.
74
+ */
75
+ static async create(sampleRate, fftLength = 1024, nMfcc = 13, nMelFilters = 26, computeMfcc = true, computeChroma = true) {
76
+ const Module = await (0, wasmLoader_web_1.getWasmModule)();
77
+ const session = new AudioFeaturesStreamingSession(Module);
78
+ Module._audio_features_init(sampleRate, fftLength, nMfcc, nMelFilters, computeMfcc ? 1 : 0, computeChroma ? 1 : 0);
79
+ // Pre-allocate result struct on WASM heap
80
+ session.resultPtr = Module._malloc(STRUCT_SIZE);
81
+ // Zero-initialize to prevent freeing garbage pointers on first use
82
+ Module.HEAPU8.fill(0, session.resultPtr, session.resultPtr + STRUCT_SIZE);
83
+ return session;
84
+ }
85
+ /**
86
+ * Compute audio features for a single frame.
87
+ * Returns null on error or if the session has been disposed.
88
+ */
89
+ computeFrame(samples) {
90
+ if (!this.resultPtr)
91
+ return null;
92
+ const Module = this.module;
93
+ // (Re-)allocate frame input buffer if needed
94
+ if (samples.length > this.frameCapacity) {
95
+ if (this.framePtr)
96
+ Module._free(this.framePtr);
97
+ this.framePtr = Module._malloc(samples.length * 4);
98
+ this.frameCapacity = samples.length;
99
+ }
100
+ // Copy samples to WASM heap
101
+ Module.HEAPF32.set(samples, this.framePtr >> 2);
102
+ const ok = Module._audio_features_compute_frame(this.framePtr, samples.length, this.resultPtr);
103
+ if (!ok)
104
+ return null;
105
+ const result = readResult(Module, this.resultPtr);
106
+ // Free internal arrays (mfcc, chromagram) allocated by C
107
+ Module._audio_features_free_arrays(this.resultPtr);
108
+ return result;
109
+ }
110
+ /**
111
+ * Free all WASM heap allocations owned by this session.
112
+ * The session must not be used after calling dispose().
113
+ */
114
+ dispose() {
115
+ const Module = this.module;
116
+ if (this.framePtr) {
117
+ Module._free(this.framePtr);
118
+ this.framePtr = 0;
119
+ this.frameCapacity = 0;
120
+ }
121
+ if (this.resultPtr) {
122
+ Module._free(this.resultPtr);
123
+ this.resultPtr = 0;
124
+ }
135
125
  }
136
- // Copy samples to WASM heap
137
- Module.HEAPF32.set(samples, streamingFramePtr >> 2);
138
- const ok = Module._audio_features_compute_frame(streamingFramePtr, samples.length, streamingResultPtr);
139
- if (!ok)
140
- return null;
141
- const result = readResult(Module, streamingResultPtr);
142
- // Free internal arrays (mfcc, chromagram) allocated by C
143
- Module._audio_features_free_arrays(streamingResultPtr);
144
- return result;
145
126
  }
127
+ exports.AudioFeaturesStreamingSession = AudioFeaturesStreamingSession;
146
128
  // --- Batch API ---
147
129
  /**
148
130
  * Compute audio features for a buffer of samples via WASM C++.
149
131
  * Lazy-loads the WASM module on first call.
150
132
  */
151
133
  async function computeAudioFeaturesWasm(audioData, sampleRate, fftLength = 1024, nMfcc = 13, nMelFilters = 26, computeMfcc = true, computeChroma = true) {
152
- const Module = await getModule();
134
+ const Module = await (0, wasmLoader_web_1.getWasmModule)();
153
135
  const numSamples = audioData.length;
154
136
  const inputPtr = Module._malloc(numSamples * 4);
155
137
  Module.HEAPF32.set(audioData, inputPtr >> 2);
@@ -1 +1 @@
1
- {"version":3,"file":"audioFeaturesWasm.web.js","sourceRoot":"","sources":["../../../src/AudioAnalysis/audioFeaturesWasm.web.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4FA,sDA6BC;AAMD,sEA6BC;AA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B,IAAI,SAAS,KAAK,CAAC,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;IAC3D,CAAC;IAED,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC5C,MAAM,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAAA;IAEtC,OAAO,MAAM,CAAA;AACjB,CAAC","sourcesContent":["import type { AudioFeaturesWasmModule } from './audio-features-wasm'\nimport { getMelSpectrogramWasmUrl } from './wasmConfig'\n\nexport interface AudioFeaturesWasmResult {\n spectralCentroid: number\n spectralFlatness: number\n spectralRolloff: number\n spectralBandwidth: number\n mfcc: number[]\n chromagram: number[]\n}\n\nlet modulePromise: Promise<AudioFeaturesWasmModule> | null = null\n\nfunction getModule(): Promise<AudioFeaturesWasmModule> {\n if (!modulePromise) {\n modulePromise = (async () => {\n const url = getMelSpectrogramWasmUrl()\n // webpackIgnore + @vite-ignore prevent bundlers from trying to resolve the URL\n const mod = await import(/* webpackIgnore: true */ /* @vite-ignore */ url)\n const factory = mod.default ?? mod\n return factory() as Promise<AudioFeaturesWasmModule>\n })().catch((err) => {\n modulePromise = null\n throw err\n })\n }\n return modulePromise\n}\n\n// --- Struct layout for CAudioFeaturesResult (wasm32) ---\n// Offset 0: float spectralCentroid (4 bytes)\n// Offset 4: float spectralFlatness (4 bytes)\n// Offset 8: float spectralRolloff (4 bytes)\n// Offset 12: float spectralBandwidth (4 bytes)\n// Offset 16: float* mfcc (4 bytes pointer)\n// Offset 20: int mfccCount (4 bytes)\n// Offset 24: float* chromagram (4 bytes pointer)\n// Offset 28: int chromagramCount (4 bytes)\nconst STRUCT_SIZE = 32\n\nfunction readResult(\n Module: AudioFeaturesWasmModule,\n ptr: number\n): AudioFeaturesWasmResult {\n const spectralCentroid = Module.getValue(ptr, 'float')\n const spectralFlatness = Module.getValue(ptr + 4, 'float')\n const spectralRolloff = Module.getValue(ptr + 8, 'float')\n const spectralBandwidth = Module.getValue(ptr + 12, 'float')\n\n const mfccPtr = Module.getValue(ptr + 16, 'i32')\n const mfccCount = Module.getValue(ptr + 20, 'i32')\n const chromaPtr = Module.getValue(ptr + 24, 'i32')\n const chromaCount = Module.getValue(ptr + 28, 'i32')\n\n const mfcc: number[] = []\n if (mfccPtr && mfccCount > 0) {\n const offset = mfccPtr >> 2\n for (let i = 0; i < mfccCount; i++) {\n mfcc.push(Module.HEAPF32[offset + i])\n }\n }\n\n const chromagram: number[] = []\n if (chromaPtr && chromaCount > 0) {\n const offset = chromaPtr >> 2\n for (let i = 0; i < chromaCount; i++) {\n chromagram.push(Module.HEAPF32[offset + i])\n }\n }\n\n return {\n spectralCentroid,\n spectralFlatness,\n spectralRolloff,\n spectralBandwidth,\n mfcc,\n chromagram,\n }\n}\n\n// --- Streaming (per-frame) API ---\n\nlet streamingModule: AudioFeaturesWasmModule | null = null\nlet streamingFramePtr = 0\nlet streamingFrameCapacity = 0\nlet streamingResultPtr = 0\n\n/**\n * Initialise the WASM streaming audio features processor.\n * Call once before computeAudioFeaturesFrameWasm().\n */\nexport async function initAudioFeaturesWasm(\n sampleRate: number,\n fftLength = 1024,\n nMfcc = 13,\n nMelFilters = 26,\n computeMfcc = true,\n computeChroma = true\n): Promise<void> {\n const Module = await getModule()\n streamingModule = Module\n\n Module._audio_features_init(\n sampleRate,\n fftLength,\n nMfcc,\n nMelFilters,\n computeMfcc ? 1 : 0,\n computeChroma ? 1 : 0\n )\n\n // Pre-allocate result struct on WASM heap\n if (streamingResultPtr) Module._free(streamingResultPtr)\n streamingResultPtr = Module._malloc(STRUCT_SIZE)\n // Zero-initialize to prevent freeing garbage pointers on first use\n Module.HEAPU8.fill(0, streamingResultPtr, streamingResultPtr + STRUCT_SIZE)\n\n // Frame input buffer allocated on demand\n streamingFrameCapacity = 0\n streamingFramePtr = 0\n}\n\n/**\n * Compute audio features for a single frame via WASM C++.\n * Returns null if not initialised or on error.\n */\nexport function computeAudioFeaturesFrameWasm(\n samples: Float32Array\n): AudioFeaturesWasmResult | null {\n if (!streamingModule || !streamingResultPtr) return null\n const Module = streamingModule\n\n // (Re-)allocate frame input buffer if needed\n if (samples.length > streamingFrameCapacity) {\n if (streamingFramePtr) Module._free(streamingFramePtr)\n streamingFramePtr = Module._malloc(samples.length * 4)\n streamingFrameCapacity = samples.length\n }\n\n // Copy samples to WASM heap\n Module.HEAPF32.set(samples, streamingFramePtr >> 2)\n\n const ok = Module._audio_features_compute_frame(\n streamingFramePtr,\n samples.length,\n streamingResultPtr\n )\n if (!ok) return null\n\n const result = readResult(Module, streamingResultPtr)\n\n // Free internal arrays (mfcc, chromagram) allocated by C\n Module._audio_features_free_arrays(streamingResultPtr)\n\n return result\n}\n\n// --- Batch API ---\n\n/**\n * Compute audio features for a buffer of samples via WASM C++.\n * Lazy-loads the WASM module on first call.\n */\nexport async function computeAudioFeaturesWasm(\n audioData: Float32Array,\n sampleRate: number,\n fftLength = 1024,\n nMfcc = 13,\n nMelFilters = 26,\n computeMfcc = true,\n computeChroma = true\n): Promise<AudioFeaturesWasmResult> {\n const Module = await getModule()\n\n const numSamples = audioData.length\n const inputPtr = Module._malloc(numSamples * 4)\n Module.HEAPF32.set(audioData, inputPtr >> 2)\n\n const resultPtr = Module._audio_features_compute(\n inputPtr,\n numSamples,\n sampleRate,\n fftLength,\n nMfcc,\n nMelFilters,\n computeMfcc ? 1 : 0,\n computeChroma ? 1 : 0\n )\n\n Module._free(inputPtr)\n\n if (resultPtr === 0) {\n throw new Error('audio_features_compute returned null')\n }\n\n const result = readResult(Module, resultPtr)\n Module._audio_features_free(resultPtr)\n\n return result\n}\n"]}
1
+ {"version":3,"file":"audioFeaturesWasm.web.js","sourceRoot":"","sources":["../../../src/AudioAnalysis/audioFeaturesWasm.web.ts"],"names":[],"mappings":";;;AA8KA,4DAoCC;AAhND,qDAAgD;AAEhD,0DAA0D;AAC1D,+CAA+C;AAC/C,+CAA+C;AAC/C,+CAA+C;AAC/C,+CAA+C;AAC/C,uDAA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,KAAK,CAAC,sCAAsC,CAAC,CAAA;IAC3D,CAAC;IAED,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC5C,MAAM,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAAA;IAEtC,OAAO,MAAM,CAAA;AACjB,CAAC","sourcesContent":["import type { AudioFeaturesWasmResult } from './AudioAnalysis.types'\nimport type { AudioFeaturesWasmModule } from './audio-features-wasm'\nimport { getWasmModule } from './wasmLoader.web'\n\n// --- Struct layout for CAudioFeaturesResult (wasm32) ---\n// Offset 0: float spectralCentroid (4 bytes)\n// Offset 4: float spectralFlatness (4 bytes)\n// Offset 8: float spectralRolloff (4 bytes)\n// Offset 12: float spectralBandwidth (4 bytes)\n// Offset 16: float* mfcc (4 bytes pointer)\n// Offset 20: int mfccCount (4 bytes)\n// Offset 24: float* chromagram (4 bytes pointer)\n// Offset 28: int chromagramCount (4 bytes)\nconst STRUCT_SIZE = 32\n\nfunction readResult(\n Module: AudioFeaturesWasmModule,\n ptr: number\n): AudioFeaturesWasmResult {\n const spectralCentroid = Module.getValue(ptr, 'float')\n const spectralFlatness = Module.getValue(ptr + 4, 'float')\n const spectralRolloff = Module.getValue(ptr + 8, 'float')\n const spectralBandwidth = Module.getValue(ptr + 12, 'float')\n\n const mfccPtr = Module.getValue(ptr + 16, 'i32')\n const mfccCount = Module.getValue(ptr + 20, 'i32')\n const chromaPtr = Module.getValue(ptr + 24, 'i32')\n const chromaCount = Module.getValue(ptr + 28, 'i32')\n\n const mfcc: number[] = []\n if (mfccPtr && mfccCount > 0) {\n const offset = mfccPtr >> 2\n for (let i = 0; i < mfccCount; i++) {\n mfcc.push(Module.HEAPF32[offset + i])\n }\n }\n\n const chromagram: number[] = []\n if (chromaPtr && chromaCount > 0) {\n const offset = chromaPtr >> 2\n for (let i = 0; i < chromaCount; i++) {\n chromagram.push(Module.HEAPF32[offset + i])\n }\n }\n\n return {\n spectralCentroid,\n spectralFlatness,\n spectralRolloff,\n spectralBandwidth,\n mfcc,\n chromagram,\n }\n}\n\n// --- Streaming (per-frame) API ---\n\n/**\n * Encapsulates a single WASM streaming audio features session.\n * Each instance owns its own WASM heap allocations; multiple sessions\n * can exist concurrently without interfering with each other.\n *\n * Usage:\n * const session = await AudioFeaturesStreamingSession.create(sampleRate)\n * try {\n * for (const frame of frames) {\n * const result = session.computeFrame(frame)\n * }\n * } finally {\n * session.dispose()\n * }\n */\nexport class AudioFeaturesStreamingSession {\n private module: AudioFeaturesWasmModule\n private framePtr = 0\n private frameCapacity = 0\n private resultPtr = 0\n\n private constructor(module: AudioFeaturesWasmModule) {\n this.module = module\n }\n\n /**\n * Initialise a new streaming session. Loads the WASM module if needed.\n */\n static async create(\n sampleRate: number,\n fftLength = 1024,\n nMfcc = 13,\n nMelFilters = 26,\n computeMfcc = true,\n computeChroma = true\n ): Promise<AudioFeaturesStreamingSession> {\n const Module = await getWasmModule()\n const session = new AudioFeaturesStreamingSession(Module)\n\n Module._audio_features_init(\n sampleRate,\n fftLength,\n nMfcc,\n nMelFilters,\n computeMfcc ? 1 : 0,\n computeChroma ? 1 : 0\n )\n\n // Pre-allocate result struct on WASM heap\n session.resultPtr = Module._malloc(STRUCT_SIZE)\n // Zero-initialize to prevent freeing garbage pointers on first use\n Module.HEAPU8.fill(\n 0,\n session.resultPtr,\n session.resultPtr + STRUCT_SIZE\n )\n\n return session\n }\n\n /**\n * Compute audio features for a single frame.\n * Returns null on error or if the session has been disposed.\n */\n computeFrame(samples: Float32Array): AudioFeaturesWasmResult | null {\n if (!this.resultPtr) return null\n const Module = this.module\n\n // (Re-)allocate frame input buffer if needed\n if (samples.length > this.frameCapacity) {\n if (this.framePtr) Module._free(this.framePtr)\n this.framePtr = Module._malloc(samples.length * 4)\n this.frameCapacity = samples.length\n }\n\n // Copy samples to WASM heap\n Module.HEAPF32.set(samples, this.framePtr >> 2)\n\n const ok = Module._audio_features_compute_frame(\n this.framePtr,\n samples.length,\n this.resultPtr\n )\n if (!ok) return null\n\n const result = readResult(Module, this.resultPtr)\n\n // Free internal arrays (mfcc, chromagram) allocated by C\n Module._audio_features_free_arrays(this.resultPtr)\n\n return result\n }\n\n /**\n * Free all WASM heap allocations owned by this session.\n * The session must not be used after calling dispose().\n */\n dispose(): void {\n const Module = this.module\n if (this.framePtr) {\n Module._free(this.framePtr)\n this.framePtr = 0\n this.frameCapacity = 0\n }\n if (this.resultPtr) {\n Module._free(this.resultPtr)\n this.resultPtr = 0\n }\n }\n}\n\n// --- Batch API ---\n\n/**\n * Compute audio features for a buffer of samples via WASM C++.\n * Lazy-loads the WASM module on first call.\n */\nexport async function computeAudioFeaturesWasm(\n audioData: Float32Array,\n sampleRate: number,\n fftLength = 1024,\n nMfcc = 13,\n nMelFilters = 26,\n computeMfcc = true,\n computeChroma = true\n): Promise<AudioFeaturesWasmResult> {\n const Module = await getWasmModule()\n\n const numSamples = audioData.length\n const inputPtr = Module._malloc(numSamples * 4)\n Module.HEAPF32.set(audioData, inputPtr >> 2)\n\n const resultPtr = Module._audio_features_compute(\n inputPtr,\n numSamples,\n sampleRate,\n fftLength,\n nMfcc,\n nMelFilters,\n computeMfcc ? 1 : 0,\n computeChroma ? 1 : 0\n )\n\n Module._free(inputPtr)\n\n if (resultPtr === 0) {\n throw new Error('audio_features_compute returned null')\n }\n\n const result = readResult(Module, resultPtr)\n Module._audio_features_free(resultPtr)\n\n return result\n}\n"]}
@@ -14,6 +14,13 @@ const crc32_1 = __importDefault(require("../utils/crc32"));
14
14
  const getWavFileInfo_1 = require("../utils/getWavFileInfo");
15
15
  const InlineFeaturesExtractor_web_1 = require("../workers/InlineFeaturesExtractor.web");
16
16
  const wasmGlueString_web_1 = require("../workers/wasmGlueString.web");
17
+ function createAnalysisWorker() {
18
+ const blob = new Blob([wasmGlueString_web_1.wasmGlueJs, '\n', InlineFeaturesExtractor_web_1.InlineFeaturesExtractor], {
19
+ type: 'application/javascript',
20
+ });
21
+ const workerUrl = URL.createObjectURL(blob);
22
+ return { worker: new Worker(workerUrl), workerUrl };
23
+ }
17
24
  function calculateCRC32ForDataPoint(data) {
18
25
  // Convert float array to byte array for CRC32
19
26
  const byteArray = new Uint8Array(data.length * 4);
@@ -56,12 +63,12 @@ async function extractAudioAnalysis(props) {
56
63
  });
57
64
  const channelData = processedBuffer.buffer.getChannelData(0);
58
65
  // Create worker blob: WASM glue (defines createMelSpectrogramModule) + worker code
59
- const blob = new Blob([wasmGlueString_web_1.wasmGlueJs, '\n', InlineFeaturesExtractor_web_1.InlineFeaturesExtractor], { type: 'application/javascript' });
60
- const workerUrl = URL.createObjectURL(blob);
61
- const worker = new Worker(workerUrl);
66
+ const { worker, workerUrl } = createAnalysisWorker();
62
67
  return new Promise((resolve, reject) => {
63
68
  worker.onmessage = (event) => {
64
69
  if (event.data.error) {
70
+ URL.revokeObjectURL(workerUrl);
71
+ worker.terminate();
65
72
  reject(new Error(event.data.error));
66
73
  return;
67
74
  }
@@ -163,18 +170,14 @@ arrayBuffer, bitDepth, durationMs, sampleRate, numberOfChannels, features, logge
163
170
  const endIndex = length ? startIndex + length : channelData.length;
164
171
  const constrainedChannelData = channelData.slice(startIndex, endIndex);
165
172
  return new Promise((resolve, reject) => {
166
- const blob = new Blob([wasmGlueString_web_1.wasmGlueJs, '\n', InlineFeaturesExtractor_web_1.InlineFeaturesExtractor], {
167
- type: 'application/javascript',
168
- });
169
- const url = URL.createObjectURL(blob);
170
- const worker = new Worker(url);
173
+ const { worker, workerUrl } = createAnalysisWorker();
171
174
  worker.onmessage = (event) => {
172
- URL.revokeObjectURL(url);
175
+ URL.revokeObjectURL(workerUrl);
173
176
  worker.terminate();
174
177
  resolve(event.data.result);
175
178
  };
176
179
  worker.onerror = (error) => {
177
- URL.revokeObjectURL(url);
180
+ URL.revokeObjectURL(workerUrl);
178
181
  worker.terminate();
179
182
  reject(error);
180
183
  };
@@ -198,13 +201,13 @@ arrayBuffer, bitDepth, durationMs, sampleRate, numberOfChannels, features, logge
198
201
  fileUri,
199
202
  segmentDurationMs,
200
203
  });
201
- const res = await AudioStudioModule_1.default.extractAudioAnalysis({
204
+ const res = await AudioStudioModule_1.default.extractAudioAnalysis((0, cleanNativeOptions_1.cleanNativeOptions)({
202
205
  fileUri,
203
206
  segmentDurationMs,
204
207
  features,
205
208
  position,
206
209
  length,
207
- });
210
+ }));
208
211
  logger?.log(`extractAudioAnalysis`, res);
209
212
  return res;
210
213
  }
@@ -1 +1 @@
1
- {"version":3,"file":"extractAudioAnalysis.js","sourceRoot":"","sources":["../../../src/AudioAnalysis/extractAudioAnalysis.ts"],"names":[],"mappings":";;;;;;AAkGA,oDAgIC;AA1ND,6EAAoD;AACpD,4CAAoC;AAOpC,8DAA6D;AAC7D,oEAAgE;AAChE,sEAAkE;AAClE,2DAAkC;AAClC,4DAAqE;AACrE,wFAAgF;AAChF,sEAA0D;AAE1D,SAAS,0BAA0B,CAAC,IAAkB;IAClD,8CAA8C;IAC9C,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACjD,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,SAAS,CAAC,MAAM,CAAC,CAAA;IAE/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,QAAQ,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;IAC7C,CAAC;IAED,OAAO,eAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;AAC/B,CAAC;AAwDD;;;;;;;GAOG;AACI,KAAK,UAAU,oBAAoB,CACtC,KAAgC;IAEhC,MAAM,EACF,OAAO,EACP,WAAW,EACX,eAAe,EACf,MAAM,EACN,iBAAiB,GAAG,GAAG,EACvB,QAAQ,GACX,GAAG,KAAK,CAAA;IAET,IAAI,iBAAK,EAAE,CAAC;QACR,IA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gBAAgB;aACnB,CAAC,CAAA;QACN,CAAC,CAAC,CAAA;IACN,CAAC;SAAM,CAAC;QACJ,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;QAC1C,CAAC;QACD,MAAM,EAAE,GAAG,CAAC,sBAAsB,EAAE;YAChC,OAAO;YACP,iBAAiB;SACpB,CAAC,CAAA;QACF,MAAM,GAAG,GAAG,MAAM,2BAAiB,CAAC,oBAAoB,CAAC;YACrD,OAAO;YACP,iBAAiB;YACjB,QAAQ;YACR,QAAQ;YACR,MAAM;SACT,CAAC,CAAA;QACF,MAAM,EAAE,GAAG,CAAC,sBAAsB,EAAE,GAAG,CAAC,CAAA;QACxC,OAAO,GAAG,CAAA;IACd,CAAC;AACL,CAAC,CAAA;AAlHY,QAAA,qBAAqB,yBAkHjC","sourcesContent":["// packages/audio-studio/src/AudioAnalysis/extractAudioAnalysis.ts\n/**\n * This module provides functions for extracting and analyzing audio data.\n * - `extractAudioAnalysis`: For detailed analysis with customizable ranges and decoding options.\n * - `extractWavAudioAnalysis`: For analyzing WAV files without decoding, preserving original PCM values.\n * - `extractPreview`: For generating quick previews of audio waveforms, optimized for UI rendering.\n */\nimport { ConsoleLike } from '../AudioStudio.types'\nimport AudioStudioModule from '../AudioStudioModule'\nimport { isWeb } from '../constants'\nimport {\n AudioAnalysis,\n AudioFeaturesOptions,\n DataPoint,\n DecodingConfig,\n} from './AudioAnalysis.types'\nimport { processAudioBuffer } from '../utils/audioProcessing'\nimport { cleanNativeOptions } from '../utils/cleanNativeOptions'\nimport { convertPCMToFloat32 } from '../utils/convertPCMToFloat32'\nimport crc32 from '../utils/crc32'\nimport { getWavFileInfo, WavFileInfo } from '../utils/getWavFileInfo'\nimport { InlineFeaturesExtractor } from '../workers/InlineFeaturesExtractor.web'\nimport { wasmGlueJs } from '../workers/wasmGlueString.web'\n\nfunction calculateCRC32ForDataPoint(data: Float32Array): number {\n // Convert float array to byte array for CRC32\n const byteArray = new Uint8Array(data.length * 4)\n const dataView = new DataView(byteArray.buffer)\n\n for (let i = 0; i < data.length; i++) {\n dataView.setFloat32(i * 4, data[i], true)\n }\n\n return crc32.buf(byteArray)\n}\n\nexport interface ExtractWavAudioAnalysisProps {\n fileUri?: string // should provide either fileUri or arrayBuffer\n wavMetadata?: WavFileInfo\n arrayBuffer?: ArrayBuffer\n bitDepth?: number\n durationMs?: number\n sampleRate?: number\n numberOfChannels?: number\n position?: number // Optional number of bytes to skip. Default is 0\n length?: number // Optional number of bytes to read.\n segmentDurationMs?: number // Optional number of points per second. Use to reduce the number of points and compute the number of datapoints to return.\n features?: AudioFeaturesOptions\n featuresExtratorUrl?: string\n logger?: ConsoleLike\n decodingOptions?: DecodingConfig\n}\n\n// Define base options interface with common properties\ninterface BaseExtractOptions {\n fileUri?: string\n arrayBuffer?: ArrayBuffer\n /**\n * Duration of each analysis segment in milliseconds. Defaults to 100ms if not specified.\n */\n segmentDurationMs?: number\n features?: AudioFeaturesOptions\n decodingOptions?: DecodingConfig\n logger?: ConsoleLike\n}\n\n// Time-based range options\ninterface TimeRangeOptions extends BaseExtractOptions {\n startTimeMs?: number\n endTimeMs?: number\n position?: never\n length?: never\n}\n\n// Byte-based range options\ninterface ByteRangeOptions extends BaseExtractOptions {\n position?: number\n length?: number\n startTimeMs?: never\n endTimeMs?: never\n}\n\n/**\n * Options for extracting audio analysis.\n * - For time-based analysis, provide `startTimeMs` and `endTimeMs`.\n * - For byte-based analysis, provide `position` and `length`.\n * - Do not mix time and byte ranges.\n */\nexport type ExtractAudioAnalysisProps = TimeRangeOptions | ByteRangeOptions\n\n/**\n * Extracts detailed audio analysis from the specified audio file or buffer.\n * Supports either time-based or byte-based ranges for flexibility in analysis.\n *\n * @param props - The options for extraction, including file URI, ranges, and decoding settings.\n * @returns A promise that resolves to the audio analysis data.\n * @throws {Error} If both time and byte ranges are provided or if required parameters are missing.\n */\nexport async function extractAudioAnalysis(\n props: ExtractAudioAnalysisProps\n): Promise<AudioAnalysis> {\n const {\n fileUri,\n arrayBuffer,\n decodingOptions,\n logger,\n segmentDurationMs = 100,\n features,\n } = props\n\n if (isWeb) {\n try {\n // Create AudioContext here\n const audioContext = new (window.AudioContext ||\n (window as any).webkitAudioContext)({\n sampleRate: decodingOptions?.targetSampleRate ?? 16000,\n })\n\n try {\n const processedBuffer = await processAudioBuffer({\n arrayBuffer,\n fileUri,\n targetSampleRate:\n decodingOptions?.targetSampleRate ?? 16000,\n targetChannels: decodingOptions?.targetChannels ?? 1,\n normalizeAudio: decodingOptions?.normalizeAudio ?? false,\n startTimeMs:\n 'startTimeMs' in props ? props.startTimeMs : undefined,\n endTimeMs:\n 'endTimeMs' in props ? props.endTimeMs : undefined,\n position: 'position' in props ? props.position : undefined,\n length: 'length' in props ? props.length : undefined,\n audioContext, // Pass the context we created\n logger,\n })\n\n const channelData = processedBuffer.buffer.getChannelData(0)\n\n // Create worker blob: WASM glue (defines createMelSpectrogramModule) + worker code\n const blob = new Blob(\n [wasmGlueJs, '\\n', InlineFeaturesExtractor],\n { type: 'application/javascript' }\n )\n const workerUrl = URL.createObjectURL(blob)\n const worker = new Worker(workerUrl)\n\n return new Promise((resolve, reject) => {\n worker.onmessage = (event) => {\n if (event.data.error) {\n reject(new Error(event.data.error))\n return\n }\n\n const result: AudioAnalysis = event.data.result\n // Calculate CRC32 after worker completes if requested\n if (features?.crc32) {\n const samplesPerSegment = Math.floor(\n (processedBuffer.sampleRate *\n segmentDurationMs) /\n 1000\n )\n\n result.dataPoints = result.dataPoints.map(\n (point: DataPoint, index: number) => {\n const startSample =\n index * samplesPerSegment\n const segmentData = channelData.slice(\n startSample,\n startSample + samplesPerSegment\n )\n\n return {\n ...point,\n features: {\n ...point.features,\n crc32: calculateCRC32ForDataPoint(\n segmentData\n ),\n },\n }\n }\n )\n }\n\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n resolve(result)\n }\n\n worker.onerror = (error) => {\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n reject(error)\n }\n\n worker.postMessage({\n channelData,\n sampleRate: processedBuffer.sampleRate,\n segmentDurationMs,\n bitDepth: decodingOptions?.targetBitDepth ?? 32,\n numberOfChannels: processedBuffer.channels,\n fullAudioDurationMs: processedBuffer.durationMs,\n // enableLogging: !!logger,\n features,\n })\n })\n } finally {\n await audioContext.close()\n }\n } catch (error) {\n logger?.error('Failed to process audio:', error)\n throw error\n }\n } else {\n // Strip non-serializable fields — logger and arrayBuffer cause\n // \"Cannot convert '[object Object]' to a Kotlin type\" on Android.\n const {\n logger: _logger,\n arrayBuffer: _arrayBuffer,\n ...nativeOptions\n } = props\n // Clean undefined values to avoid Android Kotlin bridge crash\n return await AudioStudioModule.extractAudioAnalysis(\n cleanNativeOptions(nativeOptions)\n )\n }\n}\n\n/**\n * Analyzes WAV files without decoding, preserving original PCM values.\n * Use this function when you need to ensure the analysis matches other software by avoiding any transformations.\n *\n * @param props - The options for WAV analysis, including file URI and range.\n * @returns A promise that resolves to the audio analysis data.\n */\nexport const extractRawWavAnalysis = async ({\n fileUri,\n segmentDurationMs = 100, // Default to 100ms\n arrayBuffer,\n bitDepth,\n durationMs,\n sampleRate,\n numberOfChannels,\n features,\n logger,\n position = 0,\n length,\n}: ExtractWavAudioAnalysisProps): Promise<AudioAnalysis> => {\n if (isWeb) {\n if (!arrayBuffer && !fileUri) {\n throw new Error('Either arrayBuffer or fileUri must be provided')\n }\n\n if (!arrayBuffer) {\n logger?.log(`fetching fileUri`, fileUri)\n const response = await fetch(fileUri!)\n\n if (!response.ok) {\n throw new Error(\n `Failed to fetch fileUri: ${response.statusText}`\n )\n }\n\n arrayBuffer = await response.arrayBuffer()\n logger?.log(`fetched fileUri`, arrayBuffer.byteLength, arrayBuffer)\n }\n\n // Create a new copy of the ArrayBuffer to avoid detachment issues\n const bufferCopy = arrayBuffer.slice(0)\n logger?.log(\n `extractAudioAnalysis bitDepth=${bitDepth} len=${bufferCopy.byteLength}`,\n bufferCopy.slice(0, 100)\n )\n\n let actualBitDepth = bitDepth\n if (!actualBitDepth) {\n logger?.log(\n `extractAudioAnalysis bitDepth not provided -- getting wav file info`\n )\n const fileInfo = await getWavFileInfo(bufferCopy)\n actualBitDepth = fileInfo.bitDepth\n }\n logger?.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`)\n\n const {\n pcmValues: channelData,\n min,\n max,\n } = await convertPCMToFloat32({\n buffer: arrayBuffer,\n bitDepth: actualBitDepth,\n })\n logger?.log(\n `extractAudioAnalysis convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`\n )\n\n // Apply position and length constraints to channelData if specified\n const startIndex = position\n const endIndex = length ? startIndex + length : channelData.length\n const constrainedChannelData = channelData.slice(startIndex, endIndex)\n\n return new Promise((resolve, reject) => {\n const blob = new Blob([wasmGlueJs, '\\n', InlineFeaturesExtractor], {\n type: 'application/javascript',\n })\n const url = URL.createObjectURL(blob)\n const worker = new Worker(url)\n\n worker.onmessage = (event) => {\n URL.revokeObjectURL(url)\n worker.terminate()\n resolve(event.data.result)\n }\n\n worker.onerror = (error) => {\n URL.revokeObjectURL(url)\n worker.terminate()\n reject(error)\n }\n\n worker.postMessage({\n command: 'process',\n channelData: constrainedChannelData,\n sampleRate,\n segmentDurationMs,\n logger,\n bitDepth,\n fullAudioDurationMs: durationMs,\n numberOfChannels,\n })\n })\n } else {\n if (!fileUri) {\n throw new Error('fileUri is required')\n }\n logger?.log(`extractAudioAnalysis`, {\n fileUri,\n segmentDurationMs,\n })\n const res = await AudioStudioModule.extractAudioAnalysis({\n fileUri,\n segmentDurationMs,\n features,\n position,\n length,\n })\n logger?.log(`extractAudioAnalysis`, res)\n return res\n }\n}\n"]}
1
+ {"version":3,"file":"extractAudioAnalysis.js","sourceRoot":"","sources":["../../../src/AudioAnalysis/extractAudioAnalysis.ts"],"names":[],"mappings":";;;;;;AA0GA,oDA6HC;AA/ND,6EAAoD;AACpD,4CAAoC;AAOpC,8DAA6D;AAC7D,oEAAgE;AACh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,CAAC,sBAAsB,EAAE,GAAG,CAAC,CAAA;QACxC,OAAO,GAAG,CAAA;IACd,CAAC;AACL,CAAC,CAAA;AAhHY,QAAA,qBAAqB,yBAgHjC","sourcesContent":["// packages/audio-studio/src/AudioAnalysis/extractAudioAnalysis.ts\n/**\n * This module provides functions for extracting and analyzing audio data.\n * - `extractAudioAnalysis`: For detailed analysis with customizable ranges and decoding options.\n * - `extractWavAudioAnalysis`: For analyzing WAV files without decoding, preserving original PCM values.\n * - `extractPreview`: For generating quick previews of audio waveforms, optimized for UI rendering.\n */\nimport { ConsoleLike } from '../AudioStudio.types'\nimport AudioStudioModule from '../AudioStudioModule'\nimport { isWeb } from '../constants'\nimport {\n AudioAnalysis,\n AudioFeaturesOptions,\n DataPoint,\n DecodingConfig,\n} from './AudioAnalysis.types'\nimport { processAudioBuffer } from '../utils/audioProcessing'\nimport { cleanNativeOptions } from '../utils/cleanNativeOptions'\nimport { convertPCMToFloat32 } from '../utils/convertPCMToFloat32'\nimport crc32 from '../utils/crc32'\nimport { getWavFileInfo, WavFileInfo } from '../utils/getWavFileInfo'\nimport { InlineFeaturesExtractor } from '../workers/InlineFeaturesExtractor.web'\nimport { wasmGlueJs } from '../workers/wasmGlueString.web'\n\nfunction createAnalysisWorker(): { worker: Worker; workerUrl: string } {\n const blob = new Blob([wasmGlueJs, '\\n', InlineFeaturesExtractor], {\n type: 'application/javascript',\n })\n const workerUrl = URL.createObjectURL(blob)\n return { worker: new Worker(workerUrl), workerUrl }\n}\n\nfunction calculateCRC32ForDataPoint(data: Float32Array): number {\n // Convert float array to byte array for CRC32\n const byteArray = new Uint8Array(data.length * 4)\n const dataView = new DataView(byteArray.buffer)\n\n for (let i = 0; i < data.length; i++) {\n dataView.setFloat32(i * 4, data[i], true)\n }\n\n return crc32.buf(byteArray)\n}\n\nexport interface ExtractWavAudioAnalysisProps {\n fileUri?: string // should provide either fileUri or arrayBuffer\n wavMetadata?: WavFileInfo\n arrayBuffer?: ArrayBuffer\n bitDepth?: number\n durationMs?: number\n sampleRate?: number\n numberOfChannels?: number\n position?: number // Optional number of bytes to skip. Default is 0\n length?: number // Optional number of bytes to read.\n segmentDurationMs?: number // Optional number of points per second. Use to reduce the number of points and compute the number of datapoints to return.\n features?: AudioFeaturesOptions\n featuresExtratorUrl?: string\n logger?: ConsoleLike\n decodingOptions?: DecodingConfig\n}\n\n// Define base options interface with common properties\ninterface BaseExtractOptions {\n fileUri?: string\n arrayBuffer?: ArrayBuffer\n /**\n * Duration of each analysis segment in milliseconds. Defaults to 100ms if not specified.\n */\n segmentDurationMs?: number\n features?: AudioFeaturesOptions\n decodingOptions?: DecodingConfig\n logger?: ConsoleLike\n}\n\n// Time-based range options\ninterface TimeRangeOptions extends BaseExtractOptions {\n startTimeMs?: number\n endTimeMs?: number\n position?: never\n length?: never\n}\n\n// Byte-based range options\ninterface ByteRangeOptions extends BaseExtractOptions {\n position?: number\n length?: number\n startTimeMs?: never\n endTimeMs?: never\n}\n\n/**\n * Options for extracting audio analysis.\n * - For time-based analysis, provide `startTimeMs` and `endTimeMs`.\n * - For byte-based analysis, provide `position` and `length`.\n * - Do not mix time and byte ranges.\n */\nexport type ExtractAudioAnalysisProps = TimeRangeOptions | ByteRangeOptions\n\n/**\n * Extracts detailed audio analysis from the specified audio file or buffer.\n * Supports either time-based or byte-based ranges for flexibility in analysis.\n *\n * @param props - The options for extraction, including file URI, ranges, and decoding settings.\n * @returns A promise that resolves to the audio analysis data.\n * @throws {Error} If both time and byte ranges are provided or if required parameters are missing.\n */\nexport async function extractAudioAnalysis(\n props: ExtractAudioAnalysisProps\n): Promise<AudioAnalysis> {\n const {\n fileUri,\n arrayBuffer,\n decodingOptions,\n logger,\n segmentDurationMs = 100,\n features,\n } = props\n\n if (isWeb) {\n try {\n // Create AudioContext here\n const audioContext = new (window.AudioContext ||\n (window as any).webkitAudioContext)({\n sampleRate: decodingOptions?.targetSampleRate ?? 16000,\n })\n\n try {\n const processedBuffer = await processAudioBuffer({\n arrayBuffer,\n fileUri,\n targetSampleRate:\n decodingOptions?.targetSampleRate ?? 16000,\n targetChannels: decodingOptions?.targetChannels ?? 1,\n normalizeAudio: decodingOptions?.normalizeAudio ?? false,\n startTimeMs:\n 'startTimeMs' in props ? props.startTimeMs : undefined,\n endTimeMs:\n 'endTimeMs' in props ? props.endTimeMs : undefined,\n position: 'position' in props ? props.position : undefined,\n length: 'length' in props ? props.length : undefined,\n audioContext, // Pass the context we created\n logger,\n })\n\n const channelData = processedBuffer.buffer.getChannelData(0)\n\n // Create worker blob: WASM glue (defines createMelSpectrogramModule) + worker code\n const { worker, workerUrl } = createAnalysisWorker()\n\n return new Promise((resolve, reject) => {\n worker.onmessage = (event) => {\n if (event.data.error) {\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n reject(new Error(event.data.error))\n return\n }\n\n const result: AudioAnalysis = event.data.result\n // Calculate CRC32 after worker completes if requested\n if (features?.crc32) {\n const samplesPerSegment = Math.floor(\n (processedBuffer.sampleRate *\n segmentDurationMs) /\n 1000\n )\n\n result.dataPoints = result.dataPoints.map(\n (point: DataPoint, index: number) => {\n const startSample =\n index * samplesPerSegment\n const segmentData = channelData.slice(\n startSample,\n startSample + samplesPerSegment\n )\n\n return {\n ...point,\n features: {\n ...point.features,\n crc32: calculateCRC32ForDataPoint(\n segmentData\n ),\n },\n }\n }\n )\n }\n\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n resolve(result)\n }\n\n worker.onerror = (error) => {\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n reject(error)\n }\n\n worker.postMessage({\n channelData,\n sampleRate: processedBuffer.sampleRate,\n segmentDurationMs,\n bitDepth: decodingOptions?.targetBitDepth ?? 32,\n numberOfChannels: processedBuffer.channels,\n fullAudioDurationMs: processedBuffer.durationMs,\n // enableLogging: !!logger,\n features,\n })\n })\n } finally {\n await audioContext.close()\n }\n } catch (error) {\n logger?.error('Failed to process audio:', error)\n throw error\n }\n } else {\n // Strip non-serializable fields — logger and arrayBuffer cause\n // \"Cannot convert '[object Object]' to a Kotlin type\" on Android.\n const {\n logger: _logger,\n arrayBuffer: _arrayBuffer,\n ...nativeOptions\n } = props\n // Clean undefined values to avoid Android Kotlin bridge crash\n return await AudioStudioModule.extractAudioAnalysis(\n cleanNativeOptions(nativeOptions)\n )\n }\n}\n\n/**\n * Analyzes WAV files without decoding, preserving original PCM values.\n * Use this function when you need to ensure the analysis matches other software by avoiding any transformations.\n *\n * @param props - The options for WAV analysis, including file URI and range.\n * @returns A promise that resolves to the audio analysis data.\n */\nexport const extractRawWavAnalysis = async ({\n fileUri,\n segmentDurationMs = 100, // Default to 100ms\n arrayBuffer,\n bitDepth,\n durationMs,\n sampleRate,\n numberOfChannels,\n features,\n logger,\n position = 0,\n length,\n}: ExtractWavAudioAnalysisProps): Promise<AudioAnalysis> => {\n if (isWeb) {\n if (!arrayBuffer && !fileUri) {\n throw new Error('Either arrayBuffer or fileUri must be provided')\n }\n\n if (!arrayBuffer) {\n logger?.log(`fetching fileUri`, fileUri)\n const response = await fetch(fileUri!)\n\n if (!response.ok) {\n throw new Error(\n `Failed to fetch fileUri: ${response.statusText}`\n )\n }\n\n arrayBuffer = await response.arrayBuffer()\n logger?.log(`fetched fileUri`, arrayBuffer.byteLength, arrayBuffer)\n }\n\n // Create a new copy of the ArrayBuffer to avoid detachment issues\n const bufferCopy = arrayBuffer.slice(0)\n logger?.log(\n `extractAudioAnalysis bitDepth=${bitDepth} len=${bufferCopy.byteLength}`,\n bufferCopy.slice(0, 100)\n )\n\n let actualBitDepth = bitDepth\n if (!actualBitDepth) {\n logger?.log(\n `extractAudioAnalysis bitDepth not provided -- getting wav file info`\n )\n const fileInfo = await getWavFileInfo(bufferCopy)\n actualBitDepth = fileInfo.bitDepth\n }\n logger?.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`)\n\n const {\n pcmValues: channelData,\n min,\n max,\n } = await convertPCMToFloat32({\n buffer: arrayBuffer,\n bitDepth: actualBitDepth,\n })\n logger?.log(\n `extractAudioAnalysis convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`\n )\n\n // Apply position and length constraints to channelData if specified\n const startIndex = position\n const endIndex = length ? startIndex + length : channelData.length\n const constrainedChannelData = channelData.slice(startIndex, endIndex)\n\n return new Promise((resolve, reject) => {\n const { worker, workerUrl } = createAnalysisWorker()\n\n worker.onmessage = (event) => {\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n resolve(event.data.result)\n }\n\n worker.onerror = (error) => {\n URL.revokeObjectURL(workerUrl)\n worker.terminate()\n reject(error)\n }\n\n worker.postMessage({\n command: 'process',\n channelData: constrainedChannelData,\n sampleRate,\n segmentDurationMs,\n logger,\n bitDepth,\n fullAudioDurationMs: durationMs,\n numberOfChannels,\n })\n })\n } else {\n if (!fileUri) {\n throw new Error('fileUri is required')\n }\n logger?.log(`extractAudioAnalysis`, {\n fileUri,\n segmentDurationMs,\n })\n const res = await AudioStudioModule.extractAudioAnalysis(\n cleanNativeOptions({\n fileUri,\n segmentDurationMs,\n features,\n position,\n length,\n })\n )\n logger?.log(`extractAudioAnalysis`, res)\n return res\n }\n}\n"]}
@@ -6,11 +6,153 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.extractAudioData = void 0;
7
7
  const AudioStudioModule_1 = __importDefault(require("../AudioStudioModule"));
8
8
  const constants_1 = require("../constants");
9
+ const audioProcessing_1 = require("../utils/audioProcessing");
9
10
  const cleanNativeOptions_1 = require("../utils/cleanNativeOptions");
11
+ const crc32_1 = __importDefault(require("../utils/crc32"));
12
+ const writeWavHeader_1 = require("../utils/writeWavHeader");
10
13
  const extractAudioData = async (props) => {
11
14
  if (constants_1.isWeb) {
12
- // Web implementation handles logger natively in AudioStudioModule.ts
13
- return await AudioStudioModule_1.default.extractAudioData(props);
15
+ try {
16
+ const { fileUri, position, length, startTimeMs, endTimeMs, decodingOptions, includeNormalizedData, includeBase64Data, includeWavHeader = false, logger, } = props;
17
+ logger?.debug('EXTRACT AUDIO - Step 1: Initial request', {
18
+ fileUri,
19
+ extractionParams: {
20
+ position,
21
+ length,
22
+ startTimeMs,
23
+ endTimeMs,
24
+ },
25
+ decodingOptions: {
26
+ targetSampleRate: decodingOptions?.targetSampleRate ?? 16000,
27
+ targetChannels: decodingOptions?.targetChannels ?? 1,
28
+ targetBitDepth: decodingOptions?.targetBitDepth ?? 16,
29
+ normalizeAudio: decodingOptions?.normalizeAudio ?? false,
30
+ },
31
+ outputOptions: {
32
+ includeNormalizedData,
33
+ includeBase64Data,
34
+ includeWavHeader,
35
+ },
36
+ });
37
+ // Process the audio using shared helper function
38
+ const processedBuffer = await (0, audioProcessing_1.processAudioBuffer)({
39
+ fileUri,
40
+ targetSampleRate: decodingOptions?.targetSampleRate ?? 16000,
41
+ targetChannels: decodingOptions?.targetChannels ?? 1,
42
+ normalizeAudio: decodingOptions?.normalizeAudio ?? false,
43
+ position,
44
+ length,
45
+ startTimeMs,
46
+ endTimeMs,
47
+ logger,
48
+ });
49
+ logger?.debug('EXTRACT AUDIO - Step 2: Audio processing complete', {
50
+ processedData: {
51
+ samples: processedBuffer.samples,
52
+ sampleRate: processedBuffer.sampleRate,
53
+ channels: processedBuffer.channels,
54
+ durationMs: processedBuffer.durationMs,
55
+ },
56
+ });
57
+ const channelData = processedBuffer.channelData;
58
+ const bitDepth = (decodingOptions?.targetBitDepth ?? 16);
59
+ const bytesPerSample = bitDepth / 8;
60
+ const numSamples = processedBuffer.samples;
61
+ logger?.debug('EXTRACT AUDIO - Step 3: PCM conversion setup', {
62
+ channelData: {
63
+ length: channelData.length,
64
+ first: channelData[0],
65
+ last: channelData[channelData.length - 1],
66
+ },
67
+ calculation: {
68
+ bitDepth,
69
+ bytesPerSample,
70
+ numSamples,
71
+ expectedBytes: numSamples * bytesPerSample,
72
+ },
73
+ });
74
+ // Create PCM data with correct length based on original byte length
75
+ const pcmData = new Uint8Array(numSamples * bytesPerSample);
76
+ let offset = 0;
77
+ // Convert Float32 samples to PCM format
78
+ for (let i = 0; i < numSamples; i++) {
79
+ const sample = channelData[i];
80
+ const value = Math.max(-1, Math.min(1, sample));
81
+ // Convert to 16-bit signed integer
82
+ let intValue = Math.round(value * 32767);
83
+ // Handle negative values correctly
84
+ if (intValue < 0) {
85
+ intValue = 65536 + intValue;
86
+ }
87
+ // Write as little-endian
88
+ pcmData[offset++] = intValue & 255; // Low byte
89
+ pcmData[offset++] = (intValue >> 8) & 255; // High byte
90
+ }
91
+ const durationMs = Math.round((numSamples / processedBuffer.sampleRate) * 1000);
92
+ logger?.debug('EXTRACT AUDIO - Step 4: Final output', {
93
+ pcmData: {
94
+ length: pcmData.length,
95
+ first: pcmData[0],
96
+ last: pcmData[pcmData.length - 1],
97
+ },
98
+ timing: {
99
+ numSamples,
100
+ sampleRate: processedBuffer.sampleRate,
101
+ durationMs,
102
+ shouldBe3000ms: endTimeMs
103
+ ? endTimeMs - (startTimeMs ?? 0) === 3000
104
+ : undefined,
105
+ },
106
+ });
107
+ const result = {
108
+ pcmData: new Uint8Array(pcmData.buffer),
109
+ sampleRate: processedBuffer.sampleRate,
110
+ channels: processedBuffer.channels,
111
+ bitDepth,
112
+ durationMs,
113
+ format: `pcm_${bitDepth}bit`,
114
+ samples: numSamples,
115
+ };
116
+ // Add WAV header if requested
117
+ if (includeWavHeader) {
118
+ logger?.debug('EXTRACT AUDIO - Step 5: Adding WAV header', {
119
+ originalLength: pcmData.length,
120
+ newLength: result.pcmData.length,
121
+ firstBytes: Array.from(result.pcmData.slice(0, 44)), // WAV header is 44 bytes
122
+ });
123
+ const wavBuffer = (0, writeWavHeader_1.writeWavHeader)({
124
+ buffer: pcmData.buffer.slice(0, pcmData.length),
125
+ sampleRate: processedBuffer.sampleRate,
126
+ numChannels: processedBuffer.channels,
127
+ bitDepth,
128
+ });
129
+ result.pcmData = new Uint8Array(wavBuffer);
130
+ result.hasWavHeader = true;
131
+ }
132
+ if (includeNormalizedData) {
133
+ result.normalizedData = channelData;
134
+ }
135
+ if (includeBase64Data) {
136
+ result.base64Data = btoa(String.fromCharCode(...new Uint8Array(pcmData.buffer)));
137
+ }
138
+ if (props.computeChecksum) {
139
+ result.checksum = crc32_1.default.buf(pcmData);
140
+ }
141
+ logger?.debug('EXTRACT AUDIO - Step 3: PCM conversion complete', {
142
+ pcmStats: {
143
+ length: pcmData.length,
144
+ bytesPerSample,
145
+ totalSamples: numSamples,
146
+ firstBytes: Array.from(pcmData.slice(0, 16)),
147
+ lastBytes: Array.from(pcmData.slice(-16)),
148
+ },
149
+ });
150
+ return result;
151
+ }
152
+ catch (error) {
153
+ props.logger?.error('EXTRACT AUDIO - Error:', error);
154
+ throw error;
155
+ }
14
156
  }
15
157
  // Native: only pass serializable fields — logger causes crash on Android
16
158
  const { logger: _logger, ...nativeOptions } = props;
@@ -1 +1 @@
1
- {"version":3,"file":"extractAudioData.js","sourceRoot":"","sources":["../../../src/AudioAnalysis/extractAudioData.ts"],"names":[],"mappings":";;;;;;AACA,6EAAoD;AACpD,4CAAoC;AACpC,oEAAgE;AAEzD,MAAM,gBAAgB,GAAG,KAAK,EAAE,KAA8B,EAAE,EAAE;IACrE,IAAI,iBAAK,EAAE,CAAC;QACR,qEAAqE;QACrE,OAAO,MAAM,2BAAiB,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;IAC1D,CAAC;IACD,yEAAyE;IACzE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,aAAa,EAAE,GAAG,KAAK,CAAA;IACnD,8DAA8D;IAC9D,OAAO,MAAM,2BAAiB,CAAC,gBAAgB,CAC3C,IAAA,uCAAkB,EAAC,aAAa,CAAC,CACpC,CAAA;AACL,CAAC,CAAA;AAXY,QAAA,gBAAgB,oBAW5B","sourcesContent":["import { ExtractAudioDataOptions } from '../AudioStudio.types'\nimport AudioStudioModule from '../AudioStudioModule'\nimport { isWeb } from '../constants'\nimport { cleanNativeOptions } from '../utils/cleanNativeOptions'\n\nexport const extractAudioData = async (props: ExtractAudioDataOptions) => {\n if (isWeb) {\n // Web implementation handles logger natively in AudioStudioModule.ts\n return await AudioStudioModule.extractAudioData(props)\n }\n // Native: only pass serializable fields — logger causes crash on Android\n const { logger: _logger, ...nativeOptions } = props\n // Clean undefined values to avoid Android Kotlin bridge crash\n return await AudioStudioModule.extractAudioData(\n cleanNativeOptions(nativeOptions)\n )\n}\n"]}
1
+ {"version":3,"file":"extractAudioData.js","sourceRoot":"","sources":["../../../src/AudioAnalysis/extractAudioData.ts"],"names":[],"mappings":";;;;;;AAKA,6EAAoD;AACpD,4CAAoC;AACpC,8DAA6D;AAC7D,oEAAgE;AAChE,2DAAkC;AAClC,4DAAwD;AAEjD,MAAM,gBAAgB,GAAG,KAAK,EACjC,KAA8B,EACH,EAAE;IAC7B,IAAI,iBAAK,EAAE,CAAC;QACR,IAAI,CAAC;YACD,MAAM,EACF,OAAO,EACP,QAAQ,EACR,MAAM,EACN,WAAW,EACX,SAAS,EACT,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EACjB,gBAAgB,GAAG,KAAK,EACxB,MAAM,GACT,GAAG,KAAK,CAAA;YAET,MAAM,EAAE,KAAK,CAAC,yCAAyC,EAAE;gBACrD,OAAO;gBACP,gBAAgB,EAAE;oBACd,QAAQ;oBACR,MAAM;oBACN,WAAW;oBACX,SAAS;iBACZ;gBACD,eAAe,EAAE;oBACb,gBAAgB,EACZ,eAAe,EAAE,gBAAgB,IAAI,KAAK;oBAC9C,cAAc,EAAE,eAAe,EAAE,cAAc,IAAI,CAAC;oBACpD,cAAc,EAAE,eAAe,EAAE,cAAc,IAAI,EAAE;oBACrD,cAAc,EAAE,eAAe,EAAE,cAAc,IAAI,KAAK;iBAC3D;gBACD,aAAa,EAAE;oBACX,qBAAqB;oBACrB,iBAAiB;oBACjB,gBAAgB;iBACnB;aACJ,CAAC,CAAA;YAEF,iDAAiD;YACjD,MAAM,eAAe,GAAG,MAAM,IAAA,oCAAkB,EAAC;gBAC7C,OAAO;gBACP,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,IA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C,GAAG,CAAC,OAAO,CAAC,CAAA;YACxC,CAAC;YAED,MAAM,EAAE,KAAK,CAAC,iDAAiD,EAAE;gBAC7D,QAAQ,EAAE;oBACN,MAAM,EAAE,OAAO,CAAC,MAAM;oBACtB,cAAc;oBACd,YAAY,EAAE,UAAU;oBACxB,UAAU,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;oBAC5C,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;iBAC5C;aACJ,CAAC,CAAA;YAEF,OAAO,MAAM,CAAA;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAA;YACpD,MAAM,KAAK,CAAA;QACf,CAAC;IACL,CAAC;IAED,yEAAyE;IACzE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,aAAa,EAAE,GAAG,KAAK,CAAA;IACnD,8DAA8D;IAC9D,OAAO,MAAM,2BAAiB,CAAC,gBAAgB,CAC3C,IAAA,uCAAkB,EAAC,aAAa,CAAC,CACpC,CAAA;AACL,CAAC,CAAA;AA1LY,QAAA,gBAAgB,oBA0L5B","sourcesContent":["import {\n BitDepth,\n ExtractAudioDataOptions,\n ExtractedAudioData,\n} from '../AudioStudio.types'\nimport AudioStudioModule from '../AudioStudioModule'\nimport { isWeb } from '../constants'\nimport { processAudioBuffer } from '../utils/audioProcessing'\nimport { cleanNativeOptions } from '../utils/cleanNativeOptions'\nimport crc32 from '../utils/crc32'\nimport { writeWavHeader } from '../utils/writeWavHeader'\n\nexport const extractAudioData = async (\n props: ExtractAudioDataOptions\n): Promise<ExtractedAudioData> => {\n if (isWeb) {\n try {\n const {\n fileUri,\n position,\n length,\n startTimeMs,\n endTimeMs,\n decodingOptions,\n includeNormalizedData,\n includeBase64Data,\n includeWavHeader = false,\n logger,\n } = props\n\n logger?.debug('EXTRACT AUDIO - Step 1: Initial request', {\n fileUri,\n extractionParams: {\n position,\n length,\n startTimeMs,\n endTimeMs,\n },\n decodingOptions: {\n targetSampleRate:\n decodingOptions?.targetSampleRate ?? 16000,\n targetChannels: decodingOptions?.targetChannels ?? 1,\n targetBitDepth: decodingOptions?.targetBitDepth ?? 16,\n normalizeAudio: decodingOptions?.normalizeAudio ?? false,\n },\n outputOptions: {\n includeNormalizedData,\n includeBase64Data,\n includeWavHeader,\n },\n })\n\n // Process the audio using shared helper function\n const processedBuffer = await processAudioBuffer({\n fileUri,\n targetSampleRate: decodingOptions?.targetSampleRate ?? 16000,\n targetChannels: decodingOptions?.targetChannels ?? 1,\n normalizeAudio: decodingOptions?.normalizeAudio ?? false,\n position,\n length,\n startTimeMs,\n endTimeMs,\n logger,\n })\n\n logger?.debug('EXTRACT AUDIO - Step 2: Audio processing complete', {\n processedData: {\n samples: processedBuffer.samples,\n sampleRate: processedBuffer.sampleRate,\n channels: processedBuffer.channels,\n durationMs: processedBuffer.durationMs,\n },\n })\n\n const channelData = processedBuffer.channelData\n const bitDepth = (decodingOptions?.targetBitDepth ?? 16) as BitDepth\n const bytesPerSample = bitDepth / 8\n const numSamples = processedBuffer.samples\n\n logger?.debug('EXTRACT AUDIO - Step 3: PCM conversion setup', {\n channelData: {\n length: channelData.length,\n first: channelData[0],\n last: channelData[channelData.length - 1],\n },\n calculation: {\n bitDepth,\n bytesPerSample,\n numSamples,\n expectedBytes: numSamples * bytesPerSample,\n },\n })\n\n // Create PCM data with correct length based on original byte length\n const pcmData = new Uint8Array(numSamples * bytesPerSample)\n let offset = 0\n\n // Convert Float32 samples to PCM format\n for (let i = 0; i < numSamples; i++) {\n const sample = channelData[i]\n const value = Math.max(-1, Math.min(1, sample))\n // Convert to 16-bit signed integer\n let intValue = Math.round(value * 32767)\n\n // Handle negative values correctly\n if (intValue < 0) {\n intValue = 65536 + intValue\n }\n\n // Write as little-endian\n pcmData[offset++] = intValue & 255 // Low byte\n pcmData[offset++] = (intValue >> 8) & 255 // High byte\n }\n\n const durationMs = Math.round(\n (numSamples / processedBuffer.sampleRate) * 1000\n )\n\n logger?.debug('EXTRACT AUDIO - Step 4: Final output', {\n pcmData: {\n length: pcmData.length,\n first: pcmData[0],\n last: pcmData[pcmData.length - 1],\n },\n timing: {\n numSamples,\n sampleRate: processedBuffer.sampleRate,\n durationMs,\n shouldBe3000ms: endTimeMs\n ? endTimeMs - (startTimeMs ?? 0) === 3000\n : undefined,\n },\n })\n\n const result: ExtractedAudioData = {\n pcmData: new Uint8Array(pcmData.buffer),\n sampleRate: processedBuffer.sampleRate,\n channels: processedBuffer.channels,\n bitDepth,\n durationMs,\n format: `pcm_${bitDepth}bit` as const,\n samples: numSamples,\n }\n\n // Add WAV header if requested\n if (includeWavHeader) {\n logger?.debug('EXTRACT AUDIO - Step 5: Adding WAV header', {\n originalLength: pcmData.length,\n newLength: result.pcmData.length,\n firstBytes: Array.from(result.pcmData.slice(0, 44)), // WAV header is 44 bytes\n })\n const wavBuffer = writeWavHeader({\n buffer: pcmData.buffer.slice(0, pcmData.length),\n sampleRate: processedBuffer.sampleRate,\n numChannels: processedBuffer.channels,\n bitDepth,\n })\n result.pcmData = new Uint8Array(wavBuffer)\n result.hasWavHeader = true\n }\n\n if (includeNormalizedData) {\n result.normalizedData = channelData\n }\n\n if (includeBase64Data) {\n result.base64Data = btoa(\n String.fromCharCode(...new Uint8Array(pcmData.buffer))\n )\n }\n\n if (props.computeChecksum) {\n result.checksum = crc32.buf(pcmData)\n }\n\n logger?.debug('EXTRACT AUDIO - Step 3: PCM conversion complete', {\n pcmStats: {\n length: pcmData.length,\n bytesPerSample,\n totalSamples: numSamples,\n firstBytes: Array.from(pcmData.slice(0, 16)),\n lastBytes: Array.from(pcmData.slice(-16)),\n },\n })\n\n return result\n } catch (error) {\n props.logger?.error('EXTRACT AUDIO - Error:', error)\n throw error\n }\n }\n\n // Native: only pass serializable fields — logger causes crash on Android\n const { logger: _logger, ...nativeOptions } = props\n // Clean undefined values to avoid Android Kotlin bridge crash\n return await AudioStudioModule.extractAudioData(\n cleanNativeOptions(nativeOptions)\n )\n}\n"]}