gravix-rtc-react-native 0.0.0-stage → 0.2.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 (378) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/LICENSE +20 -0
  3. package/LICENSES/Apache-2.0.txt +175 -0
  4. package/NOTICE +68 -0
  5. package/README.md +313 -2
  6. package/android/build.gradle +137 -0
  7. package/android/consumer-rules.pro +29 -0
  8. package/android/src/main/AndroidManifest.xml +9 -0
  9. package/android/src/main/cpp/CMakeLists.txt +48 -0
  10. package/android/src/main/cpp/gx_dfn_engine.c +332 -0
  11. package/android/src/main/cpp/gx_fx_jni.cpp +34 -0
  12. package/android/src/main/cpp/gx_ns_jni.c +162 -0
  13. package/android/src/main/cpp/third_party/onnxruntime/LICENSE +21 -0
  14. package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_c_api.h +9227 -0
  15. package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_ep_c_api.h +3315 -0
  16. package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_error_code.h +98 -0
  17. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixAudioHooks.kt +144 -0
  18. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixAudioModule.kt +105 -0
  19. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixNativeAudio.kt +310 -0
  20. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtc.kt +134 -0
  21. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtcModule.kt +176 -0
  22. package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtcPackage.kt +39 -0
  23. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioDeviceKind.java +44 -0
  24. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioManagerUtils.kt +120 -0
  25. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioSwitchManager.java +270 -0
  26. package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioType.kt +50 -0
  27. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicCaptureRouter.kt +375 -0
  28. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicDecoder.kt +339 -0
  29. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicInterruptionWatcher.kt +82 -0
  30. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMicRelease.kt +117 -0
  31. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMixKernel.kt +191 -0
  32. package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMixerEngine.kt +617 -0
  33. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/AudioRoutePolicy.kt +137 -0
  34. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GravixCaptureProcessing.kt +67 -0
  35. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxCaptureDump.kt +143 -0
  36. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxCaptureSwitcher.kt +196 -0
  37. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNoiseProcessor.kt +795 -0
  38. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsLogic.kt +180 -0
  39. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsNative.kt +83 -0
  40. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsStudio.kt +372 -0
  41. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxRecordStartGuard.kt +76 -0
  42. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxRecorderSeam.kt +434 -0
  43. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxSingerSource.kt +37 -0
  44. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxVoiceFx.kt +160 -0
  45. package/android/src/main/java/com/gravitycompile/gravixrtc/processing/OtherAppAudioPolicy.kt +143 -0
  46. package/android/src/main/java/org/webrtc/audio/WebRtcAudioTrackHelper.kt +43 -0
  47. package/docs/ANDROID_NATIVE_HOOKS.md +102 -0
  48. package/docs/FEATURE_MATRIX.md +192 -0
  49. package/docs/IOS_NATIVE_HOOKS.md +190 -0
  50. package/gravix-rtc-react-native.podspec +49 -0
  51. package/ios/GravixRtc.h +7 -0
  52. package/ios/GravixRtc.mm +273 -0
  53. package/ios/GravixRtcSetup.h +29 -0
  54. package/ios/GravixRtcSetup.m +52 -0
  55. package/ios/audio/GravixRtcIosAudio.m +157 -0
  56. package/ios/audio/GxAudioDevice.h +62 -0
  57. package/ios/audio/GxAudioDevice.m +857 -0
  58. package/ios/audio/GxAudioSetup.h +34 -0
  59. package/ios/audio/GxAudioSetup.m +70 -0
  60. package/ios/audio/GxCompat.swift +53 -0
  61. package/ios/audio/GxIosAudio.swift +99 -0
  62. package/ios/audio/GxMusicController.swift +150 -0
  63. package/ios/audio/GxMusicPost.swift +362 -0
  64. package/ios/audio/GxNoiseProcessor.swift +718 -0
  65. package/ios/audio/GxNsDfn3CoreML.swift +195 -0
  66. package/ios/audio/GxNsStudioEngine.swift +237 -0
  67. package/ios/audio/GxNsStudioPack.swift +212 -0
  68. package/ios/audio/GxVoiceFx.swift +161 -0
  69. package/ios/audio/gx_adm_state.h +153 -0
  70. package/ios/gxcore/apple/gx_music_post.c +312 -0
  71. package/ios/gxcore/apple/gx_pcm.c +30 -0
  72. package/ios/gxcore/apple/gxns_build.h +19 -0
  73. package/ios/gxcore/apple/gxns_ios.c +17 -0
  74. package/ios/gxcore/apple/include/gx_fx.h +3 -0
  75. package/ios/gxcore/apple/include/gx_music_post.h +88 -0
  76. package/ios/gxcore/apple/include/gx_ns.h +3 -0
  77. package/ios/gxcore/apple/include/gx_pcm.h +33 -0
  78. package/ios/gxcore/apple/include/gxns_ios.h +18 -0
  79. package/ios/gxcore/apple/include/module.modulemap +10 -0
  80. package/ios/gxcore/apple/shim/rnnoise.h +2 -0
  81. package/ios/gxcore/apple/shim/speex_resampler.h +2 -0
  82. package/ios/gxcore/apple/units/gx_fx_forward.cpp +3 -0
  83. package/ios/gxcore/apple/units/gx_ns.c +7 -0
  84. package/ios/gxcore/apple/units/gx_ns_timer.c +7 -0
  85. package/ios/gxcore/apple/units/rnnoise_celt_lpc.c +7 -0
  86. package/ios/gxcore/apple/units/rnnoise_denoise.c +7 -0
  87. package/ios/gxcore/apple/units/rnnoise_kiss_fft.c +7 -0
  88. package/ios/gxcore/apple/units/rnnoise_pitch.c +7 -0
  89. package/ios/gxcore/apple/units/rnnoise_rnn.c +7 -0
  90. package/ios/gxcore/apple/units/rnnoise_rnn_data.c +7 -0
  91. package/ios/gxcore/apple/units/speexdsp_resample.c +7 -0
  92. package/ios/resources/ns/DFN3-NOTICE.txt +10 -0
  93. package/ios/resources/ns/DeepFilterNet-LICENSE-APACHE +201 -0
  94. package/ios/resources/ns/DeepFilterNet-LICENSE-MIT +19 -0
  95. package/ios/resources/ns/gxns_dfn3.mlmodelc/analytics/coremldata.bin +0 -0
  96. package/ios/resources/ns/gxns_dfn3.mlmodelc/coremldata.bin +0 -0
  97. package/ios/resources/ns/gxns_dfn3.mlmodelc/metadata.json +338 -0
  98. package/ios/resources/ns/gxns_dfn3.mlmodelc/model.mil +927 -0
  99. package/ios/resources/ns/gxns_dfn3.mlmodelc/weights/weight.bin +0 -0
  100. package/lib/module/android/bindings.js +49 -0
  101. package/lib/module/android/bindings.js.map +1 -0
  102. package/lib/module/android/index.js +13 -0
  103. package/lib/module/android/index.js.map +1 -0
  104. package/lib/module/android/micMute.js +341 -0
  105. package/lib/module/android/micMute.js.map +1 -0
  106. package/lib/module/android/nativeAudio.js +47 -0
  107. package/lib/module/android/nativeAudio.js.map +1 -0
  108. package/lib/module/android/noiseSuppression.js +170 -0
  109. package/lib/module/android/noiseSuppression.js.map +1 -0
  110. package/lib/module/android/roomMusic.js +142 -0
  111. package/lib/module/android/roomMusic.js.map +1 -0
  112. package/lib/module/android/singerMode.js +31 -0
  113. package/lib/module/android/singerMode.js.map +1 -0
  114. package/lib/module/android/voiceEffects.js +65 -0
  115. package/lib/module/android/voiceEffects.js.map +1 -0
  116. package/lib/module/audio/AudioManager.js +79 -0
  117. package/lib/module/audio/AudioManager.js.map +1 -0
  118. package/lib/module/audio/AudioSession.js +90 -0
  119. package/lib/module/audio/AudioSession.js.map +1 -0
  120. package/lib/module/audio/remoteVolume.js +54 -0
  121. package/lib/module/audio/remoteVolume.js.map +1 -0
  122. package/lib/module/components/VideoView.js +109 -0
  123. package/lib/module/components/VideoView.js.map +1 -0
  124. package/lib/module/features/audioScenario.js +53 -0
  125. package/lib/module/features/audioScenario.js.map +1 -0
  126. package/lib/module/features/beauty.js +828 -0
  127. package/lib/module/features/beauty.js.map +1 -0
  128. package/lib/module/features/micRelease.js +89 -0
  129. package/lib/module/features/micRelease.js.map +1 -0
  130. package/lib/module/features/noiseSuppression.js +213 -0
  131. package/lib/module/features/noiseSuppression.js.map +1 -0
  132. package/lib/module/features/roomMusic.js +90 -0
  133. package/lib/module/features/roomMusic.js.map +1 -0
  134. package/lib/module/features/roomMusicAndroid.js +294 -0
  135. package/lib/module/features/roomMusicAndroid.js.map +1 -0
  136. package/lib/module/features/roomMusicIos.js +250 -0
  137. package/lib/module/features/roomMusicIos.js.map +1 -0
  138. package/lib/module/features/roomMusicTypes.js +53 -0
  139. package/lib/module/features/roomMusicTypes.js.map +1 -0
  140. package/lib/module/features/screenShare.js +22 -0
  141. package/lib/module/features/screenShare.js.map +1 -0
  142. package/lib/module/features/singerMode.js +73 -0
  143. package/lib/module/features/singerMode.js.map +1 -0
  144. package/lib/module/features/voiceEffects.js +148 -0
  145. package/lib/module/features/voiceEffects.js.map +1 -0
  146. package/lib/module/hooks/context.js +34 -0
  147. package/lib/module/hooks/context.js.map +1 -0
  148. package/lib/module/hooks/hooks.js +145 -0
  149. package/lib/module/hooks/hooks.js.map +1 -0
  150. package/lib/module/hooks/types.js +4 -0
  151. package/lib/module/hooks/types.js.map +1 -0
  152. package/lib/module/hooks/useRoomEvents.js +30 -0
  153. package/lib/module/hooks/useRoomEvents.js.map +1 -0
  154. package/lib/module/index.js +43 -0
  155. package/lib/module/index.js.map +1 -0
  156. package/lib/module/ios/autoAttach.js +14 -0
  157. package/lib/module/ios/autoAttach.js.map +1 -0
  158. package/lib/module/ios/iosAudio.js +149 -0
  159. package/lib/module/ios/iosAudio.js.map +1 -0
  160. package/lib/module/join/appStateLifecycle.js +54 -0
  161. package/lib/module/join/appStateLifecycle.js.map +1 -0
  162. package/lib/module/micPolicy.js +249 -0
  163. package/lib/module/micPolicy.js.map +1 -0
  164. package/lib/module/native/NativeGravixAudio.js +18 -0
  165. package/lib/module/native/NativeGravixAudio.js.map +1 -0
  166. package/lib/module/native/NativeGravixRtc.js +13 -0
  167. package/lib/module/native/NativeGravixRtc.js.map +1 -0
  168. package/lib/module/native/module.js +23 -0
  169. package/lib/module/native/module.js.map +1 -0
  170. package/lib/module/package.json +1 -0
  171. package/lib/module/polyfills/DOMException.js +22 -0
  172. package/lib/module/polyfills/DOMException.js.map +1 -0
  173. package/lib/module/polyfills/early.js +18 -0
  174. package/lib/module/polyfills/early.js.map +1 -0
  175. package/lib/module/polyfills/event.js +71 -0
  176. package/lib/module/polyfills/event.js.map +1 -0
  177. package/lib/module/polyfills/sha256.js +92 -0
  178. package/lib/module/polyfills/sha256.js.map +1 -0
  179. package/lib/module/polyfills/subtleHmac.js +91 -0
  180. package/lib/module/polyfills/subtleHmac.js.map +1 -0
  181. package/lib/module/polyfills/textCoding.js +164 -0
  182. package/lib/module/polyfills/textCoding.js.map +1 -0
  183. package/lib/module/registerGlobals.js +135 -0
  184. package/lib/module/registerGlobals.js.map +1 -0
  185. package/lib/module/unsupported.js +90 -0
  186. package/lib/module/unsupported.js.map +1 -0
  187. package/lib/module/version.js +7 -0
  188. package/lib/module/version.js.map +1 -0
  189. package/lib/typescript/package.json +1 -0
  190. package/lib/typescript/src/android/bindings.d.ts +14 -0
  191. package/lib/typescript/src/android/bindings.d.ts.map +1 -0
  192. package/lib/typescript/src/android/index.d.ts +9 -0
  193. package/lib/typescript/src/android/index.d.ts.map +1 -0
  194. package/lib/typescript/src/android/micMute.d.ts +124 -0
  195. package/lib/typescript/src/android/micMute.d.ts.map +1 -0
  196. package/lib/typescript/src/android/nativeAudio.d.ts +40 -0
  197. package/lib/typescript/src/android/nativeAudio.d.ts.map +1 -0
  198. package/lib/typescript/src/android/noiseSuppression.d.ts +48 -0
  199. package/lib/typescript/src/android/noiseSuppression.d.ts.map +1 -0
  200. package/lib/typescript/src/android/roomMusic.d.ts +80 -0
  201. package/lib/typescript/src/android/roomMusic.d.ts.map +1 -0
  202. package/lib/typescript/src/android/singerMode.d.ts +27 -0
  203. package/lib/typescript/src/android/singerMode.d.ts.map +1 -0
  204. package/lib/typescript/src/android/voiceEffects.d.ts +30 -0
  205. package/lib/typescript/src/android/voiceEffects.d.ts.map +1 -0
  206. package/lib/typescript/src/audio/AudioManager.d.ts +32 -0
  207. package/lib/typescript/src/audio/AudioManager.d.ts.map +1 -0
  208. package/lib/typescript/src/audio/AudioSession.d.ts +70 -0
  209. package/lib/typescript/src/audio/AudioSession.d.ts.map +1 -0
  210. package/lib/typescript/src/audio/remoteVolume.d.ts +8 -0
  211. package/lib/typescript/src/audio/remoteVolume.d.ts.map +1 -0
  212. package/lib/typescript/src/components/VideoView.d.ts +44 -0
  213. package/lib/typescript/src/components/VideoView.d.ts.map +1 -0
  214. package/lib/typescript/src/features/audioScenario.d.ts +24 -0
  215. package/lib/typescript/src/features/audioScenario.d.ts.map +1 -0
  216. package/lib/typescript/src/features/beauty.d.ts +377 -0
  217. package/lib/typescript/src/features/beauty.d.ts.map +1 -0
  218. package/lib/typescript/src/features/micRelease.d.ts +28 -0
  219. package/lib/typescript/src/features/micRelease.d.ts.map +1 -0
  220. package/lib/typescript/src/features/noiseSuppression.d.ts +64 -0
  221. package/lib/typescript/src/features/noiseSuppression.d.ts.map +1 -0
  222. package/lib/typescript/src/features/roomMusic.d.ts +41 -0
  223. package/lib/typescript/src/features/roomMusic.d.ts.map +1 -0
  224. package/lib/typescript/src/features/roomMusicAndroid.d.ts +60 -0
  225. package/lib/typescript/src/features/roomMusicAndroid.d.ts.map +1 -0
  226. package/lib/typescript/src/features/roomMusicIos.d.ts +56 -0
  227. package/lib/typescript/src/features/roomMusicIos.d.ts.map +1 -0
  228. package/lib/typescript/src/features/roomMusicTypes.d.ts +76 -0
  229. package/lib/typescript/src/features/roomMusicTypes.d.ts.map +1 -0
  230. package/lib/typescript/src/features/screenShare.d.ts +12 -0
  231. package/lib/typescript/src/features/screenShare.d.ts.map +1 -0
  232. package/lib/typescript/src/features/singerMode.d.ts +20 -0
  233. package/lib/typescript/src/features/singerMode.d.ts.map +1 -0
  234. package/lib/typescript/src/features/voiceEffects.d.ts +46 -0
  235. package/lib/typescript/src/features/voiceEffects.d.ts.map +1 -0
  236. package/lib/typescript/src/hooks/context.d.ts +12 -0
  237. package/lib/typescript/src/hooks/context.d.ts.map +1 -0
  238. package/lib/typescript/src/hooks/hooks.d.ts +54 -0
  239. package/lib/typescript/src/hooks/hooks.d.ts.map +1 -0
  240. package/lib/typescript/src/hooks/types.d.ts +8 -0
  241. package/lib/typescript/src/hooks/types.d.ts.map +1 -0
  242. package/lib/typescript/src/hooks/useRoomEvents.d.ts +13 -0
  243. package/lib/typescript/src/hooks/useRoomEvents.d.ts.map +1 -0
  244. package/lib/typescript/src/index.d.ts +37 -0
  245. package/lib/typescript/src/index.d.ts.map +1 -0
  246. package/lib/typescript/src/ios/autoAttach.d.ts +2 -0
  247. package/lib/typescript/src/ios/autoAttach.d.ts.map +1 -0
  248. package/lib/typescript/src/ios/iosAudio.d.ts +96 -0
  249. package/lib/typescript/src/ios/iosAudio.d.ts.map +1 -0
  250. package/lib/typescript/src/join/appStateLifecycle.d.ts +14 -0
  251. package/lib/typescript/src/join/appStateLifecycle.d.ts.map +1 -0
  252. package/lib/typescript/src/micPolicy.d.ts +104 -0
  253. package/lib/typescript/src/micPolicy.d.ts.map +1 -0
  254. package/lib/typescript/src/native/NativeGravixAudio.d.ts +21 -0
  255. package/lib/typescript/src/native/NativeGravixAudio.d.ts.map +1 -0
  256. package/lib/typescript/src/native/NativeGravixRtc.d.ts +31 -0
  257. package/lib/typescript/src/native/NativeGravixRtc.d.ts.map +1 -0
  258. package/lib/typescript/src/native/module.d.ts +7 -0
  259. package/lib/typescript/src/native/module.d.ts.map +1 -0
  260. package/lib/typescript/src/polyfills/DOMException.d.ts +3 -0
  261. package/lib/typescript/src/polyfills/DOMException.d.ts.map +1 -0
  262. package/lib/typescript/src/polyfills/early.d.ts +2 -0
  263. package/lib/typescript/src/polyfills/early.d.ts.map +1 -0
  264. package/lib/typescript/src/polyfills/event.d.ts +2 -0
  265. package/lib/typescript/src/polyfills/event.d.ts.map +1 -0
  266. package/lib/typescript/src/polyfills/sha256.d.ts +3 -0
  267. package/lib/typescript/src/polyfills/sha256.d.ts.map +1 -0
  268. package/lib/typescript/src/polyfills/subtleHmac.d.ts +37 -0
  269. package/lib/typescript/src/polyfills/subtleHmac.d.ts.map +1 -0
  270. package/lib/typescript/src/polyfills/textCoding.d.ts +36 -0
  271. package/lib/typescript/src/polyfills/textCoding.d.ts.map +1 -0
  272. package/lib/typescript/src/registerGlobals.d.ts +42 -0
  273. package/lib/typescript/src/registerGlobals.d.ts.map +1 -0
  274. package/lib/typescript/src/unsupported.d.ts +42 -0
  275. package/lib/typescript/src/unsupported.d.ts.map +1 -0
  276. package/lib/typescript/src/version.d.ts +3 -0
  277. package/lib/typescript/src/version.d.ts.map +1 -0
  278. package/metro/index.js +39 -0
  279. package/metro/noise-stub.js +10 -0
  280. package/native/gxfx/include/gx_fx.h +82 -0
  281. package/native/gxfx/src/gx_fx.cpp +527 -0
  282. package/native/gxfx/third_party/README.md +4 -0
  283. package/native/gxfx/third_party/signalsmith-linear/LICENSE.txt +21 -0
  284. package/native/gxfx/third_party/signalsmith-linear/approx.h +191 -0
  285. package/native/gxfx/third_party/signalsmith-linear/fft.h +1386 -0
  286. package/native/gxfx/third_party/signalsmith-linear/linear.h +1129 -0
  287. package/native/gxfx/third_party/signalsmith-linear/platform/approx-cmsisdsp.h +40 -0
  288. package/native/gxfx/third_party/signalsmith-linear/platform/basic-fill-warnings.h +61 -0
  289. package/native/gxfx/third_party/signalsmith-linear/platform/fft-accelerate.h +340 -0
  290. package/native/gxfx/third_party/signalsmith-linear/platform/fft-cmsisdsp.h +307 -0
  291. package/native/gxfx/third_party/signalsmith-linear/platform/fft-ipp.h +532 -0
  292. package/native/gxfx/third_party/signalsmith-linear/platform/fft-linear.h +213 -0
  293. package/native/gxfx/third_party/signalsmith-linear/platform/fft-pffft-double.h +221 -0
  294. package/native/gxfx/third_party/signalsmith-linear/platform/fft-pffft.h +221 -0
  295. package/native/gxfx/third_party/signalsmith-linear/platform/linear-accelerate.h +593 -0
  296. package/native/gxfx/third_party/signalsmith-linear/platform/linear-cmsisdsp.h +713 -0
  297. package/native/gxfx/third_party/signalsmith-linear/platform/linear-xsimd-dispatch.cpp +6 -0
  298. package/native/gxfx/third_party/signalsmith-linear/platform/linear-xsimd.h +818 -0
  299. package/native/gxfx/third_party/signalsmith-linear/stft.h +700 -0
  300. package/native/gxfx/third_party/signalsmith-stretch-LICENSE.txt +21 -0
  301. package/native/gxfx/third_party/signalsmith-stretch.h +1060 -0
  302. package/native/gxns/CMakeLists.txt +67 -0
  303. package/native/gxns/include/gx_ns.h +285 -0
  304. package/native/gxns/src/gx_ns.c +1518 -0
  305. package/native/gxns/src/gx_ns_timer.c +84 -0
  306. package/native/gxns/src/gx_ns_timer.h +29 -0
  307. package/native/gxns/third_party/VENDOR.md +43 -0
  308. package/native/gxns/third_party/rnnoise/AUTHORS +1 -0
  309. package/native/gxns/third_party/rnnoise/COPYING +31 -0
  310. package/native/gxns/third_party/rnnoise/include/rnnoise.h +117 -0
  311. package/native/gxns/third_party/rnnoise/src/_kiss_fft_guts.h +182 -0
  312. package/native/gxns/third_party/rnnoise/src/arch.h +261 -0
  313. package/native/gxns/third_party/rnnoise/src/celt_lpc.c +172 -0
  314. package/native/gxns/third_party/rnnoise/src/celt_lpc.h +45 -0
  315. package/native/gxns/third_party/rnnoise/src/common.h +48 -0
  316. package/native/gxns/third_party/rnnoise/src/denoise.c +664 -0
  317. package/native/gxns/third_party/rnnoise/src/kiss_fft.c +601 -0
  318. package/native/gxns/third_party/rnnoise/src/kiss_fft.h +203 -0
  319. package/native/gxns/third_party/rnnoise/src/opus_types.h +159 -0
  320. package/native/gxns/third_party/rnnoise/src/pitch.c +523 -0
  321. package/native/gxns/third_party/rnnoise/src/pitch.h +149 -0
  322. package/native/gxns/third_party/rnnoise/src/rnn.c +178 -0
  323. package/native/gxns/third_party/rnnoise/src/rnn.h +65 -0
  324. package/native/gxns/third_party/rnnoise/src/rnn_data.c +11051 -0
  325. package/native/gxns/third_party/rnnoise/src/rnn_data.h +34 -0
  326. package/native/gxns/third_party/rnnoise/src/tansig_table.h +45 -0
  327. package/native/gxns/third_party/speexdsp/AUTHORS +18 -0
  328. package/native/gxns/third_party/speexdsp/COPYING +35 -0
  329. package/native/gxns/third_party/speexdsp/arch.h +232 -0
  330. package/native/gxns/third_party/speexdsp/resample.c +1239 -0
  331. package/native/gxns/third_party/speexdsp/resample_neon.h +339 -0
  332. package/native/gxns/third_party/speexdsp/resample_sse.h +128 -0
  333. package/native/gxns/third_party/speexdsp/speex_resampler.h +343 -0
  334. package/package.json +186 -4
  335. package/src/android/bindings.ts +61 -0
  336. package/src/android/index.ts +10 -0
  337. package/src/android/micMute.ts +422 -0
  338. package/src/android/nativeAudio.ts +94 -0
  339. package/src/android/noiseSuppression.ts +209 -0
  340. package/src/android/roomMusic.ts +193 -0
  341. package/src/android/singerMode.ts +53 -0
  342. package/src/android/voiceEffects.ts +126 -0
  343. package/src/audio/AudioManager.ts +111 -0
  344. package/src/audio/AudioSession.ts +190 -0
  345. package/src/audio/remoteVolume.ts +58 -0
  346. package/src/components/VideoView.tsx +152 -0
  347. package/src/features/audioScenario.ts +58 -0
  348. package/src/features/beauty.ts +1057 -0
  349. package/src/features/micRelease.ts +108 -0
  350. package/src/features/noiseSuppression.ts +251 -0
  351. package/src/features/roomMusic.ts +106 -0
  352. package/src/features/roomMusicAndroid.ts +310 -0
  353. package/src/features/roomMusicIos.ts +300 -0
  354. package/src/features/roomMusicTypes.ts +97 -0
  355. package/src/features/screenShare.ts +19 -0
  356. package/src/features/singerMode.ts +70 -0
  357. package/src/features/voiceEffects.ts +245 -0
  358. package/src/hooks/context.tsx +38 -0
  359. package/src/hooks/hooks.ts +247 -0
  360. package/src/hooks/types.ts +10 -0
  361. package/src/hooks/useRoomEvents.ts +35 -0
  362. package/src/index.tsx +134 -0
  363. package/src/ios/autoAttach.ts +11 -0
  364. package/src/ios/iosAudio.ts +202 -0
  365. package/src/join/appStateLifecycle.ts +55 -0
  366. package/src/micPolicy.ts +312 -0
  367. package/src/native/NativeGravixAudio.ts +30 -0
  368. package/src/native/NativeGravixRtc.ts +38 -0
  369. package/src/native/module.ts +22 -0
  370. package/src/polyfills/DOMException.ts +20 -0
  371. package/src/polyfills/early.ts +16 -0
  372. package/src/polyfills/event.ts +88 -0
  373. package/src/polyfills/sha256.ts +112 -0
  374. package/src/polyfills/subtleHmac.ts +124 -0
  375. package/src/polyfills/textCoding.ts +197 -0
  376. package/src/registerGlobals.ts +201 -0
  377. package/src/unsupported.ts +139 -0
  378. package/src/version.ts +4 -0
@@ -0,0 +1,270 @@
1
+ // Copyright 2023-2026 LiveKit, Inc.
2
+ // Modifications Copyright 2026 Gravity Compile.
3
+ // Licensed under the Apache License, Version 2.0; see LICENSES/Apache-2.0.txt.
4
+
5
+ package com.gravitycompile.gravixrtc.audio;
6
+
7
+ import android.content.Context;
8
+ import android.media.AudioAttributes;
9
+ import android.media.AudioManager;
10
+ import android.os.Handler;
11
+ import android.os.Looper;
12
+
13
+ import androidx.annotation.NonNull;
14
+ import androidx.annotation.Nullable;
15
+
16
+ import com.twilio.audioswitch.AudioDevice;
17
+ import com.twilio.audioswitch.AudioSwitch;
18
+
19
+ import java.util.ArrayList;
20
+ import java.util.Collections;
21
+ import java.util.List;
22
+ import java.util.Objects;
23
+
24
+ import kotlin.Unit;
25
+ import kotlin.jvm.functions.Function2;
26
+
27
+ public class AudioSwitchManager {
28
+ @NonNull
29
+ private final Context context;
30
+ @NonNull
31
+ private final AudioManager audioManager;
32
+
33
+ public boolean loggingEnabled;
34
+ @NonNull
35
+ public Function2<
36
+ ? super List<? extends AudioDevice>,
37
+ ? super AudioDevice,
38
+ Unit> audioDeviceChangeListener = (devices, currentDevice) -> null;
39
+
40
+ @NonNull
41
+ public AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = (i -> {
42
+ });
43
+
44
+ @NonNull
45
+ public List<Class<? extends AudioDevice>> preferredDeviceList;
46
+
47
+ // AudioSwitch is not threadsafe, so all calls should be done on the main thread.
48
+ private final Handler handler = new Handler(Looper.getMainLooper());
49
+
50
+ @Nullable
51
+ private AudioSwitch audioSwitch;
52
+
53
+ /**
54
+ * When true, AudioSwitchHandler will request audio focus on start and abandon on stop.
55
+ *
56
+ * Defaults to true.
57
+ */
58
+ private boolean manageAudioFocus = true;
59
+
60
+ /**
61
+ * The audio focus mode to use while started.
62
+ *
63
+ * Defaults to [AudioManager.AUDIOFOCUS_GAIN].
64
+ */
65
+ private int focusMode = AudioManager.AUDIOFOCUS_GAIN;
66
+
67
+ /**
68
+ * The audio mode to use while started.
69
+ *
70
+ * Defaults to AudioManager.MODE_IN_COMMUNICATION.
71
+ */
72
+ private int audioMode = AudioManager.MODE_IN_COMMUNICATION;
73
+
74
+ /**
75
+ * The audio stream type to use when requesting audio focus on pre-O devices.
76
+ *
77
+ * Defaults to [AudioManager.STREAM_VOICE_CALL].
78
+ *
79
+ * Refer to this [compatibility table](https://source.android.com/docs/core/audio/attributes#compatibility)
80
+ * to ensure that your values match between android versions.
81
+ *
82
+ * Note: Manual audio routing may not work appropriately when using non-default values.
83
+ */
84
+ private int audioStreamType = AudioManager.STREAM_VOICE_CALL;
85
+
86
+ /**
87
+ * The audio attribute usage type to use when requesting audio focus on devices O and beyond.
88
+ *
89
+ * Defaults to [AudioAttributes.USAGE_VOICE_COMMUNICATION].
90
+ *
91
+ * Refer to this [compatibility table](https://source.android.com/docs/core/audio/attributes#compatibility)
92
+ * to ensure that your values match between android versions.
93
+ *
94
+ * Note: Manual audio routing may not work appropriately when using non-default values.
95
+ */
96
+ private int audioAttributeUsageType = AudioAttributes.USAGE_VOICE_COMMUNICATION;
97
+
98
+ /**
99
+ * The audio attribute content type to use when requesting audio focus on devices O and beyond.
100
+ *
101
+ * Defaults to [AudioAttributes.CONTENT_TYPE_SPEECH].
102
+ *
103
+ * Refer to this [compatibility table](https://source.android.com/docs/core/audio/attributes#compatibility)
104
+ * to ensure that your values match between android versions.
105
+ *
106
+ * Note: Manual audio routing may not work appropriately when using non-default values.
107
+ */
108
+ private int audioAttributeContentType = AudioAttributes.CONTENT_TYPE_SPEECH;
109
+
110
+ /**
111
+ * On certain Android devices, audio routing does not function properly and bluetooth microphones will not work
112
+ * unless audio mode is set to [AudioManager.MODE_IN_COMMUNICATION] or [AudioManager.MODE_IN_CALL].
113
+ *
114
+ * AudioSwitchManager by default will not handle audio routing in those cases to avoid audio issues.
115
+ *
116
+ * If this set to true, AudioSwitchManager will attempt to do audio routing, though behavior is undefined.
117
+ */
118
+ private boolean forceHandleAudioRouting = false;
119
+
120
+ public AudioSwitchManager(@NonNull Context context) {
121
+ this.context = context;
122
+ this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
123
+
124
+ preferredDeviceList = new ArrayList<>();
125
+ preferredDeviceList.add(AudioDevice.BluetoothHeadset.class);
126
+ preferredDeviceList.add(AudioDevice.WiredHeadset.class);
127
+ preferredDeviceList.add(AudioDevice.Speakerphone.class);
128
+ preferredDeviceList.add(AudioDevice.Earpiece.class);
129
+ }
130
+
131
+ public void start() {
132
+ if (audioSwitch == null) {
133
+ handler.removeCallbacksAndMessages(null);
134
+ handler.postAtFrontOfQueue(() -> {
135
+ audioSwitch = new AudioSwitch(
136
+ context,
137
+ loggingEnabled,
138
+ audioFocusChangeListener,
139
+ preferredDeviceList
140
+ );
141
+ audioSwitch.setManageAudioFocus(manageAudioFocus);
142
+ audioSwitch.setFocusMode(focusMode);
143
+ audioSwitch.setAudioMode(audioMode);
144
+ audioSwitch.setAudioStreamType(audioStreamType);
145
+ audioSwitch.setAudioAttributeContentType(audioAttributeContentType);
146
+ audioSwitch.setAudioAttributeUsageType(audioAttributeUsageType);
147
+ audioSwitch.setForceHandleAudioRouting(forceHandleAudioRouting);
148
+ audioSwitch.start(audioDeviceChangeListener);
149
+ audioSwitch.activate();
150
+ });
151
+ }
152
+ }
153
+
154
+ public void stop() {
155
+ handler.removeCallbacksAndMessages(null);
156
+ handler.postAtFrontOfQueue(() -> {
157
+ if (audioSwitch != null) {
158
+ audioSwitch.stop();
159
+ }
160
+ audioSwitch = null;
161
+ });
162
+ }
163
+
164
+ public void setMicrophoneMute(boolean mute) {
165
+ audioManager.setMicrophoneMute(mute);
166
+ }
167
+
168
+ @Nullable
169
+ public AudioDevice selectedAudioDevice() {
170
+ AudioSwitch audioSwitchTemp = audioSwitch;
171
+ if (audioSwitchTemp != null) {
172
+ return audioSwitchTemp.getSelectedAudioDevice();
173
+ } else {
174
+ return null;
175
+ }
176
+ }
177
+
178
+ @NonNull
179
+ public List<AudioDevice> availableAudioDevices() {
180
+ AudioSwitch audioSwitchTemp = audioSwitch;
181
+ if (audioSwitchTemp != null) {
182
+ return audioSwitchTemp.getAvailableAudioDevices();
183
+ } else {
184
+ return Collections.emptyList();
185
+ }
186
+ }
187
+
188
+ public void selectAudioOutput(@NonNull Class<? extends AudioDevice> audioDeviceClass) {
189
+ handler.post(() -> {
190
+ if (audioSwitch != null) {
191
+ List<AudioDevice> devices = availableAudioDevices();
192
+ AudioDevice audioDevice = null;
193
+
194
+ for (AudioDevice device : devices) {
195
+ if (device.getClass().equals(audioDeviceClass)) {
196
+ audioDevice = device;
197
+ break;
198
+ }
199
+ }
200
+
201
+ if (audioDevice != null) {
202
+ audioSwitch.selectDevice(audioDevice);
203
+ }
204
+ }
205
+ });
206
+ }
207
+
208
+ public void enableSpeakerphone(boolean enable) {
209
+ if (enable) {
210
+ audioManager.setSpeakerphoneOn(true);
211
+ } else {
212
+ audioManager.setSpeakerphoneOn(false);
213
+ }
214
+ }
215
+
216
+ public void selectAudioOutput(@Nullable AudioDeviceKind kind) {
217
+ if (kind != null) {
218
+ selectAudioOutput(kind.audioDeviceClass);
219
+ }
220
+ }
221
+
222
+ public void setManageAudioFocus(boolean manage) {
223
+ this.manageAudioFocus = manage;
224
+ if (audioSwitch != null) {
225
+ Objects.requireNonNull(audioSwitch).setManageAudioFocus(this.manageAudioFocus);
226
+ }
227
+ }
228
+
229
+ public void setFocusMode(int focusMode) {
230
+ this.focusMode = focusMode;
231
+ if (audioSwitch != null) {
232
+ Objects.requireNonNull(audioSwitch).setFocusMode(this.focusMode);
233
+ }
234
+ }
235
+
236
+ public void setAudioMode(int audioMode) {
237
+ this.audioMode = audioMode;
238
+ if (audioSwitch != null) {
239
+ Objects.requireNonNull(audioSwitch).setAudioMode(this.audioMode);
240
+ }
241
+ }
242
+
243
+ public void setAudioStreamType(int streamType) {
244
+ this.audioStreamType = streamType;
245
+ if (audioSwitch != null) {
246
+ Objects.requireNonNull(audioSwitch).setAudioStreamType(this.audioStreamType);
247
+ }
248
+ }
249
+
250
+ public void setAudioAttributesUsageType(int usageType) {
251
+ this.audioAttributeUsageType = usageType;
252
+ if (audioSwitch != null) {
253
+ Objects.requireNonNull(audioSwitch).setAudioAttributeUsageType(this.audioAttributeUsageType);
254
+ }
255
+ }
256
+
257
+ public void setAudioAttributesContentType(int contentType) {
258
+ this.audioAttributeContentType = contentType;
259
+ if (audioSwitch != null) {
260
+ Objects.requireNonNull(audioSwitch).setAudioAttributeContentType(this.audioAttributeContentType);
261
+ }
262
+ }
263
+
264
+ public void setForceHandleAudioRouting(boolean force) {
265
+ this.forceHandleAudioRouting = force;
266
+ if (audioSwitch != null) {
267
+ Objects.requireNonNull(audioSwitch).setForceHandleAudioRouting(this.forceHandleAudioRouting);
268
+ }
269
+ }
270
+ }
@@ -0,0 +1,50 @@
1
+ // Copyright 2023-2026 LiveKit, Inc.
2
+ // Modifications Copyright 2026 Gravity Compile.
3
+ // Licensed under the Apache License, Version 2.0; see LICENSES/Apache-2.0.txt.
4
+
5
+ package com.gravitycompile.gravixrtc.audio
6
+
7
+ import android.media.AudioAttributes
8
+ import android.media.AudioManager
9
+
10
+ sealed class AudioType(
11
+ val audioMode: Int,
12
+ val audioAttributes: AudioAttributes,
13
+ val audioStreamType: Int
14
+ ) {
15
+ /**
16
+ * An audio type for general media playback usage (i.e. listener-only use cases).
17
+ *
18
+ * Audio routing is handled automatically by the system in normal media mode,
19
+ * and bluetooth microphones may not work on some devices.
20
+ */
21
+ class MediaAudioType : AudioType(
22
+ AudioManager.MODE_NORMAL,
23
+ AudioAttributes.Builder()
24
+ .setUsage(AudioAttributes.USAGE_MEDIA)
25
+ .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
26
+ .build(),
27
+ AudioManager.STREAM_MUSIC
28
+ )
29
+
30
+ /**
31
+ * An audio type for communications (i.e. participating a call or otherwise
32
+ * publishing local microphone).
33
+ *
34
+ * Audio routing can be manually controlled.
35
+ */
36
+ class CommunicationAudioType : AudioType(
37
+ AudioManager.MODE_IN_COMMUNICATION,
38
+ AudioAttributes.Builder()
39
+ .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
40
+ .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
41
+ .build(),
42
+ AudioManager.STREAM_VOICE_CALL
43
+ )
44
+
45
+ /**
46
+ * An audio type that takes in a user-defined [AudioAttributes] and audio stream type.
47
+ */
48
+ class CustomAudioType(audioMode: Int, audioAttributes: AudioAttributes, audioStreamType: Int) :
49
+ AudioType(audioMode, audioAttributes, audioStreamType)
50
+ }
@@ -0,0 +1,375 @@
1
+ package com.gravitycompile.gravixrtc.music
2
+
3
+ import java.nio.ByteBuffer
4
+ import java.nio.ByteOrder
5
+ import java.nio.ShortBuffer
6
+
7
+ /**
8
+ * Where the room music joins the capture stream (Android types free, so it is
9
+ * unit-tested on the JVM: MusicCaptureRouterTest).
10
+ *
11
+ * - PRE (default, and always with no voice effect): mixed into the
12
+ * WebRtcAudioRecord buffer before WebRTC's APM, exactly as before this
13
+ * class existed. The voice effect is bypassed (dry) so the music is never
14
+ * pitch shifted.
15
+ * - POST (a voice effect is on): the pre-APM callback only consumes the ring
16
+ * (position, completion, mic gain, ducking level) and hands the samples to
17
+ * the APM capturePostProcessing processor, which adds them AFTER the
18
+ * effect. The effect stays on the voice and never touches the music.
19
+ *
20
+ * SYNC: on POST the voice reaches the post site later than the music by the
21
+ * noise suppressor's and the effect chain's delay; the music goes through a
22
+ * delay line of that length ([delayTargetSamples], set by the control side
23
+ * from gx_ns / gx_fx status), so listeners hear both as on PRE. The host's
24
+ * monitor is not delayed (as on PRE). A delay change (effect switched) fades
25
+ * the music out, re-times while silent, and fades it back in.
26
+ *
27
+ * LEVEL: the post-site music is the kernel's output unchanged (file level x
28
+ * music volume), the same level PRE gives whenever WebRTC's AGC/NS are off,
29
+ * which GravixRoomMusic does for every session (relaxVoiceProcessing). With
30
+ * WebRTC's AGC left on (raw peers, relaxVoiceProcessing=false) PRE music is
31
+ * lifted by the AGC and POST is not.
32
+ *
33
+ * One mix site per buffer. A mid-song switch fades the music out at the old
34
+ * site (MusicMixKernel.gate, ~20 ms), waits for the delay line to drain,
35
+ * flips, and fades it in at the new one. PRE -> POST keeps the effect bypassed
36
+ * until the pre-mixed music has drained out of NS and the effect chain. The
37
+ * hand-off to the post site is a one-shot token: when the processor does not
38
+ * run (not installed, APM shape changed), the next capture callback falls
39
+ * back to PRE. A capture restart (mic republish: a gap of > 100 ms between
40
+ * capture callbacks) discards the token without falling back.
41
+ *
42
+ * Threads: [pre] and [post] run on WebRTC's record thread (the APM runs
43
+ * synchronously inside the record callback); [begin]/[end] and the volatile
44
+ * inputs on the control thread.
45
+ */
46
+ class MusicCaptureRouter(private val kernel: MusicMixKernel) {
47
+
48
+ enum class Route { PRE, POST }
49
+
50
+ /** The playing track, as the router needs it. */
51
+ interface Track {
52
+ /** Not consuming (paused). */
53
+ val paused: Boolean
54
+ /** Ring read + position bookkeeping; returns the samples read. */
55
+ fun read(dst: ShortArray, n: Int): Int
56
+ /** [read] returned less than asked (underrun / end of track). */
57
+ fun onShort(got: Int)
58
+ /** The exact music samples mixed (host monitor). */
59
+ fun monitor(musicOut: ShortArray, n: Int)
60
+ /** The fade envelope reached 0 (pause / stop requests complete). */
61
+ fun onEnvZero()
62
+ }
63
+
64
+ /** The voice-effect side: bypass = music is in the pre-APM buffer. */
65
+ interface Fx {
66
+ fun setMusicBypass(on: Boolean)
67
+ }
68
+
69
+ companion object {
70
+ const val MAX_DELAY = 8192 // samples (170 ms at 48 kHz)
71
+ private const val LINE = 16384
72
+ private const val MASK = LINE - 1
73
+ private const val GAP_NS = 100_000_000L
74
+ }
75
+
76
+ /** A voice effect (not "none") is configured. Control thread. */
77
+ @Volatile var effectWanted: Boolean = false
78
+
79
+ /**
80
+ * Voice delay between the capture callback and the post site (NS + effect
81
+ * chain), in samples at the capture rate. Control thread; -1 = unknown.
82
+ */
83
+ @Volatile var delayTargetSamples: Int = -1
84
+
85
+ @Volatile var fx: Fx? = null
86
+
87
+ @Volatile var route: Route = Route.PRE
88
+ private set
89
+
90
+ /** Hand-offs the post site never took (processor not running). */
91
+ @Volatile var droppedTokens: Int = 0
92
+ private set
93
+
94
+ /** Route flips (logs). */
95
+ @Volatile var switches: Int = 0
96
+ private set
97
+
98
+ /** Capture restarts seen (gaps), delay re-timings (logs). */
99
+ @Volatile var captureGaps: Int = 0
100
+ private set
101
+ @Volatile var retimes: Int = 0
102
+ private set
103
+
104
+ /** Music delay in use on POST, samples. */
105
+ @Volatile var delaySamples: Int = 0
106
+ private set
107
+
108
+ // ---- post-site shape, written by [post] ----
109
+ @Volatile private var postFrames = 0
110
+ @Volatile private var postShapeOk = false
111
+ @Volatile private var postLastNs = 0L
112
+ private var preSincePost = Int.MAX_VALUE / 2
113
+ private var lastPreNs = 0L
114
+
115
+ // ---- record-thread state ----
116
+ private var switching = false
117
+ private var retiming = false
118
+ private var bypassHoldBuffers = 0
119
+ private var tokenPending = false
120
+ private var tokenHasMusic = false
121
+ private var tokenN = 0
122
+ private var tokenTrack: Track? = null
123
+ private var voice = ShortArray(0)
124
+ private var music = ShortArray(0)
125
+ private var musicOut = ShortArray(0)
126
+ private var postMusic = ShortArray(0)
127
+ private var postMusicOut = ShortArray(0)
128
+ private val line = ShortArray(LINE)
129
+ private var lineW = 0
130
+ /** Consecutive zero samples written to the delay line (drained when >= [delaySamples]). */
131
+ private var zeroRun = Int.MAX_VALUE / 2
132
+
133
+ /**
134
+ * The post site ran recently with a frame that can take a [rate]/[channels]
135
+ * track one-to-one: mono, 10 ms frames at the same rate. Any thread.
136
+ */
137
+ fun postCompatible(rate: Int, channels: Int, nowNs: Long = System.nanoTime()): Boolean =
138
+ channels == 1 && postShapeOk && postFrames * 100 == rate &&
139
+ postLastNs != 0L && nowNs - postLastNs < 500_000_000L
140
+
141
+ /** For logs and getState: why the route is what it is. */
142
+ fun describe(): String =
143
+ "route=$route effect=$effectWanted postFrames=$postFrames postOk=$postShapeOk " +
144
+ "postAgeMs=${if (postLastNs == 0L) -1 else (System.nanoTime() - postLastNs) / 1_000_000} " +
145
+ "delay=$delaySamples/$delayTargetSamples switches=$switches dropped=$droppedTokens " +
146
+ "gaps=$captureGaps retimes=$retimes"
147
+
148
+ private fun target(): Int = delayTargetSamples.coerceIn(0, MAX_DELAY)
149
+
150
+ /** Session start (control thread, before the first [pre]). */
151
+ fun begin(rate: Int, channels: Int) {
152
+ tokenPending = false
153
+ tokenTrack = null
154
+ switching = false
155
+ retiming = false
156
+ bypassHoldBuffers = 0
157
+ preSincePost = 0
158
+ lastPreNs = 0L
159
+ route = if (effectWanted && postCompatible(rate, channels)) Route.POST else Route.PRE
160
+ if (route == Route.POST) retimeIfDrained()
161
+ fx?.setMusicBypass(route == Route.PRE)
162
+ }
163
+
164
+ /**
165
+ * Session end (control thread, after the fade-out). The delay line keeps
166
+ * draining its faded tail at the post site.
167
+ */
168
+ fun end() {
169
+ tokenPending = false
170
+ tokenTrack = null
171
+ switching = false
172
+ retiming = false
173
+ bypassHoldBuffers = 0
174
+ route = Route.PRE
175
+ fx?.setMusicBypass(false)
176
+ }
177
+
178
+ /**
179
+ * Pre-APM capture callback for one buffer of [samples] 16-bit samples in
180
+ * [sb] (a view over the whole buffer). [channels]/[sampleRate] = capture
181
+ * format, [bps] = buffers per second.
182
+ */
183
+ fun pre(
184
+ sb: ShortBuffer, samples: Int, channels: Int, sampleRate: Int, bps: Int, t: Track,
185
+ nowNs: Long = System.nanoTime(),
186
+ ) {
187
+ if (voice.size != samples) {
188
+ voice = ShortArray(samples)
189
+ music = ShortArray(samples)
190
+ musicOut = ShortArray(samples)
191
+ }
192
+ val gap = lastPreNs != 0L && nowNs - lastPreNs > GAP_NS
193
+ lastPreNs = nowNs
194
+ if (gap) {
195
+ // capture restart (mic republish, track change): the post site stopped with
196
+ // the capture; whatever it was handed last is stale, it is not dead
197
+ if (tokenPending) {
198
+ tokenPending = false
199
+ tokenTrack = null
200
+ }
201
+ captureGaps++
202
+ preSincePost = 0
203
+ } else if (tokenPending) {
204
+ // the post site never took the last buffer: it is not running
205
+ tokenPending = false
206
+ tokenTrack = null
207
+ droppedTokens++
208
+ preSincePost = Int.MAX_VALUE / 2
209
+ }
210
+ if (preSincePost < Int.MAX_VALUE / 2) preSincePost++
211
+ val alive = preSincePost <= 3
212
+ val shapeOk = channels == 1 && postShapeOk && postFrames == samples && postFrames * 100 == sampleRate
213
+ val want = effectWanted && alive && shapeOk
214
+
215
+ if (route == Route.POST && (!alive || !shapeOk)) {
216
+ // the processor stopped or its frame no longer fits (it drops our tokens):
217
+ // back to PRE right away (nothing of ours is in flight there)
218
+ switching = false
219
+ retiming = false
220
+ flip(Route.PRE)
221
+ kernel.snapGate(0f)
222
+ kernel.gateTarget = 1f
223
+ } else if (route == Route.POST && !want && !switching) {
224
+ switching = true // fades out at the post site, flips there
225
+ kernel.gateTarget = 0f
226
+ } else if (route == Route.PRE && want && !switching) {
227
+ switching = true
228
+ kernel.gateTarget = 0f
229
+ } else if (switching && want == (route == Route.POST)) {
230
+ switching = false // changed its mind mid-fade: fade back in where we are
231
+ kernel.gateTarget = if (retiming) 0f else 1f
232
+ }
233
+ if (route == Route.POST && !switching && !retiming && target() != delaySamples) {
234
+ retiming = true // the effect's delay changed: fade out, re-time while silent, fade in
235
+ kernel.gateTarget = 0f
236
+ retimes++
237
+ }
238
+
239
+ sb.get(voice, 0, samples)
240
+ val consuming = !t.paused
241
+ if (consuming) {
242
+ val got = t.read(music, samples)
243
+ if (got < samples) {
244
+ // underrun or end of track: pad the tail with silence
245
+ java.util.Arrays.fill(music, got, samples, 0)
246
+ t.onShort(got)
247
+ }
248
+ }
249
+
250
+ if (route == Route.PRE) {
251
+ kernel.mix(voice, if (consuming) music else null, musicOut, samples, bps)
252
+ sb.position(0)
253
+ sb.put(voice, 0, samples)
254
+ if (consuming) {
255
+ t.monitor(musicOut, samples)
256
+ if (kernel.env == 0f) t.onEnvZero()
257
+ }
258
+ if (switching && kernel.gate == 0f) {
259
+ switching = false
260
+ flip(Route.POST)
261
+ kernel.gateTarget = 1f
262
+ retimeIfDrained()
263
+ // the effect stays dry until the music mixed so far has left NS + the chain
264
+ val ms = (if (delayTargetSamples >= 0) delayTargetSamples * 1000 / sampleRate.coerceAtLeast(1) else 50) + 20
265
+ bypassHoldBuffers = ((ms * bps + 999) / 1000).coerceAtLeast(1)
266
+ }
267
+ return
268
+ }
269
+
270
+ // POST: the voice keeps its gain and sets the ducking here; the music waits for the post site
271
+ kernel.voicePass(voice, samples, bps)
272
+ if (kernel.micGain != 1f) {
273
+ sb.position(0)
274
+ sb.put(voice, 0, samples)
275
+ }
276
+ if (postMusic.size != samples) {
277
+ postMusic = ShortArray(samples)
278
+ postMusicOut = ShortArray(samples)
279
+ }
280
+ if (consuming) System.arraycopy(music, 0, postMusic, 0, samples)
281
+ tokenHasMusic = consuming
282
+ tokenN = samples
283
+ tokenTrack = t
284
+ tokenPending = true
285
+ }
286
+
287
+ /**
288
+ * APM capturePostProcessing, after the voice effect: [frame] = [numFrames]
289
+ * int16-scale floats, [channels] from the APM's initialize (0 = unknown).
290
+ */
291
+ fun post(frame: ByteBuffer, numFrames: Int, channels: Int, nowNs: Long = System.nanoTime()) {
292
+ postFrames = numFrames
293
+ postShapeOk = frame.isDirect && channels <= 1 && frame.capacity() == numFrames * 4
294
+ postLastNs = nowNs
295
+ preSincePost = 0
296
+ if (!tokenPending) {
297
+ // no music handed over: the delay line's tail (a fade-out) still plays out
298
+ if (zeroRun < delaySamples && postShapeOk) {
299
+ if (postMusicOut.size < numFrames) postMusicOut = ShortArray(numFrames)
300
+ java.util.Arrays.fill(postMusicOut, 0, numFrames, 0)
301
+ addDelayed(frame, postMusicOut, numFrames)
302
+ }
303
+ return
304
+ }
305
+ tokenPending = false
306
+ val t = tokenTrack
307
+ tokenTrack = null
308
+ if (t == null || numFrames != tokenN || !postShapeOk) {
309
+ droppedTokens++
310
+ return
311
+ }
312
+ val n = tokenN
313
+ kernel.postMusic(if (tokenHasMusic) postMusic else null, postMusicOut, n)
314
+ if (tokenHasMusic) {
315
+ t.monitor(postMusicOut, n) // the host hears it undelayed, as on PRE
316
+ if (kernel.env == 0f) t.onEnvZero()
317
+ }
318
+ addDelayed(frame, postMusicOut, n)
319
+ if (bypassHoldBuffers > 0 && --bypassHoldBuffers == 0) fx?.setMusicBypass(false)
320
+ val silent = kernel.gate == 0f && zeroRun >= delaySamples
321
+ if (switching && silent) {
322
+ switching = false
323
+ retiming = false
324
+ flip(Route.PRE)
325
+ kernel.gateTarget = 1f
326
+ } else if (retiming && silent) {
327
+ retiming = false
328
+ retimeIfDrained()
329
+ kernel.gateTarget = 1f
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Takes the current target delay once the line's audible content has
335
+ * played out; the line is cleared so a longer delay never replays old music.
336
+ */
337
+ private fun retimeIfDrained() {
338
+ if (zeroRun < delaySamples) return
339
+ java.util.Arrays.fill(line, 0)
340
+ zeroRun = Int.MAX_VALUE / 2
341
+ delaySamples = target()
342
+ }
343
+
344
+ /** Writes [m] into the delay line and adds the line's output, [delaySamples] back, to [frame]. */
345
+ private fun addDelayed(frame: ByteBuffer, m: ShortArray, n: Int) {
346
+ val order = frame.order()
347
+ frame.order(ByteOrder.nativeOrder())
348
+ val d = delaySamples
349
+ var w = lineW
350
+ var z = zeroRun
351
+ for (i in 0 until n) {
352
+ val s = m[i]
353
+ line[w] = s
354
+ if (s.toInt() == 0) { if (z < Int.MAX_VALUE / 2) z++ } else z = 0
355
+ val out = line[(w - d) and MASK].toInt()
356
+ w = (w + 1) and MASK
357
+ if (out != 0) {
358
+ val at = i * 4
359
+ frame.putFloat(at, (frame.getFloat(at) + out).coerceIn(-32768f, 32767f))
360
+ }
361
+ }
362
+ lineW = w
363
+ zeroRun = z
364
+ frame.order(order)
365
+ }
366
+
367
+ private fun flip(to: Route) {
368
+ route = to
369
+ switches++
370
+ bypassHoldBuffers = 0
371
+ // PRE: dry right away (the next music enters the chain >= 10 ms later);
372
+ // POST: released by [post] after the drain
373
+ if (to == Route.PRE) fx?.setMusicBypass(true)
374
+ }
375
+ }