@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,76 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.trimAudio = trimAudio;
7
+ exports.trimAudioSimple = trimAudioSimple;
8
+ const expo_modules_core_1 = require("expo-modules-core");
9
+ const AudioStudioModule_1 = __importDefault(require("./AudioStudioModule"));
10
+ const cleanNativeOptions_1 = require("./utils/cleanNativeOptions");
11
+ // Create a single emitter instance
12
+ const emitter = new expo_modules_core_1.LegacyEventEmitter(AudioStudioModule_1.default);
13
+ /**
14
+ * Trims an audio file based on the provided options.
15
+ *
16
+ * @experimental This API is experimental and not fully optimized for production use.
17
+ * Performance may vary based on file size and device capabilities.
18
+ * Future versions may include breaking changes.
19
+ *
20
+ * @param options Configuration options for the trimming operation
21
+ * @param progressCallback Optional callback to receive progress updates
22
+ * @returns Promise resolving to the trimmed audio file information, including processing time
23
+ */
24
+ async function trimAudio(options, progressCallback) {
25
+ // Validation
26
+ if (!options.fileUri) {
27
+ throw new Error('fileUri is required');
28
+ }
29
+ const mode = options.mode ?? 'single';
30
+ if (mode === 'single') {
31
+ if (options.startTimeMs === undefined &&
32
+ options.endTimeMs === undefined) {
33
+ throw new Error('At least one of startTimeMs or endTimeMs must be provided in single mode');
34
+ }
35
+ }
36
+ else if (mode === 'keep' || mode === 'remove') {
37
+ if (!options.ranges || options.ranges.length === 0) {
38
+ throw new Error('ranges must be provided and non-empty for keep or remove modes');
39
+ }
40
+ }
41
+ else {
42
+ throw new Error(`Invalid mode: ${mode}. Must be 'single', 'keep', or 'remove'`);
43
+ }
44
+ // Set up progress event listener if callback is provided
45
+ let subscription;
46
+ if (progressCallback) {
47
+ subscription = emitter.addListener('TrimProgress', (event) => {
48
+ progressCallback(event);
49
+ });
50
+ }
51
+ try {
52
+ // Clean non-serializable/undefined values to avoid Android Kotlin bridge crash
53
+ const result = await AudioStudioModule_1.default.trimAudio((0, cleanNativeOptions_1.cleanNativeOptions)(options));
54
+ return result;
55
+ }
56
+ finally {
57
+ if (subscription) {
58
+ subscription.remove();
59
+ }
60
+ }
61
+ }
62
+ /**
63
+ * Simplified version of trimAudio that returns only the URI of the trimmed file.
64
+ *
65
+ * @experimental This API is experimental and not fully optimized for production use.
66
+ * Performance may vary based on file size and device capabilities.
67
+ * Future versions may include breaking changes.
68
+ *
69
+ * @param options Configuration options for the trimming operation
70
+ * @returns Promise resolving to the URI of the trimmed audio file
71
+ */
72
+ async function trimAudioSimple(options) {
73
+ const result = await trimAudio(options);
74
+ return result.uri;
75
+ }
76
+ //# sourceMappingURL=trimAudio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trimAudio.js","sourceRoot":"","sources":["../../src/trimAudio.ts"],"names":[],"mappings":";;;;;AAwBA,8BAoDC;AAYD,0CAKC;AA7FD,yDAA8E;AAO9E,4EAAmD;AACnD,mEAA+D;AAE/D,mCAAmC;AACnC,MAAM,OAAO,GAAG,IAAI,sCAAkB,CAAC,2BAAiB,CAAC,CAAA;AAEzD;;;;;;;;;;GAUG;AACI,KAAK,UAAU,SAAS,CAC3B,OAAyB,EACzB,gBAAqD;IAErD,aAAa;IACb,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;IAC1C,CAAC;IACD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,QAAQ,CAAA;IACrC,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QACpB,IACI,OAAO,CAAC,WAAW,KAAK,SAAS;YACjC,OAAO,CAAC,SAAS,KAAK,SAAS,EACjC,CAAC;YACC,MAAM,IAAI,KAAK,CACX,0EAA0E,CAC7E,CAAA;QACL,CAAC;IACL,CAAC;SAAM,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9C,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,KAAK,CACX,gEAAgE,CACnE,CAAA;QACL,CAAC;IACL,CAAC;SAAM,CAAC;QACJ,MAAM,IAAI,KAAK,CACX,iBAAiB,IAAI,yCAAyC,CACjE,CAAA;IACL,CAAC;IAED,yDAAyD;IACzD,IAAI,YAA2C,CAAA;IAC/C,IAAI,gBAAgB,EAAE,CAAC;QACnB,YAAY,GAAG,OAAO,CAAC,WAAW,CAC9B,cAAc,EACd,CAAC,KAAwB,EAAE,EAAE;YACzB,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC,CACJ,CAAA;IACL,CAAC;IAED,IAAI,CAAC;QACD,+EAA+E;QAC/E,MAAM,MAAM,GAAG,MAAM,2BAAiB,CAAC,SAAS,CAC5C,IAAA,uCAAkB,EAAC,OAAO,CAAC,CAC9B,CAAA;QACD,OAAO,MAAM,CAAA;IACjB,CAAC;YAAS,CAAC;QACP,IAAI,YAAY,EAAE,CAAC;YACf,YAAY,CAAC,MAAM,EAAE,CAAA;QACzB,CAAC;IACL,CAAC;AACL,CAAC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,eAAe,CACjC,OAAyB;IAEzB,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,CAAA;IACvC,OAAO,MAAM,CAAC,GAAG,CAAA;AACrB,CAAC","sourcesContent":["import { LegacyEventEmitter, type EventSubscription } from 'expo-modules-core'\n\nimport {\n TrimAudioOptions,\n TrimAudioResult,\n TrimProgressEvent,\n} from './AudioStudio.types'\nimport AudioStudioModule from './AudioStudioModule'\nimport { cleanNativeOptions } from './utils/cleanNativeOptions'\n\n// Create a single emitter instance\nconst emitter = new LegacyEventEmitter(AudioStudioModule)\n\n/**\n * Trims an audio file based on the provided options.\n *\n * @experimental This API is experimental and not fully optimized for production use.\n * Performance may vary based on file size and device capabilities.\n * Future versions may include breaking changes.\n *\n * @param options Configuration options for the trimming operation\n * @param progressCallback Optional callback to receive progress updates\n * @returns Promise resolving to the trimmed audio file information, including processing time\n */\nexport async function trimAudio(\n options: TrimAudioOptions,\n progressCallback?: (event: TrimProgressEvent) => void\n): Promise<TrimAudioResult> {\n // Validation\n if (!options.fileUri) {\n throw new Error('fileUri is required')\n }\n const mode = options.mode ?? 'single'\n if (mode === 'single') {\n if (\n options.startTimeMs === undefined &&\n options.endTimeMs === undefined\n ) {\n throw new Error(\n 'At least one of startTimeMs or endTimeMs must be provided in single mode'\n )\n }\n } else if (mode === 'keep' || mode === 'remove') {\n if (!options.ranges || options.ranges.length === 0) {\n throw new Error(\n 'ranges must be provided and non-empty for keep or remove modes'\n )\n }\n } else {\n throw new Error(\n `Invalid mode: ${mode}. Must be 'single', 'keep', or 'remove'`\n )\n }\n\n // Set up progress event listener if callback is provided\n let subscription: EventSubscription | undefined\n if (progressCallback) {\n subscription = emitter.addListener(\n 'TrimProgress',\n (event: TrimProgressEvent) => {\n progressCallback(event)\n }\n )\n }\n\n try {\n // Clean non-serializable/undefined values to avoid Android Kotlin bridge crash\n const result = await AudioStudioModule.trimAudio(\n cleanNativeOptions(options)\n )\n return result\n } finally {\n if (subscription) {\n subscription.remove()\n }\n }\n}\n\n/**\n * Simplified version of trimAudio that returns only the URI of the trimmed file.\n *\n * @experimental This API is experimental and not fully optimized for production use.\n * Performance may vary based on file size and device capabilities.\n * Future versions may include breaking changes.\n *\n * @param options Configuration options for the trimming operation\n * @returns Promise resolving to the URI of the trimmed audio file\n */\nexport async function trimAudioSimple(\n options: TrimAudioOptions\n): Promise<string> {\n const result = await trimAudio(options)\n return result.uri\n}\n"]}
@@ -0,0 +1,535 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useAudioRecorder = useAudioRecorder;
7
+ // src/useAudioRecorder.ts
8
+ const expo_modules_core_1 = require("expo-modules-core");
9
+ const react_1 = require("react");
10
+ const AudioDeviceManager_1 = require("./AudioDeviceManager");
11
+ const AudioStudioModule_1 = __importDefault(require("./AudioStudioModule"));
12
+ const platformLimitations_1 = require("./constants/platformLimitations");
13
+ const events_1 = require("./events");
14
+ const cleanNativeOptions_1 = require("./utils/cleanNativeOptions");
15
+ const defaultAnalysis = {
16
+ segmentDurationMs: 100,
17
+ bitDepth: 32,
18
+ numberOfChannels: 1,
19
+ durationMs: 0,
20
+ sampleRate: 44100,
21
+ samples: 0,
22
+ dataPoints: [],
23
+ rmsRange: {
24
+ min: Number.POSITIVE_INFINITY,
25
+ max: Number.NEGATIVE_INFINITY,
26
+ },
27
+ amplitudeRange: {
28
+ min: Number.POSITIVE_INFINITY,
29
+ max: Number.NEGATIVE_INFINITY,
30
+ },
31
+ extractionTimeMs: 0,
32
+ };
33
+ function audioRecorderReducer(state, action) {
34
+ switch (action.type) {
35
+ case 'START':
36
+ return {
37
+ ...state,
38
+ isRecording: true,
39
+ isPaused: false,
40
+ durationMs: 0,
41
+ size: 0,
42
+ compression: undefined,
43
+ analysisData: defaultAnalysis,
44
+ };
45
+ case 'STOP':
46
+ return {
47
+ ...state,
48
+ isRecording: false,
49
+ isPaused: false,
50
+ durationMs: 0,
51
+ size: 0,
52
+ compression: undefined,
53
+ analysisData: undefined,
54
+ };
55
+ case 'PAUSE':
56
+ return { ...state, isPaused: true, isRecording: false };
57
+ case 'RESUME':
58
+ return { ...state, isPaused: false, isRecording: true };
59
+ case 'UPDATE_RECORDING_STATE':
60
+ return {
61
+ ...state,
62
+ isPaused: action.payload.isPaused,
63
+ isRecording: action.payload.isRecording,
64
+ };
65
+ case 'UPDATE_STATUS': {
66
+ const newState = {
67
+ ...state,
68
+ durationMs: action.payload.durationMs,
69
+ size: action.payload.size,
70
+ compression: action.payload.compression
71
+ ? {
72
+ size: action.payload.compression.size,
73
+ mimeType: action.payload.compression.mimeType,
74
+ bitrate: action.payload.compression.bitrate,
75
+ format: action.payload.compression.format,
76
+ }
77
+ : undefined,
78
+ };
79
+ return newState;
80
+ }
81
+ case 'UPDATE_ANALYSIS':
82
+ return {
83
+ ...state,
84
+ analysisData: action.payload,
85
+ };
86
+ default:
87
+ return state;
88
+ }
89
+ }
90
+ function useAudioRecorder({ logger, audioWorkletUrl, featuresExtratorUrl, } = {}) {
91
+ // Initialize AudioDeviceManager with logger (once)
92
+ if (logger) {
93
+ AudioDeviceManager_1.audioDeviceManager.setLogger(logger);
94
+ }
95
+ const [state, dispatch] = (0, react_1.useReducer)(audioRecorderReducer, {
96
+ isRecording: false,
97
+ isPaused: false,
98
+ durationMs: 0,
99
+ size: 0,
100
+ compression: undefined,
101
+ analysisData: undefined,
102
+ });
103
+ const startResultRef = (0, react_1.useRef)(null);
104
+ const analysisListenerRef = (0, react_1.useRef)(null);
105
+ // analysisRef is the current analysis data (last 10 seconds by default)
106
+ const analysisRef = (0, react_1.useRef)({ ...defaultAnalysis });
107
+ // fullAnalysisRef is the full analysis data (all data points)
108
+ const fullAnalysisRef = (0, react_1.useRef)({
109
+ ...defaultAnalysis,
110
+ });
111
+ // Instantiate the module for web with URLs
112
+ const audioStudio = expo_modules_core_1.Platform.OS === 'web'
113
+ ? (0, AudioStudioModule_1.default)({
114
+ audioWorkletUrl,
115
+ featuresExtratorUrl,
116
+ logger,
117
+ })
118
+ : AudioStudioModule_1.default;
119
+ const onAudioStreamRef = (0, react_1.useRef)(null);
120
+ const stateRef = (0, react_1.useRef)({
121
+ isRecording: false,
122
+ isPaused: false,
123
+ durationMs: 0,
124
+ size: 0,
125
+ compression: undefined,
126
+ });
127
+ const recordingConfigRef = (0, react_1.useRef)(null);
128
+ // Generate unique instance ID for debugging
129
+ const instanceId = (0, react_1.useId)().replace(/:/g, '').slice(0, 5);
130
+ const handleAudioAnalysis = (0, react_1.useCallback)(async ({ analysis, visualizationDuration, }) => {
131
+ const savedAnalysisData = analysisRef.current || {
132
+ ...defaultAnalysis,
133
+ };
134
+ const maxDuration = visualizationDuration;
135
+ logger?.debug(`[handleAudioAnalysis] Received audio analysis: maxDuration=${maxDuration} analysis.dataPoints=${analysis.dataPoints.length} analysisData.dataPoints=${savedAnalysisData.dataPoints.length}`);
136
+ // Combine data points
137
+ const combinedDataPoints = [
138
+ ...savedAnalysisData.dataPoints,
139
+ ...analysis.dataPoints,
140
+ ];
141
+ const fullCombinedDataPoints = [
142
+ ...(fullAnalysisRef.current?.dataPoints ?? []),
143
+ ...analysis.dataPoints,
144
+ ];
145
+ // Calculate the new duration
146
+ // The number of segments is based on how many segments of segmentDurationMs can fit in visualizationDuration
147
+ const numberOfSegments = Math.ceil(visualizationDuration / analysis.segmentDurationMs);
148
+ // maxDataPoints should be the number of data points, not milliseconds
149
+ const maxDataPoints = numberOfSegments;
150
+ logger?.debug(`[handleAudioAnalysis] Combined data points before trimming: numberOfSegments=${numberOfSegments} visualizationDuration=${visualizationDuration} combinedDataPointsLength=${combinedDataPoints.length} vs maxDataPoints=${maxDataPoints}`);
151
+ // Trim data points to keep within the maximum number of data points
152
+ if (combinedDataPoints.length > maxDataPoints) {
153
+ combinedDataPoints.splice(0, combinedDataPoints.length - maxDataPoints);
154
+ }
155
+ // Keep the full data points
156
+ fullAnalysisRef.current = {
157
+ ...fullAnalysisRef.current,
158
+ dataPoints: fullCombinedDataPoints,
159
+ };
160
+ fullAnalysisRef.current.durationMs =
161
+ fullCombinedDataPoints.length * analysis.segmentDurationMs;
162
+ savedAnalysisData.dataPoints = combinedDataPoints;
163
+ savedAnalysisData.bitDepth =
164
+ analysis.bitDepth || savedAnalysisData.bitDepth;
165
+ savedAnalysisData.durationMs =
166
+ combinedDataPoints.length * analysis.segmentDurationMs;
167
+ // Update amplitude range
168
+ const newMin = Math.min(savedAnalysisData.amplitudeRange.min, analysis.amplitudeRange.min);
169
+ const newMax = Math.max(savedAnalysisData.amplitudeRange.max, analysis.amplitudeRange.max);
170
+ savedAnalysisData.amplitudeRange = {
171
+ min: newMin,
172
+ max: newMax,
173
+ };
174
+ fullAnalysisRef.current.amplitudeRange = {
175
+ min: newMin,
176
+ max: newMax,
177
+ };
178
+ logger?.debug(`[handleAudioAnalysis] Updated analysis data: durationMs=${savedAnalysisData.durationMs}`, { dataPoints: savedAnalysisData.dataPoints.length });
179
+ // Call the onAudioAnalysis callback if it exists in the recording config
180
+ if (recordingConfigRef.current?.onAudioAnalysis) {
181
+ recordingConfigRef.current
182
+ .onAudioAnalysis(analysis)
183
+ .catch((error) => {
184
+ logger?.warn(`Error processing audio analysis:`, error);
185
+ });
186
+ }
187
+ // Update the ref
188
+ analysisRef.current = savedAnalysisData;
189
+ // Dispatch the updated analysis data to state to trigger re-render
190
+ dispatch({
191
+ type: 'UPDATE_ANALYSIS',
192
+ payload: { ...savedAnalysisData },
193
+ });
194
+ }, [dispatch]);
195
+ const handleAudioEvent = (0, react_1.useCallback)(async (eventData) => {
196
+ const { fileUri, deltaSize, totalSize, lastEmittedSize, position, streamUuid, encoded, pcmFloat32, mimeType, buffer, compression, } = eventData;
197
+ logger?.debug(`[handleAudioEvent] Received audio event:`, {
198
+ fileUri,
199
+ deltaSize,
200
+ totalSize,
201
+ position,
202
+ mimeType,
203
+ lastEmittedSize,
204
+ streamUuid,
205
+ encodedLength: encoded?.length,
206
+ compression,
207
+ });
208
+ if (deltaSize === 0) {
209
+ // Ignore packet with no data
210
+ return;
211
+ }
212
+ try {
213
+ // Coming from native ( ios / android ) otherwise buffer is set
214
+ if (expo_modules_core_1.Platform.OS !== 'web') {
215
+ const compressionPayload = compression && startResultRef.current?.compression
216
+ ? {
217
+ data: compression.data,
218
+ size: compression.totalSize,
219
+ mimeType: startResultRef.current.compression
220
+ ?.mimeType,
221
+ bitrate: startResultRef.current.compression
222
+ ?.bitrate,
223
+ format: startResultRef.current.compression
224
+ ?.format,
225
+ }
226
+ : undefined;
227
+ if (pcmFloat32 != null) {
228
+ // Android new arch delivers Float32Array; iOS delivers number[] — normalize both
229
+ const float32 = pcmFloat32 instanceof Float32Array
230
+ ? pcmFloat32
231
+ : new Float32Array(pcmFloat32);
232
+ onAudioStreamRef.current?.({
233
+ data: float32,
234
+ streamFormat: 'float32',
235
+ position,
236
+ fileUri,
237
+ eventDataSize: deltaSize,
238
+ totalSize,
239
+ compression: compressionPayload,
240
+ });
241
+ }
242
+ else {
243
+ if (!encoded) {
244
+ logger?.error(`Encoded audio data is missing`);
245
+ throw new Error('Encoded audio data is missing');
246
+ }
247
+ onAudioStreamRef.current?.({
248
+ data: encoded,
249
+ position,
250
+ fileUri,
251
+ eventDataSize: deltaSize,
252
+ totalSize,
253
+ compression: compressionPayload,
254
+ });
255
+ }
256
+ }
257
+ else if (buffer) {
258
+ // Coming from web
259
+ const webEvent = {
260
+ data: buffer,
261
+ position,
262
+ fileUri,
263
+ eventDataSize: deltaSize,
264
+ totalSize,
265
+ compression: compression && startResultRef.current?.compression
266
+ ? {
267
+ data: compression.data,
268
+ size: compression.totalSize,
269
+ mimeType: startResultRef.current.compression
270
+ ?.mimeType,
271
+ bitrate: startResultRef.current.compression
272
+ ?.bitrate,
273
+ format: startResultRef.current.compression
274
+ ?.format,
275
+ }
276
+ : undefined,
277
+ };
278
+ onAudioStreamRef.current?.(webEvent);
279
+ logger?.debug(`[handleAudioEvent] Audio data sent to onAudioStream`, webEvent);
280
+ }
281
+ }
282
+ catch (error) {
283
+ logger?.error(`Error processing audio event:`, error);
284
+ }
285
+ }, []);
286
+ const checkStatus = (0, react_1.useCallback)(async () => {
287
+ try {
288
+ const status = audioStudio.status();
289
+ logger?.debug(`Status: paused: ${status.isPaused} isRecording: ${status.isRecording} durationMs: ${status.durationMs} size: ${status.size}`, status.compression);
290
+ // Only dispatch if values actually changed
291
+ if (status.isRecording !== stateRef.current.isRecording ||
292
+ status.isPaused !== stateRef.current.isPaused) {
293
+ stateRef.current.isRecording = status.isRecording;
294
+ stateRef.current.isPaused = status.isPaused;
295
+ dispatch({
296
+ type: 'UPDATE_RECORDING_STATE',
297
+ payload: {
298
+ isRecording: status.isRecording,
299
+ isPaused: status.isPaused,
300
+ },
301
+ });
302
+ }
303
+ if (status.durationMs !== stateRef.current.durationMs ||
304
+ status.size !== stateRef.current.size) {
305
+ stateRef.current.durationMs = status.durationMs;
306
+ stateRef.current.size = status.size;
307
+ stateRef.current.compression = status.compression;
308
+ dispatch({
309
+ type: 'UPDATE_STATUS',
310
+ payload: {
311
+ durationMs: status.durationMs,
312
+ size: status.size,
313
+ compression: status.compression,
314
+ },
315
+ });
316
+ }
317
+ }
318
+ catch (error) {
319
+ logger?.error(`Error getting status:`, error);
320
+ }
321
+ }, [audioStudio, logger]); // Only depend on audioStudio and logger
322
+ // Update ref when state changes
323
+ (0, react_1.useEffect)(() => {
324
+ stateRef.current = {
325
+ isRecording: state.isRecording,
326
+ isPaused: state.isPaused,
327
+ durationMs: state.durationMs,
328
+ size: state.size,
329
+ compression: state.compression,
330
+ };
331
+ }, [
332
+ state.isRecording,
333
+ state.isPaused,
334
+ state.durationMs,
335
+ state.size,
336
+ state.compression,
337
+ ]);
338
+ const startRecording = (0, react_1.useCallback)(async (recordingOptions) => {
339
+ // Validate the encoding configuration
340
+ const validationResult = (0, platformLimitations_1.validateRecordingConfig)({
341
+ encoding: recordingOptions.encoding,
342
+ });
343
+ // Log warnings if any
344
+ if (validationResult.warnings.length > 0) {
345
+ validationResult.warnings.forEach((warning) => {
346
+ logger?.warn(warning);
347
+ });
348
+ }
349
+ // Update recording options with validated values
350
+ const validatedOptions = {
351
+ ...recordingOptions,
352
+ encoding: validationResult.encoding,
353
+ };
354
+ recordingConfigRef.current = validatedOptions;
355
+ logger?.debug(`start recording with validated config`, validatedOptions);
356
+ analysisRef.current = { ...defaultAnalysis }; // Reset analysis data
357
+ fullAnalysisRef.current = { ...defaultAnalysis };
358
+ const { onAudioStream, onRecordingInterrupted, onAudioAnalysis, ...options } = validatedOptions;
359
+ const { enableProcessing } = options;
360
+ const maxRecentDataDuration = 10000; // TODO compute maxRecentDataDuration based on screen dimensions
361
+ if (typeof onAudioStream === 'function') {
362
+ onAudioStreamRef.current = onAudioStream;
363
+ }
364
+ else {
365
+ logger?.warn(`onAudioStream is not a function`, onAudioStream);
366
+ onAudioStreamRef.current = null;
367
+ }
368
+ // Strip undefined values and functions that can't cross the native bridge
369
+ const cleanOptions = (0, cleanNativeOptions_1.cleanNativeOptions)(options);
370
+ const startResult = await audioStudio.startRecording(cleanOptions);
371
+ dispatch({ type: 'START' });
372
+ startResultRef.current = startResult;
373
+ if (enableProcessing) {
374
+ logger?.debug(`Enabling audio analysis listener`);
375
+ const listener = (0, events_1.addAudioAnalysisListener)(async (analysisData) => {
376
+ try {
377
+ await handleAudioAnalysis({
378
+ analysis: analysisData,
379
+ visualizationDuration: maxRecentDataDuration,
380
+ });
381
+ }
382
+ catch (error) {
383
+ logger?.warn(`Error processing audio analysis:`, error);
384
+ }
385
+ });
386
+ analysisListenerRef.current = listener;
387
+ }
388
+ return startResult;
389
+ }, [handleAudioAnalysis, dispatch]);
390
+ const prepareRecording = (0, react_1.useCallback)(async (recordingOptions) => {
391
+ recordingConfigRef.current = recordingOptions;
392
+ logger?.debug(`preparing recording`, recordingOptions);
393
+ analysisRef.current = { ...defaultAnalysis }; // Reset analysis data
394
+ fullAnalysisRef.current = { ...defaultAnalysis };
395
+ const { onAudioStream, onRecordingInterrupted, onAudioAnalysis, ...options } = recordingOptions;
396
+ // Store onAudioStream for later use when recording starts
397
+ if (typeof onAudioStream === 'function') {
398
+ onAudioStreamRef.current = onAudioStream;
399
+ }
400
+ else {
401
+ logger?.warn(`onAudioStream is not a function`, onAudioStream);
402
+ onAudioStreamRef.current = null;
403
+ }
404
+ // Strip undefined values and functions that can't cross the native bridge
405
+ const cleanOptions = (0, cleanNativeOptions_1.cleanNativeOptions)(options);
406
+ // Call the native prepareRecording method
407
+ await audioStudio.prepareRecording(cleanOptions);
408
+ logger?.debug(`recording prepared successfully`);
409
+ }, []);
410
+ const stopRecording = (0, react_1.useCallback)(async () => {
411
+ logger?.debug(`stoping recording`);
412
+ const stopResult = await audioStudio.stopRecording();
413
+ stopResult.analysisData = fullAnalysisRef.current;
414
+ if (analysisListenerRef.current) {
415
+ analysisListenerRef.current.remove();
416
+ analysisListenerRef.current = null;
417
+ }
418
+ onAudioStreamRef.current = null;
419
+ // Note: We deliberately DON'T clear recordingConfigRef here to preserve interruption callback
420
+ logger?.debug(`recording stopped`, stopResult);
421
+ dispatch({ type: 'STOP' });
422
+ return stopResult;
423
+ }, [dispatch]);
424
+ const pauseRecording = (0, react_1.useCallback)(async () => {
425
+ logger?.debug(`pause recording`);
426
+ const pauseResult = await audioStudio.pauseRecording();
427
+ dispatch({ type: 'PAUSE' });
428
+ return pauseResult;
429
+ }, [dispatch]);
430
+ const resumeRecording = (0, react_1.useCallback)(async () => {
431
+ logger?.debug(`resume recording`);
432
+ const resumeResult = await audioStudio.resumeRecording();
433
+ dispatch({ type: 'RESUME' });
434
+ return resumeResult;
435
+ }, [dispatch]);
436
+ (0, react_1.useEffect)(() => {
437
+ let intervalId;
438
+ if (state.isRecording || state.isPaused) {
439
+ // Immediately check status when starting
440
+ checkStatus();
441
+ // Start interval
442
+ intervalId = setInterval(checkStatus, 1000);
443
+ }
444
+ return () => {
445
+ if (intervalId) {
446
+ clearInterval(intervalId);
447
+ intervalId = undefined;
448
+ }
449
+ };
450
+ }, [checkStatus, state.isRecording, state.isPaused]);
451
+ (0, react_1.useEffect)(() => {
452
+ logger?.debug(`Registering audio event listener`);
453
+ const subscribeAudio = (0, events_1.addAudioEventListener)(handleAudioEvent);
454
+ logger?.debug(`Subscribed to audio event listener and analysis listener`, {
455
+ subscribeAudio,
456
+ });
457
+ return () => {
458
+ logger?.debug(`Removing audio event listener`);
459
+ subscribeAudio.remove();
460
+ };
461
+ }, [handleAudioEvent, handleAudioAnalysis]);
462
+ (0, react_1.useEffect)(() => {
463
+ // Add event subscription for recording interruptions
464
+ logger?.debug(`Setting up recording interruption listener [${instanceId}]`);
465
+ const subscription = (0, events_1.addRecordingInterruptionListener)((event) => {
466
+ logger?.debug(`[${instanceId}] Received recording interruption event:`, event);
467
+ // Handle device disconnection for UI updates
468
+ if (event.reason === 'deviceDisconnected') {
469
+ logger?.debug(`[${instanceId}] Device disconnected - temporarily hiding last device from UI`);
470
+ // Get current device list before the native layer updates
471
+ const currentDevices = AudioDeviceManager_1.audioDeviceManager.getRawDevices();
472
+ // Wait a moment for native layer to update, then compare
473
+ setTimeout(async () => {
474
+ try {
475
+ // Get updated devices without notifying yet
476
+ const updatedDevices = await AudioDeviceManager_1.audioDeviceManager.getAvailableDevices({
477
+ refresh: true,
478
+ });
479
+ // Find missing devices by comparing lists
480
+ const missingDevices = currentDevices.filter((oldDevice) => !updatedDevices.some((newDevice) => newDevice.id === oldDevice.id));
481
+ if (missingDevices.length > 0) {
482
+ // Mark all missing devices as disconnected (silently)
483
+ missingDevices.forEach((missingDevice) => {
484
+ logger?.debug(`[${instanceId}] Confirmed disconnected device: ${missingDevice.name} (${missingDevice.id})`);
485
+ AudioDeviceManager_1.audioDeviceManager.markDeviceAsDisconnected(missingDevice.id, false);
486
+ });
487
+ }
488
+ // Notify listeners once with the final filtered state
489
+ AudioDeviceManager_1.audioDeviceManager.notifyListeners();
490
+ }
491
+ catch (error) {
492
+ logger?.warn(`[${instanceId}] Error in delayed device disconnection handling:`, error);
493
+ }
494
+ }, 500); // 500ms delay to let native layer update
495
+ }
496
+ else if (event.reason === 'deviceConnected') {
497
+ // Device reconnected - force refresh to show it immediately
498
+ logger?.debug(`[${instanceId}] Device connected, forcing refresh`);
499
+ AudioDeviceManager_1.audioDeviceManager.forceRefreshDevices();
500
+ }
501
+ // Check if we have a callback configured
502
+ logger?.debug(`[${instanceId}] recordingConfigRef.current exists:`, !!recordingConfigRef.current);
503
+ if (recordingConfigRef.current?.onRecordingInterrupted) {
504
+ try {
505
+ logger?.debug(`[${instanceId}] Calling recording interruption callback`);
506
+ recordingConfigRef.current.onRecordingInterrupted(event);
507
+ }
508
+ catch (error) {
509
+ logger?.error(`[${instanceId}] Error in recording interruption callback:`, error);
510
+ }
511
+ }
512
+ else {
513
+ logger?.debug(`[${instanceId}] No recording interruption callback configured`);
514
+ }
515
+ });
516
+ return () => {
517
+ logger?.debug(`[${instanceId}] Removing recording interruption listener`);
518
+ subscription.remove();
519
+ };
520
+ }, [instanceId, logger]); // Include instanceId and logger in dependencies
521
+ return {
522
+ prepareRecording,
523
+ startRecording,
524
+ stopRecording,
525
+ pauseRecording,
526
+ resumeRecording,
527
+ isPaused: state.isPaused,
528
+ isRecording: state.isRecording,
529
+ durationMs: state.durationMs,
530
+ size: state.size,
531
+ compression: state.compression,
532
+ analysisData: state.analysisData,
533
+ };
534
+ }
535
+ //# sourceMappingURL=useAudioRecorder.js.map