@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,704 @@
1
+ // src/AudioStudio.web.ts
2
+ import { LegacyEventEmitter } from 'expo-modules-core';
3
+ import { WebRecorder } from './WebRecorder.web';
4
+ import { DEFAULT_BIT_DEPTH, DEFAULT_INTERVAL_MS, DEFAULT_ANALYSIS_INTERVAL_MS, DEFAULT_MAX_BUFFER_SIZE, } from './constants';
5
+ import { encodingToBitDepth } from './utils/encodingToBitDepth';
6
+ export class AudioStudioWeb extends LegacyEventEmitter {
7
+ customRecorder;
8
+ audioChunks;
9
+ isRecording;
10
+ isPaused;
11
+ recordingStartTime;
12
+ pausedTime;
13
+ currentDurationMs;
14
+ currentSize;
15
+ currentInterval;
16
+ currentIntervalAnalysis;
17
+ lastEmittedSize;
18
+ lastEmittedTime;
19
+ lastEmittedCompressionSize;
20
+ lastEmittedAnalysisTime;
21
+ streamUuid;
22
+ extension = 'wav'; // Default extension is 'wav'
23
+ recordingConfig;
24
+ bitDepth; // Bit depth of the audio
25
+ audioWorkletUrl;
26
+ featuresExtratorUrl;
27
+ logger;
28
+ latestPosition = 0;
29
+ totalCompressedSize = 0;
30
+ maxBufferSize;
31
+ eventCallback;
32
+ constructor({ audioWorkletUrl, featuresExtratorUrl, logger, maxBufferSize = DEFAULT_MAX_BUFFER_SIZE, }) {
33
+ const mockNativeModule = {
34
+ addListener: () => { },
35
+ removeListeners: () => { },
36
+ };
37
+ super(mockNativeModule); // Pass the mock native module to the parent class
38
+ this.logger = logger;
39
+ this.customRecorder = null;
40
+ this.audioChunks = [];
41
+ this.isRecording = false;
42
+ this.isPaused = false;
43
+ this.recordingStartTime = 0;
44
+ this.pausedTime = 0;
45
+ this.currentDurationMs = 0;
46
+ this.currentSize = 0;
47
+ this.bitDepth = DEFAULT_BIT_DEPTH;
48
+ this.currentInterval = DEFAULT_INTERVAL_MS;
49
+ this.currentIntervalAnalysis = DEFAULT_ANALYSIS_INTERVAL_MS;
50
+ this.lastEmittedSize = 0;
51
+ this.lastEmittedTime = 0;
52
+ this.latestPosition = 0;
53
+ this.lastEmittedCompressionSize = 0;
54
+ this.lastEmittedAnalysisTime = 0;
55
+ this.streamUuid = null; // Initialize UUID on first recording start
56
+ this.audioWorkletUrl = audioWorkletUrl;
57
+ this.featuresExtratorUrl = featuresExtratorUrl;
58
+ this.maxBufferSize = maxBufferSize;
59
+ }
60
+ // Utility to handle user media stream
61
+ async getMediaStream() {
62
+ try {
63
+ this.logger?.debug('Requesting user media (microphone)...');
64
+ // First check if the browser supports the necessary audio APIs
65
+ if (!navigator?.mediaDevices?.getUserMedia) {
66
+ this.logger?.error('Browser does not support mediaDevices.getUserMedia');
67
+ throw new Error('Browser does not support audio recording');
68
+ }
69
+ // Get media with detailed audio constraints for better diagnostics
70
+ const constraints = {
71
+ audio: {
72
+ echoCancellation: true,
73
+ noiseSuppression: true,
74
+ autoGainControl: true,
75
+ // Add deviceId constraint if specified
76
+ ...(this.recordingConfig?.deviceId
77
+ ? {
78
+ deviceId: {
79
+ exact: this.recordingConfig.deviceId,
80
+ },
81
+ }
82
+ : {}),
83
+ },
84
+ };
85
+ this.logger?.debug('Media constraints:', constraints);
86
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
87
+ // Get detailed info about the audio track for debugging
88
+ const audioTracks = stream.getAudioTracks();
89
+ if (audioTracks.length > 0) {
90
+ const track = audioTracks[0];
91
+ const settings = track.getSettings();
92
+ this.logger?.debug('Audio track obtained:', {
93
+ label: track.label,
94
+ id: track.id,
95
+ enabled: track.enabled,
96
+ muted: track.muted,
97
+ readyState: track.readyState,
98
+ settings,
99
+ });
100
+ }
101
+ else {
102
+ this.logger?.warn('Stream has no audio tracks!');
103
+ }
104
+ return stream;
105
+ }
106
+ catch (error) {
107
+ this.logger?.error('Failed to get media stream:', error);
108
+ throw error;
109
+ }
110
+ }
111
+ // Prepare recording with options
112
+ async prepareRecording(recordingConfig = {}) {
113
+ if (this.isRecording) {
114
+ this.logger?.warn('Cannot prepare: Recording is already in progress');
115
+ return false;
116
+ }
117
+ try {
118
+ // Check permissions and initialize basic settings
119
+ await this.getMediaStream().then((stream) => {
120
+ // Just verify we can access the microphone by getting a stream, then release it
121
+ stream.getTracks().forEach((track) => track.stop());
122
+ });
123
+ this.bitDepth = encodingToBitDepth({
124
+ encoding: recordingConfig.encoding ?? 'pcm_32bit',
125
+ });
126
+ // Store recording configuration for later use
127
+ this.recordingConfig = recordingConfig;
128
+ // Use custom filename if provided, otherwise fallback to timestamp
129
+ if (recordingConfig.filename) {
130
+ // Remove any existing extension from the filename
131
+ this.streamUuid = recordingConfig.filename.replace(/\.[^/.]+$/, '');
132
+ }
133
+ else {
134
+ this.streamUuid = Date.now().toString();
135
+ }
136
+ this.logger?.debug('Recording preparation completed successfully');
137
+ return true;
138
+ }
139
+ catch (error) {
140
+ this.logger?.error('Error preparing recording:', error);
141
+ return false;
142
+ }
143
+ }
144
+ // Start recording with options
145
+ async startRecording(recordingConfig = {}) {
146
+ if (this.isRecording) {
147
+ throw new Error('Recording is already in progress');
148
+ }
149
+ // If we haven't prepared or have different settings, prepare now
150
+ if (!this.recordingConfig ||
151
+ this.recordingConfig.sampleRate !== recordingConfig.sampleRate ||
152
+ this.recordingConfig.channels !== recordingConfig.channels ||
153
+ this.recordingConfig.encoding !== recordingConfig.encoding) {
154
+ await this.prepareRecording(recordingConfig);
155
+ }
156
+ else {
157
+ this.logger?.debug('Using previously prepared recording configuration');
158
+ }
159
+ // Save recording config for reference
160
+ this.recordingConfig = recordingConfig;
161
+ const audioContext = new (window.AudioContext ||
162
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
163
+ // @ts-ignore - Allow webkitAudioContext for Safari
164
+ window.webkitAudioContext)();
165
+ const stream = await this.getMediaStream();
166
+ const source = audioContext.createMediaStreamSource(stream);
167
+ this.customRecorder = new WebRecorder({
168
+ logger: this.logger,
169
+ audioContext,
170
+ source,
171
+ recordingConfig,
172
+ emitAudioEventCallback: this.customRecorderEventCallback.bind(this),
173
+ emitAudioAnalysisCallback: this.customRecorderAnalysisCallback.bind(this),
174
+ onInterruption: this.handleRecordingInterruption.bind(this),
175
+ });
176
+ await this.customRecorder.init();
177
+ this.customRecorder.start();
178
+ this.isRecording = true;
179
+ this.recordingStartTime = Date.now();
180
+ this.pausedTime = 0;
181
+ this.isPaused = false;
182
+ this.lastEmittedSize = 0;
183
+ this.lastEmittedTime = 0;
184
+ this.lastEmittedCompressionSize = 0;
185
+ this.currentInterval = recordingConfig.interval ?? 1000;
186
+ this.currentIntervalAnalysis = recordingConfig.intervalAnalysis ?? 500;
187
+ this.lastEmittedAnalysisTime = Date.now();
188
+ // Use custom filename if provided, otherwise fallback to timestamp
189
+ if (recordingConfig.filename) {
190
+ // Remove any existing extension from the filename
191
+ this.streamUuid = recordingConfig.filename.replace(/\.[^/.]+$/, '');
192
+ }
193
+ else {
194
+ this.streamUuid = Date.now().toString();
195
+ }
196
+ const fileUri = `${this.streamUuid}.${this.extension}`;
197
+ const streamConfig = {
198
+ fileUri,
199
+ mimeType: `audio/${this.extension}`,
200
+ bitDepth: this.bitDepth,
201
+ channels: recordingConfig.channels ?? 1,
202
+ sampleRate: recordingConfig.sampleRate ?? 44100,
203
+ compression: recordingConfig.output?.compressed?.enabled
204
+ ? {
205
+ ...recordingConfig.output.compressed,
206
+ bitrate: recordingConfig.output.compressed.bitrate ?? 128000,
207
+ size: 0,
208
+ mimeType: 'audio/webm',
209
+ format: recordingConfig.output.compressed.format ?? 'opus',
210
+ compressedFileUri: '',
211
+ }
212
+ : undefined,
213
+ };
214
+ return streamConfig;
215
+ }
216
+ /**
217
+ * Centralized handler for recording interruptions
218
+ */
219
+ handleRecordingInterruption(event) {
220
+ this.logger?.debug(`Received recording interruption: ${event.reason}`);
221
+ // Update local state if the interruption should pause recording
222
+ if (event.isPaused) {
223
+ this.isPaused = true;
224
+ // If this is a device disconnection, handle according to behavior setting
225
+ if (event.reason === 'deviceDisconnected') {
226
+ this.pausedTime = Date.now();
227
+ // Check if we should try fallback to another device
228
+ if (this.recordingConfig?.deviceDisconnectionBehavior ===
229
+ 'fallback') {
230
+ this.logger?.debug('Device disconnected with fallback behavior - attempting to switch to default device');
231
+ // Try to restart with default device
232
+ this.handleDeviceFallback().catch((error) => {
233
+ // If fallback fails, emit warning
234
+ this.logger?.error('Device fallback failed:', error);
235
+ this.emit('onRecordingInterrupted', {
236
+ reason: 'deviceSwitchFailed',
237
+ isPaused: true,
238
+ timestamp: Date.now(),
239
+ message: 'Failed to switch to fallback device. Recording paused.',
240
+ });
241
+ });
242
+ }
243
+ else {
244
+ // Just warn about disconnection if fallback not enabled
245
+ this.logger?.warn('Device disconnected - recording paused automatically');
246
+ this.emit('onRecordingInterrupted', event);
247
+ }
248
+ }
249
+ else {
250
+ // For other interruption types, just emit the event
251
+ this.emit('onRecordingInterrupted', event);
252
+ }
253
+ }
254
+ else {
255
+ // If not causing a pause, just forward the event
256
+ this.emit('onRecordingInterrupted', event);
257
+ }
258
+ }
259
+ /**
260
+ * Handler for audio events from the WebRecorder
261
+ */
262
+ customRecorderEventCallback({ data, position, compression, }) {
263
+ // Keep only the latest chunks based on maxBufferSize
264
+ this.audioChunks.push(new Float32Array(data));
265
+ if (this.audioChunks.length > this.maxBufferSize) {
266
+ this.audioChunks.shift(); // Remove oldest chunk
267
+ }
268
+ this.currentSize += data.byteLength;
269
+ this.emitAudioEvent({ data, position, compression });
270
+ this.lastEmittedTime = Date.now();
271
+ this.lastEmittedSize = this.currentSize;
272
+ this.lastEmittedCompressionSize = compression?.size ?? 0;
273
+ }
274
+ /**
275
+ * Handler for audio analysis events from the WebRecorder
276
+ */
277
+ customRecorderAnalysisCallback(audioAnalysisData) {
278
+ this.emit('AudioAnalysis', audioAnalysisData);
279
+ }
280
+ // Get recording duration
281
+ getRecordingDuration() {
282
+ if (!this.isRecording) {
283
+ return 0;
284
+ }
285
+ return this.currentDurationMs;
286
+ }
287
+ emitAudioEvent({ data, position, compression }) {
288
+ const fileUri = `${this.streamUuid}.${this.extension}`;
289
+ if (compression?.size) {
290
+ this.lastEmittedCompressionSize = compression.size;
291
+ this.totalCompressedSize = compression.totalSize;
292
+ }
293
+ // Update latest position for tracking
294
+ this.latestPosition = position;
295
+ // Calculate duration of this chunk in ms
296
+ const sampleRate = this.recordingConfig?.sampleRate || 44100;
297
+ const chunkDurationMs = (data.length / sampleRate) * 1000;
298
+ // Handle duration calculation
299
+ if (this.customRecorder?.isFirstChunkAfterSwitch) {
300
+ this.logger?.debug(`Processing first chunk after device switch, duration preserved at ${this.currentDurationMs}ms`);
301
+ this.customRecorder.isFirstChunkAfterSwitch = false;
302
+ }
303
+ else {
304
+ this.currentDurationMs += chunkDurationMs;
305
+ }
306
+ const audioEventPayload = {
307
+ fileUri,
308
+ mimeType: `audio/${this.extension}`,
309
+ lastEmittedSize: this.lastEmittedSize,
310
+ deltaSize: data.byteLength,
311
+ position,
312
+ totalSize: this.currentSize,
313
+ buffer: data,
314
+ streamUuid: this.streamUuid ?? '',
315
+ compression: compression
316
+ ? {
317
+ data: compression?.data,
318
+ totalSize: this.totalCompressedSize,
319
+ eventDataSize: compression?.size ?? 0,
320
+ position,
321
+ }
322
+ : undefined,
323
+ };
324
+ this.emit('AudioData', audioEventPayload);
325
+ }
326
+ // Stop recording
327
+ async stopRecording() {
328
+ if (!this.customRecorder) {
329
+ throw new Error('Recorder is not initialized');
330
+ }
331
+ this.logger?.debug('Starting stop process');
332
+ try {
333
+ const { compressedBlob, uncompressedBlob } = await this.customRecorder.stop();
334
+ this.isRecording = false;
335
+ this.isPaused = false;
336
+ let compression;
337
+ let fileUri = `${this.streamUuid}.${this.extension}`;
338
+ let mimeType = `audio/${this.extension}`;
339
+ // Handle both compressed and uncompressed blobs according to new output configuration
340
+ const primaryEnabled = this.recordingConfig?.output?.primary?.enabled ?? true;
341
+ const compressedEnabled = this.recordingConfig?.output?.compressed?.enabled ?? false;
342
+ // Process compressed blob if available and enabled
343
+ if (compressedBlob && compressedEnabled) {
344
+ const compressedUri = URL.createObjectURL(compressedBlob);
345
+ const compressedInfo = {
346
+ compressedFileUri: compressedUri,
347
+ size: compressedBlob.size,
348
+ mimeType: 'audio/webm',
349
+ format: this.recordingConfig?.output?.compressed?.format ??
350
+ 'opus',
351
+ bitrate: this.recordingConfig?.output?.compressed?.bitrate ??
352
+ 128000,
353
+ };
354
+ // Store compression info
355
+ compression = compressedInfo;
356
+ // If primary is disabled, use compressed as main file
357
+ if (!primaryEnabled) {
358
+ this.logger?.debug('Using compressed audio as primary output (primary disabled)');
359
+ fileUri = compressedUri;
360
+ mimeType = 'audio/webm';
361
+ }
362
+ }
363
+ // Process uncompressed WAV if available and primary is enabled
364
+ if (uncompressedBlob && primaryEnabled) {
365
+ const wavUri = URL.createObjectURL(uncompressedBlob);
366
+ fileUri = wavUri;
367
+ mimeType = 'audio/wav';
368
+ }
369
+ else if (!primaryEnabled && !compressedEnabled) {
370
+ // No outputs enabled - streaming only mode
371
+ this.logger?.debug('No outputs enabled - streaming only mode');
372
+ fileUri = '';
373
+ mimeType = 'audio/wav';
374
+ }
375
+ // Use the stored streamUuid for the final filename
376
+ const filename = fileUri
377
+ ? `${this.streamUuid}.${this.extension}`
378
+ : 'stream-only';
379
+ const result = {
380
+ fileUri,
381
+ filename,
382
+ bitDepth: this.bitDepth,
383
+ createdAt: this.recordingStartTime,
384
+ channels: this.recordingConfig?.channels ?? 1,
385
+ sampleRate: this.recordingConfig?.sampleRate ?? 44100,
386
+ durationMs: this.currentDurationMs,
387
+ size: primaryEnabled ? this.currentSize : 0,
388
+ mimeType,
389
+ compression,
390
+ };
391
+ // Reset after creating the result
392
+ this.streamUuid = null;
393
+ // Reset recording state variables to prepare for next recording
394
+ this.currentDurationMs = 0;
395
+ this.currentSize = 0;
396
+ this.lastEmittedSize = 0;
397
+ this.totalCompressedSize = 0;
398
+ this.lastEmittedCompressionSize = 0;
399
+ this.audioChunks = [];
400
+ return result;
401
+ }
402
+ catch (error) {
403
+ this.logger?.error('Error stopping recording:', error);
404
+ throw error;
405
+ }
406
+ }
407
+ // Pause recording
408
+ async pauseRecording() {
409
+ if (!this.isRecording) {
410
+ throw new Error('Recording is not active');
411
+ }
412
+ if (this.isPaused) {
413
+ this.logger?.debug('Recording already paused, skipping');
414
+ return;
415
+ }
416
+ try {
417
+ if (this.customRecorder) {
418
+ this.customRecorder.pause();
419
+ }
420
+ this.isPaused = true;
421
+ this.pausedTime = Date.now();
422
+ }
423
+ catch (error) {
424
+ this.logger?.error('Error in pauseRecording', error);
425
+ // Even if the pause operation failed, make sure our state is consistent
426
+ this.isPaused = true;
427
+ this.pausedTime = Date.now();
428
+ }
429
+ }
430
+ // Resume recording
431
+ async resumeRecording() {
432
+ if (!this.isPaused) {
433
+ throw new Error('Recording is not paused');
434
+ }
435
+ this.logger?.debug('Resuming recording', {
436
+ deviceDisconnectionBehavior: this.recordingConfig?.deviceDisconnectionBehavior,
437
+ isDeviceDisconnected: this.customRecorder?.isDeviceDisconnected,
438
+ });
439
+ try {
440
+ // If we have no recorder, or if the device is disconnected, always attempt fallback
441
+ if (!this.customRecorder ||
442
+ this.customRecorder.isDeviceDisconnected) {
443
+ this.logger?.debug('No recorder exists or device disconnected - attempting fallback on resume');
444
+ await this.handleDeviceFallback();
445
+ // handleDeviceFallback will manage resuming if successful, or emit error if failed.
446
+ return;
447
+ }
448
+ // Normal resume path - device is still connected
449
+ this.customRecorder.resume();
450
+ this.isPaused = false;
451
+ // Adjust the recording start time to account for the pause duration
452
+ const pauseDuration = Date.now() - this.pausedTime;
453
+ this.recordingStartTime += pauseDuration;
454
+ this.pausedTime = 0;
455
+ this.emit('onRecordingInterrupted', {
456
+ reason: 'userResumed',
457
+ isPaused: false,
458
+ timestamp: Date.now(),
459
+ });
460
+ }
461
+ catch (error) {
462
+ this.logger?.error('Resume failed:', error);
463
+ // Fallback to emitting a general failure if resume fails unexpectedly
464
+ this.emit('onRecordingInterrupted', {
465
+ reason: 'resumeFailed', // Use a more specific reason
466
+ isPaused: true, // Remain paused if resume fails
467
+ timestamp: Date.now(),
468
+ message: 'Failed to resume recording. Please stop and start again.',
469
+ });
470
+ }
471
+ }
472
+ // Get current status
473
+ status() {
474
+ const durationMs = this.getRecordingDuration();
475
+ const status = {
476
+ isRecording: this.isRecording,
477
+ isPaused: this.isPaused,
478
+ durationMs,
479
+ size: this.currentSize,
480
+ interval: this.currentInterval,
481
+ intervalAnalysis: this.currentIntervalAnalysis,
482
+ mimeType: `audio/${this.extension}`,
483
+ compression: this.recordingConfig?.output?.compressed?.enabled
484
+ ? {
485
+ size: this.totalCompressedSize,
486
+ mimeType: 'audio/webm',
487
+ format: this.recordingConfig.output.compressed.format ??
488
+ 'opus',
489
+ bitrate: this.recordingConfig.output.compressed.bitrate ??
490
+ 128000,
491
+ compressedFileUri: `${this.streamUuid}.webm`,
492
+ }
493
+ : undefined,
494
+ };
495
+ return status;
496
+ }
497
+ /**
498
+ * Handles device fallback when the current device is disconnected
499
+ */
500
+ async handleDeviceFallback() {
501
+ this.logger?.debug('Starting device fallback procedure');
502
+ if (!this.isRecording) {
503
+ return false;
504
+ }
505
+ try {
506
+ // Save important state before switching
507
+ const currentPosition = this.latestPosition;
508
+ const existingAudioChunks = [...this.audioChunks];
509
+ // Save compressed chunks if available
510
+ let compressedChunks = [];
511
+ if (this.customRecorder) {
512
+ try {
513
+ compressedChunks = this.customRecorder.getCompressedChunks();
514
+ }
515
+ catch (err) {
516
+ this.logger?.warn('Failed to get compressed chunks:', err);
517
+ }
518
+ }
519
+ // Save the current counter value for continuity
520
+ let currentDataPointCounter = 0;
521
+ if (this.customRecorder) {
522
+ currentDataPointCounter =
523
+ this.customRecorder.getDataPointCounter();
524
+ }
525
+ // Clean up existing recorder
526
+ if (this.customRecorder) {
527
+ try {
528
+ this.customRecorder.cleanup();
529
+ }
530
+ catch (cleanupError) {
531
+ this.logger?.warn('Error during cleanup:', cleanupError);
532
+ }
533
+ }
534
+ // Keep recording state true but mark as paused
535
+ this.isPaused = true;
536
+ this.pausedTime = Date.now();
537
+ // Store current size and other stats
538
+ const previousTotalSize = this.currentSize;
539
+ const previousLastEmittedSize = this.lastEmittedSize;
540
+ const previousCompressedSize = this.totalCompressedSize;
541
+ // Try to get a fallback device
542
+ const fallbackDeviceInfo = await this.getFallbackDevice();
543
+ if (!fallbackDeviceInfo) {
544
+ this.emit('onRecordingInterrupted', {
545
+ reason: 'deviceSwitchFailed',
546
+ isPaused: true,
547
+ timestamp: Date.now(),
548
+ message: 'Failed to switch to fallback device. Recording paused.',
549
+ });
550
+ return false;
551
+ }
552
+ // Start recording with the new device
553
+ try {
554
+ const stream = await this.requestPermissionsAndGetUserMedia(fallbackDeviceInfo.deviceId);
555
+ const audioContext = new (window.AudioContext ||
556
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
557
+ // @ts-ignore - Allow webkitAudioContext for Safari
558
+ window.webkitAudioContext)();
559
+ const source = audioContext.createMediaStreamSource(stream);
560
+ // Create a new recorder with the fallback device
561
+ this.customRecorder = new WebRecorder({
562
+ logger: this.logger,
563
+ audioContext,
564
+ source,
565
+ recordingConfig: this.recordingConfig || {},
566
+ emitAudioEventCallback: this.customRecorderEventCallback.bind(this),
567
+ emitAudioAnalysisCallback: this.customRecorderAnalysisCallback.bind(this),
568
+ onInterruption: this.handleRecordingInterruption.bind(this),
569
+ });
570
+ await this.customRecorder.init();
571
+ // Set the initial position to continue from the previous device
572
+ this.customRecorder.setPosition(currentPosition);
573
+ // Reset the data point counter to continue from where the previous device left off
574
+ if (currentDataPointCounter > 0) {
575
+ this.customRecorder.resetDataPointCounter(currentDataPointCounter);
576
+ }
577
+ // Prepare the recorder to handle the device switch properly
578
+ this.customRecorder.prepareForDeviceSwitch();
579
+ // Restore the existing audio chunks
580
+ if (existingAudioChunks.length > 0) {
581
+ this.audioChunks = existingAudioChunks;
582
+ }
583
+ // Restore compressed chunks if available
584
+ if (compressedChunks.length > 0) {
585
+ this.customRecorder.setCompressedChunks(compressedChunks);
586
+ }
587
+ // Start the new recorder while preserving counters
588
+ this.customRecorder.start(true);
589
+ // Update recording state
590
+ this.isPaused = false;
591
+ this.recordingStartTime = Date.now();
592
+ // Restore size counters to maintain continuity
593
+ this.currentSize = previousTotalSize;
594
+ this.lastEmittedSize = previousLastEmittedSize;
595
+ this.totalCompressedSize = previousCompressedSize;
596
+ // Notify that we switched to a fallback device
597
+ if (this.eventCallback) {
598
+ this.eventCallback({
599
+ type: 'deviceFallback',
600
+ device: fallbackDeviceInfo.deviceId,
601
+ timestamp: new Date(),
602
+ });
603
+ }
604
+ return true;
605
+ }
606
+ catch (error) {
607
+ this.logger?.error('Failed to start recording with fallback device', error);
608
+ this.isPaused = true;
609
+ this.emit('onRecordingInterrupted', {
610
+ reason: 'deviceSwitchFailed',
611
+ isPaused: true,
612
+ timestamp: Date.now(),
613
+ message: 'Failed to switch to fallback device. Recording paused.',
614
+ });
615
+ return false;
616
+ }
617
+ }
618
+ catch (error) {
619
+ this.logger?.error('Failed to use fallback device', error);
620
+ this.isPaused = true;
621
+ this.emit('onRecordingInterrupted', {
622
+ reason: 'deviceSwitchFailed',
623
+ isPaused: true,
624
+ timestamp: Date.now(),
625
+ message: 'Failed to switch to fallback device. Recording paused.',
626
+ });
627
+ return false;
628
+ }
629
+ }
630
+ /**
631
+ * Attempts to get a fallback audio device
632
+ */
633
+ async getFallbackDevice() {
634
+ try {
635
+ // Get list of available audio input devices
636
+ const devices = await navigator.mediaDevices.enumerateDevices();
637
+ const audioInputDevices = devices.filter((device) => device.kind === 'audioinput');
638
+ if (audioInputDevices.length === 0) {
639
+ return null;
640
+ }
641
+ // Try to find a device that's not the current one
642
+ if (this.customRecorder) {
643
+ try {
644
+ // Use mediaDevices.enumerateDevices to find the current active device
645
+ const tracks = navigator.mediaDevices
646
+ .getUserMedia({ audio: true })
647
+ .then((stream) => {
648
+ const track = stream.getAudioTracks()[0];
649
+ return track ? track.label : '';
650
+ })
651
+ .catch(() => '');
652
+ const currentTrackLabel = await tracks;
653
+ if (currentTrackLabel) {
654
+ // Find a device with a different label
655
+ const differentDevice = audioInputDevices.find((device) => device.label &&
656
+ device.label !== currentTrackLabel);
657
+ if (differentDevice) {
658
+ return differentDevice;
659
+ }
660
+ }
661
+ }
662
+ catch (err) {
663
+ this.logger?.warn('Error determining current device, using default', err);
664
+ }
665
+ }
666
+ // Return the first available device (default device)
667
+ return audioInputDevices[0];
668
+ }
669
+ catch (error) {
670
+ this.logger?.error('Error finding fallback device:', error);
671
+ return null;
672
+ }
673
+ }
674
+ /**
675
+ * Gets user media with specific device ID
676
+ */
677
+ async requestPermissionsAndGetUserMedia(deviceId) {
678
+ try {
679
+ // Request the specific device
680
+ return await navigator.mediaDevices.getUserMedia({
681
+ audio: {
682
+ deviceId: { exact: deviceId },
683
+ },
684
+ });
685
+ }
686
+ catch (error) {
687
+ this.logger?.error(`Failed to get media for device ${deviceId}`, error);
688
+ // Try with default constraints as fallback
689
+ return await navigator.mediaDevices.getUserMedia({ audio: true });
690
+ }
691
+ }
692
+ init(options) {
693
+ try {
694
+ this.logger = options?.logger;
695
+ this.eventCallback = options?.eventCallback;
696
+ return Promise.resolve();
697
+ }
698
+ catch (error) {
699
+ this.logger?.error('Error initializing AudioStudio', error);
700
+ return Promise.reject(error);
701
+ }
702
+ }
703
+ }
704
+ //# sourceMappingURL=AudioStudio.web.js.map