@siteed/expo-audio-stream 1.0.1 → 1.0.2

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 (85) hide show
  1. package/README.md +6 -6
  2. package/android/build.gradle +5 -0
  3. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +120 -0
  4. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +34 -4
  5. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +635 -0
  6. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +194 -79
  7. package/android/src/main/java/net/siteed/audiostream/Constants.kt +1 -0
  8. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +48 -2
  9. package/android/src/main/java/net/siteed/audiostream/FFT.kt +44 -0
  10. package/android/src/main/java/net/siteed/audiostream/Features.kt +56 -0
  11. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +12 -0
  12. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +56 -0
  13. package/app.plugin.js +1 -1
  14. package/build/AudioRecorder.provider.js +1 -1
  15. package/build/AudioRecorder.provider.js.map +1 -1
  16. package/build/ExpoAudioStream.native.d.ts +3 -0
  17. package/build/ExpoAudioStream.native.d.ts.map +1 -0
  18. package/build/ExpoAudioStream.native.js +6 -0
  19. package/build/ExpoAudioStream.native.js.map +1 -0
  20. package/build/ExpoAudioStream.types.d.ts +79 -6
  21. package/build/ExpoAudioStream.types.d.ts.map +1 -1
  22. package/build/ExpoAudioStream.types.js.map +1 -1
  23. package/build/ExpoAudioStream.web.d.ts +41 -0
  24. package/build/ExpoAudioStream.web.d.ts.map +1 -0
  25. package/build/ExpoAudioStream.web.js +184 -0
  26. package/build/ExpoAudioStream.web.js.map +1 -0
  27. package/build/ExpoAudioStreamModule.d.ts +2 -2
  28. package/build/ExpoAudioStreamModule.d.ts.map +1 -1
  29. package/build/ExpoAudioStreamModule.js +12 -3
  30. package/build/ExpoAudioStreamModule.js.map +1 -1
  31. package/build/WebRecorder.d.ts +47 -0
  32. package/build/WebRecorder.d.ts.map +1 -0
  33. package/build/WebRecorder.js +243 -0
  34. package/build/WebRecorder.js.map +1 -0
  35. package/build/index.d.ts +14 -5
  36. package/build/index.d.ts.map +1 -1
  37. package/build/index.js +106 -7
  38. package/build/index.js.map +1 -1
  39. package/build/inlineAudioWebWorker.d.ts +3 -0
  40. package/build/inlineAudioWebWorker.d.ts.map +1 -0
  41. package/build/inlineAudioWebWorker.js +340 -0
  42. package/build/inlineAudioWebWorker.js.map +1 -0
  43. package/build/useAudioRecording.d.ts +24 -9
  44. package/build/useAudioRecording.d.ts.map +1 -1
  45. package/build/useAudioRecording.js +107 -29
  46. package/build/useAudioRecording.js.map +1 -1
  47. package/build/utils.d.ts +31 -0
  48. package/build/utils.d.ts.map +1 -0
  49. package/build/utils.js +143 -0
  50. package/build/utils.js.map +1 -0
  51. package/expo-module.config.json +13 -4
  52. package/ios/AudioAnalysisData.swift +39 -0
  53. package/ios/AudioProcessingHelpers.swift +59 -0
  54. package/ios/AudioProcessor.swift +317 -0
  55. package/ios/AudioStreamError.swift +7 -0
  56. package/ios/AudioStreamManager.swift +204 -52
  57. package/ios/AudioStreamManagerDelegate.swift +4 -0
  58. package/ios/DataPoint.swift +41 -0
  59. package/ios/ExpoAudioStreamModule.swift +188 -6
  60. package/ios/Features.swift +44 -0
  61. package/ios/RecordingResult.swift +19 -0
  62. package/ios/RecordingSettings.swift +13 -0
  63. package/ios/WaveformExtractor.swift +105 -0
  64. package/package.json +9 -9
  65. package/plugin/tsconfig.json +13 -8
  66. package/publish.sh +8 -0
  67. package/src/AudioRecorder.provider.tsx +1 -1
  68. package/src/ExpoAudioStream.native.ts +6 -0
  69. package/src/ExpoAudioStream.types.ts +97 -11
  70. package/src/ExpoAudioStream.web.ts +228 -0
  71. package/src/ExpoAudioStreamModule.ts +17 -3
  72. package/src/WebRecorder.ts +364 -0
  73. package/src/index.ts +166 -20
  74. package/src/inlineAudioWebWorker.tsx +340 -0
  75. package/src/useAudioRecording.tsx +410 -0
  76. package/src/utils.ts +189 -0
  77. package/build/ExpoAudioStreamModule.web.d.ts +0 -37
  78. package/build/ExpoAudioStreamModule.web.d.ts.map +0 -1
  79. package/build/ExpoAudioStreamModule.web.js +0 -156
  80. package/build/ExpoAudioStreamModule.web.js.map +0 -1
  81. package/docs/demo.gif +0 -0
  82. package/release-it.js +0 -18
  83. package/src/ExpoAudioStreamModule.web.ts +0 -181
  84. package/src/useAudioRecording.ts +0 -268
  85. package/yarn-error.log +0 -7793
@@ -0,0 +1,243 @@
1
+ import { encodingToBitDepth } from "./utils";
2
+ const DEFAULT_WEB_BITDEPTH = 32;
3
+ const DEFAULT_WEB_POINTS_PER_SECOND = 10;
4
+ const DEFAULT_WEB_INTERVAL = 500;
5
+ const DEFAULT_WEB_NUMBER_OF_CHANNELS = 1;
6
+ // const log = debug("expo-audio-stream:WebRecorder");
7
+ const log = console.log;
8
+ export class WebRecorder {
9
+ audioContext;
10
+ audioWorkletNode;
11
+ featureExtractorWorker;
12
+ source;
13
+ audioWorkletUrl;
14
+ emitAudioEventCallback;
15
+ emitAudioAnalysisCallback;
16
+ config;
17
+ position; // Track the cumulative position
18
+ numberOfChannels; // Number of audio channels
19
+ bitDepth; // Bit depth of the audio
20
+ exportBitDepth; // Bit depth of the audio
21
+ buffers; // Array to store the buffers
22
+ audioAnalysisData; // Keep updating the full audio analysis data with latest events
23
+ constructor({ audioContext, source, recordingConfig, featuresExtratorUrl, audioWorkletUrl, emitAudioEventCallback, emitAudioAnalysisCallback, }) {
24
+ this.audioContext = audioContext;
25
+ this.source = source;
26
+ this.audioWorkletUrl = audioWorkletUrl;
27
+ this.emitAudioEventCallback = emitAudioEventCallback;
28
+ this.emitAudioAnalysisCallback = emitAudioAnalysisCallback;
29
+ this.config = recordingConfig;
30
+ this.position = 0;
31
+ this.buffers = []; // Initialize the buffers array
32
+ const audioContextFormat = this.checkAudioContextFormat({
33
+ sampleRate: this.audioContext.sampleRate,
34
+ });
35
+ log("Initialized WebRecorder with config:", {
36
+ sampleRate: audioContextFormat.sampleRate,
37
+ bitDepth: audioContextFormat.bitDepth,
38
+ numberOfChannels: audioContextFormat.numberOfChannels,
39
+ });
40
+ this.bitDepth = audioContextFormat.bitDepth;
41
+ this.numberOfChannels =
42
+ audioContextFormat.numberOfChannels || DEFAULT_WEB_NUMBER_OF_CHANNELS; // Default to 1 if not available
43
+ this.exportBitDepth =
44
+ encodingToBitDepth({
45
+ encoding: recordingConfig.encoding ?? "pcm_32bit",
46
+ }) ||
47
+ audioContextFormat.bitDepth ||
48
+ DEFAULT_WEB_BITDEPTH;
49
+ this.audioAnalysisData = {
50
+ amplitudeRange: { min: 0, max: 0 },
51
+ dataPoints: [],
52
+ durationMs: 0,
53
+ samples: 0,
54
+ bitDepth: this.bitDepth,
55
+ numberOfChannels: this.numberOfChannels,
56
+ sampleRate: this.config.sampleRate || this.audioContext.sampleRate,
57
+ pointsPerSecond: this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,
58
+ speakerChanges: [],
59
+ };
60
+ // Initialize the feature extractor worker
61
+ //TODO: create audio feature extractor from a Blob instead of url since we cannot include the url directly in the library
62
+ // We keep the url during dev and use the blob in production.
63
+ this.featureExtractorWorker = new Worker(new URL(featuresExtratorUrl, window.location.href));
64
+ this.featureExtractorWorker.onmessage =
65
+ this.handleFeatureExtractorMessage.bind(this);
66
+ }
67
+ async init() {
68
+ try {
69
+ // TODO: Use the inline processor script for the audio worklet if the script is not available
70
+ // const blob = new Blob([InlineProcessorScrippt], {
71
+ // type: "application/javascript",
72
+ // });
73
+ // const url = URL.createObjectURL(blob);
74
+ // await this.audioContext.audioWorklet.addModule(url);
75
+ await this.audioContext.audioWorklet.addModule(this.audioWorkletUrl);
76
+ this.audioWorkletNode = new AudioWorkletNode(this.audioContext, "recorder-processor");
77
+ this.audioWorkletNode.port.onmessage = async (event) => {
78
+ const command = event.data.command;
79
+ if (command !== "newData") {
80
+ return;
81
+ }
82
+ // Handle the audio blob (e.g., send it to the server or process it further)
83
+ log("Received audio blob from processor", event);
84
+ const pcmBuffer = event.data.recordedData;
85
+ if (!pcmBuffer) {
86
+ return;
87
+ }
88
+ this.buffers.push(pcmBuffer); // Store the buffer
89
+ const sampleRate = event.data.sampleRate ?? this.audioContext.sampleRate;
90
+ const otherSampleRate = this.audioContext.sampleRate;
91
+ // Pass the intermediary buffer to the feature extractor worker
92
+ const pcmBufferCopy = pcmBuffer.slice(0);
93
+ const channelData = new Float32Array(pcmBufferCopy);
94
+ const duration = channelData.length / sampleRate; // Calculate duration of the current buffer
95
+ const otherDuration = pcmBuffer.byteLength /
96
+ (otherSampleRate * (this.exportBitDepth / this.numberOfChannels)); // Calculate duration of the current buffer
97
+ log(`sampleRate=${sampleRate} Duration: ${duration} -- otherSampleRate=${otherSampleRate} Other duration: ${otherDuration}`);
98
+ this.emitAudioEventCallback({
99
+ data: pcmBuffer,
100
+ position: this.position,
101
+ });
102
+ this.position += duration; // Update position
103
+ this.featureExtractorWorker.postMessage({
104
+ command: "process",
105
+ channelData,
106
+ sampleRate: this.audioContext.sampleRate,
107
+ pointsPerSecond: this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,
108
+ algorithm: this.config.algorithm || "rms",
109
+ bitDepth: this.bitDepth,
110
+ fullAudioDurationMs: this.position * 1000,
111
+ numberOfChannels: this.numberOfChannels,
112
+ features: this.config.features,
113
+ }, []);
114
+ };
115
+ log(`WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`, this.config);
116
+ this.audioWorkletNode.port.postMessage({
117
+ command: "init",
118
+ recordSampleRate: this.audioContext.sampleRate, // Pass the original sample rate
119
+ exportSampleRate: this.config.sampleRate ?? this.audioContext.sampleRate,
120
+ bitDepth: this.bitDepth,
121
+ exportBitDepth: this.exportBitDepth,
122
+ channels: this.numberOfChannels,
123
+ interval: this.config.interval ?? DEFAULT_WEB_INTERVAL,
124
+ });
125
+ // Connect the source to the AudioWorkletNode and start recording
126
+ this.source.connect(this.audioWorkletNode);
127
+ this.audioWorkletNode.connect(this.audioContext.destination);
128
+ }
129
+ catch (error) {
130
+ console.error("Failed to initialize WebRecorder", error);
131
+ }
132
+ }
133
+ handleFeatureExtractorMessage(event) {
134
+ if (event.data.command === "features") {
135
+ const segmentResult = event.data.result;
136
+ // Merge the segment result with the full audio analysis data
137
+ this.audioAnalysisData.dataPoints.push(...segmentResult.dataPoints);
138
+ this.audioAnalysisData.speakerChanges?.push(...(segmentResult.speakerChanges ?? []));
139
+ this.audioAnalysisData.durationMs = segmentResult.durationMs;
140
+ if (segmentResult.amplitudeRange) {
141
+ this.audioAnalysisData.amplitudeRange = {
142
+ min: Math.min(this.audioAnalysisData.amplitudeRange.min, segmentResult.amplitudeRange.min),
143
+ max: Math.max(this.audioAnalysisData.amplitudeRange.max, segmentResult.amplitudeRange.max),
144
+ };
145
+ }
146
+ // Handle the extracted features (e.g., emit an event or log them)
147
+ log("features event segmentResult", segmentResult);
148
+ log("features event audioAnalysisData", this.audioAnalysisData);
149
+ this.emitAudioAnalysisCallback(segmentResult);
150
+ }
151
+ }
152
+ start() {
153
+ this.source.connect(this.audioWorkletNode);
154
+ this.audioWorkletNode.connect(this.audioContext.destination);
155
+ }
156
+ stop() {
157
+ return new Promise((resolve, reject) => {
158
+ try {
159
+ if (this.audioWorkletNode) {
160
+ // this.source.disconnect(this.audioWorkletNode);
161
+ // this.audioWorkletNode.disconnect(this.audioContext.destination);
162
+ this.audioWorkletNode.port.postMessage({ command: "stop" });
163
+ // Set a timeout to reject the promise if no message is received within 5 seconds
164
+ const timeout = setTimeout(() => {
165
+ this.audioWorkletNode.port.removeEventListener("message", onMessage);
166
+ reject(new Error("Timeout error, audioWorkletNode didn't complete."));
167
+ }, 5000);
168
+ // Listen for the recordedData message to confirm stopping
169
+ const onMessage = async (event) => {
170
+ const command = event.data.command;
171
+ if (command === "recordedData") {
172
+ clearTimeout(timeout); // Clear the timeout
173
+ const rawPCMDataFull = event.data.recordedData?.slice(0);
174
+ // Compute duration of the recorded data
175
+ const duration = rawPCMDataFull.byteLength /
176
+ (this.audioContext.sampleRate *
177
+ (this.exportBitDepth / this.numberOfChannels));
178
+ log(`Received recorded data -- Duration: ${duration} vs ${rawPCMDataFull.byteLength / this.audioContext.sampleRate} seconds`);
179
+ log(`recordedData.length=${rawPCMDataFull.byteLength} vs transmittedData.length=${this.buffers[0].byteLength}`);
180
+ // Remove the event listener after receiving the final data
181
+ this.audioWorkletNode.port.removeEventListener("message", onMessage);
182
+ resolve(this.buffers); // Resolve the promise with the collected buffers
183
+ }
184
+ };
185
+ this.audioWorkletNode.port.addEventListener("message", onMessage);
186
+ }
187
+ // Stop all media stream tracks to stop the browser recording
188
+ this.stopMediaStreamTracks();
189
+ }
190
+ catch (error) {
191
+ reject(error);
192
+ }
193
+ });
194
+ }
195
+ pause() {
196
+ this.source.disconnect(this.audioWorkletNode); // Disconnect the source from the AudioWorkletNode
197
+ this.audioWorkletNode.disconnect(this.audioContext.destination); // Disconnect the AudioWorkletNode from the destination
198
+ this.audioWorkletNode.port.postMessage({ command: "pause" });
199
+ }
200
+ stopMediaStreamTracks() {
201
+ // Stop all audio tracks to stop the recording icon
202
+ const tracks = this.source.mediaStream.getTracks();
203
+ tracks.forEach((track) => track.stop());
204
+ }
205
+ async playRecordedData({ recordedData, }) {
206
+ try {
207
+ const blob = new Blob([recordedData]);
208
+ const url = URL.createObjectURL(blob);
209
+ const response = await fetch(url);
210
+ const arrayBuffer = await response.arrayBuffer();
211
+ // Decode the audio data
212
+ const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
213
+ // Create a buffer source node and play the audio
214
+ const bufferSource = this.audioContext.createBufferSource();
215
+ bufferSource.buffer = audioBuffer;
216
+ bufferSource.connect(this.audioContext.destination);
217
+ bufferSource.start();
218
+ log("Playing recorded data", recordedData);
219
+ }
220
+ catch (error) {
221
+ console.error(`Failed to play recorded data:`, error);
222
+ }
223
+ }
224
+ checkAudioContextFormat({ sampleRate }) {
225
+ // Create a silent AudioBuffer
226
+ const frameCount = sampleRate * 1.0; // 1 second buffer
227
+ const audioBuffer = this.audioContext.createBuffer(1, frameCount, sampleRate);
228
+ // Check the format
229
+ const channelData = audioBuffer.getChannelData(0);
230
+ const bitDepth = channelData.BYTES_PER_ELEMENT * 8; // 4 bytes per element means 32-bit
231
+ return {
232
+ sampleRate: audioBuffer.sampleRate,
233
+ bitDepth,
234
+ numberOfChannels: audioBuffer.numberOfChannels,
235
+ };
236
+ }
237
+ resume() {
238
+ this.source.connect(this.audioWorkletNode);
239
+ this.audioWorkletNode.connect(this.audioContext.destination);
240
+ this.audioWorkletNode.port.postMessage({ command: "resume" });
241
+ }
242
+ }
243
+ //# sourceMappingURL=WebRecorder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebRecorder.js","sourceRoot":"","sources":["../src/WebRecorder.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAgB7C,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,6BAA6B,GAAG,EAAE,CAAC;AACzC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,MAAM,8BAA8B,GAAG,CAAC,CAAC;AAEzC,sDAAsD;AACtD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;AACxB,MAAM,OAAO,WAAW;IACd,YAAY,CAAe;IAC3B,gBAAgB,CAAoB;IACpC,sBAAsB,CAAS;IAC/B,MAAM,CAA6B;IACnC,eAAe,CAAS;IACxB,sBAAsB,CAAyB;IAC/C,yBAAyB,CAA4B;IACrD,MAAM,CAAkB;IACxB,QAAQ,CAAS,CAAC,gCAAgC;IAClD,gBAAgB,CAAS,CAAC,2BAA2B;IACrD,QAAQ,CAAS,CAAC,yBAAyB;IAC3C,cAAc,CAAS,CAAC,yBAAyB;IACjD,OAAO,CAAgB,CAAC,6BAA6B;IACrD,iBAAiB,C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sourcesContent":["// src/WebRecorder.ts\nimport { AudioAnalysisData, RecordingConfig } from \"./ExpoAudioStream.types\";\nimport {\n EmitAudioAnalysisFunction,\n EmitAudioEventFunction,\n} from \"./ExpoAudioStream.web\";\nimport { encodingToBitDepth } from \"./utils\";\ninterface AudioWorkletEvent {\n data: {\n command: string;\n recordedData?: ArrayBuffer;\n sampleRate?: number;\n };\n}\n\ninterface AudioFeaturesEvent {\n data: {\n command: string;\n result: AudioAnalysisData;\n };\n}\n\nconst DEFAULT_WEB_BITDEPTH = 32;\nconst DEFAULT_WEB_POINTS_PER_SECOND = 10;\nconst DEFAULT_WEB_INTERVAL = 500;\nconst DEFAULT_WEB_NUMBER_OF_CHANNELS = 1;\n\n// const log = debug(\"expo-audio-stream:WebRecorder\");\nconst log = console.log;\nexport class WebRecorder {\n private audioContext: AudioContext;\n private audioWorkletNode!: AudioWorkletNode;\n private featureExtractorWorker: Worker;\n private source: MediaStreamAudioSourceNode;\n private audioWorkletUrl: string;\n private emitAudioEventCallback: EmitAudioEventFunction;\n private emitAudioAnalysisCallback: EmitAudioAnalysisFunction;\n private config: RecordingConfig;\n private position: number; // Track the cumulative position\n private numberOfChannels: number; // Number of audio channels\n private bitDepth: number; // Bit depth of the audio\n private exportBitDepth: number; // Bit depth of the audio\n private buffers: ArrayBuffer[]; // Array to store the buffers\n private audioAnalysisData: AudioAnalysisData; // Keep updating the full audio analysis data with latest events\n\n constructor({\n audioContext,\n source,\n recordingConfig,\n featuresExtratorUrl,\n audioWorkletUrl,\n emitAudioEventCallback,\n emitAudioAnalysisCallback,\n }: {\n audioContext: AudioContext;\n source: MediaStreamAudioSourceNode;\n recordingConfig: RecordingConfig;\n featuresExtratorUrl: string;\n audioWorkletUrl: string;\n emitAudioEventCallback: EmitAudioEventFunction;\n emitAudioAnalysisCallback: EmitAudioAnalysisFunction;\n }) {\n this.audioContext = audioContext;\n this.source = source;\n this.audioWorkletUrl = audioWorkletUrl;\n this.emitAudioEventCallback = emitAudioEventCallback;\n this.emitAudioAnalysisCallback = emitAudioAnalysisCallback;\n this.config = recordingConfig;\n this.position = 0;\n this.buffers = []; // Initialize the buffers array\n\n const audioContextFormat = this.checkAudioContextFormat({\n sampleRate: this.audioContext.sampleRate,\n });\n log(\"Initialized WebRecorder with config:\", {\n sampleRate: audioContextFormat.sampleRate,\n bitDepth: audioContextFormat.bitDepth,\n numberOfChannels: audioContextFormat.numberOfChannels,\n });\n\n this.bitDepth = audioContextFormat.bitDepth;\n this.numberOfChannels =\n audioContextFormat.numberOfChannels || DEFAULT_WEB_NUMBER_OF_CHANNELS; // Default to 1 if not available\n this.exportBitDepth =\n encodingToBitDepth({\n encoding: recordingConfig.encoding ?? \"pcm_32bit\",\n }) ||\n audioContextFormat.bitDepth ||\n DEFAULT_WEB_BITDEPTH;\n\n this.audioAnalysisData = {\n amplitudeRange: { min: 0, max: 0 },\n dataPoints: [],\n durationMs: 0,\n samples: 0,\n bitDepth: this.bitDepth,\n numberOfChannels: this.numberOfChannels,\n sampleRate: this.config.sampleRate || this.audioContext.sampleRate,\n pointsPerSecond:\n this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,\n speakerChanges: [],\n };\n\n // Initialize the feature extractor worker\n //TODO: create audio feature extractor from a Blob instead of url since we cannot include the url directly in the library\n // We keep the url during dev and use the blob in production.\n this.featureExtractorWorker = new Worker(\n new URL(featuresExtratorUrl, window.location.href),\n );\n this.featureExtractorWorker.onmessage =\n this.handleFeatureExtractorMessage.bind(this);\n }\n\n async init() {\n try {\n // TODO: Use the inline processor script for the audio worklet if the script is not available\n // const blob = new Blob([InlineProcessorScrippt], {\n // type: \"application/javascript\",\n // });\n // const url = URL.createObjectURL(blob);\n // await this.audioContext.audioWorklet.addModule(url);\n await this.audioContext.audioWorklet.addModule(this.audioWorkletUrl);\n\n this.audioWorkletNode = new AudioWorkletNode(\n this.audioContext,\n \"recorder-processor\",\n );\n\n this.audioWorkletNode.port.onmessage = async (\n event: AudioWorkletEvent,\n ) => {\n const command = event.data.command;\n if (command !== \"newData\") {\n return;\n }\n // Handle the audio blob (e.g., send it to the server or process it further)\n log(\"Received audio blob from processor\", event);\n const pcmBuffer = event.data.recordedData;\n\n if (!pcmBuffer) {\n return;\n }\n\n this.buffers.push(pcmBuffer); // Store the buffer\n const sampleRate =\n event.data.sampleRate ?? this.audioContext.sampleRate;\n const otherSampleRate = this.audioContext.sampleRate;\n\n // Pass the intermediary buffer to the feature extractor worker\n const pcmBufferCopy = pcmBuffer.slice(0);\n const channelData = new Float32Array(pcmBufferCopy);\n\n const duration = channelData.length / sampleRate; // Calculate duration of the current buffer\n const otherDuration =\n pcmBuffer.byteLength /\n (otherSampleRate * (this.exportBitDepth / this.numberOfChannels)); // Calculate duration of the current buffer\n log(\n `sampleRate=${sampleRate} Duration: ${duration} -- otherSampleRate=${otherSampleRate} Other duration: ${otherDuration}`,\n );\n\n this.emitAudioEventCallback({\n data: pcmBuffer,\n position: this.position,\n });\n this.position += duration; // Update position\n\n this.featureExtractorWorker.postMessage(\n {\n command: \"process\",\n channelData,\n sampleRate: this.audioContext.sampleRate,\n pointsPerSecond:\n this.config.pointsPerSecond || DEFAULT_WEB_POINTS_PER_SECOND,\n algorithm: this.config.algorithm || \"rms\",\n bitDepth: this.bitDepth,\n fullAudioDurationMs: this.position * 1000,\n numberOfChannels: this.numberOfChannels,\n features: this.config.features,\n },\n [],\n );\n };\n\n log(\n `WebRecorder initialized -- recordSampleRate=${this.audioContext.sampleRate}`,\n this.config,\n );\n this.audioWorkletNode.port.postMessage({\n command: \"init\",\n recordSampleRate: this.audioContext.sampleRate, // Pass the original sample rate\n exportSampleRate:\n this.config.sampleRate ?? this.audioContext.sampleRate,\n bitDepth: this.bitDepth,\n exportBitDepth: this.exportBitDepth,\n channels: this.numberOfChannels,\n interval: this.config.interval ?? DEFAULT_WEB_INTERVAL,\n });\n\n // Connect the source to the AudioWorkletNode and start recording\n this.source.connect(this.audioWorkletNode);\n this.audioWorkletNode.connect(this.audioContext.destination);\n } catch (error) {\n console.error(\"Failed to initialize WebRecorder\", error);\n }\n }\n\n handleFeatureExtractorMessage(event: AudioFeaturesEvent) {\n if (event.data.command === \"features\") {\n const segmentResult = event.data.result;\n\n // Merge the segment result with the full audio analysis data\n this.audioAnalysisData.dataPoints.push(...segmentResult.dataPoints);\n this.audioAnalysisData.speakerChanges?.push(\n ...(segmentResult.speakerChanges ?? []),\n );\n this.audioAnalysisData.durationMs = segmentResult.durationMs;\n if (segmentResult.amplitudeRange) {\n this.audioAnalysisData.amplitudeRange = {\n min: Math.min(\n this.audioAnalysisData.amplitudeRange.min,\n segmentResult.amplitudeRange.min,\n ),\n max: Math.max(\n this.audioAnalysisData.amplitudeRange.max,\n segmentResult.amplitudeRange.max,\n ),\n };\n }\n // Handle the extracted features (e.g., emit an event or log them)\n log(\"features event segmentResult\", segmentResult);\n log(\"features event audioAnalysisData\", this.audioAnalysisData);\n this.emitAudioAnalysisCallback(segmentResult);\n }\n }\n\n start() {\n this.source.connect(this.audioWorkletNode);\n this.audioWorkletNode.connect(this.audioContext.destination);\n }\n\n stop() {\n return new Promise((resolve, reject) => {\n try {\n if (this.audioWorkletNode) {\n // this.source.disconnect(this.audioWorkletNode);\n // this.audioWorkletNode.disconnect(this.audioContext.destination);\n this.audioWorkletNode.port.postMessage({ command: \"stop\" });\n\n // Set a timeout to reject the promise if no message is received within 5 seconds\n const timeout = setTimeout(() => {\n this.audioWorkletNode.port.removeEventListener(\n \"message\",\n onMessage,\n );\n reject(\n new Error(\"Timeout error, audioWorkletNode didn't complete.\"),\n );\n }, 5000);\n\n // Listen for the recordedData message to confirm stopping\n const onMessage = async (event: AudioWorkletEvent) => {\n const command = event.data.command;\n if (command === \"recordedData\") {\n clearTimeout(timeout); // Clear the timeout\n\n const rawPCMDataFull = event.data.recordedData?.slice(\n 0,\n ) as ArrayBuffer;\n\n // Compute duration of the recorded data\n const duration =\n rawPCMDataFull.byteLength /\n (this.audioContext.sampleRate *\n (this.exportBitDepth / this.numberOfChannels));\n log(\n `Received recorded data -- Duration: ${duration} vs ${rawPCMDataFull.byteLength / this.audioContext.sampleRate} seconds`,\n );\n log(\n `recordedData.length=${rawPCMDataFull.byteLength} vs transmittedData.length=${this.buffers[0].byteLength}`,\n );\n\n // Remove the event listener after receiving the final data\n this.audioWorkletNode.port.removeEventListener(\n \"message\",\n onMessage,\n );\n resolve(this.buffers); // Resolve the promise with the collected buffers\n }\n };\n this.audioWorkletNode.port.addEventListener(\"message\", onMessage);\n }\n\n // Stop all media stream tracks to stop the browser recording\n this.stopMediaStreamTracks();\n } catch (error) {\n reject(error);\n }\n });\n }\n\n pause() {\n this.source.disconnect(this.audioWorkletNode); // Disconnect the source from the AudioWorkletNode\n this.audioWorkletNode.disconnect(this.audioContext.destination); // Disconnect the AudioWorkletNode from the destination\n this.audioWorkletNode.port.postMessage({ command: \"pause\" });\n }\n\n stopMediaStreamTracks() {\n // Stop all audio tracks to stop the recording icon\n const tracks = this.source.mediaStream.getTracks();\n tracks.forEach((track) => track.stop());\n }\n\n async playRecordedData({\n recordedData,\n }: {\n recordedData: ArrayBuffer;\n mimeType?: string;\n }) {\n try {\n const blob = new Blob([recordedData]);\n const url = URL.createObjectURL(blob);\n const response = await fetch(url);\n const arrayBuffer = await response.arrayBuffer();\n\n // Decode the audio data\n const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);\n\n // Create a buffer source node and play the audio\n const bufferSource = this.audioContext.createBufferSource();\n bufferSource.buffer = audioBuffer;\n bufferSource.connect(this.audioContext.destination);\n bufferSource.start();\n log(\"Playing recorded data\", recordedData);\n } catch (error) {\n console.error(`Failed to play recorded data:`, error);\n }\n }\n\n private checkAudioContextFormat({ sampleRate }: { sampleRate: number }) {\n // Create a silent AudioBuffer\n const frameCount = sampleRate * 1.0; // 1 second buffer\n const audioBuffer = this.audioContext.createBuffer(\n 1,\n frameCount,\n sampleRate,\n );\n\n // Check the format\n const channelData = audioBuffer.getChannelData(0);\n const bitDepth = channelData.BYTES_PER_ELEMENT * 8; // 4 bytes per element means 32-bit\n\n return {\n sampleRate: audioBuffer.sampleRate,\n bitDepth,\n numberOfChannels: audioBuffer.numberOfChannels,\n };\n }\n\n resume() {\n this.source.connect(this.audioWorkletNode);\n this.audioWorkletNode.connect(this.audioContext.destination);\n this.audioWorkletNode.port.postMessage({ command: \"resume\" });\n }\n}\n"]}
package/build/index.d.ts CHANGED
@@ -1,9 +1,18 @@
1
1
  import { type Subscription } from "expo-modules-core";
2
2
  import { AudioRecorderProvider, useSharedAudioRecorder } from "./AudioRecorder.provider";
3
- import { AudioEventPayload } from "./ExpoAudioStream.types";
4
- import { AudioDataEvent, UseAudioRecorderState, useAudioRecorder } from "./useAudioRecording";
5
- export declare function test(): void;
3
+ import { AudioAnalysisData, AudioEventPayload } from "./ExpoAudioStream.types";
4
+ import { ExtractMetadataProps, useAudioRecorder } from "./useAudioRecording";
5
+ import { convertPCMToFloat32, getWavFileInfo, writeWavHeader } from "./utils";
6
6
  export declare function addAudioEventListener(listener: (event: AudioEventPayload) => Promise<void>): Subscription;
7
- export { AudioRecorderProvider, useAudioRecorder, useSharedAudioRecorder };
8
- export type { AudioDataEvent, AudioEventPayload, UseAudioRecorderState };
7
+ export declare function addAudioAnalysisListener(listener: (event: AudioAnalysisData) => Promise<void>): Subscription;
8
+ export declare const extractAudioAnalysis: ({ fileUri, pointsPerSecond, arrayBuffer, bitDepth, skipWavHeader, durationMs, sampleRate, numberOfChannels, algorithm, features, featuresExtratorUrl, }: ExtractMetadataProps) => Promise<AudioAnalysisData>;
9
+ export interface ExtractWaveformProps {
10
+ fileUri: string;
11
+ numberOfSamples: number;
12
+ offset?: number;
13
+ length?: number;
14
+ }
15
+ export declare const extractWaveform: ({ fileUri, numberOfSamples, offset, length, }: ExtractWaveformProps) => Promise<unknown>;
16
+ export { AudioRecorderProvider, convertPCMToFloat32, getWavFileInfo, useAudioRecorder, useSharedAudioRecorder, writeWavHeader as writeWaveHeader, };
17
+ export * from "./ExpoAudioStream.types";
9
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,gBAAgB,EACjB,MAAM,qBAAqB,CAAC;AAM7B,wBAAgB,IAAI,IAAI,IAAI,CAE3B;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,OAAO,CAAC,IAAI,CAAC,GACpD,YAAY,CAGd;AAED,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,CAAC;AAC3E,YAAY,EAAE,cAAc,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAKpE,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE/E,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAI9E,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,OAAO,CAAC,IAAI,CAAC,GACpD,YAAY,CAGd;AAED,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,OAAO,CAAC,IAAI,CAAC,GACpD,YAAY,CAGd;AAID,eAAO,MAAM,oBAAoB,4JAY9B,oBAAoB,KAAG,OAAO,CAAC,iBAAiB,CAwGlD,CAAC;AAEF,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AACD,eAAO,MAAM,eAAe,kDAKzB,oBAAoB,KAAG,OAAO,CAAC,OAAO,CASxC,CAAC;AAEF,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,sBAAsB,EACtB,cAAc,IAAI,eAAe,GAClC,CAAC;AAEF,cAAc,yBAAyB,CAAC"}
package/build/index.js CHANGED
@@ -1,16 +1,115 @@
1
- import { EventEmitter, NativeModulesProxy, } from "expo-modules-core";
1
+ // src/index.ts
2
+ import { EventEmitter } from "expo-modules-core";
3
+ import { Platform } from "react-native";
2
4
  // Import the native module. On web, it will be resolved to ExpoAudioStream.web.ts
3
5
  // and on native platforms to ExpoAudioStream.ts
4
6
  import { AudioRecorderProvider, useSharedAudioRecorder, } from "./AudioRecorder.provider";
5
7
  import ExpoAudioStreamModule from "./ExpoAudioStreamModule";
6
- import { useAudioRecorder, } from "./useAudioRecording";
7
- const emitter = new EventEmitter(ExpoAudioStreamModule ?? NativeModulesProxy.ExpoAudioStream);
8
- export function test() {
9
- return ExpoAudioStreamModule.test();
10
- }
8
+ import { useAudioRecorder } from "./useAudioRecording";
9
+ import { convertPCMToFloat32, getWavFileInfo, writeWavHeader } from "./utils";
10
+ const emitter = new EventEmitter(ExpoAudioStreamModule);
11
11
  export function addAudioEventListener(listener) {
12
12
  console.log(`addAudioEventListener`, listener);
13
13
  return emitter.addListener("AudioData", listener);
14
14
  }
15
- export { AudioRecorderProvider, useAudioRecorder, useSharedAudioRecorder };
15
+ export function addAudioAnalysisListener(listener) {
16
+ console.log(`addAudioAnalysisListener`, listener);
17
+ return emitter.addListener("AudioAnalysis", listener);
18
+ }
19
+ const isWeb = Platform.OS === "web";
20
+ export const extractAudioAnalysis = async ({ fileUri, pointsPerSecond = 20, arrayBuffer, bitDepth, skipWavHeader, durationMs, sampleRate, numberOfChannels, algorithm = "rms", features, featuresExtratorUrl = "/audio-features-extractor.js", }) => {
21
+ if (isWeb) {
22
+ if (!arrayBuffer && !fileUri) {
23
+ throw new Error("Either arrayBuffer or fileUri must be provided");
24
+ }
25
+ if (!arrayBuffer) {
26
+ console.log(`fetching fileUri`, fileUri);
27
+ const response = await fetch(fileUri);
28
+ if (!response.ok) {
29
+ throw new Error(`Failed to fetch fileUri: ${response.statusText}`);
30
+ }
31
+ arrayBuffer = (await response.arrayBuffer()).slice(0);
32
+ console.log(`fetched fileUri`, arrayBuffer.byteLength, arrayBuffer);
33
+ }
34
+ // Create a new copy of the ArrayBuffer to avoid detachment issues
35
+ const bufferCopy = arrayBuffer.slice(0);
36
+ console.log(`extractAudioAnalysis skipWavHeader=${skipWavHeader} bitDepth=${bitDepth} len=${bufferCopy.byteLength}`, bufferCopy.slice(0, 100));
37
+ let actualBitDepth = bitDepth;
38
+ if (!actualBitDepth) {
39
+ console.log(`extractAudioAnalysis bitDepth not provided -- getting wav file info`);
40
+ const fileInfo = await getWavFileInfo(bufferCopy);
41
+ actualBitDepth = fileInfo.bitDepth;
42
+ }
43
+ console.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`);
44
+ // let copyChannelData: Float32Array;
45
+ // try {
46
+ // const audioContext = new (window.AudioContext ||
47
+ // // @ts-ignore
48
+ // window.webkitAudioContext)();
49
+ // const audioBuffer = await audioContext.decodeAudioData(bufferCopy);
50
+ // const channelData = audioBuffer.getChannelData(0); // Use only the first channel
51
+ // copyChannelData = new Float32Array(channelData); // Create a new Float32Array
52
+ // } catch (error) {
53
+ // console.warn("Failed to decode audio data:", error);
54
+ // // Fall back to creating a new Float32Array from the ArrayBuffer if decoding fails
55
+ // copyChannelData = new Float32Array(arrayBuffer);
56
+ // }
57
+ const { pcmValues: channelData, min, max, } = convertPCMToFloat32({
58
+ buffer: arrayBuffer,
59
+ bitDepth: actualBitDepth,
60
+ skipWavHeader,
61
+ });
62
+ console.log(`extractAudioAnalysis skipWaveHeader=${skipWavHeader} convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`);
63
+ return new Promise((resolve, reject) => {
64
+ const worker = new Worker(new URL(featuresExtratorUrl, window.location.href));
65
+ worker.onmessage = (event) => {
66
+ resolve(event.data.result);
67
+ };
68
+ worker.onerror = (error) => {
69
+ reject(error);
70
+ };
71
+ worker.postMessage({
72
+ command: "process",
73
+ channelData,
74
+ sampleRate,
75
+ pointsPerSecond,
76
+ algorithm,
77
+ bitDepth,
78
+ fullAudioDurationMs: durationMs,
79
+ numberOfChannels,
80
+ });
81
+ });
82
+ }
83
+ else {
84
+ if (!fileUri) {
85
+ throw new Error("fileUri is required");
86
+ }
87
+ console.log(`extractAudioAnalysis`, {
88
+ fileUri,
89
+ pointsPerSecond,
90
+ algorithm,
91
+ });
92
+ const res = await ExpoAudioStreamModule.extractAudioAnalysis({
93
+ fileUri,
94
+ pointsPerSecond,
95
+ skipWavHeader,
96
+ algorithm,
97
+ features,
98
+ });
99
+ console.log(`extractAudioAnalysis`, res);
100
+ return res;
101
+ }
102
+ };
103
+ export const extractWaveform = async ({ fileUri, numberOfSamples, offset = 0, length, }) => {
104
+ const res = await ExpoAudioStreamModule.extractAudioAnalysis({
105
+ fileUri,
106
+ numberOfSamples,
107
+ offset,
108
+ length,
109
+ });
110
+ console.log(`extractWaveform`, res);
111
+ return res;
112
+ };
113
+ export { AudioRecorderProvider, convertPCMToFloat32, getWavFileInfo, useAudioRecorder, useSharedAudioRecorder, writeWavHeader as writeWaveHeader, };
114
+ export * from "./ExpoAudioStream.types";
16
115
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,kBAAkB,GAEnB,MAAM,mBAAmB,CAAC;AAE3B,kFAAkF;AAClF,gDAAgD;AAChD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAElC,OAAO,qBAAqB,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAGL,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,OAAO,GAAG,IAAI,YAAY,CAC9B,qBAAqB,IAAI,kBAAkB,CAAC,eAAe,CAC5D,CAAC;AAEF,MAAM,UAAU,IAAI;IAClB,OAAO,qBAAqB,CAAC,IAAI,EAAE,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,qBAAqB,CACnC,QAAqD;IAErD,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,QAAQ,CAAC,CAAC;IAC/C,OAAO,OAAO,CAAC,WAAW,CAAoB,WAAW,EAAE,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,CAAC","sourcesContent":["import {\n EventEmitter,\n NativeModulesProxy,\n type Subscription,\n} from \"expo-modules-core\";\n\n// Import the native module. On web, it will be resolved to ExpoAudioStream.web.ts\n// and on native platforms to ExpoAudioStream.ts\nimport {\n AudioRecorderProvider,\n useSharedAudioRecorder,\n} from \"./AudioRecorder.provider\";\nimport { AudioEventPayload } from \"./ExpoAudioStream.types\";\nimport ExpoAudioStreamModule from \"./ExpoAudioStreamModule\";\nimport {\n AudioDataEvent,\n UseAudioRecorderState,\n useAudioRecorder,\n} from \"./useAudioRecording\";\n\nconst emitter = new EventEmitter(\n ExpoAudioStreamModule ?? NativeModulesProxy.ExpoAudioStream,\n);\n\nexport function test(): void {\n return ExpoAudioStreamModule.test();\n}\n\nexport function addAudioEventListener(\n listener: (event: AudioEventPayload) => Promise<void>,\n): Subscription {\n console.log(`addAudioEventListener`, listener);\n return emitter.addListener<AudioEventPayload>(\"AudioData\", listener);\n}\n\nexport { AudioRecorderProvider, useAudioRecorder, useSharedAudioRecorder };\nexport type { AudioDataEvent, AudioEventPayload, UseAudioRecorderState };\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,OAAO,EAAE,YAAY,EAAqB,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,kFAAkF;AAClF,gDAAgD;AAChD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAElC,OAAO,qBAAqB,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAwB,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9E,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,qBAAqB,CAAC,CAAC;AAExD,MAAM,UAAU,qBAAqB,CACnC,QAAqD;IAErD,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,QAAQ,CAAC,CAAC;IAC/C,OAAO,OAAO,CAAC,WAAW,CAAoB,WAAW,EAAE,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,QAAqD;IAErD,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,QAAQ,CAAC,CAAC;IAClD,OAAO,OAAO,CAAC,WAAW,CAAoB,eAAe,EAAE,QAAQ,CAAC,CAAC;AAC3E,CAAC;AAED,MAAM,KAAK,GAAG,QAAQ,CAAC,EAAE,KAAK,KAAK,CAAC;AAEpC,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,EAAE,EACzC,OAAO,EACP,eAAe,GAAG,EAAE,EACpB,WAAW,EACX,QAAQ,EACR,aAAa,EAC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,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,CAAC,GAAG,CAAC,sBAAsB,EAAE;YAClC,OAAO;YACP,eAAe;YACf,SAAS;SACV,CAAC,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,qBAAqB,CAAC,oBAAoB,CAAC;YAC3D,OAAO;YACP,eAAe;YACf,aAAa;YACb,SAAS;YACT,QAAQ;SACT,CAAC,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,sBAAsB,EAAE,GAAG,CAAC,CAAC;QACzC,OAAO,GAAG,CAAC;IACb,CAAC;AACH,CAAC,CAAC;AAQF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,EACpC,OAAO,EACP,eAAe,EACf,MAAM,GAAG,CAAC,EACV,MAAM,GACe,EAAoB,EAAE;IAC3C,MAAM,GAAG,GAAG,MAAM,qBAAqB,CAAC,oBAAoB,CAAC;QAC3D,OAAO;QACP,eAAe;QACf,MAAM;QACN,MAAM;KACP,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAC;IACpC,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,sBAAsB,EACtB,cAAc,IAAI,eAAe,GAClC,CAAC;AAEF,cAAc,yBAAyB,CAAC","sourcesContent":["// src/index.ts\nimport { EventEmitter, type Subscription } from \"expo-modules-core\";\nimport { Platform } from \"react-native\";\n\n// Import the native module. On web, it will be resolved to ExpoAudioStream.web.ts\n// and on native platforms to ExpoAudioStream.ts\nimport {\n AudioRecorderProvider,\n useSharedAudioRecorder,\n} from \"./AudioRecorder.provider\";\nimport { AudioAnalysisData, AudioEventPayload } from \"./ExpoAudioStream.types\";\nimport ExpoAudioStreamModule from \"./ExpoAudioStreamModule\";\nimport { ExtractMetadataProps, useAudioRecorder } from \"./useAudioRecording\";\nimport { convertPCMToFloat32, getWavFileInfo, writeWavHeader } from \"./utils\";\n\nconst emitter = new EventEmitter(ExpoAudioStreamModule);\n\nexport function addAudioEventListener(\n listener: (event: AudioEventPayload) => Promise<void>,\n): Subscription {\n console.log(`addAudioEventListener`, listener);\n return emitter.addListener<AudioEventPayload>(\"AudioData\", listener);\n}\n\nexport function addAudioAnalysisListener(\n listener: (event: AudioAnalysisData) => Promise<void>,\n): Subscription {\n console.log(`addAudioAnalysisListener`, listener);\n return emitter.addListener<AudioAnalysisData>(\"AudioAnalysis\", listener);\n}\n\nconst isWeb = Platform.OS === \"web\";\n\nexport const extractAudioAnalysis = async ({\n fileUri,\n pointsPerSecond = 20,\n arrayBuffer,\n bitDepth,\n skipWavHeader,\n durationMs,\n sampleRate,\n numberOfChannels,\n algorithm = \"rms\",\n features,\n featuresExtratorUrl = \"/audio-features-extractor.js\",\n}: ExtractMetadataProps): Promise<AudioAnalysisData> => {\n if (isWeb) {\n if (!arrayBuffer && !fileUri) {\n throw new Error(\"Either arrayBuffer or fileUri must be provided\");\n }\n\n if (!arrayBuffer) {\n console.log(`fetching fileUri`, fileUri);\n const response = await fetch(fileUri!);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch fileUri: ${response.statusText}`);\n }\n\n arrayBuffer = (await response.arrayBuffer()).slice(0);\n console.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 console.log(\n `extractAudioAnalysis skipWavHeader=${skipWavHeader} bitDepth=${bitDepth} len=${bufferCopy.byteLength}`,\n bufferCopy.slice(0, 100),\n );\n\n let actualBitDepth = bitDepth;\n if (!actualBitDepth) {\n console.log(\n `extractAudioAnalysis bitDepth not provided -- getting wav file info`,\n );\n const fileInfo = await getWavFileInfo(bufferCopy);\n actualBitDepth = fileInfo.bitDepth;\n }\n console.log(`extractAudioAnalysis actualBitDepth=${actualBitDepth}`);\n // let copyChannelData: Float32Array;\n // try {\n // const audioContext = new (window.AudioContext ||\n // // @ts-ignore\n // window.webkitAudioContext)();\n // const audioBuffer = await audioContext.decodeAudioData(bufferCopy);\n // const channelData = audioBuffer.getChannelData(0); // Use only the first channel\n // copyChannelData = new Float32Array(channelData); // Create a new Float32Array\n // } catch (error) {\n // console.warn(\"Failed to decode audio data:\", error);\n // // Fall back to creating a new Float32Array from the ArrayBuffer if decoding fails\n // copyChannelData = new Float32Array(arrayBuffer);\n // }\n\n const {\n pcmValues: channelData,\n min,\n max,\n } = convertPCMToFloat32({\n buffer: arrayBuffer,\n bitDepth: actualBitDepth,\n skipWavHeader,\n });\n console.log(\n `extractAudioAnalysis skipWaveHeader=${skipWavHeader} convertPCMToFloat32 length=${channelData.length} range: [ ${min} :: ${max} ]`,\n );\n\n return new Promise((resolve, reject) => {\n const worker = new Worker(\n new URL(featuresExtratorUrl, window.location.href),\n );\n\n worker.onmessage = (event) => {\n resolve(event.data.result);\n };\n\n worker.onerror = (error) => {\n reject(error);\n };\n\n worker.postMessage({\n command: \"process\",\n channelData,\n sampleRate,\n pointsPerSecond,\n algorithm,\n bitDepth,\n fullAudioDurationMs: durationMs,\n numberOfChannels,\n });\n });\n } else {\n if (!fileUri) {\n throw new Error(\"fileUri is required\");\n }\n console.log(`extractAudioAnalysis`, {\n fileUri,\n pointsPerSecond,\n algorithm,\n });\n const res = await ExpoAudioStreamModule.extractAudioAnalysis({\n fileUri,\n pointsPerSecond,\n skipWavHeader,\n algorithm,\n features,\n });\n console.log(`extractAudioAnalysis`, res);\n return res;\n }\n};\n\nexport interface ExtractWaveformProps {\n fileUri: string;\n numberOfSamples: number;\n offset?: number;\n length?: number;\n}\nexport const extractWaveform = async ({\n fileUri,\n numberOfSamples,\n offset = 0,\n length,\n}: ExtractWaveformProps): Promise<unknown> => {\n const res = await ExpoAudioStreamModule.extractAudioAnalysis({\n fileUri,\n numberOfSamples,\n offset,\n length,\n });\n console.log(`extractWaveform`, res);\n return res;\n};\n\nexport {\n AudioRecorderProvider,\n convertPCMToFloat32,\n getWavFileInfo,\n useAudioRecorder,\n useSharedAudioRecorder,\n writeWavHeader as writeWaveHeader,\n};\n\nexport * from \"./ExpoAudioStream.types\";\n"]}
@@ -0,0 +1,3 @@
1
+ export declare const SimpleInlineProcessorScript = "\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.recordedBuffers = []; // Float32Array\n this.newRecBuffer = []; // Float32Array\n this.exportIntervalSamples = 0;\n this.samplesSinceLastExport = 0;\n this.recordSampleRate = 44100; // To be overwritten\n this.exportSampleRate = 44100; // To be overwritten\n this.channels = 1; // Default to 1 channel (mono)\n this.bitDepth = 32; // Default to 32-bit depth\n this.isRecording = true;\n this.port.onmessage = this.handleMessage.bind(this);\n }\n\n handleMessage(event) {\n switch (event.data.command) {\n case 'init':\n this.recordSampleRate = event.data.recordSampleRate;\n this.exportSampleRate = event.data.exportSampleRate || event.data.recordSampleRate;\n this.exportIntervalSamples = this.recordSampleRate * (event.data.interval / 1000);\n break;\n case 'stop':\n this.isRecording = false;\n const fullRecordedData = this.getAllRecordedData();\n this.port.postMessage({ command: 'recordedData', recordedData: fullRecordedData });\n break;\n }\n }\n\n process(inputs, outputs, parameters) {\n if (!this.isRecording) return true;\n const input = inputs[0];\n if (input.length > 0) {\n const newBuffer = new Float32Array(input[0]);\n this.newRecBuffer.push(newBuffer);\n this.recordedBuffers.push(newBuffer);\n this.samplesSinceLastExport += newBuffer.length;\n\n if (this.samplesSinceLastExport >= this.exportIntervalSamples) {\n this.exportNewData();\n this.samplesSinceLastExport = 0;\n }\n }\n return true;\n }\n\n mergeBuffers(bufferArray, recLength) {\n const result = new Float32Array(recLength);\n let offset = 0;\n for (let i = 0; i < bufferArray.length; i++) {\n result.set(bufferArray[i], offset);\n offset += bufferArray[i].length;\n }\n return result;\n }\n\n floatTo16BitPCM(output, offset, input) {\n for (let i = 0; i < input.length; i++, offset += 2) {\n const s = Math.max(-1, Math.min(1, input[i]));\n output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);\n }\n console.debug('Float to 16-bit PCM conversion complete. Output byte length:', offset);\n }\n\n floatTo32BitPCM(output, offset, input) {\n for (let i = 0; i < input.length; i++, offset += 4) {\n output.setFloat32(offset, input[i], true);\n }\n console.debug('Float to 32-bit PCM (no conversion) complete. Output byte length:', offset);\n }\n\n writeString(view, offset, string) {\n for (let i = 0; i < string.length; i++) {\n view.setUint8(offset + i, string.charCodeAt(i));\n }\n }\n\n encodeWAV(samples, includeHeader = true) {\n const sampleCount = samples.length;\n const buffer = new ArrayBuffer((includeHeader ? 44 : 0) + sampleCount * 4);\n const view = new DataView(buffer);\n\n if (includeHeader) {\n this.writeString(view, 0, 'RIFF');\n view.setUint32(4, 36 + sampleCount * 4, true); // File size - 8 bytes\n this.writeString(view, 8, 'WAVE');\n this.writeString(view, 12, 'fmt ');\n view.setUint32(16, 16, true); // PCM format\n view.setUint16(20, 3, true); // Format code 3 for float\n view.setUint16(22, 1, true); // Mono channel\n view.setUint32(24, this.recordSampleRate, true); // Sample rate\n view.setUint32(28, this.recordSampleRate * 4, true); // Byte rate\n view.setUint16(32, 4, true); // Block align (4 bytes for 32-bit float)\n view.setUint16(34, 32, true); // Bits per sample (32-bit float)\n this.writeString(view, 36, 'data');\n view.setUint32(40, sampleCount * 4, true); // Data chunk size\n }\n\n console.debug('Writing PCM samples to DataView. Offset:', includeHeader ? 44 : 0, 'Samples length:', sampleCount);\n this.floatTo32BitPCM(view, includeHeader ? 44 : 0, samples);\n // this.floatTo16BitPCM(view, includeHeader ? 44 : 0, samples);\n\n console.debug('Encoded WAV DataView:', view);\n console.debug('Encoded WAV length:', view.byteLength);\n\n return view;\n }\n\n\n exportNewData() {\n // Calculate the total length of the new recorded buffers\n const length = this.newRecBuffer.reduce((acc, buffer) => acc + buffer.length, 0);\n\n // Merge all new recorded buffers into a single buffer\n const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length);\n\n // Encode the merged buffer into a WAV format\n const encodedWav = this.encodeWAV(mergedBuffer, false);\n\n // Clear the new recorded buffers after they have been processed\n this.newRecBuffer.length = 0;\n\n // Post the message to the main thread\n // The first argument is the message data, containing the encoded WAV buffer\n // The second argument is the transfer list, which transfers ownership of the ArrayBuffer\n // to the main thread, avoiding the need to copy the buffer and improving performance\n this.port.postMessage({ recordedData: encodedWav.buffer }, [encodedWav.buffer]);\n }\n\n getAllRecordedData() {\n const length = this.recordedBuffers.reduce((acc, buffer) => acc + buffer.length, 0);\n const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length);\n\n\n // Calculate the duration based on the sample count and sample rate\n const sampleCount = mergedBuffer.length;\n const mergedBufferDuration = sampleCount / this.recordSampleRate;\n\n const encodedWav = this.encodeWAV(mergedBuffer, true);\n\n // Calculate and log the duration for encodedWav.buffer based on sample count\n const encodedWavBufferDuration = sampleCount / this.recordSampleRate;\n\n this.recordedBuffers.length = 0; // Clear the buffers after extraction\n\n // Returning both for testing, comment one of the returns based on your test\n console.debug('mergedBuffer:', mergedBuffer);\n console.debug('encodedWav.buffer:', encodedWav.buffer);\n\n // Uncomment the appropriate return for testing\n // return mergedBuffer; // This works when played\n return encodedWav.buffer; // This doesn't work when played\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor);\n";
2
+ export declare const InlineProcessorScrippt = "\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.recLength = 0;\n this.recBuffer = [];\n this.headerSent = false;\n this.newRecBuffer = [];\n this.exportIntervalSamples = 0;\n this.samplesSinceLastExport = 0;\n this.recordSampleRate = 44100; // To be overwrited\n this.exportSampleRate = 44100; // To be overwrited\n this.isRecording = true;\n\n this.port.onmessage = this.handleMessage.bind(this);\n }\n\n handleMessage(event) {\n switch (event.data.command) {\n case 'init':\n this.recordSampleRate = event.data.recordSampleRate;\n this.exportSampleRate = event.data.exportSampleRate || event.data.recordSampleRate;\n this.exportIntervalSamples = this.recordSampleRate * (event.data.interval / 1000);\n break;\n case 'stop':\n this.isRecording = false;\n break;\n case 'getRecordedData':\n const recordedData = this.getRecordedData();\n this.port.postMessage({ command: 'recordedData', recordedData });\n break;\n }\n }\n\n process(inputs) {\n if (!this.isRecording) {\n return true; // Exit early if not recording\n }\n\n if (inputs.length === 0 || inputs[0].length === 0) {\n console.warn('RecorderProcessor -- No input received.');\n return true; // Exit early if no input\n }\n\n const buffer = inputs[0];\n if (!buffer) {\n console.error('Input buffer is null.');\n return true; // Exit early if buffer is null\n }\n\n try {\n this.record(buffer);\n\n // this.samplesSinceLastExport += buffer.length;\n // if (this.samplesSinceLastExport >= this.exportIntervalSamples) {\n // this.exportBuffer();\n // this.samplesSinceLastExport = 0;\n // }\n } catch (error) {\n console.error('Error during processing:', error);\n }\n return true;\n }\n\n record(inputBuffer) {\n this.recBuffer.push(inputBuffer);\n this.newRecBuffer.push(inputBuffer);\n this.recLength += inputBuffer.length;\n }\n\n exportBuffer() {\n const mergedBuffers = this.mergeBuffers(this.newRecBuffer, this.newRecBuffer.reduce((len, buf) => len + buf.length, 0));\n console.log('Merged buffer length:', mergedBuffers.length); // Debug log\n\n const downsampledBuffer = this.downsampleBuffer(mergedBuffers, this.exportSampleRate);\n console.log('Downsampled buffer length:', downsampledBuffer.length); // Debug log\n\n const encodedWav = downsampledBuffer;\n // const encodedWav = this.encodeWAV(downsampledBuffer);\n // console.log('Encoded WAV length:', encodedWav.byteLength); // Debug log\n\n this.port.postMessage({ encodedWav, sampleRate: this.exportSampleRate });\n this.newRecBuffer = []; // Clear the new data buffer after export\n this.headerSent = true; // Indicate that the header has been sent\n }\n\n downsampleBuffer(buffer, exportSampleRate) {\n console.log('Original buffer length:', buffer.length); // Debug log\n console.log('Record sample rate:', this.recordSampleRate); // Debug log\n console.log('Export sample rate:', exportSampleRate); // Debug log\n if (exportSampleRate === this.recordSampleRate) {\n return buffer;\n }\n\n const sampleRateRatio = this.recordSampleRate / exportSampleRate;\n const newLength = Math.round(buffer.length / sampleRateRatio);\n console.log('Sample rate ratio:', sampleRateRatio); // Debug log\n console.log('New length after downsampling:', newLength); // Debug log\n \n if (newLength <= 0) {\n console.error('New length is zero or negative, returning empty buffer.'); // Debug log\n return new Float32Array(0);\n }\n const result = new Float32Array(newLength);\n let offsetResult = 0;\n let offsetBuffer = 0;\n while (offsetResult < result.length) {\n const nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);\n let accum = 0, count = 0;\n for (let i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {\n accum += buffer[i];\n count++;\n }\n result[offsetResult] = accum / count;\n offsetResult++;\n offsetBuffer = nextOffsetBuffer;\n }\n return result;\n }\n\n mergeBuffers(bufferArray, recLength) {\n const result = new Float32Array(recLength);\n let offset = 0;\n for (let i = 0; i < bufferArray.length; i++) {\n result.set(bufferArray[i], offset);\n offset += bufferArray[i].length;\n }\n return result;\n }\n\n floatTo16BitPCM(output, offset, input) {\n for (let i = 0; i < input.length; i++, offset += 2) {\n const s = Math.max(-1, Math.min(1, input[i]));\n output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);\n }\n }\n\n writeString(view, offset, string) {\n for (let i = 0; i < string.length; i++) {\n view.setUint8(offset + i, string.charCodeAt(i));\n }\n }\n\n getRecordedData() {\n const length = this.recBuffer.reduce((acc, buffer) => acc + buffer.length, 0);\n const result = new Float32Array(length);\n let offset = 0;\n for (const buffer of this.recBuffer) {\n result.set(buffer, offset);\n offset += buffer.length;\n }\n this.recBuffer.length = 0; // Clear the buffers after extraction\n\n return result;\n }\n\n encodeWAV(samples) {\n const buffer = new ArrayBuffer(44 + samples.length * 2);\n const view = new DataView(buffer);\n this.writeString(view, 0, 'RIFF');\n view.setUint32(4, 32 + samples.length * 2, true);\n this.writeString(view, 8, 'WAVE');\n this.writeString(view, 12, 'fmt ');\n view.setUint32(16, 16, true);\n view.setUint16(20, 1, true);\n view.setUint16(22, 1, true);\n view.setUint32(24, sampleRate, true);\n view.setUint32(28, sampleRate * 2, true);\n view.setUint16(32, 2, true);\n view.setUint16(34, 16, true);\n this.writeString(view, 36, 'data');\n view.setUint32(40, samples.length * 2, true);\n this.floatTo16BitPCM(view, 44, samples);\n return view;\n }\n}\n\nregisterProcessor('recorder-processor', RecorderProcessor);\n";
3
+ //# sourceMappingURL=inlineAudioWebWorker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inlineAudioWebWorker.d.ts","sourceRoot":"","sources":["../src/inlineAudioWebWorker.tsx"],"names":[],"mappings":"AAAA,eAAO,MAAM,2BAA2B,62MA8JvC,CAAC;AAGF,eAAO,MAAM,sBAAsB,89LAkLlC,CAAC"}