@siteed/audio-studio 3.0.0

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 (375) hide show
  1. package/CHANGELOG.md +535 -0
  2. package/LICENSE +21 -0
  3. package/README.md +167 -0
  4. package/android/build.gradle +143 -0
  5. package/android/src/androidTest/assets/chorus.wav +0 -0
  6. package/android/src/androidTest/assets/jfk.wav +0 -0
  7. package/android/src/androidTest/assets/osr_us_000_0010_8k.wav +0 -0
  8. package/android/src/androidTest/assets/recorder_hello_world.wav +0 -0
  9. package/android/src/androidTest/java/net/siteed/audiostudio/AudioProcessorInstrumentedTest.kt +197 -0
  10. package/android/src/androidTest/java/net/siteed/audiostudio/AudioRecorderInstrumentedTest.kt +541 -0
  11. package/android/src/androidTest/java/net/siteed/audiostudio/AudioRecorderPerformanceInstrumentedTest.kt +234 -0
  12. package/android/src/androidTest/java/net/siteed/audiostudio/integration/AudioFocusStrategyIntegrationTest.kt +332 -0
  13. package/android/src/androidTest/java/net/siteed/audiostudio/integration/BufferDurationIntegrationTest.kt +324 -0
  14. package/android/src/androidTest/java/net/siteed/audiostudio/integration/CompressedOnlyOutputTest.kt +253 -0
  15. package/android/src/androidTest/java/net/siteed/audiostudio/integration/DeviceDisconnectionFallbackTest.kt +218 -0
  16. package/android/src/androidTest/java/net/siteed/audiostudio/integration/EventEmissionIntervalTest.kt +120 -0
  17. package/android/src/androidTest/java/net/siteed/audiostudio/integration/M4aFormatTest.kt +345 -0
  18. package/android/src/androidTest/java/net/siteed/audiostudio/integration/OutputControlIntegrationTest.kt +340 -0
  19. package/android/src/androidTest/java/net/siteed/audiostudio/integration/PcmStreamingDurationTest.kt +252 -0
  20. package/android/src/androidTest/java/net/siteed/audiostudio/integration/README.md +95 -0
  21. package/android/src/androidTest/java/net/siteed/audiostudio/integration/run_integration_tests.sh +43 -0
  22. package/android/src/main/AndroidManifest.xml +30 -0
  23. package/android/src/main/CMakeLists.txt +29 -0
  24. package/android/src/main/java/net/siteed/audiostudio/AudioAnalysisData.kt +188 -0
  25. package/android/src/main/java/net/siteed/audiostudio/AudioDataEncoder.kt +9 -0
  26. package/android/src/main/java/net/siteed/audiostudio/AudioDeviceManager.kt +1741 -0
  27. package/android/src/main/java/net/siteed/audiostudio/AudioFeaturesNative.kt +26 -0
  28. package/android/src/main/java/net/siteed/audiostudio/AudioFileHandler.kt +136 -0
  29. package/android/src/main/java/net/siteed/audiostudio/AudioFormatUtils.kt +354 -0
  30. package/android/src/main/java/net/siteed/audiostudio/AudioNotificationsManager.kt +439 -0
  31. package/android/src/main/java/net/siteed/audiostudio/AudioProcessor.kt +2237 -0
  32. package/android/src/main/java/net/siteed/audiostudio/AudioRecorderManager.kt +2163 -0
  33. package/android/src/main/java/net/siteed/audiostudio/AudioRecordingService.kt +167 -0
  34. package/android/src/main/java/net/siteed/audiostudio/AudioStudioModule.kt +1112 -0
  35. package/android/src/main/java/net/siteed/audiostudio/AudioTrimmer.kt +1099 -0
  36. package/android/src/main/java/net/siteed/audiostudio/Constants.kt +37 -0
  37. package/android/src/main/java/net/siteed/audiostudio/EventSender.kt +7 -0
  38. package/android/src/main/java/net/siteed/audiostudio/FFT.kt +100 -0
  39. package/android/src/main/java/net/siteed/audiostudio/Features.kt +98 -0
  40. package/android/src/main/java/net/siteed/audiostudio/LogUtils.kt +93 -0
  41. package/android/src/main/java/net/siteed/audiostudio/MelSpectrogramNative.kt +36 -0
  42. package/android/src/main/java/net/siteed/audiostudio/NotificationConfig.kt +72 -0
  43. package/android/src/main/java/net/siteed/audiostudio/PermissionUtils.kt +68 -0
  44. package/android/src/main/java/net/siteed/audiostudio/RecordingActionReceiver.kt +59 -0
  45. package/android/src/main/java/net/siteed/audiostudio/RecordingConfig.kt +259 -0
  46. package/android/src/main/java/net/siteed/audiostudio/WaveformConfig.kt +19 -0
  47. package/android/src/main/java/net/siteed/audiostudio/WaveformRenderer.kt +159 -0
  48. package/android/src/main/jni/AudioFeaturesJNI.cpp +152 -0
  49. package/android/src/main/jni/MelSpectrogramJNI.cpp +165 -0
  50. package/android/src/main/res/drawable/ic_default_action_icon.xml +16 -0
  51. package/android/src/main/res/drawable/ic_microphone.xml +13 -0
  52. package/android/src/main/res/drawable/ic_pause.xml +10 -0
  53. package/android/src/main/res/drawable/ic_play.xml +10 -0
  54. package/android/src/main/res/drawable/ic_stop.xml +10 -0
  55. package/android/src/main/res/layout/notification_recording.xml +37 -0
  56. package/android/src/test/java/net/siteed/audiostudio/AudioFileHandlerTest.kt +279 -0
  57. package/android/src/test/java/net/siteed/audiostudio/AudioFocusStrategyTest.kt +249 -0
  58. package/android/src/test/java/net/siteed/audiostudio/AudioFormatTest.kt +151 -0
  59. package/android/src/test/java/net/siteed/audiostudio/AudioFormatUtilsTest.kt +273 -0
  60. package/android/src/test/java/net/siteed/audiostudio/DeviceDisconnectionFallbackUnitTest.kt +140 -0
  61. package/android/src/test/resources/chorus.wav +0 -0
  62. package/android/src/test/resources/generate_test_audio.py +94 -0
  63. package/android/src/test/resources/jfk.wav +0 -0
  64. package/android/src/test/resources/osr_us_000_0010_8k.wav +0 -0
  65. package/android/src/test/resources/recorder_hello_world.wav +0 -0
  66. package/app.plugin.js +3 -0
  67. package/build/cjs/AudioAnalysis/AudioAnalysis.types.js +4 -0
  68. package/build/cjs/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  69. package/build/cjs/AudioAnalysis/audioFeaturesWasm.js +164 -0
  70. package/build/cjs/AudioAnalysis/audioFeaturesWasm.js.map +1 -0
  71. package/build/cjs/AudioAnalysis/extractAudioAnalysis.js +213 -0
  72. package/build/cjs/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  73. package/build/cjs/AudioAnalysis/extractAudioData.js +21 -0
  74. package/build/cjs/AudioAnalysis/extractAudioData.js.map +1 -0
  75. package/build/cjs/AudioAnalysis/extractMelSpectrogram.js +90 -0
  76. package/build/cjs/AudioAnalysis/extractMelSpectrogram.js.map +1 -0
  77. package/build/cjs/AudioAnalysis/extractPreview.js +28 -0
  78. package/build/cjs/AudioAnalysis/extractPreview.js.map +1 -0
  79. package/build/cjs/AudioAnalysis/extractWaveform.js +18 -0
  80. package/build/cjs/AudioAnalysis/extractWaveform.js.map +1 -0
  81. package/build/cjs/AudioAnalysis/melSpectrogramWasm.js +149 -0
  82. package/build/cjs/AudioAnalysis/melSpectrogramWasm.js.map +1 -0
  83. package/build/cjs/AudioDeviceManager.js +688 -0
  84. package/build/cjs/AudioDeviceManager.js.map +1 -0
  85. package/build/cjs/AudioRecorder.provider.js +78 -0
  86. package/build/cjs/AudioRecorder.provider.js.map +1 -0
  87. package/build/cjs/AudioStudio.native.js +8 -0
  88. package/build/cjs/AudioStudio.native.js.map +1 -0
  89. package/build/cjs/AudioStudio.types.js +11 -0
  90. package/build/cjs/AudioStudio.types.js.map +1 -0
  91. package/build/cjs/AudioStudio.web.js +708 -0
  92. package/build/cjs/AudioStudio.web.js.map +1 -0
  93. package/build/cjs/AudioStudioModule.js +718 -0
  94. package/build/cjs/AudioStudioModule.js.map +1 -0
  95. package/build/cjs/WebRecorder.web.js +865 -0
  96. package/build/cjs/WebRecorder.web.js.map +1 -0
  97. package/build/cjs/constants/platformLimitations.js +99 -0
  98. package/build/cjs/constants/platformLimitations.js.map +1 -0
  99. package/build/cjs/constants.js +20 -0
  100. package/build/cjs/constants.js.map +1 -0
  101. package/build/cjs/events.js +29 -0
  102. package/build/cjs/events.js.map +1 -0
  103. package/build/cjs/hooks/useAudioDevices.js +179 -0
  104. package/build/cjs/hooks/useAudioDevices.js.map +1 -0
  105. package/build/cjs/index.js +64 -0
  106. package/build/cjs/index.js.map +1 -0
  107. package/build/cjs/trimAudio.js +76 -0
  108. package/build/cjs/trimAudio.js.map +1 -0
  109. package/build/cjs/useAudioRecorder.js +535 -0
  110. package/build/cjs/useAudioRecorder.js.map +1 -0
  111. package/build/cjs/utils/BlobFix.js +502 -0
  112. package/build/cjs/utils/BlobFix.js.map +1 -0
  113. package/build/cjs/utils/audioProcessing.js +136 -0
  114. package/build/cjs/utils/audioProcessing.js.map +1 -0
  115. package/build/cjs/utils/cleanNativeOptions.js +22 -0
  116. package/build/cjs/utils/cleanNativeOptions.js.map +1 -0
  117. package/build/cjs/utils/concatenateBuffers.js +25 -0
  118. package/build/cjs/utils/concatenateBuffers.js.map +1 -0
  119. package/build/cjs/utils/convertPCMToFloat32.js +124 -0
  120. package/build/cjs/utils/convertPCMToFloat32.js.map +1 -0
  121. package/build/cjs/utils/crc32.js +52 -0
  122. package/build/cjs/utils/crc32.js.map +1 -0
  123. package/build/cjs/utils/encodingToBitDepth.js +17 -0
  124. package/build/cjs/utils/encodingToBitDepth.js.map +1 -0
  125. package/build/cjs/utils/getWavFileInfo.js +96 -0
  126. package/build/cjs/utils/getWavFileInfo.js.map +1 -0
  127. package/build/cjs/utils/writeWavHeader.js +88 -0
  128. package/build/cjs/utils/writeWavHeader.js.map +1 -0
  129. package/build/cjs/workers/InlineFeaturesExtractor.web.js +294 -0
  130. package/build/cjs/workers/InlineFeaturesExtractor.web.js.map +1 -0
  131. package/build/cjs/workers/inlineAudioWebWorker.web.js +190 -0
  132. package/build/cjs/workers/inlineAudioWebWorker.web.js.map +1 -0
  133. package/build/cjs/workers/wasmGlueString.web.js +27 -0
  134. package/build/cjs/workers/wasmGlueString.web.js.map +1 -0
  135. package/build/esm/AudioAnalysis/AudioAnalysis.types.js +3 -0
  136. package/build/esm/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  137. package/build/esm/AudioAnalysis/audioFeaturesWasm.js +126 -0
  138. package/build/esm/AudioAnalysis/audioFeaturesWasm.js.map +1 -0
  139. package/build/esm/AudioAnalysis/extractAudioAnalysis.js +205 -0
  140. package/build/esm/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  141. package/build/esm/AudioAnalysis/extractAudioData.js +14 -0
  142. package/build/esm/AudioAnalysis/extractAudioData.js.map +1 -0
  143. package/build/esm/AudioAnalysis/extractMelSpectrogram.js +86 -0
  144. package/build/esm/AudioAnalysis/extractMelSpectrogram.js.map +1 -0
  145. package/build/esm/AudioAnalysis/extractPreview.js +25 -0
  146. package/build/esm/AudioAnalysis/extractPreview.js.map +1 -0
  147. package/build/esm/AudioAnalysis/extractWaveform.js +11 -0
  148. package/build/esm/AudioAnalysis/extractWaveform.js.map +1 -0
  149. package/build/esm/AudioAnalysis/melSpectrogramWasm.js +111 -0
  150. package/build/esm/AudioAnalysis/melSpectrogramWasm.js.map +1 -0
  151. package/build/esm/AudioDeviceManager.js +681 -0
  152. package/build/esm/AudioDeviceManager.js.map +1 -0
  153. package/build/esm/AudioRecorder.provider.js +40 -0
  154. package/build/esm/AudioRecorder.provider.js.map +1 -0
  155. package/build/esm/AudioStudio.native.js +6 -0
  156. package/build/esm/AudioStudio.native.js.map +1 -0
  157. package/build/esm/AudioStudio.types.js +8 -0
  158. package/build/esm/AudioStudio.types.js.map +1 -0
  159. package/build/esm/AudioStudio.web.js +704 -0
  160. package/build/esm/AudioStudio.web.js.map +1 -0
  161. package/build/esm/AudioStudioModule.js +713 -0
  162. package/build/esm/AudioStudioModule.js.map +1 -0
  163. package/build/esm/WebRecorder.web.js +861 -0
  164. package/build/esm/WebRecorder.web.js.map +1 -0
  165. package/build/esm/constants/platformLimitations.js +90 -0
  166. package/build/esm/constants/platformLimitations.js.map +1 -0
  167. package/build/esm/constants.js +17 -0
  168. package/build/esm/constants.js.map +1 -0
  169. package/build/esm/events.js +21 -0
  170. package/build/esm/events.js.map +1 -0
  171. package/build/esm/hooks/useAudioDevices.js +176 -0
  172. package/build/esm/hooks/useAudioDevices.js.map +1 -0
  173. package/build/esm/index.js +23 -0
  174. package/build/esm/index.js.map +1 -0
  175. package/build/esm/trimAudio.js +69 -0
  176. package/build/esm/trimAudio.js.map +1 -0
  177. package/build/esm/useAudioRecorder.js +529 -0
  178. package/build/esm/useAudioRecorder.js.map +1 -0
  179. package/build/esm/utils/BlobFix.js +498 -0
  180. package/build/esm/utils/BlobFix.js.map +1 -0
  181. package/build/esm/utils/audioProcessing.js +133 -0
  182. package/build/esm/utils/audioProcessing.js.map +1 -0
  183. package/build/esm/utils/cleanNativeOptions.js +19 -0
  184. package/build/esm/utils/cleanNativeOptions.js.map +1 -0
  185. package/build/esm/utils/concatenateBuffers.js +21 -0
  186. package/build/esm/utils/concatenateBuffers.js.map +1 -0
  187. package/build/esm/utils/convertPCMToFloat32.js +120 -0
  188. package/build/esm/utils/convertPCMToFloat32.js.map +1 -0
  189. package/build/esm/utils/crc32.js +50 -0
  190. package/build/esm/utils/crc32.js.map +1 -0
  191. package/build/esm/utils/encodingToBitDepth.js +13 -0
  192. package/build/esm/utils/encodingToBitDepth.js.map +1 -0
  193. package/build/esm/utils/getWavFileInfo.js +92 -0
  194. package/build/esm/utils/getWavFileInfo.js.map +1 -0
  195. package/build/esm/utils/writeWavHeader.js +84 -0
  196. package/build/esm/utils/writeWavHeader.js.map +1 -0
  197. package/build/esm/workers/InlineFeaturesExtractor.web.js +291 -0
  198. package/build/esm/workers/InlineFeaturesExtractor.web.js.map +1 -0
  199. package/build/esm/workers/inlineAudioWebWorker.web.js +187 -0
  200. package/build/esm/workers/inlineAudioWebWorker.web.js.map +1 -0
  201. package/build/esm/workers/wasmGlueString.web.js +24 -0
  202. package/build/esm/workers/wasmGlueString.web.js.map +1 -0
  203. package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts +198 -0
  204. package/build/types/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  205. package/build/types/AudioAnalysis/audioFeaturesWasm.d.ts +24 -0
  206. package/build/types/AudioAnalysis/audioFeaturesWasm.d.ts.map +1 -0
  207. package/build/types/AudioAnalysis/extractAudioAnalysis.d.ts +74 -0
  208. package/build/types/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  209. package/build/types/AudioAnalysis/extractAudioData.d.ts +3 -0
  210. package/build/types/AudioAnalysis/extractAudioData.d.ts.map +1 -0
  211. package/build/types/AudioAnalysis/extractMelSpectrogram.d.ts +20 -0
  212. package/build/types/AudioAnalysis/extractMelSpectrogram.d.ts.map +1 -0
  213. package/build/types/AudioAnalysis/extractPreview.d.ts +11 -0
  214. package/build/types/AudioAnalysis/extractPreview.d.ts.map +1 -0
  215. package/build/types/AudioAnalysis/extractWaveform.d.ts +8 -0
  216. package/build/types/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  217. package/build/types/AudioAnalysis/melSpectrogramWasm.d.ts +16 -0
  218. package/build/types/AudioAnalysis/melSpectrogramWasm.d.ts.map +1 -0
  219. package/build/types/AudioDeviceManager.d.ts +187 -0
  220. package/build/types/AudioDeviceManager.d.ts.map +1 -0
  221. package/build/types/AudioRecorder.provider.d.ts +11 -0
  222. package/build/types/AudioRecorder.provider.d.ts.map +1 -0
  223. package/build/types/AudioStudio.native.d.ts +3 -0
  224. package/build/types/AudioStudio.native.d.ts.map +1 -0
  225. package/build/types/AudioStudio.types.d.ts +760 -0
  226. package/build/types/AudioStudio.types.d.ts.map +1 -0
  227. package/build/types/AudioStudio.web.d.ts +96 -0
  228. package/build/types/AudioStudio.web.d.ts.map +1 -0
  229. package/build/types/AudioStudioModule.d.ts +3 -0
  230. package/build/types/AudioStudioModule.d.ts.map +1 -0
  231. package/build/types/WebRecorder.web.d.ts +208 -0
  232. package/build/types/WebRecorder.web.d.ts.map +1 -0
  233. package/build/types/constants/platformLimitations.d.ts +40 -0
  234. package/build/types/constants/platformLimitations.d.ts.map +1 -0
  235. package/build/types/constants.d.ts +14 -0
  236. package/build/types/constants.d.ts.map +1 -0
  237. package/build/types/events.d.ts +29 -0
  238. package/build/types/events.d.ts.map +1 -0
  239. package/build/types/hooks/useAudioDevices.d.ts +15 -0
  240. package/build/types/hooks/useAudioDevices.d.ts.map +1 -0
  241. package/build/types/index.d.ts +21 -0
  242. package/build/types/index.d.ts.map +1 -0
  243. package/build/types/trimAudio.d.ts +25 -0
  244. package/build/types/trimAudio.d.ts.map +1 -0
  245. package/build/types/useAudioRecorder.d.ts +22 -0
  246. package/build/types/useAudioRecorder.d.ts.map +1 -0
  247. package/build/types/utils/BlobFix.d.ts +9 -0
  248. package/build/types/utils/BlobFix.d.ts.map +1 -0
  249. package/build/types/utils/audioProcessing.d.ts +24 -0
  250. package/build/types/utils/audioProcessing.d.ts.map +1 -0
  251. package/build/types/utils/cleanNativeOptions.d.ts +15 -0
  252. package/build/types/utils/cleanNativeOptions.d.ts.map +1 -0
  253. package/build/types/utils/concatenateBuffers.d.ts +8 -0
  254. package/build/types/utils/concatenateBuffers.d.ts.map +1 -0
  255. package/build/types/utils/convertPCMToFloat32.d.ts +13 -0
  256. package/build/types/utils/convertPCMToFloat32.d.ts.map +1 -0
  257. package/build/types/utils/crc32.d.ts +7 -0
  258. package/build/types/utils/crc32.d.ts.map +1 -0
  259. package/build/types/utils/encodingToBitDepth.d.ts +5 -0
  260. package/build/types/utils/encodingToBitDepth.d.ts.map +1 -0
  261. package/build/types/utils/getWavFileInfo.d.ts +26 -0
  262. package/build/types/utils/getWavFileInfo.d.ts.map +1 -0
  263. package/build/types/utils/writeWavHeader.d.ts +34 -0
  264. package/build/types/utils/writeWavHeader.d.ts.map +1 -0
  265. package/build/types/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  266. package/build/types/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  267. package/build/types/workers/inlineAudioWebWorker.web.d.ts +2 -0
  268. package/build/types/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  269. package/build/types/workers/wasmGlueString.web.d.ts +2 -0
  270. package/build/types/workers/wasmGlueString.web.d.ts.map +1 -0
  271. package/cpp/AudioFeatures.cpp +274 -0
  272. package/cpp/AudioFeatures.h +85 -0
  273. package/cpp/AudioFeaturesBridge.cpp +146 -0
  274. package/cpp/AudioFeaturesBridge.h +47 -0
  275. package/cpp/MelSpectrogram.cpp +227 -0
  276. package/cpp/MelSpectrogram.h +82 -0
  277. package/cpp/MelSpectrogramBridge.cpp +112 -0
  278. package/cpp/MelSpectrogramBridge.h +33 -0
  279. package/cpp/kiss_fft/COPYING +11 -0
  280. package/cpp/kiss_fft/_kiss_fft_guts.h +167 -0
  281. package/cpp/kiss_fft/kiss_fft.c +424 -0
  282. package/cpp/kiss_fft/kiss_fft.h +160 -0
  283. package/cpp/kiss_fft/kiss_fft_log.h +36 -0
  284. package/cpp/kiss_fft/kiss_fftr.c +155 -0
  285. package/cpp/kiss_fft/kiss_fftr.h +54 -0
  286. package/expo-module.config.json +10 -0
  287. package/ios/AudioAnalysisData.swift +74 -0
  288. package/ios/AudioDeviceManager.swift +670 -0
  289. package/ios/AudioFeaturesWrapper.h +21 -0
  290. package/ios/AudioFeaturesWrapper.mm +63 -0
  291. package/ios/AudioNotificationManager.swift +154 -0
  292. package/ios/AudioProcessingHelpers.swift +797 -0
  293. package/ios/AudioProcessor.swift +1191 -0
  294. package/ios/AudioStreamError.swift +7 -0
  295. package/ios/AudioStreamManager.swift +2369 -0
  296. package/ios/AudioStreamManagerDelegate.swift +16 -0
  297. package/ios/AudioStudio.podspec +39 -0
  298. package/ios/AudioStudioModule.swift +1111 -0
  299. package/ios/AudioStudioTests/AudioFileHandlerTests.swift +338 -0
  300. package/ios/AudioStudioTests/AudioFormatUtilsTests.swift +331 -0
  301. package/ios/AudioStudioTests/AudioTestHelpers.swift +130 -0
  302. package/ios/AudioStudioTests/CompressedOnlyOutputTests.swift +294 -0
  303. package/ios/AudioStudioTests/EventEmissionIntervalTests.swift +105 -0
  304. package/ios/AudioStudioTests/Info.plist +22 -0
  305. package/ios/AudioStudioTests/README.md +39 -0
  306. package/ios/AudioStudioTests/SimpleAudioTest.swift +98 -0
  307. package/ios/AudioStudioTests/TestAudioGenerator.swift +75 -0
  308. package/ios/DataPoint.swift +54 -0
  309. package/ios/DecodingConfig.swift +59 -0
  310. package/ios/FFT.swift +62 -0
  311. package/ios/Features.swift +95 -0
  312. package/ios/ISSUE_IOS.md +68 -0
  313. package/ios/Logger.swift +39 -0
  314. package/ios/MelSpectrogramWrapper.h +30 -0
  315. package/ios/MelSpectrogramWrapper.mm +97 -0
  316. package/ios/NotificationExtension.swift +15 -0
  317. package/ios/RecordingResult.swift +22 -0
  318. package/ios/RecordingSettings.swift +311 -0
  319. package/ios/WaveformExtractor.swift +105 -0
  320. package/ios/tests/README.md +41 -0
  321. package/ios/tests/integration/buffer_and_fallback_test.swift +178 -0
  322. package/ios/tests/integration/buffer_duration_test.swift +185 -0
  323. package/ios/tests/integration/compressed_only_output_test.swift +271 -0
  324. package/ios/tests/integration/output_control_test.swift +322 -0
  325. package/ios/tests/integration/run_integration_tests.sh +37 -0
  326. package/ios/tests/opus_support_test_macos.swift +154 -0
  327. package/ios/tests/standalone/audio_processing_test.swift +144 -0
  328. package/ios/tests/standalone/audio_recording_test.swift +277 -0
  329. package/ios/tests/standalone/audio_streaming_test.swift +249 -0
  330. package/ios/tests/standalone/standalone_test.swift +144 -0
  331. package/package.json +146 -0
  332. package/plugin/build/index.cjs +194 -0
  333. package/plugin/build/index.d.cts +22 -0
  334. package/plugin/build/index.js +194 -0
  335. package/plugin/src/index.ts +285 -0
  336. package/plugin/tsconfig.json +10 -0
  337. package/plugin/tsconfig.tsbuildinfo +1 -0
  338. package/prebuilt/wasm/mel-spectrogram.js +18 -0
  339. package/src/AudioAnalysis/AudioAnalysis.types.ts +226 -0
  340. package/src/AudioAnalysis/audio-features-wasm.d.ts +37 -0
  341. package/src/AudioAnalysis/audioFeaturesWasm.ts +200 -0
  342. package/src/AudioAnalysis/extractAudioAnalysis.ts +350 -0
  343. package/src/AudioAnalysis/extractAudioData.ts +17 -0
  344. package/src/AudioAnalysis/extractMelSpectrogram.ts +140 -0
  345. package/src/AudioAnalysis/extractPreview.ts +34 -0
  346. package/src/AudioAnalysis/extractWaveform.ts +22 -0
  347. package/src/AudioAnalysis/mel-spectrogram-wasm.d.ts +48 -0
  348. package/src/AudioAnalysis/melSpectrogramWasm.ts +179 -0
  349. package/src/AudioDeviceManager.ts +800 -0
  350. package/src/AudioRecorder.provider.tsx +57 -0
  351. package/src/AudioStudio.native.ts +6 -0
  352. package/src/AudioStudio.types.ts +899 -0
  353. package/src/AudioStudio.web.ts +911 -0
  354. package/src/AudioStudioModule.ts +984 -0
  355. package/src/WebRecorder.web.ts +1114 -0
  356. package/src/constants/platformLimitations.ts +118 -0
  357. package/src/constants.ts +21 -0
  358. package/src/events.ts +63 -0
  359. package/src/hooks/useAudioDevices.ts +213 -0
  360. package/src/index.ts +67 -0
  361. package/src/trimAudio.ts +94 -0
  362. package/src/types/crc-32.d.ts +9 -0
  363. package/src/useAudioRecorder.tsx +784 -0
  364. package/src/utils/BlobFix.ts +561 -0
  365. package/src/utils/audioProcessing.ts +205 -0
  366. package/src/utils/cleanNativeOptions.ts +18 -0
  367. package/src/utils/concatenateBuffers.ts +24 -0
  368. package/src/utils/convertPCMToFloat32.ts +170 -0
  369. package/src/utils/crc32.ts +59 -0
  370. package/src/utils/encodingToBitDepth.ts +18 -0
  371. package/src/utils/getWavFileInfo.ts +132 -0
  372. package/src/utils/writeWavHeader.ts +115 -0
  373. package/src/workers/InlineFeaturesExtractor.web.tsx +291 -0
  374. package/src/workers/inlineAudioWebWorker.web.tsx +186 -0
  375. package/src/workers/wasmGlueString.web.ts +23 -0
@@ -0,0 +1,115 @@
1
+ // packages/audio-studio/src/utils/writeWavHeader.ts
2
+
3
+ /**
4
+ * Options for creating a WAV header.
5
+ */
6
+ export interface WavHeaderOptions {
7
+ /** Optional buffer containing audio data. If provided, it will be combined with the header. */
8
+ buffer?: ArrayBuffer
9
+ /** The sample rate of the audio in Hz (e.g., 44100). */
10
+ sampleRate: number
11
+ /** The number of audio channels (e.g., 1 for mono, 2 for stereo). */
12
+ numChannels: number
13
+ /** The bit depth of the audio (e.g., 16, 24, or 32). */
14
+ bitDepth: number
15
+ /** Whether the audio data is in float format (only applies to 32-bit) */
16
+ isFloat?: boolean
17
+ }
18
+
19
+ /**
20
+ * Writes or updates a WAV (RIFF) header based on the provided options.
21
+ *
22
+ * This function can be used in three ways:
23
+ * 1. To create a standalone WAV header (when no buffer is provided).
24
+ * 2. To create a WAV header and combine it with existing audio data (when a buffer without a header is provided).
25
+ * 3. To update an existing WAV header in the provided buffer.
26
+ *
27
+ * For streaming audio where the final size is unknown, this function sets the size fields
28
+ * to the maximum 32-bit value (0xFFFFFFFF). These can be updated later using the
29
+ * `updateWavHeaderSize` function once the final size is known.
30
+ *
31
+ * @param options - The options for creating or updating the WAV header.
32
+ * @returns An ArrayBuffer containing the WAV header, or the header combined with the provided audio data.
33
+ *
34
+ * @throws {Error} Throws an error if the provided options are invalid or if the buffer is too small.
35
+ */
36
+ export const writeWavHeader = ({
37
+ buffer,
38
+ sampleRate,
39
+ numChannels,
40
+ bitDepth,
41
+ isFloat = bitDepth === 32, // Default to float for 32-bit
42
+ }: WavHeaderOptions): ArrayBuffer => {
43
+ // For 32-bit float, we use format 3, otherwise format 1 for PCM
44
+ const audioFormat = isFloat ? 3 : 1 // 3 = IEEE float, 1 = PCM
45
+
46
+ const bytesPerSample = bitDepth / 8
47
+ const blockAlign = numChannels * bytesPerSample
48
+ const byteRate = sampleRate * blockAlign
49
+
50
+ // Function to write a string to the DataView
51
+ const writeString = (view: DataView, offset: number, string: string) => {
52
+ for (let i = 0; i < string.length; i++) {
53
+ view.setUint8(offset + i, string.charCodeAt(i))
54
+ }
55
+ }
56
+
57
+ // Function to write or update the header
58
+ const writeHeader = (view: DataView, dataSize: number = 0xffffffff) => {
59
+ // RIFF chunk descriptor
60
+ writeString(view, 0, 'RIFF') // ChunkID
61
+ view.setUint32(4, 36 + dataSize, true) // ChunkSize: 4 + (8 + 16) + (8 + dataSize)
62
+ writeString(view, 8, 'WAVE') // Format
63
+
64
+ // "fmt " sub-chunk
65
+ writeString(view, 12, 'fmt ') // Subchunk1ID
66
+ view.setUint32(16, 16, true) // Subchunk1Size (16 for PCM/Float)
67
+ view.setUint16(20, audioFormat, true) // AudioFormat (3 for float, 1 for PCM)
68
+ view.setUint16(22, numChannels, true) // NumChannels
69
+ view.setUint32(24, sampleRate, true) // SampleRate
70
+ view.setUint32(28, byteRate, true) // ByteRate = SampleRate * NumChannels * BitsPerSample/8
71
+ view.setUint16(32, blockAlign, true) // BlockAlign = NumChannels * BitsPerSample/8
72
+ view.setUint16(34, bitDepth, true) // BitsPerSample
73
+
74
+ // "data" sub-chunk
75
+ writeString(view, 36, 'data') // Subchunk2ID
76
+ view.setUint32(40, dataSize, true) // Subchunk2Size = NumSamples * NumChannels * BitsPerSample/8
77
+ }
78
+
79
+ if (buffer) {
80
+ // Handle existing buffer
81
+
82
+ // Check for minimum size
83
+ if (buffer.byteLength < 44) {
84
+ throw new Error('Buffer is too small to contain a valid WAV header')
85
+ }
86
+
87
+ const view = new DataView(buffer)
88
+
89
+ // Check if the buffer already has a WAV header by looking for "RIFF" at the start
90
+ const existingHeader = view.getUint32(0, false) === 0x52494646 // "RIFF" in ASCII
91
+
92
+ if (existingHeader) {
93
+ // Update the existing header
94
+ writeHeader(view, buffer.byteLength - 44)
95
+ return buffer
96
+ } else {
97
+ // Create a new buffer with header + data
98
+ const newBuffer = new ArrayBuffer(44 + buffer.byteLength)
99
+ const newView = new DataView(newBuffer)
100
+
101
+ // Write header to new buffer
102
+ writeHeader(newView, buffer.byteLength)
103
+
104
+ // Copy audio data after header
105
+ new Uint8Array(newBuffer).set(new Uint8Array(buffer), 44)
106
+ return newBuffer
107
+ }
108
+ } else {
109
+ // Create standalone header
110
+ const headerBuffer = new ArrayBuffer(44)
111
+ const view = new DataView(headerBuffer)
112
+ writeHeader(view)
113
+ return headerBuffer
114
+ }
115
+ }
@@ -0,0 +1,291 @@
1
+ // packages/audio-studio/src/workers/InlineFeaturesExtractor.web.tsx
2
+ //
3
+ // The worker blob is assembled at runtime by concatenating:
4
+ // 1. The WASM glue JS string (defines createMelSpectrogramModule)
5
+ // 2. This inline worker code
6
+ //
7
+ // This keeps ONE C++ implementation for spectral/MFCC/chroma across all platforms.
8
+
9
+ export const InlineFeaturesExtractor = `
10
+ // --- Constants ---
11
+ const N_FFT = 1024;
12
+ const N_CHROMA = 12;
13
+ const STRUCT_SIZE = 32; // CAudioFeaturesResult
14
+
15
+ // --- WASM module state ---
16
+ let wasmModule = null;
17
+ let wasmInitPromise = null;
18
+ let wasmFramePtr = 0;
19
+ let wasmFrameCapacity = 0;
20
+ let wasmResultPtr = 0;
21
+
22
+ function initWasm(sampleRate) {
23
+ if (wasmInitPromise) return wasmInitPromise;
24
+ wasmInitPromise = (typeof createMelSpectrogramModule === 'function'
25
+ ? createMelSpectrogramModule()
26
+ : Promise.reject(new Error('WASM glue not loaded'))
27
+ ).then(function(Module) {
28
+ wasmModule = Module;
29
+ Module._audio_features_init(sampleRate, N_FFT, 13, 26, 1, 1);
30
+ wasmResultPtr = Module._malloc(STRUCT_SIZE);
31
+ Module.HEAPU8.fill(0, wasmResultPtr, wasmResultPtr + STRUCT_SIZE);
32
+ return Module;
33
+ }).catch(function(err) {
34
+ wasmInitPromise = null;
35
+ throw err;
36
+ });
37
+ return wasmInitPromise;
38
+ }
39
+
40
+ function readWasmResult(Module, ptr) {
41
+ var getValue = Module.getValue;
42
+ var centroid = getValue(ptr, 'float');
43
+ var flatness = getValue(ptr + 4, 'float');
44
+ var rolloff = getValue(ptr + 8, 'float');
45
+ var bandwidth = getValue(ptr + 12, 'float');
46
+ var mfccPtr = getValue(ptr + 16, 'i32');
47
+ var mfccCount = getValue(ptr + 20, 'i32');
48
+ var chromaPtr = getValue(ptr + 24, 'i32');
49
+ var chromaCount = getValue(ptr + 28, 'i32');
50
+
51
+ var mfcc = [];
52
+ if (mfccPtr && mfccCount > 0) {
53
+ var off = mfccPtr >> 2;
54
+ for (var i = 0; i < mfccCount; i++) mfcc.push(Module.HEAPF32[off + i]);
55
+ }
56
+ var chromagram = [];
57
+ if (chromaPtr && chromaCount > 0) {
58
+ var off2 = chromaPtr >> 2;
59
+ for (var i = 0; i < chromaCount; i++) chromagram.push(Module.HEAPF32[off2 + i]);
60
+ }
61
+ return { centroid: centroid, flatness: flatness, rolloff: rolloff, bandwidth: bandwidth, mfcc: mfcc, chromagram: chromagram };
62
+ }
63
+
64
+ // Compute spectral/MFCC/chroma features for a segment via WASM C++
65
+ function computeFeaturesWasm(segment, sampleRate, featureOptions) {
66
+ if (!wasmModule || !wasmResultPtr) {
67
+ return { centroid: 0, flatness: 0, rolloff: 0, bandwidth: 0, mfcc: [], chromagram: [] };
68
+ }
69
+ var Module = wasmModule;
70
+ var needSpectral = featureOptions.spectralCentroid || featureOptions.spectralFlatness ||
71
+ featureOptions.spectralRolloff || featureOptions.spectralBandwidth;
72
+ var needMfcc = !!featureOptions.mfcc;
73
+ var needChroma = !!featureOptions.chromagram;
74
+
75
+ if (!needSpectral && !needMfcc && !needChroma) {
76
+ return { centroid: 0, flatness: 0, rolloff: 0, bandwidth: 0, mfcc: [], chromagram: [] };
77
+ }
78
+
79
+ // Re-init if needed (different feature flags)
80
+ Module._audio_features_init(sampleRate, N_FFT, 13, 26, needMfcc ? 1 : 0, needChroma ? 1 : 0);
81
+
82
+ // Allocate/grow input buffer on WASM heap
83
+ if (segment.length > wasmFrameCapacity) {
84
+ if (wasmFramePtr) Module._free(wasmFramePtr);
85
+ wasmFramePtr = Module._malloc(segment.length * 4);
86
+ wasmFrameCapacity = segment.length;
87
+ }
88
+ Module.HEAPF32.set(segment, wasmFramePtr >> 2);
89
+
90
+ var ok = Module._audio_features_compute_frame(wasmFramePtr, segment.length, wasmResultPtr);
91
+ if (!ok) {
92
+ return { centroid: 0, flatness: 0, rolloff: 0, bandwidth: 0, mfcc: [], chromagram: [] };
93
+ }
94
+ var result = readWasmResult(Module, wasmResultPtr);
95
+ Module._audio_features_free_arrays(wasmResultPtr);
96
+ return result;
97
+ }
98
+
99
+ // --- JS fallback for HNR (autocorrelation, no FFT needed) ---
100
+ function extractHNR(segmentData) {
101
+ var frameSize = segmentData.length;
102
+ var autocorrelation = new Float32Array(frameSize);
103
+ for (var i = 0; i < frameSize; i++) {
104
+ var sum = 0;
105
+ for (var j = 0; j < frameSize - i; j++) {
106
+ sum += segmentData[j] * segmentData[j + i];
107
+ }
108
+ autocorrelation[i] = sum;
109
+ }
110
+ var maxAutocorrelation = -Infinity;
111
+ for (var i = 1; i < autocorrelation.length; i++) {
112
+ if (autocorrelation[i] > maxAutocorrelation) {
113
+ maxAutocorrelation = autocorrelation[i];
114
+ }
115
+ }
116
+ return autocorrelation[0] !== 0
117
+ ? 10 * Math.log10(maxAutocorrelation / (autocorrelation[0] - maxAutocorrelation))
118
+ : 0;
119
+ }
120
+
121
+ // --- JS fallback for pitch estimation (simple peak-picking) ---
122
+ function estimatePitch(segment, sampleRate) {
123
+ if (!segment || segment.length < 2 || !sampleRate) return 0;
124
+ // Simple autocorrelation-based pitch
125
+ var minLag = Math.floor(sampleRate / 1000); // 1000 Hz max
126
+ var maxLag = Math.floor(sampleRate / 50); // 50 Hz min
127
+ if (maxLag >= segment.length) maxLag = segment.length - 1;
128
+ var bestCorr = -Infinity;
129
+ var bestLag = 0;
130
+ for (var lag = minLag; lag <= maxLag; lag++) {
131
+ var corr = 0;
132
+ for (var i = 0; i < segment.length - lag; i++) {
133
+ corr += segment[i] * segment[i + lag];
134
+ }
135
+ if (corr > bestCorr) {
136
+ bestCorr = corr;
137
+ bestLag = lag;
138
+ }
139
+ }
140
+ return bestLag > 0 ? sampleRate / bestLag : 0;
141
+ }
142
+
143
+ // --- Unique ID counter ---
144
+ let uniqueIdCounter = 0;
145
+
146
+ // --- Message handler ---
147
+ self.onmessage = function (event) {
148
+ var enableLogging = event.data.enableLogging || false;
149
+
150
+ // Reset command
151
+ if (event.data.command === 'resetCounter') {
152
+ var newValue = event.data.value;
153
+ uniqueIdCounter = typeof newValue === 'number' ? newValue : 0;
154
+ return;
155
+ }
156
+
157
+ var channelData = event.data.channelData;
158
+ var sampleRate = event.data.sampleRate;
159
+ var segmentDurationMs = event.data.segmentDurationMs;
160
+ var bitDepth = event.data.bitDepth;
161
+ var fullAudioDurationMs = event.data.fullAudioDurationMs;
162
+ var numberOfChannels = event.data.numberOfChannels;
163
+ var _features = event.data.features;
164
+ var features = _features || {};
165
+ var bytesPerSample = bitDepth / 8;
166
+
167
+ var subChunkStartTime = (typeof fullAudioDurationMs === 'number' && !isNaN(fullAudioDurationMs) && fullAudioDurationMs >= 0)
168
+ ? fullAudioDurationMs / 1000 : 0;
169
+
170
+ // Check if any C++-backed features are requested
171
+ var needWasm = features.spectralCentroid || features.spectralFlatness ||
172
+ features.spectralRolloff || features.spectralBandwidth ||
173
+ features.mfcc || features.chromagram;
174
+
175
+ function createFeaturesObject(maxAmp, rms, sumSquares, zeroCrossings, segLen, wasmResult, startIdx, endIdx) {
176
+ if (!Object.values(features).some(function(v) { return v; })) return undefined;
177
+ var result = {};
178
+ if (features.energy) result.energy = sumSquares;
179
+ if (features.rms) result.rms = rms;
180
+ result.minAmplitude = -maxAmp;
181
+ result.maxAmplitude = maxAmp;
182
+ if (features.zcr) result.zcr = zeroCrossings / segLen;
183
+ if (features.spectralCentroid) result.spectralCentroid = wasmResult.centroid;
184
+ if (features.spectralFlatness) result.spectralFlatness = wasmResult.flatness;
185
+ if (features.spectralRolloff) result.spectralRolloff = wasmResult.rolloff;
186
+ if (features.spectralBandwidth) result.spectralBandwidth = wasmResult.bandwidth;
187
+ if (features.mfcc) result.mfcc = wasmResult.mfcc;
188
+ if (features.chromagram) result.chromagram = wasmResult.chromagram;
189
+ if (features.hnr) result.hnr = extractHNR(channelData.slice(startIdx, endIdx));
190
+ if (features.pitch) result.pitch = estimatePitch(channelData.slice(startIdx, endIdx), sampleRate);
191
+ return result;
192
+ }
193
+
194
+ function processSegment(startIdx, endIdx, segLen) {
195
+ var sumSquares = 0, maxAmp = 0, zeroCrossings = 0;
196
+ for (var j = startIdx; j < endIdx; j++) {
197
+ var value = channelData[j];
198
+ sumSquares += value * value;
199
+ if (Math.abs(value) > maxAmp) maxAmp = Math.abs(value);
200
+ if (j > 0 && value * channelData[j - 1] < 0) zeroCrossings++;
201
+ }
202
+ var rms = Math.sqrt(sumSquares / segLen);
203
+ var wasmResult = needWasm
204
+ ? computeFeaturesWasm(channelData.slice(startIdx, endIdx), sampleRate, features)
205
+ : { centroid: 0, flatness: 0, rolloff: 0, bandwidth: 0, mfcc: [], chromagram: [] };
206
+
207
+ var dataPoint = {
208
+ id: uniqueIdCounter++,
209
+ amplitude: maxAmp,
210
+ rms: rms,
211
+ startTime: subChunkStartTime + (startIdx / sampleRate),
212
+ endTime: subChunkStartTime + (endIdx / sampleRate),
213
+ dB: 20 * Math.log10(rms + 1e-6),
214
+ silent: rms < 0.01,
215
+ startPosition: startIdx * (numberOfChannels || 1) * bytesPerSample,
216
+ endPosition: endIdx * (numberOfChannels || 1) * bytesPerSample,
217
+ samples: segLen,
218
+ };
219
+ var ef = createFeaturesObject(maxAmp, rms, sumSquares, zeroCrossings, segLen, wasmResult, startIdx, endIdx);
220
+ if (ef) dataPoint.features = ef;
221
+ return dataPoint;
222
+ }
223
+
224
+ function extractWaveform() {
225
+ var totalSamples = channelData.length;
226
+ var durationMs = (totalSamples / sampleRate) * 1000;
227
+ var samplesPerSegment = Math.floor(sampleRate * (segmentDurationMs / 1000));
228
+ var numPoints = Math.floor(totalSamples / samplesPerSegment);
229
+ var remainingSamples = totalSamples % samplesPerSegment;
230
+
231
+ var min = Infinity, max = -Infinity;
232
+ for (var i = 0; i < totalSamples; i++) {
233
+ if (channelData[i] < min) min = channelData[i];
234
+ if (channelData[i] > max) max = channelData[i];
235
+ }
236
+
237
+ var dataPoints = [];
238
+ for (var i = 0; i < numPoints; i++) {
239
+ var startIdx = i * samplesPerSegment;
240
+ dataPoints.push(processSegment(startIdx, startIdx + samplesPerSegment, samplesPerSegment));
241
+ }
242
+ if (remainingSamples > samplesPerSegment / 4) {
243
+ var startIdx = numPoints * samplesPerSegment;
244
+ dataPoints.push(processSegment(startIdx, totalSamples, totalSamples - startIdx));
245
+ }
246
+ return {
247
+ durationMs: durationMs,
248
+ dataPoints: dataPoints,
249
+ amplitudeRange: { min: min, max: max },
250
+ rmsRange: { min: 0, max: Math.max(Math.abs(min), Math.abs(max)) }
251
+ };
252
+ }
253
+
254
+ // Main: init WASM if needed, then process
255
+ var doProcess = function() {
256
+ try {
257
+ var t0 = performance.now();
258
+ var result = extractWaveform();
259
+ var t1 = performance.now();
260
+ self.postMessage({
261
+ command: 'features',
262
+ result: {
263
+ bitDepth: bitDepth,
264
+ samples: channelData.length,
265
+ numberOfChannels: numberOfChannels,
266
+ sampleRate: sampleRate,
267
+ segmentDurationMs: segmentDurationMs,
268
+ durationMs: result.durationMs,
269
+ dataPoints: result.dataPoints,
270
+ amplitudeRange: result.amplitudeRange,
271
+ rmsRange: result.rmsRange,
272
+ extractionTimeMs: t1 - t0,
273
+ }
274
+ });
275
+ } catch (error) {
276
+ console.error('[Worker] Error', { message: error.message, stack: error.stack });
277
+ self.postMessage({ error: { message: error.message, stack: error.stack, name: error.name } });
278
+ }
279
+ };
280
+
281
+ if (needWasm && !wasmModule) {
282
+ initWasm(sampleRate).then(doProcess).catch(function(e) {
283
+ console.error('[Worker] WASM init failed, processing without WASM:', e);
284
+ needWasm = false;
285
+ doProcess();
286
+ });
287
+ } else {
288
+ doProcess();
289
+ }
290
+ };
291
+ `
@@ -0,0 +1,186 @@
1
+ // packages/audio-studio/src/workers/inlineAudioWebWorker.web.tsx
2
+ export const InlineAudioWebWorker = `
3
+ const DEFAULT_BIT_DEPTH = 32
4
+ const DEFAULT_SAMPLE_RATE = 44100
5
+
6
+ class RecorderProcessor extends AudioWorkletProcessor {
7
+ constructor() {
8
+ super()
9
+ this.currentChunk = [] // Float32Array
10
+ this.samplesSinceLastExport = 0
11
+ this.recordSampleRate = DEFAULT_SAMPLE_RATE
12
+ this.exportSampleRate = DEFAULT_SAMPLE_RATE
13
+ this.recordBitDepth = DEFAULT_BIT_DEPTH
14
+ this.exportBitDepth = DEFAULT_BIT_DEPTH
15
+ this.numberOfChannels = 1
16
+ this.isRecording = true
17
+ this.port.onmessage = this.handleMessage.bind(this)
18
+ this.enableLogging = false
19
+ this.exportIntervalSamples = 0
20
+ this.currentPosition = 0 // Track current position in seconds
21
+ this.streamFormat = 'raw'
22
+ }
23
+
24
+ handleMessage(event) {
25
+ switch (event.data.command) {
26
+ case 'init':
27
+ this.enableLogging = event.data.enableLogging || false
28
+ this.recordSampleRate = event.data.recordSampleRate
29
+ this.exportSampleRate =
30
+ event.data.exportSampleRate || event.data.recordSampleRate
31
+ this.exportIntervalSamples =
32
+ this.recordSampleRate * (event.data.interval / 1000)
33
+ if (event.data.numberOfChannels) {
34
+ this.numberOfChannels = event.data.numberOfChannels
35
+ }
36
+ if (event.data.recordBitDepth) {
37
+ this.recordBitDepth = event.data.recordBitDepth
38
+ }
39
+ this.exportBitDepth =
40
+ event.data.exportBitDepth || this.recordBitDepth
41
+ this.streamFormat = event.data.streamFormat || 'raw'
42
+
43
+ // Handle position parameter for device switching
44
+ if (typeof event.data.position === 'number' && event.data.position > 0) {
45
+ this.currentPosition = event.data.position
46
+ if (this.enableLogging) {
47
+ console.log('AudioWorklet initialized with position:', this.currentPosition)
48
+ }
49
+ }
50
+ break
51
+
52
+ case 'stop':
53
+ this.isRecording = false
54
+ if (this.currentChunk.length > 0) {
55
+ this.processChunk()
56
+ }
57
+ break
58
+
59
+ case 'pause':
60
+ // Just a placeholder for pause handling
61
+ break
62
+
63
+ case 'resume':
64
+ // Just a placeholder for resume handling
65
+ break
66
+ }
67
+ }
68
+
69
+ process(inputs, _outputs, _parameters) {
70
+ if (!this.isRecording) return true
71
+ const input = inputs[0]
72
+ if (input.length > 0) {
73
+ const newBuffer = new Float32Array(input[0])
74
+ this.currentChunk.push(newBuffer)
75
+ this.samplesSinceLastExport += newBuffer.length
76
+
77
+ if (this.samplesSinceLastExport >= this.exportIntervalSamples) {
78
+ this.processChunk()
79
+ this.samplesSinceLastExport = 0
80
+ }
81
+ }
82
+ return true
83
+ }
84
+
85
+ mergeBuffers(bufferArray, recLength) {
86
+ const result = new Float32Array(recLength)
87
+ let offset = 0
88
+ for (let i = 0; i < bufferArray.length; i++) {
89
+ result.set(bufferArray[i], offset)
90
+ offset += bufferArray[i].length
91
+ }
92
+ return result
93
+ }
94
+
95
+ // Keep basic resampling for sample rate conversion
96
+ resample(samples, targetSampleRate) {
97
+ if (this.recordSampleRate === targetSampleRate) {
98
+ return samples
99
+ }
100
+ const resampledBuffer = new Float32Array(
101
+ Math.ceil(
102
+ (samples.length * targetSampleRate) / this.recordSampleRate
103
+ )
104
+ )
105
+ const ratio = this.recordSampleRate / targetSampleRate
106
+ let offset = 0
107
+ for (let i = 0; i < resampledBuffer.length; i++) {
108
+ const nextOffset = Math.floor((i + 1) * ratio)
109
+ let accum = 0
110
+ let count = 0
111
+ for (let j = offset; j < nextOffset && j < samples.length; j++) {
112
+ accum += samples[j]
113
+ count++
114
+ }
115
+ resampledBuffer[i] = count > 0 ? accum / count : 0
116
+ offset = nextOffset
117
+ }
118
+ return resampledBuffer
119
+ }
120
+
121
+ // Keep bit depth conversion if needed
122
+ convertBitDepth(input, targetBitDepth) {
123
+ if (targetBitDepth === 32) {
124
+ const output = new Int32Array(input.length)
125
+ for (let i = 0; i < input.length; i++) {
126
+ const s = Math.max(-1, Math.min(1, input[i]))
127
+ output[i] = s < 0 ? s * 0x80000000 : s * 0x7fffffff
128
+ }
129
+ return output
130
+ } else if (targetBitDepth === 16) {
131
+ const output = new Int16Array(input.length)
132
+ for (let i = 0; i < input.length; i++) {
133
+ const s = Math.max(-1, Math.min(1, input[i]))
134
+ output[i] = s < 0 ? s * 0x8000 : s * 0x7fff
135
+ }
136
+ return output
137
+ }
138
+ return input
139
+ }
140
+
141
+ processChunk() {
142
+ if (this.currentChunk.length === 0) return
143
+
144
+ // Merge buffers
145
+ const chunkLength = this.currentChunk.reduce(
146
+ (acc, buf) => acc + buf.length,
147
+ 0
148
+ )
149
+ const mergedChunk = this.mergeBuffers(this.currentChunk, chunkLength)
150
+
151
+ // Resample if needed
152
+ const resampledChunk = this.resample(mergedChunk, this.exportSampleRate)
153
+
154
+ // Convert bit depth if needed (used for file storage format)
155
+ const finalBuffer =
156
+ this.recordBitDepth !== this.exportBitDepth
157
+ ? this.convertBitDepth(resampledChunk, this.exportBitDepth)
158
+ : resampledChunk
159
+
160
+ // For float32 stream format, send the resampled Float32 data directly
161
+ // (skipping the bit-depth conversion roundtrip)
162
+ const streamData = this.streamFormat === 'float32' ? resampledChunk : finalBuffer
163
+
164
+ // Calculate the duration in seconds
165
+ const chunkDuration = resampledChunk.length / this.exportSampleRate
166
+
167
+ // Send processed chunk with the current position
168
+ this.port.postMessage({
169
+ command: 'newData',
170
+ recordedData: streamData,
171
+ sampleRate: this.exportSampleRate,
172
+ bitDepth: this.exportBitDepth,
173
+ numberOfChannels: this.numberOfChannels,
174
+ position: this.currentPosition,
175
+ })
176
+
177
+ // Update the position
178
+ this.currentPosition += chunkDuration
179
+
180
+ // Clear the current chunk
181
+ this.currentChunk = []
182
+ }
183
+ }
184
+
185
+ registerProcessor('recorder-processor', RecorderProcessor)
186
+ `