@siteed/expo-audio-stream 1.0.1 → 1.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 (142) hide show
  1. package/.size-limit.json +6 -0
  2. package/README.md +6 -6
  3. package/android/build.gradle +5 -0
  4. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +120 -0
  5. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +34 -4
  6. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +635 -0
  7. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +194 -79
  8. package/android/src/main/java/net/siteed/audiostream/Constants.kt +1 -0
  9. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +48 -2
  10. package/android/src/main/java/net/siteed/audiostream/FFT.kt +44 -0
  11. package/android/src/main/java/net/siteed/audiostream/Features.kt +56 -0
  12. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +12 -0
  13. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +56 -0
  14. package/app.plugin.js +1 -1
  15. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +76 -0
  16. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  17. package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
  18. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  19. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +4 -0
  20. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  21. package/build/AudioAnalysis/extractAudioAnalysis.js +101 -0
  22. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  23. package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
  24. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  25. package/build/AudioAnalysis/extractWaveform.js +14 -0
  26. package/build/AudioAnalysis/extractWaveform.js.map +1 -0
  27. package/build/AudioRecorder.provider.d.ts +14 -1
  28. package/build/AudioRecorder.provider.d.ts.map +1 -1
  29. package/build/AudioRecorder.provider.js +18 -5
  30. package/build/AudioRecorder.provider.js.map +1 -1
  31. package/build/ExpoAudioStream.native.d.ts +3 -0
  32. package/build/ExpoAudioStream.native.d.ts.map +1 -0
  33. package/build/ExpoAudioStream.native.js +6 -0
  34. package/build/ExpoAudioStream.native.js.map +1 -0
  35. package/build/ExpoAudioStream.types.d.ts +35 -20
  36. package/build/ExpoAudioStream.types.d.ts.map +1 -1
  37. package/build/ExpoAudioStream.types.js.map +1 -1
  38. package/build/ExpoAudioStream.web.d.ts +42 -0
  39. package/build/ExpoAudioStream.web.d.ts.map +1 -0
  40. package/build/ExpoAudioStream.web.js +185 -0
  41. package/build/ExpoAudioStream.web.js.map +1 -0
  42. package/build/ExpoAudioStreamModule.d.ts +2 -2
  43. package/build/ExpoAudioStreamModule.d.ts.map +1 -1
  44. package/build/ExpoAudioStreamModule.js +16 -3
  45. package/build/ExpoAudioStreamModule.js.map +1 -1
  46. package/build/WebRecorder.web.d.ts +51 -0
  47. package/build/WebRecorder.web.d.ts.map +1 -0
  48. package/build/WebRecorder.web.js +288 -0
  49. package/build/WebRecorder.web.js.map +1 -0
  50. package/build/constants.d.ts +11 -0
  51. package/build/constants.d.ts.map +1 -0
  52. package/build/constants.js +14 -0
  53. package/build/constants.js.map +1 -0
  54. package/build/events.d.ts +6 -0
  55. package/build/events.d.ts.map +1 -0
  56. package/build/events.js +15 -0
  57. package/build/events.js.map +1 -0
  58. package/build/index.d.ts +8 -7
  59. package/build/index.d.ts.map +1 -1
  60. package/build/index.js +7 -14
  61. package/build/index.js.map +1 -1
  62. package/build/logger.d.ts +9 -0
  63. package/build/logger.d.ts.map +1 -0
  64. package/build/logger.js +17 -0
  65. package/build/logger.js.map +1 -0
  66. package/build/useAudioRecorder.d.ts +37 -0
  67. package/build/useAudioRecorder.d.ts.map +1 -0
  68. package/build/useAudioRecorder.js +271 -0
  69. package/build/useAudioRecorder.js.map +1 -0
  70. package/build/utils/convertPCMToFloat32.d.ts +11 -0
  71. package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
  72. package/build/utils/convertPCMToFloat32.js +41 -0
  73. package/build/utils/convertPCMToFloat32.js.map +1 -0
  74. package/build/utils/encodingToBitDepth.d.ts +5 -0
  75. package/build/utils/encodingToBitDepth.d.ts.map +1 -0
  76. package/build/utils/encodingToBitDepth.js +13 -0
  77. package/build/utils/encodingToBitDepth.js.map +1 -0
  78. package/build/utils/getWavFileInfo.d.ts +25 -0
  79. package/build/utils/getWavFileInfo.d.ts.map +1 -0
  80. package/build/utils/getWavFileInfo.js +89 -0
  81. package/build/utils/getWavFileInfo.js.map +1 -0
  82. package/build/utils/writeWavHeader.d.ts +9 -0
  83. package/build/utils/writeWavHeader.d.ts.map +1 -0
  84. package/build/utils/writeWavHeader.js +41 -0
  85. package/build/utils/writeWavHeader.js.map +1 -0
  86. package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  87. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  88. package/build/workers/InlineFeaturesExtractor.web.js +303 -0
  89. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
  90. package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
  91. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  92. package/build/workers/inlineAudioWebWorker.web.js +243 -0
  93. package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
  94. package/expo-module.config.json +13 -4
  95. package/ios/AudioAnalysisData.swift +39 -0
  96. package/ios/AudioProcessingHelpers.swift +59 -0
  97. package/ios/AudioProcessor.swift +317 -0
  98. package/ios/AudioStreamError.swift +7 -0
  99. package/ios/AudioStreamManager.swift +243 -54
  100. package/ios/AudioStreamManagerDelegate.swift +4 -0
  101. package/ios/DataPoint.swift +41 -0
  102. package/ios/ExpoAudioStreamModule.swift +198 -6
  103. package/ios/Features.swift +44 -0
  104. package/ios/RecordingResult.swift +19 -0
  105. package/ios/RecordingSettings.swift +13 -0
  106. package/ios/WaveformExtractor.swift +105 -0
  107. package/package.json +13 -12
  108. package/plugin/tsconfig.json +13 -8
  109. package/publish.sh +8 -0
  110. package/src/AudioAnalysis/AudioAnalysis.types.ts +85 -0
  111. package/src/AudioAnalysis/extractAudioAnalysis.ts +136 -0
  112. package/src/AudioAnalysis/extractWaveform.ts +25 -0
  113. package/src/AudioRecorder.provider.tsx +36 -8
  114. package/src/ExpoAudioStream.native.ts +6 -0
  115. package/src/ExpoAudioStream.types.ts +50 -25
  116. package/src/ExpoAudioStream.web.ts +229 -0
  117. package/src/ExpoAudioStreamModule.ts +22 -3
  118. package/src/WebRecorder.web.ts +416 -0
  119. package/src/constants.ts +18 -0
  120. package/src/events.ts +25 -0
  121. package/src/index.ts +14 -29
  122. package/src/logger.ts +26 -0
  123. package/src/useAudioRecorder.tsx +415 -0
  124. package/src/utils/convertPCMToFloat32.ts +48 -0
  125. package/src/utils/encodingToBitDepth.ts +18 -0
  126. package/src/utils/getWavFileInfo.ts +125 -0
  127. package/src/utils/writeWavHeader.ts +56 -0
  128. package/src/workers/InlineFeaturesExtractor.web.tsx +302 -0
  129. package/src/workers/inlineAudioWebWorker.web.tsx +242 -0
  130. package/build/ExpoAudioStreamModule.web.d.ts +0 -37
  131. package/build/ExpoAudioStreamModule.web.d.ts.map +0 -1
  132. package/build/ExpoAudioStreamModule.web.js +0 -156
  133. package/build/ExpoAudioStreamModule.web.js.map +0 -1
  134. package/build/useAudioRecording.d.ts +0 -23
  135. package/build/useAudioRecording.d.ts.map +0 -1
  136. package/build/useAudioRecording.js +0 -189
  137. package/build/useAudioRecording.js.map +0 -1
  138. package/docs/demo.gif +0 -0
  139. package/release-it.js +0 -18
  140. package/src/ExpoAudioStreamModule.web.ts +0 -181
  141. package/src/useAudioRecording.ts +0 -268
  142. package/yarn-error.log +0 -7793
@@ -0,0 +1,2 @@
1
+ export declare const InlineAudioWebWorker = "\nconst DEFAULT_BIT_DEPTH = 32;\nconst DEFAULT_SAMPLE_RATE = 44100;\n\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.recordedBuffers = []; // Float32Array\n this.newRecBuffer = []; // Float32Array\n this.resampledBuffer = []; // Float32Array\n this.exportIntervalSamples = 0;\n this.samplesSinceLastExport = 0;\n this.recordSampleRate = DEFAULT_SAMPLE_RATE; // To be overwritten\n this.exportSampleRate = DEFAULT_SAMPLE_RATE; // To be overwritten\n this.recordBitDepth = DEFAULT_BIT_DEPTH; // Default to 32-bit depth\n this.exportBitDepth = DEFAULT_BIT_DEPTH; // To be overwritten\n this.numberOfChannels = 1; // Default to 1 channel (mono)\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 =\n event.data.exportSampleRate || event.data.recordSampleRate;\n this.exportIntervalSamples =\n this.recordSampleRate * (event.data.interval / 1000);\n if (event.data.numberOfChannels) {\n this.numberOfChannels = event.data.numberOfChannels;\n }\n if (event.data.recordBitDepth) {\n this.recordBitDepth = event.data.recordBitDepth;\n }\n this.exportBitDepth =\n event.data.exportBitDepth || this.recordBitDepth || DEFAULT_BIT_DEPTH;\n break;\n case \"stop\":\n this.isRecording = false;\n this.getAllRecordedData()\n .then((fullRecordedData) => {\n this.port.postMessage({\n command: \"recordedData\",\n recordedData: fullRecordedData,\n bitDepth: this.exportBitDepth,\n sampleRate: this.exportSampleRate,\n });\n return fullRecordedData;\n })\n .catch((error) => {\n console.error(\"Error extracting recorded data:\", error);\n });\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(input) {\n const output = new Int16Array(input.length);\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]));\n output[i] = s < 0 ? s * 0x8000 : s * 0x7fff;\n }\n console.debug(\n \"Float to 16-bit PCM conversion complete. Output byte length:\",\n output.byteLength,\n );\n return output;\n }\n\n floatTo32BitPCM(input) {\n const output = new Int32Array(input.length);\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]));\n output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff;\n }\n console.debug(\n \"Float to 32-bit PCM conversion complete. Output byte length:\",\n output.byteLength,\n );\n return output;\n }\n\n resample(samples, targetSampleRate) {\n if (this.recordSampleRate === targetSampleRate) {\n return samples;\n }\n const resampledBuffer = new Float32Array(\n (samples.length * targetSampleRate) / this.recordSampleRate,\n );\n const ratio = this.recordSampleRate / targetSampleRate;\n let offset = 0;\n for (let i = 0; i < resampledBuffer.length; i++) {\n const nextOffset = Math.floor((i + 1) * ratio);\n let accum = 0;\n let count = 0;\n for (let j = offset; j < nextOffset && j < samples.length; j++) {\n accum += samples[j];\n count++;\n }\n resampledBuffer[i] = accum / count;\n offset = nextOffset;\n }\n return resampledBuffer;\n }\n\n async resampleBuffer(buffer, targetSampleRate) {\n if (typeof OfflineAudioContext === \"undefined\") {\n return this.resample(buffer, targetSampleRate);\n }\n\n if (this.recordSampleRate === targetSampleRate) {\n return buffer;\n }\n const offlineContext = new OfflineAudioContext(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate,\n );\n const sourceBuffer = offlineContext.createBuffer(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate,\n );\n sourceBuffer.copyToChannel(buffer, 0);\n\n const bufferSource = offlineContext.createBufferSource();\n bufferSource.buffer = sourceBuffer;\n bufferSource.connect(offlineContext.destination);\n bufferSource.start();\n\n const renderedBuffer = await offlineContext.startRendering();\n\n const resampledBuffer = new Float32Array(renderedBuffer.length);\n renderedBuffer.copyFromChannel(resampledBuffer, 0);\n\n return resampledBuffer;\n }\n\n async exportNewData() {\n // Calculate the total length of the new recorded buffers\n const length = this.newRecBuffer.reduce(\n (acc, buffer) => acc + buffer.length,\n 0,\n );\n\n // Merge all new recorded buffers into a single buffer\n const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length);\n\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate,\n );\n\n let finalBuffer = resampledBuffer; // Float32Array\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer);\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer);\n }\n }\n\n const originalSize = mergedBuffer.byteLength;\n const resampledSize = resampledBuffer.byteLength;\n const finalSize = finalBuffer.byteLength;\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, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);\n this.port.postMessage({\n command: \"newData\",\n recordedData: finalBuffer.buffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\n });\n }\n\n async getAllRecordedData() {\n const length = this.recordedBuffers.reduce(\n (acc, buffer) => acc + buffer.length,\n 0,\n );\n const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length);\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate,\n );\n // Convert to the desired bit depth if necessary\n let finalBuffer = resampledBuffer;\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer);\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer);\n }\n }\n\n const originalSize = mergedBuffer.byteLength;\n const resampledSize = resampledBuffer.byteLength;\n const finalSize = finalBuffer.byteLength;\n\n this.recordedBuffers.length = 0; // Clear the buffers after extraction\n\n return finalBuffer.buffer;\n }\n}\n\nregisterProcessor(\"recorder-processor\", RecorderProcessor);\n";
2
+ //# sourceMappingURL=inlineAudioWebWorker.web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inlineAudioWebWorker.web.d.ts","sourceRoot":"","sources":["../../src/workers/inlineAudioWebWorker.web.tsx"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,4/PAiPhC,CAAC"}
@@ -0,0 +1,243 @@
1
+ export const InlineAudioWebWorker = `
2
+ const DEFAULT_BIT_DEPTH = 32;
3
+ const DEFAULT_SAMPLE_RATE = 44100;
4
+
5
+ class RecorderProcessor extends AudioWorkletProcessor {
6
+ constructor() {
7
+ super();
8
+ this.recordedBuffers = []; // Float32Array
9
+ this.newRecBuffer = []; // Float32Array
10
+ this.resampledBuffer = []; // Float32Array
11
+ this.exportIntervalSamples = 0;
12
+ this.samplesSinceLastExport = 0;
13
+ this.recordSampleRate = DEFAULT_SAMPLE_RATE; // To be overwritten
14
+ this.exportSampleRate = DEFAULT_SAMPLE_RATE; // To be overwritten
15
+ this.recordBitDepth = DEFAULT_BIT_DEPTH; // Default to 32-bit depth
16
+ this.exportBitDepth = DEFAULT_BIT_DEPTH; // To be overwritten
17
+ this.numberOfChannels = 1; // Default to 1 channel (mono)
18
+ this.isRecording = true;
19
+ this.port.onmessage = this.handleMessage.bind(this);
20
+ }
21
+
22
+ handleMessage(event) {
23
+ switch (event.data.command) {
24
+ case "init":
25
+ this.recordSampleRate = event.data.recordSampleRate;
26
+ this.exportSampleRate =
27
+ event.data.exportSampleRate || event.data.recordSampleRate;
28
+ this.exportIntervalSamples =
29
+ this.recordSampleRate * (event.data.interval / 1000);
30
+ if (event.data.numberOfChannels) {
31
+ this.numberOfChannels = event.data.numberOfChannels;
32
+ }
33
+ if (event.data.recordBitDepth) {
34
+ this.recordBitDepth = event.data.recordBitDepth;
35
+ }
36
+ this.exportBitDepth =
37
+ event.data.exportBitDepth || this.recordBitDepth || DEFAULT_BIT_DEPTH;
38
+ break;
39
+ case "stop":
40
+ this.isRecording = false;
41
+ this.getAllRecordedData()
42
+ .then((fullRecordedData) => {
43
+ this.port.postMessage({
44
+ command: "recordedData",
45
+ recordedData: fullRecordedData,
46
+ bitDepth: this.exportBitDepth,
47
+ sampleRate: this.exportSampleRate,
48
+ });
49
+ return fullRecordedData;
50
+ })
51
+ .catch((error) => {
52
+ console.error("Error extracting recorded data:", error);
53
+ });
54
+ break;
55
+ }
56
+ }
57
+
58
+ process(inputs, outputs, parameters) {
59
+ if (!this.isRecording) return true;
60
+ const input = inputs[0];
61
+ if (input.length > 0) {
62
+ const newBuffer = new Float32Array(input[0]);
63
+ this.newRecBuffer.push(newBuffer);
64
+ this.recordedBuffers.push(newBuffer);
65
+ this.samplesSinceLastExport += newBuffer.length;
66
+
67
+ if (this.samplesSinceLastExport >= this.exportIntervalSamples) {
68
+ this.exportNewData();
69
+ this.samplesSinceLastExport = 0;
70
+ }
71
+ }
72
+ return true;
73
+ }
74
+
75
+ mergeBuffers(bufferArray, recLength) {
76
+ const result = new Float32Array(recLength);
77
+ let offset = 0;
78
+ for (let i = 0; i < bufferArray.length; i++) {
79
+ result.set(bufferArray[i], offset);
80
+ offset += bufferArray[i].length;
81
+ }
82
+ return result;
83
+ }
84
+
85
+ floatTo16BitPCM(input) {
86
+ const output = new Int16Array(input.length);
87
+ for (let i = 0; i < input.length; i++) {
88
+ const s = Math.max(-1, Math.min(1, input[i]));
89
+ output[i] = s < 0 ? s * 0x8000 : s * 0x7fff;
90
+ }
91
+ console.debug(
92
+ "Float to 16-bit PCM conversion complete. Output byte length:",
93
+ output.byteLength,
94
+ );
95
+ return output;
96
+ }
97
+
98
+ floatTo32BitPCM(input) {
99
+ const output = new Int32Array(input.length);
100
+ for (let i = 0; i < input.length; i++) {
101
+ const s = Math.max(-1, Math.min(1, input[i]));
102
+ output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff;
103
+ }
104
+ console.debug(
105
+ "Float to 32-bit PCM conversion complete. Output byte length:",
106
+ output.byteLength,
107
+ );
108
+ return output;
109
+ }
110
+
111
+ resample(samples, targetSampleRate) {
112
+ if (this.recordSampleRate === targetSampleRate) {
113
+ return samples;
114
+ }
115
+ const resampledBuffer = new Float32Array(
116
+ (samples.length * targetSampleRate) / this.recordSampleRate,
117
+ );
118
+ const ratio = this.recordSampleRate / targetSampleRate;
119
+ let offset = 0;
120
+ for (let i = 0; i < resampledBuffer.length; i++) {
121
+ const nextOffset = Math.floor((i + 1) * ratio);
122
+ let accum = 0;
123
+ let count = 0;
124
+ for (let j = offset; j < nextOffset && j < samples.length; j++) {
125
+ accum += samples[j];
126
+ count++;
127
+ }
128
+ resampledBuffer[i] = accum / count;
129
+ offset = nextOffset;
130
+ }
131
+ return resampledBuffer;
132
+ }
133
+
134
+ async resampleBuffer(buffer, targetSampleRate) {
135
+ if (typeof OfflineAudioContext === "undefined") {
136
+ return this.resample(buffer, targetSampleRate);
137
+ }
138
+
139
+ if (this.recordSampleRate === targetSampleRate) {
140
+ return buffer;
141
+ }
142
+ const offlineContext = new OfflineAudioContext(
143
+ this.numberOfChannels,
144
+ buffer.length,
145
+ this.recordSampleRate,
146
+ );
147
+ const sourceBuffer = offlineContext.createBuffer(
148
+ this.numberOfChannels,
149
+ buffer.length,
150
+ this.recordSampleRate,
151
+ );
152
+ sourceBuffer.copyToChannel(buffer, 0);
153
+
154
+ const bufferSource = offlineContext.createBufferSource();
155
+ bufferSource.buffer = sourceBuffer;
156
+ bufferSource.connect(offlineContext.destination);
157
+ bufferSource.start();
158
+
159
+ const renderedBuffer = await offlineContext.startRendering();
160
+
161
+ const resampledBuffer = new Float32Array(renderedBuffer.length);
162
+ renderedBuffer.copyFromChannel(resampledBuffer, 0);
163
+
164
+ return resampledBuffer;
165
+ }
166
+
167
+ async exportNewData() {
168
+ // Calculate the total length of the new recorded buffers
169
+ const length = this.newRecBuffer.reduce(
170
+ (acc, buffer) => acc + buffer.length,
171
+ 0,
172
+ );
173
+
174
+ // Merge all new recorded buffers into a single buffer
175
+ const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length);
176
+
177
+ const resampledBuffer = await this.resampleBuffer(
178
+ mergedBuffer,
179
+ this.exportSampleRate,
180
+ );
181
+
182
+ let finalBuffer = resampledBuffer; // Float32Array
183
+ if (this.recordBitDepth !== this.exportBitDepth) {
184
+ if (this.exportBitDepth === 16) {
185
+ finalBuffer = this.floatTo16BitPCM(resampledBuffer);
186
+ } else if (this.exportBitDepth === 32) {
187
+ finalBuffer = this.floatTo32BitPCM(resampledBuffer);
188
+ }
189
+ }
190
+
191
+ const originalSize = mergedBuffer.byteLength;
192
+ const resampledSize = resampledBuffer.byteLength;
193
+ const finalSize = finalBuffer.byteLength;
194
+
195
+ // Clear the new recorded buffers after they have been processed
196
+ this.newRecBuffer.length = 0;
197
+
198
+ // Post the message to the main thread
199
+ // The first argument is the message data, containing the encoded WAV buffer
200
+ // The second argument is the transfer list, which transfers ownership of the ArrayBuffer
201
+ // to the main thread, avoiding the need to copy the buffer and improving performance
202
+ // this.port.postMessage({ recordedData: encodedWav.buffer, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);
203
+ this.port.postMessage({
204
+ command: "newData",
205
+ recordedData: finalBuffer.buffer,
206
+ sampleRate: this.exportSampleRate,
207
+ bitDepth: this.exportBitDepth,
208
+ });
209
+ }
210
+
211
+ async getAllRecordedData() {
212
+ const length = this.recordedBuffers.reduce(
213
+ (acc, buffer) => acc + buffer.length,
214
+ 0,
215
+ );
216
+ const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length);
217
+ const resampledBuffer = await this.resampleBuffer(
218
+ mergedBuffer,
219
+ this.exportSampleRate,
220
+ );
221
+ // Convert to the desired bit depth if necessary
222
+ let finalBuffer = resampledBuffer;
223
+ if (this.recordBitDepth !== this.exportBitDepth) {
224
+ if (this.exportBitDepth === 16) {
225
+ finalBuffer = this.floatTo16BitPCM(resampledBuffer);
226
+ } else if (this.exportBitDepth === 32) {
227
+ finalBuffer = this.floatTo32BitPCM(resampledBuffer);
228
+ }
229
+ }
230
+
231
+ const originalSize = mergedBuffer.byteLength;
232
+ const resampledSize = resampledBuffer.byteLength;
233
+ const finalSize = finalBuffer.byteLength;
234
+
235
+ this.recordedBuffers.length = 0; // Clear the buffers after extraction
236
+
237
+ return finalBuffer.buffer;
238
+ }
239
+ }
240
+
241
+ registerProcessor("recorder-processor", RecorderProcessor);
242
+ `;
243
+ //# sourceMappingURL=inlineAudioWebWorker.web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inlineAudioWebWorker.web.js","sourceRoot":"","sources":["../../src/workers/inlineAudioWebWorker.web.tsx"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiPnC,CAAC","sourcesContent":["export const InlineAudioWebWorker = `\nconst DEFAULT_BIT_DEPTH = 32;\nconst DEFAULT_SAMPLE_RATE = 44100;\n\nclass RecorderProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.recordedBuffers = []; // Float32Array\n this.newRecBuffer = []; // Float32Array\n this.resampledBuffer = []; // Float32Array\n this.exportIntervalSamples = 0;\n this.samplesSinceLastExport = 0;\n this.recordSampleRate = DEFAULT_SAMPLE_RATE; // To be overwritten\n this.exportSampleRate = DEFAULT_SAMPLE_RATE; // To be overwritten\n this.recordBitDepth = DEFAULT_BIT_DEPTH; // Default to 32-bit depth\n this.exportBitDepth = DEFAULT_BIT_DEPTH; // To be overwritten\n this.numberOfChannels = 1; // Default to 1 channel (mono)\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 =\n event.data.exportSampleRate || event.data.recordSampleRate;\n this.exportIntervalSamples =\n this.recordSampleRate * (event.data.interval / 1000);\n if (event.data.numberOfChannels) {\n this.numberOfChannels = event.data.numberOfChannels;\n }\n if (event.data.recordBitDepth) {\n this.recordBitDepth = event.data.recordBitDepth;\n }\n this.exportBitDepth =\n event.data.exportBitDepth || this.recordBitDepth || DEFAULT_BIT_DEPTH;\n break;\n case \"stop\":\n this.isRecording = false;\n this.getAllRecordedData()\n .then((fullRecordedData) => {\n this.port.postMessage({\n command: \"recordedData\",\n recordedData: fullRecordedData,\n bitDepth: this.exportBitDepth,\n sampleRate: this.exportSampleRate,\n });\n return fullRecordedData;\n })\n .catch((error) => {\n console.error(\"Error extracting recorded data:\", error);\n });\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(input) {\n const output = new Int16Array(input.length);\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]));\n output[i] = s < 0 ? s * 0x8000 : s * 0x7fff;\n }\n console.debug(\n \"Float to 16-bit PCM conversion complete. Output byte length:\",\n output.byteLength,\n );\n return output;\n }\n\n floatTo32BitPCM(input) {\n const output = new Int32Array(input.length);\n for (let i = 0; i < input.length; i++) {\n const s = Math.max(-1, Math.min(1, input[i]));\n output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff;\n }\n console.debug(\n \"Float to 32-bit PCM conversion complete. Output byte length:\",\n output.byteLength,\n );\n return output;\n }\n\n resample(samples, targetSampleRate) {\n if (this.recordSampleRate === targetSampleRate) {\n return samples;\n }\n const resampledBuffer = new Float32Array(\n (samples.length * targetSampleRate) / this.recordSampleRate,\n );\n const ratio = this.recordSampleRate / targetSampleRate;\n let offset = 0;\n for (let i = 0; i < resampledBuffer.length; i++) {\n const nextOffset = Math.floor((i + 1) * ratio);\n let accum = 0;\n let count = 0;\n for (let j = offset; j < nextOffset && j < samples.length; j++) {\n accum += samples[j];\n count++;\n }\n resampledBuffer[i] = accum / count;\n offset = nextOffset;\n }\n return resampledBuffer;\n }\n\n async resampleBuffer(buffer, targetSampleRate) {\n if (typeof OfflineAudioContext === \"undefined\") {\n return this.resample(buffer, targetSampleRate);\n }\n\n if (this.recordSampleRate === targetSampleRate) {\n return buffer;\n }\n const offlineContext = new OfflineAudioContext(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate,\n );\n const sourceBuffer = offlineContext.createBuffer(\n this.numberOfChannels,\n buffer.length,\n this.recordSampleRate,\n );\n sourceBuffer.copyToChannel(buffer, 0);\n\n const bufferSource = offlineContext.createBufferSource();\n bufferSource.buffer = sourceBuffer;\n bufferSource.connect(offlineContext.destination);\n bufferSource.start();\n\n const renderedBuffer = await offlineContext.startRendering();\n\n const resampledBuffer = new Float32Array(renderedBuffer.length);\n renderedBuffer.copyFromChannel(resampledBuffer, 0);\n\n return resampledBuffer;\n }\n\n async exportNewData() {\n // Calculate the total length of the new recorded buffers\n const length = this.newRecBuffer.reduce(\n (acc, buffer) => acc + buffer.length,\n 0,\n );\n\n // Merge all new recorded buffers into a single buffer\n const mergedBuffer = this.mergeBuffers(this.newRecBuffer, length);\n\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate,\n );\n\n let finalBuffer = resampledBuffer; // Float32Array\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer);\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer);\n }\n }\n\n const originalSize = mergedBuffer.byteLength;\n const resampledSize = resampledBuffer.byteLength;\n const finalSize = finalBuffer.byteLength;\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, sampleRate: this.recordSampleRate }, [encodedWav.buffer]);\n this.port.postMessage({\n command: \"newData\",\n recordedData: finalBuffer.buffer,\n sampleRate: this.exportSampleRate,\n bitDepth: this.exportBitDepth,\n });\n }\n\n async getAllRecordedData() {\n const length = this.recordedBuffers.reduce(\n (acc, buffer) => acc + buffer.length,\n 0,\n );\n const mergedBuffer = this.mergeBuffers(this.recordedBuffers, length);\n const resampledBuffer = await this.resampleBuffer(\n mergedBuffer,\n this.exportSampleRate,\n );\n // Convert to the desired bit depth if necessary\n let finalBuffer = resampledBuffer;\n if (this.recordBitDepth !== this.exportBitDepth) {\n if (this.exportBitDepth === 16) {\n finalBuffer = this.floatTo16BitPCM(resampledBuffer);\n } else if (this.exportBitDepth === 32) {\n finalBuffer = this.floatTo32BitPCM(resampledBuffer);\n }\n }\n\n const originalSize = mergedBuffer.byteLength;\n const resampledSize = resampledBuffer.byteLength;\n const finalSize = finalBuffer.byteLength;\n\n this.recordedBuffers.length = 0; // Clear the buffers after extraction\n\n return finalBuffer.buffer;\n }\n}\n\nregisterProcessor(\"recorder-processor\", RecorderProcessor);\n`;\n"]}
@@ -1,9 +1,18 @@
1
1
  {
2
- "platforms": ["ios", "tvos", "android", "web"],
2
+ "platforms": [
3
+ "ios",
4
+ "tvos",
5
+ "android",
6
+ "web"
7
+ ],
3
8
  "ios": {
4
- "modules": ["ExpoAudioStreamModule"]
9
+ "modules": [
10
+ "ExpoAudioStreamModule"
11
+ ]
5
12
  },
6
13
  "android": {
7
- "modules": ["net.siteed.audiostream.ExpoAudioStreamModule"]
14
+ "modules": [
15
+ "net.siteed.audiostream.ExpoAudioStreamModule"
16
+ ]
8
17
  }
9
- }
18
+ }
@@ -0,0 +1,39 @@
1
+ //
2
+ // AudioAnalysisData.swift
3
+ // ExpoAudioStream
4
+ //
5
+ // Created by Arthur Breton on 23/6/2024.
6
+ //
7
+
8
+ import Foundation
9
+
10
+ public struct AudioAnalysisData {
11
+ public var pointsPerSecond: Int
12
+ public var durationMs: Float
13
+ public var bitDepth: Int
14
+ public var numberOfChannels: Int
15
+ public var sampleRate: Float
16
+ public var samples: Int
17
+ public var dataPoints: [DataPoint]
18
+ public var amplitudeRange: (min: Float, max: Float)
19
+ public var speakerChanges: [(timestamp: Float, speaker: Int)]?
20
+ public var extractionTimeMs: Float
21
+ }
22
+
23
+ extension AudioAnalysisData {
24
+ func toDictionary() -> [String: Any] {
25
+ let dataPointsArray = dataPoints.map { $0.toDictionary() }
26
+ return [
27
+ "pointsPerSecond": pointsPerSecond,
28
+ "durationMs": durationMs,
29
+ "bitDepth": bitDepth,
30
+ "numberOfChannels": numberOfChannels,
31
+ "sampleRate": sampleRate,
32
+ "samples": samples,
33
+ "dataPoints": dataPointsArray,
34
+ "amplitudeRange": ["min": amplitudeRange.min, "max": amplitudeRange.max],
35
+ "speakerChanges": speakerChanges?.map { ["timestamp": $0.timestamp, "speaker": $0.speaker] } ?? [],
36
+ "extractionTimeMs": extractionTimeMs
37
+ ]
38
+ }
39
+ }
@@ -0,0 +1,59 @@
1
+ // AudioProcessingHelpers.swift
2
+
3
+ import Accelerate
4
+
5
+ func extractMFCC(from segment: [Float], sampleRate: Float) -> [Float] {
6
+ // Placeholder for MFCC extraction logic
7
+ return []
8
+ }
9
+
10
+ func extractSpectralCentroid(from segment: [Float], sampleRate: Float) -> Float {
11
+ Logger.debug("Extracting Spectral Centroid from segment of length \(segment.count)")
12
+ return 0.0 // TODO: Implement spectral centroid extraction logic
13
+ }
14
+
15
+ func extractSpectralFlatness(from segment: [Float]) -> Float {
16
+ Logger.debug("Extracting Spectral Flatness from segment of length \(segment.count)")
17
+
18
+ var mean: Float = 0.0
19
+ var geometricMean: Float = 1.0
20
+ let count = vDSP_Length(segment.count)
21
+
22
+ vDSP_meamgv(segment, 1, &mean, count)
23
+
24
+ var sumLogValues: Float = 0.0
25
+ for value in segment {
26
+ let adjustedValue = max(value, 1e-10)
27
+ sumLogValues += log(adjustedValue)
28
+ }
29
+ geometricMean = exp(sumLogValues / Float(count))
30
+
31
+ let spectralFlatness = mean > 0 ? geometricMean / mean : 0.0
32
+ Logger.debug("Spectral Flatness: \(spectralFlatness)")
33
+ return spectralFlatness
34
+ }
35
+
36
+ func extractSpectralRollOff(from segment: [Float], sampleRate: Float) -> Float {
37
+ // Implement spectral roll-off extraction logic
38
+ return 0.0
39
+ }
40
+
41
+ func extractSpectralBandwidth(from segment: [Float], sampleRate: Float) -> Float {
42
+ // Implement spectral bandwidth extraction logic
43
+ return 0.0
44
+ }
45
+
46
+ func extractChromagram(from segment: [Float], sampleRate: Float) -> [Float] {
47
+ // Implement chromagram extraction logic
48
+ return []
49
+ }
50
+
51
+ func extractTempo(from segment: [Float], sampleRate: Float) -> Float {
52
+ // Implement tempo extraction logic
53
+ return 0.0
54
+ }
55
+
56
+ func extractHNR(from segment: [Float]) -> Float {
57
+ // Implement harmonic-to-noise ratio extraction logic
58
+ return 0.0
59
+ }