@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,561 @@
1
+ /*
2
+ * There is a bug where `navigator.mediaDevices.getUserMedia` + `MediaRecorder`
3
+ * creates WEBM files without duration metadata. See:
4
+ * - https://bugs.chromium.org/p/chromium/issues/detail?id=642012
5
+ * - https://stackoverflow.com/a/39971175/13989043
6
+ *
7
+ * This file contains a function that fixes the duration metadata of a WEBM file.
8
+ * - Answer found: https://stackoverflow.com/a/75218309/13989043
9
+ * - Code adapted from: https://github.com/mat-sz/webm-fix-duration
10
+ * (forked from https://github.com/yusitnikov/fix-webm-duration)
11
+ */
12
+
13
+ /*
14
+ * This is the list of possible WEBM file sections by their IDs.
15
+ * Possible types: Container, Binary, Uint, Int, String, Float, Date
16
+ */
17
+ interface Section {
18
+ name: string
19
+ type: string
20
+ }
21
+
22
+ const sections: Record<number, Section> = {
23
+ 0xa45dfa3: { name: 'EBML', type: 'Container' },
24
+ 0x286: { name: 'EBMLVersion', type: 'Uint' },
25
+ 0x2f7: { name: 'EBMLReadVersion', type: 'Uint' },
26
+ 0x2f2: { name: 'EBMLMaxIDLength', type: 'Uint' },
27
+ 0x2f3: { name: 'EBMLMaxSizeLength', type: 'Uint' },
28
+ 0x282: { name: 'DocType', type: 'String' },
29
+ 0x287: { name: 'DocTypeVersion', type: 'Uint' },
30
+ 0x285: { name: 'DocTypeReadVersion', type: 'Uint' },
31
+ 0x6c: { name: 'Void', type: 'Binary' },
32
+ 0x3f: { name: 'CRC-32', type: 'Binary' },
33
+ 0xb538667: { name: 'SignatureSlot', type: 'Container' },
34
+ 0x3e8a: { name: 'SignatureAlgo', type: 'Uint' },
35
+ 0x3e9a: { name: 'SignatureHash', type: 'Uint' },
36
+ 0x3ea5: { name: 'SignaturePublicKey', type: 'Binary' },
37
+ 0x3eb5: { name: 'Signature', type: 'Binary' },
38
+ 0x3e5b: { name: 'SignatureElements', type: 'Container' },
39
+ 0x3e7b: { name: 'SignatureElementList', type: 'Container' },
40
+ 0x2532: { name: 'SignedElement', type: 'Binary' },
41
+ 0x8538067: { name: 'Segment', type: 'Container' },
42
+ 0x14d9b74: { name: 'SeekHead', type: 'Container' },
43
+ 0xdbb: { name: 'Seek', type: 'Container' },
44
+ 0x13ab: { name: 'SeekID', type: 'Binary' },
45
+ 0x13ac: { name: 'SeekPosition', type: 'Uint' },
46
+ 0x549a966: { name: 'Info', type: 'Container' },
47
+ 0x33a4: { name: 'SegmentUID', type: 'Binary' },
48
+ 0x3384: { name: 'SegmentFilename', type: 'String' },
49
+ 0x1cb923: { name: 'PrevUID', type: 'Binary' },
50
+ 0x1c83ab: { name: 'PrevFilename', type: 'String' },
51
+ 0x1eb923: { name: 'NextUID', type: 'Binary' },
52
+ 0x1e83bb: { name: 'NextFilename', type: 'String' },
53
+ 0x444: { name: 'SegmentFamily', type: 'Binary' },
54
+ 0x2924: { name: 'ChapterTranslate', type: 'Container' },
55
+ 0x29fc: { name: 'ChapterTranslateEditionUID', type: 'Uint' },
56
+ 0x29bf: { name: 'ChapterTranslateCodec', type: 'Uint' },
57
+ 0x29a5: { name: 'ChapterTranslateID', type: 'Binary' },
58
+ 0xad7b1: { name: 'TimecodeScale', type: 'Uint' },
59
+ 0x489: { name: 'Duration', type: 'Float' },
60
+ 0x461: { name: 'DateUTC', type: 'Date' },
61
+ 0x3ba9: { name: 'Title', type: 'String' },
62
+ 0xd80: { name: 'MuxingApp', type: 'String' },
63
+ 0x1741: { name: 'WritingApp', type: 'String' },
64
+ // 0xf43b675: { name: 'Cluster', type: 'Container' },
65
+ 0x67: { name: 'Timecode', type: 'Uint' },
66
+ 0x1854: { name: 'SilentTracks', type: 'Container' },
67
+ 0x18d7: { name: 'SilentTrackNumber', type: 'Uint' },
68
+ 0x27: { name: 'Position', type: 'Uint' },
69
+ 0x2b: { name: 'PrevSize', type: 'Uint' },
70
+ 0x23: { name: 'SimpleBlock', type: 'Binary' },
71
+ 0x20: { name: 'BlockGroup', type: 'Container' },
72
+ 0x21: { name: 'Block', type: 'Binary' },
73
+ 0x22: { name: 'BlockVirtual', type: 'Binary' },
74
+ 0x35a1: { name: 'BlockAdditions', type: 'Container' },
75
+ 0x26: { name: 'BlockMore', type: 'Container' },
76
+ 0x6e: { name: 'BlockAddID', type: 'Uint' },
77
+ 0x25: { name: 'BlockAdditional', type: 'Binary' },
78
+ 0x1b: { name: 'BlockDuration', type: 'Uint' },
79
+ 0x7a: { name: 'ReferencePriority', type: 'Uint' },
80
+ 0x7b: { name: 'ReferenceBlock', type: 'Int' },
81
+ 0x7d: { name: 'ReferenceVirtual', type: 'Int' },
82
+ 0x24: { name: 'CodecState', type: 'Binary' },
83
+ 0x35a2: { name: 'DiscardPadding', type: 'Int' },
84
+ 0xe: { name: 'Slices', type: 'Container' },
85
+ 0x68: { name: 'TimeSlice', type: 'Container' },
86
+ 0x4c: { name: 'LaceNumber', type: 'Uint' },
87
+ 0x4d: { name: 'FrameNumber', type: 'Uint' },
88
+ 0x4b: { name: 'BlockAdditionID', type: 'Uint' },
89
+ 0x4e: { name: 'Delay', type: 'Uint' },
90
+ 0x4f: { name: 'SliceDuration', type: 'Uint' },
91
+ 0x48: { name: 'ReferenceFrame', type: 'Container' },
92
+ 0x49: { name: 'ReferenceOffset', type: 'Uint' },
93
+ 0x4a: { name: 'ReferenceTimeCode', type: 'Uint' },
94
+ 0x2f: { name: 'EncryptedBlock', type: 'Binary' },
95
+ 0x654ae6b: { name: 'Tracks', type: 'Container' },
96
+ 0x2e: { name: 'TrackEntry', type: 'Container' },
97
+ 0x57: { name: 'TrackNumber', type: 'Uint' },
98
+ 0x33c5: { name: 'TrackUID', type: 'Uint' },
99
+ 0x3: { name: 'TrackType', type: 'Uint' },
100
+ 0x39: { name: 'FlagEnabled', type: 'Uint' },
101
+ 0x8: { name: 'FlagDefault', type: 'Uint' },
102
+ 0x15aa: { name: 'FlagForced', type: 'Uint' },
103
+ 0x1c: { name: 'FlagLacing', type: 'Uint' },
104
+ 0x2de7: { name: 'MinCache', type: 'Uint' },
105
+ 0x2df8: { name: 'MaxCache', type: 'Uint' },
106
+ 0x3e383: { name: 'DefaultDuration', type: 'Uint' },
107
+ 0x34e7a: { name: 'DefaultDecodedFieldDuration', type: 'Uint' },
108
+ 0x3314f: { name: 'TrackTimecodeScale', type: 'Float' },
109
+ 0x137f: { name: 'TrackOffset', type: 'Int' },
110
+ 0x15ee: { name: 'MaxBlockAdditionID', type: 'Uint' },
111
+ 0x136e: { name: 'Name', type: 'String' },
112
+ 0x2b59c: { name: 'Language', type: 'String' },
113
+ 0x6: { name: 'CodecID', type: 'String' },
114
+ 0x23a2: { name: 'CodecPrivate', type: 'Binary' },
115
+ 0x58688: { name: 'CodecName', type: 'String' },
116
+ 0x3446: { name: 'AttachmentLink', type: 'Uint' },
117
+ 0x1a9697: { name: 'CodecSettings', type: 'String' },
118
+ 0x1b4040: { name: 'CodecInfoURL', type: 'String' },
119
+ 0x6b240: { name: 'CodecDownloadURL', type: 'String' },
120
+ 0x2a: { name: 'CodecDecodeAll', type: 'Uint' },
121
+ 0x2fab: { name: 'TrackOverlay', type: 'Uint' },
122
+ 0x16aa: { name: 'CodecDelay', type: 'Uint' },
123
+ 0x16bb: { name: 'SeekPreRoll', type: 'Uint' },
124
+ 0x2624: { name: 'TrackTranslate', type: 'Container' },
125
+ 0x26fc: { name: 'TrackTranslateEditionUID', type: 'Uint' },
126
+ 0x26bf: { name: 'TrackTranslateCodec', type: 'Uint' },
127
+ 0x26a5: { name: 'TrackTranslateTrackID', type: 'Binary' },
128
+ 0x60: { name: 'Video', type: 'Container' },
129
+ 0x1a: { name: 'FlagInterlaced', type: 'Uint' },
130
+ 0x13b8: { name: 'StereoMode', type: 'Uint' },
131
+ 0x13c0: { name: 'AlphaMode', type: 'Uint' },
132
+ 0x13b9: { name: 'OldStereoMode', type: 'Uint' },
133
+ 0x30: { name: 'PixelWidth', type: 'Uint' },
134
+ 0x3a: { name: 'PixelHeight', type: 'Uint' },
135
+ 0x14aa: { name: 'PixelCropBottom', type: 'Uint' },
136
+ 0x14bb: { name: 'PixelCropTop', type: 'Uint' },
137
+ 0x14cc: { name: 'PixelCropLeft', type: 'Uint' },
138
+ 0x14dd: { name: 'PixelCropRight', type: 'Uint' },
139
+ 0x14b0: { name: 'DisplayWidth', type: 'Uint' },
140
+ 0x14ba: { name: 'DisplayHeight', type: 'Uint' },
141
+ 0x14b2: { name: 'DisplayUnit', type: 'Uint' },
142
+ 0x14b3: { name: 'AspectRatioType', type: 'Uint' },
143
+ 0xeb524: { name: 'ColourSpace', type: 'Binary' },
144
+ 0xfb523: { name: 'GammaValue', type: 'Float' },
145
+ 0x383e3: { name: 'FrameRate', type: 'Float' },
146
+ 0x61: { name: 'Audio', type: 'Container' },
147
+ 0x35: { name: 'SamplingFrequency', type: 'Float' },
148
+ 0x38b5: { name: 'OutputSamplingFrequency', type: 'Float' },
149
+ 0x1f: { name: 'Channels', type: 'Uint' },
150
+ 0x3d7b: { name: 'ChannelPositions', type: 'Binary' },
151
+ 0x2264: { name: 'BitDepth', type: 'Uint' },
152
+ 0x62: { name: 'TrackOperation', type: 'Container' },
153
+ 0x63: { name: 'TrackCombinePlanes', type: 'Container' },
154
+ 0x64: { name: 'TrackPlane', type: 'Container' },
155
+ 0x65: { name: 'TrackPlaneUID', type: 'Uint' },
156
+ 0x66: { name: 'TrackPlaneType', type: 'Uint' },
157
+ 0x69: { name: 'TrackJoinBlocks', type: 'Container' },
158
+ 0x6d: { name: 'TrackJoinUID', type: 'Uint' },
159
+ 0x40: { name: 'TrickTrackUID', type: 'Uint' },
160
+ 0x41: { name: 'TrickTrackSegmentUID', type: 'Binary' },
161
+ 0x46: { name: 'TrickTrackFlag', type: 'Uint' },
162
+ 0x47: { name: 'TrickMasterTrackUID', type: 'Uint' },
163
+ 0x44: { name: 'TrickMasterTrackSegmentUID', type: 'Binary' },
164
+ 0x2d80: { name: 'ContentEncodings', type: 'Container' },
165
+ 0x2240: { name: 'ContentEncoding', type: 'Container' },
166
+ 0x1031: { name: 'ContentEncodingOrder', type: 'Uint' },
167
+ 0x1032: { name: 'ContentEncodingScope', type: 'Uint' },
168
+ 0x1033: { name: 'ContentEncodingType', type: 'Uint' },
169
+ 0x1034: { name: 'ContentCompression', type: 'Container' },
170
+ 0x254: { name: 'ContentCompAlgo', type: 'Uint' },
171
+ 0x255: { name: 'ContentCompSettings', type: 'Binary' },
172
+ 0x1035: { name: 'ContentEncryption', type: 'Container' },
173
+ 0x7e1: { name: 'ContentEncAlgo', type: 'Uint' },
174
+ 0x7e2: { name: 'ContentEncKeyID', type: 'Binary' },
175
+ 0x7e3: { name: 'ContentSignature', type: 'Binary' },
176
+ 0x7e4: { name: 'ContentSigKeyID', type: 'Binary' },
177
+ 0x7e5: { name: 'ContentSigAlgo', type: 'Uint' },
178
+ 0x7e6: { name: 'ContentSigHashAlgo', type: 'Uint' },
179
+ 0xc53bb6b: { name: 'Cues', type: 'Container' },
180
+ 0x3b: { name: 'CuePoint', type: 'Container' },
181
+ 0x33: { name: 'CueTime', type: 'Uint' },
182
+ 0x37: { name: 'CueTrackPositions', type: 'Container' },
183
+ 0x77: { name: 'CueTrack', type: 'Uint' },
184
+ 0x71: { name: 'CueClusterPosition', type: 'Uint' },
185
+ 0x70: { name: 'CueRelativePosition', type: 'Uint' },
186
+ 0x32: { name: 'CueDuration', type: 'Uint' },
187
+ 0x1378: { name: 'CueBlockNumber', type: 'Uint' },
188
+ 0x6a: { name: 'CueCodecState', type: 'Uint' },
189
+ 0x5b: { name: 'CueReference', type: 'Container' },
190
+ 0x16: { name: 'CueRefTime', type: 'Uint' },
191
+ 0x17: { name: 'CueRefCluster', type: 'Uint' },
192
+ 0x135f: { name: 'CueRefNumber', type: 'Uint' },
193
+ 0x6b: { name: 'CueRefCodecState', type: 'Uint' },
194
+ 0x941a469: { name: 'Attachments', type: 'Container' },
195
+ 0x21a7: { name: 'AttachedFile', type: 'Container' },
196
+ 0x67e: { name: 'FileDescription', type: 'String' },
197
+ 0x66e: { name: 'FileName', type: 'String' },
198
+ 0x660: { name: 'FileMimeType', type: 'String' },
199
+ 0x65c: { name: 'FileData', type: 'Binary' },
200
+ 0x6ae: { name: 'FileUID', type: 'Uint' },
201
+ 0x675: { name: 'FileReferral', type: 'Binary' },
202
+ 0x661: { name: 'FileUsedStartTime', type: 'Uint' },
203
+ 0x662: { name: 'FileUsedEndTime', type: 'Uint' },
204
+ 0x43a770: { name: 'Chapters', type: 'Container' },
205
+ 0x5b9: { name: 'EditionEntry', type: 'Container' },
206
+ 0x5bc: { name: 'EditionUID', type: 'Uint' },
207
+ 0x5bd: { name: 'EditionFlagHidden', type: 'Uint' },
208
+ 0x5db: { name: 'EditionFlagDefault', type: 'Uint' },
209
+ 0x5dd: { name: 'EditionFlagOrdered', type: 'Uint' },
210
+ 0x36: { name: 'ChapterAtom', type: 'Container' },
211
+ 0x33c4: { name: 'ChapterUID', type: 'Uint' },
212
+ 0x1654: { name: 'ChapterStringUID', type: 'String' },
213
+ 0x11: { name: 'ChapterTimeStart', type: 'Uint' },
214
+ 0x12: { name: 'ChapterTimeEnd', type: 'Uint' },
215
+ 0x18: { name: 'ChapterFlagHidden', type: 'Uint' },
216
+ 0x598: { name: 'ChapterFlagEnabled', type: 'Uint' },
217
+ 0x2e67: { name: 'ChapterSegmentUID', type: 'Binary' },
218
+ 0x2ebc: { name: 'ChapterSegmentEditionUID', type: 'Uint' },
219
+ 0x23c3: { name: 'ChapterPhysicalEquiv', type: 'Uint' },
220
+ 0xf: { name: 'ChapterTrack', type: 'Container' },
221
+ 0x9: { name: 'ChapterTrackNumber', type: 'Uint' },
222
+ 0x0: { name: 'ChapterDisplay', type: 'Container' },
223
+ 0x5: { name: 'ChapString', type: 'String' },
224
+ 0x37c: { name: 'ChapLanguage', type: 'String' },
225
+ 0x37e: { name: 'ChapCountry', type: 'String' },
226
+ 0x2944: { name: 'ChapProcess', type: 'Container' },
227
+ 0x2955: { name: 'ChapProcessCodecID', type: 'Uint' },
228
+ 0x50d: { name: 'ChapProcessPrivate', type: 'Binary' },
229
+ 0x2911: { name: 'ChapProcessCommand', type: 'Container' },
230
+ 0x2922: { name: 'ChapProcessTime', type: 'Uint' },
231
+ 0x2933: { name: 'ChapProcessData', type: 'Binary' },
232
+ 0x254c367: { name: 'Tags', type: 'Container' },
233
+ 0x3373: { name: 'Tag', type: 'Container' },
234
+ 0x23c0: { name: 'Targets', type: 'Container' },
235
+ 0x28ca: { name: 'TargetTypeValue', type: 'Uint' },
236
+ 0x23ca: { name: 'TargetType', type: 'String' },
237
+ 0x23c5: { name: 'TagTrackUID', type: 'Uint' },
238
+ 0x23c9: { name: 'TagEditionUID', type: 'Uint' },
239
+ 0x23c4: { name: 'TagChapterUID', type: 'Uint' },
240
+ 0x23c6: { name: 'TagAttachmentUID', type: 'Uint' },
241
+ 0x27c8: { name: 'SimpleTag', type: 'Container' },
242
+ 0x5a3: { name: 'TagName', type: 'String' },
243
+ 0x47a: { name: 'TagLanguage', type: 'String' },
244
+ 0x484: { name: 'TagDefault', type: 'Uint' },
245
+ 0x487: { name: 'TagString', type: 'String' },
246
+ 0x485: { name: 'TagBinary', type: 'Binary' },
247
+ }
248
+
249
+ class WebmBase<T> {
250
+ source?: Uint8Array
251
+ data?: T
252
+ name: string
253
+ type: string
254
+
255
+ constructor(name = 'Unknown', type = 'Unknown') {
256
+ this.name = name
257
+ this.type = type
258
+ }
259
+
260
+ updateBySource() {}
261
+
262
+ setSource(source: Uint8Array) {
263
+ this.source = source
264
+ this.updateBySource()
265
+ }
266
+
267
+ updateByData() {}
268
+
269
+ setData(data: T) {
270
+ this.data = data
271
+ this.updateByData()
272
+ }
273
+ }
274
+
275
+ class WebmUint extends WebmBase<string> {
276
+ constructor(name: string, type: string) {
277
+ super(name, type || 'Uint')
278
+ }
279
+
280
+ updateBySource() {
281
+ // use hex representation of a number instead of number value
282
+ this.data = ''
283
+ for (let i = 0; i < this.source!.length; i++) {
284
+ const hex = this.source![i].toString(16)
285
+ this.data += padHex(hex)
286
+ }
287
+ }
288
+
289
+ updateByData() {
290
+ const length = this.data!.length / 2
291
+ this.source = new Uint8Array(length)
292
+ for (let i = 0; i < length; i++) {
293
+ const hex = this.data!.substr(i * 2, 2)
294
+ this.source[i] = parseInt(hex, 16)
295
+ }
296
+ }
297
+
298
+ getValue() {
299
+ return parseInt(this.data!, 16)
300
+ }
301
+
302
+ setValue(value: number) {
303
+ this.setData(padHex(value.toString(16)))
304
+ }
305
+ }
306
+
307
+ function padHex(hex: string) {
308
+ return hex.length % 2 === 1 ? '0' + hex : hex
309
+ }
310
+
311
+ class WebmFloat extends WebmBase<number> {
312
+ constructor(name: string, type: string) {
313
+ super(name, type || 'Float')
314
+ }
315
+
316
+ getFloatArrayType() {
317
+ return this.source && this.source.length === 4
318
+ ? Float32Array
319
+ : Float64Array
320
+ }
321
+ updateBySource() {
322
+ const byteArray = this.source!.reverse()
323
+ const floatArrayType = this.getFloatArrayType()
324
+ const buffer = new ArrayBuffer(byteArray.buffer.byteLength)
325
+ new Uint8Array(buffer).set(new Uint8Array(byteArray.buffer))
326
+ const floatArray = new floatArrayType(buffer)
327
+ this.data! = floatArray[0]
328
+ }
329
+ updateByData() {
330
+ const floatArrayType = this.getFloatArrayType()
331
+ const floatArray = new floatArrayType([this.data!])
332
+ const byteArray = new Uint8Array(floatArray.buffer)
333
+ this.source = byteArray.reverse()
334
+ }
335
+ getValue() {
336
+ return this.data
337
+ }
338
+ setValue(value: number) {
339
+ this.setData(value)
340
+ }
341
+ }
342
+
343
+ interface ContainerData {
344
+ id: number
345
+ idHex?: string
346
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
347
+ data: WebmBase<any>
348
+ }
349
+
350
+ class WebmContainer extends WebmBase<ContainerData[]> {
351
+ offset: number = 0
352
+ data: ContainerData[] = []
353
+
354
+ constructor(name: string, type: string) {
355
+ super(name, type || 'Container')
356
+ }
357
+
358
+ readByte() {
359
+ return this.source![this.offset++]
360
+ }
361
+ readUint() {
362
+ const firstByte = this.readByte()
363
+ const bytes = 8 - firstByte.toString(2).length
364
+ let value = firstByte - (1 << (7 - bytes))
365
+ for (let i = 0; i < bytes; i++) {
366
+ // don't use bit operators to support x86
367
+ value *= 256
368
+ value += this.readByte()
369
+ }
370
+ return value
371
+ }
372
+ updateBySource() {
373
+ let end: number | undefined = undefined
374
+ this.data = []
375
+ for (
376
+ this.offset = 0;
377
+ this.offset < this.source!.length;
378
+ this.offset = end
379
+ ) {
380
+ const id = this.readUint()
381
+ const len = this.readUint()
382
+ end = Math.min(this.offset + len, this.source!.length)
383
+ const data = this.source!.slice(this.offset, end)
384
+
385
+ const info = sections[id] || { name: 'Unknown', type: 'Unknown' }
386
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
387
+ let ctr: any = WebmBase
388
+ switch (info.type) {
389
+ case 'Container':
390
+ ctr = WebmContainer
391
+ break
392
+ case 'Uint':
393
+ ctr = WebmUint
394
+ break
395
+ case 'Float':
396
+ ctr = WebmFloat
397
+ break
398
+ }
399
+ const section = new ctr(info.name, info.type)
400
+ section.setSource(data)
401
+ this.data.push({
402
+ id,
403
+ idHex: id.toString(16),
404
+ data: section,
405
+ })
406
+ }
407
+ }
408
+ writeUint(x: number, draft = false) {
409
+ let flag = 0x80
410
+ let bytes = 1
411
+ // eslint-disable-next-line no-empty
412
+ for (; x >= flag && bytes < 8; bytes++, flag *= 0x80) {}
413
+
414
+ if (!draft) {
415
+ let value = flag + x
416
+ for (let i = bytes - 1; i >= 0; i--) {
417
+ // don't use bit operators to support x86
418
+ const c = value % 256
419
+ this.source![this.offset! + i] = c
420
+ value = (value - c) / 256
421
+ }
422
+ }
423
+
424
+ this.offset += bytes
425
+ }
426
+
427
+ writeSections(draft = false) {
428
+ this.offset = 0
429
+ for (let i = 0; i < this.data.length; i++) {
430
+ const section = this.data[i],
431
+ content = section.data.source,
432
+ contentLength = content!.length
433
+ this.writeUint(section.id, draft)
434
+ this.writeUint(contentLength, draft)
435
+ if (!draft) {
436
+ this.source!.set(content!, this.offset)
437
+ }
438
+ this.offset += contentLength
439
+ }
440
+ return this.offset
441
+ }
442
+
443
+ updateByData() {
444
+ // run without accessing this.source to determine total length - need to know it to create Uint8Array
445
+ const length = this.writeSections(true)
446
+ this.source = new Uint8Array(length)
447
+ // now really write data
448
+ this.writeSections()
449
+ }
450
+
451
+ getSectionById(id: number) {
452
+ for (let i = 0; i < this.data.length; i++) {
453
+ const section = this.data[i]
454
+ if (section.id === id) {
455
+ return section.data
456
+ }
457
+ }
458
+
459
+ return undefined
460
+ }
461
+ }
462
+
463
+ class WebmFile extends WebmContainer {
464
+ constructor(source: Uint8Array) {
465
+ super('File', 'File')
466
+ this.setSource(source)
467
+ }
468
+
469
+ fixDuration(duration: number) {
470
+ const segmentSection = this.getSectionById(0x8538067) as WebmContainer
471
+ if (!segmentSection) {
472
+ return false
473
+ }
474
+
475
+ const infoSection = segmentSection.getSectionById(
476
+ 0x549a966
477
+ ) as WebmContainer
478
+ if (!infoSection) {
479
+ return false
480
+ }
481
+
482
+ const timeScaleSection = infoSection.getSectionById(
483
+ 0xad7b1
484
+ ) as WebmFloat
485
+ if (!timeScaleSection) {
486
+ return false
487
+ }
488
+
489
+ let durationSection = infoSection.getSectionById(0x489) as WebmFloat
490
+ if (durationSection) {
491
+ if (durationSection.getValue()! <= 0) {
492
+ durationSection.setValue(duration)
493
+ } else {
494
+ return false
495
+ }
496
+ } else {
497
+ // append Duration section
498
+ durationSection = new WebmFloat('Duration', 'Float')
499
+ durationSection.setValue(duration)
500
+ infoSection.data.push({
501
+ id: 0x489,
502
+ data: durationSection,
503
+ })
504
+ }
505
+
506
+ // set default time scale to 1 millisecond (1000000 nanoseconds)
507
+ timeScaleSection.setValue(1000000)
508
+ infoSection.updateByData()
509
+ segmentSection.updateByData()
510
+ this.updateByData()
511
+
512
+ return true
513
+ }
514
+
515
+ toBlob(type = 'video/webm') {
516
+ const buffer = new ArrayBuffer(this.source!.buffer.byteLength)
517
+ new Uint8Array(buffer).set(new Uint8Array(this.source!.buffer))
518
+ return new Blob([buffer], { type })
519
+ }
520
+ }
521
+
522
+ /**
523
+ * Fixes duration on MediaRecorder output.
524
+ * @param blob Input Blob with incorrect duration.
525
+ * @param duration Correct duration (in milliseconds).
526
+ * @param type Output blob mimetype (default: video/webm).
527
+ * @returns
528
+ */
529
+ export const webmFixDuration = (
530
+ blob: Blob,
531
+ duration: number,
532
+ type = 'video/webm'
533
+ ): Promise<Blob> => {
534
+ return new Promise((resolve, reject) => {
535
+ try {
536
+ const reader = new FileReader()
537
+
538
+ reader.addEventListener('loadend', () => {
539
+ try {
540
+ const result = reader.result as ArrayBuffer
541
+ const file = new WebmFile(new Uint8Array(result))
542
+ if (file.fixDuration(duration)) {
543
+ resolve(file.toBlob(type))
544
+ } else {
545
+ resolve(blob)
546
+ }
547
+ } catch (ex) {
548
+ reject(ex)
549
+ }
550
+ })
551
+
552
+ reader.addEventListener('error', () =>
553
+ reject(new Error('FileReader error'))
554
+ )
555
+
556
+ reader.readAsArrayBuffer(blob)
557
+ } catch (ex) {
558
+ reject(ex)
559
+ }
560
+ })
561
+ }