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,1060 @@
1
+ #ifndef SIGNALSMITH_STRETCH_H
2
+ #define SIGNALSMITH_STRETCH_H
3
+
4
+ #include "signalsmith-linear/stft.h" // https://github.com/Signalsmith-Audio/linear
5
+
6
+ #include <vector>
7
+ #include <array>
8
+ #include <algorithm>
9
+ #include <functional>
10
+ #include <random>
11
+ #include <limits>
12
+ #include <type_traits>
13
+
14
+ namespace signalsmith { namespace stretch {
15
+
16
+ namespace _impl {
17
+ template<bool conjugateSecond=false, typename V>
18
+ static std::complex<V> mul(const std::complex<V> &a, const std::complex<V> &b) {
19
+ return conjugateSecond ? std::complex<V>{
20
+ b.real()*a.real() + b.imag()*a.imag(),
21
+ b.real()*a.imag() - b.imag()*a.real()
22
+ } : std::complex<V>{
23
+ a.real()*b.real() - a.imag()*b.imag(),
24
+ a.real()*b.imag() + a.imag()*b.real()
25
+ };
26
+ }
27
+ template<typename V>
28
+ static V norm(const std::complex<V> &a) {
29
+ V r = a.real(), i = a.imag();
30
+ return r*r + i*i;
31
+ }
32
+ }
33
+
34
+ template<typename Sample=float, class RandomEngine=void>
35
+ struct SignalsmithStretch {
36
+ static constexpr size_t version[3] = {1, 3, 2};
37
+
38
+ SignalsmithStretch() : randomEngine(std::random_device{}()) {}
39
+ SignalsmithStretch(long seed) : randomEngine(seed) {}
40
+
41
+ // The difference between the internal position (centre of a block) and the input samples you're supplying
42
+ int inputLatency() const {
43
+ return int(stft.analysisLatency());
44
+ }
45
+ int outputLatency() const {
46
+ return int(stft.synthesisLatency() + _splitComputation*stft.defaultInterval());
47
+ }
48
+
49
+ void reset() {
50
+ stft.reset(0.1);
51
+ stashedInput = stft.input;
52
+ stashedOutput = stft.output;
53
+
54
+ prevInputOffset = -1;
55
+ _channelBands.assign(_channelBands.size(), Band());
56
+ silenceCounter = 0;
57
+ didSeek = false;
58
+ blockProcess = {};
59
+ freqEstimateWeighted = freqEstimateWeight = 0;
60
+ }
61
+
62
+ // Configures using a default preset
63
+ void presetDefault(int nChannels, Sample sampleRate, bool splitComputation=false) {
64
+ configure(nChannels, static_cast<int>(sampleRate*0.12), static_cast<int>(sampleRate*0.03), splitComputation);
65
+ }
66
+ void presetCheaper(int nChannels, Sample sampleRate, bool splitComputation=true) {
67
+ configure(nChannels, static_cast<int>(sampleRate*0.1), static_cast<int>(sampleRate*0.04), splitComputation);
68
+ }
69
+
70
+ // Manual setup
71
+ void configure(int nChannels, int blockSamples, int intervalSamples, bool splitComputation=false) {
72
+ _splitComputation = splitComputation;
73
+ channels = nChannels;
74
+ stft.configure(channels, channels, blockSamples, intervalSamples + 1);
75
+ stft.setInterval(intervalSamples, stft.kaiser);
76
+ stft.reset(Sample(0.1));
77
+ stashedInput = stft.input;
78
+ stashedOutput = stft.output;
79
+
80
+ bands = int(stft.bands());
81
+ _channelBands.assign(bands*channels, Band());
82
+
83
+ peaks.reserve(bands/2);
84
+ energy.resize(bands);
85
+ smoothedEnergy.resize(bands);
86
+ outputMap.resize(bands);
87
+ channelPredictions.resize(channels*bands);
88
+
89
+ blockProcess = {};
90
+ formantMetric.resize(bands + 2);
91
+
92
+ tmpProcessBuffer.resize(blockSamples + intervalSamples);
93
+ tmpPreRollBuffer.resize(outputLatency()*channels);
94
+ }
95
+ // For querying the existing config
96
+ int blockSamples() const {
97
+ return int(stft.blockSamples());
98
+ }
99
+ int intervalSamples() const {
100
+ return int(stft.defaultInterval());
101
+ }
102
+ bool splitComputation() const {
103
+ return _splitComputation;
104
+ }
105
+
106
+ /// Frequency multiplier, and optional tonality limit (as multiple of sample-rate)
107
+ void setTransposeFactor(Sample multiplier, Sample tonalityLimit=0) {
108
+ freqMultiplier = multiplier;
109
+ if (tonalityLimit > 0) {
110
+ freqTonalityLimit = tonalityLimit/std::sqrt(multiplier); // compromise between input and output limits
111
+ } else {
112
+ freqTonalityLimit = 1;
113
+ }
114
+ customFreqMap = nullptr;
115
+ }
116
+ void setTransposeSemitones(Sample semitones, Sample tonalityLimit=0) {
117
+ setTransposeFactor(std::pow(2, semitones/12), tonalityLimit);
118
+ }
119
+ // Sets a custom frequency map - should be monotonically increasing
120
+ void setFreqMap(std::function<Sample(Sample)> inputToOutput) {
121
+ customFreqMap = inputToOutput;
122
+ }
123
+
124
+ void setFormantFactor(Sample multiplier, bool compensatePitch=false) {
125
+ formantMultiplier = multiplier;
126
+ invFormantMultiplier = 1/multiplier;
127
+ formantCompensation = compensatePitch;
128
+ }
129
+ void setFormantSemitones(Sample semitones, bool compensatePitch=false) {
130
+ setFormantFactor(std::pow(2, semitones/12), compensatePitch);
131
+ }
132
+ // Rough guesstimate of the fundamental frequency, used for formant analysis. 0 means attempting to detect the pitch
133
+ void setFormantBase(Sample baseFreq=0) {
134
+ formantBaseFreq = baseFreq;
135
+ }
136
+
137
+ // Provide previous input ("pre-roll") to smoothly change the input location without interrupting the output. This doesn't do any calculation, just copies intput to a buffer.
138
+ // You should ideally feed it `seekLength()` frames of input, unless it's directly after a `.reset()` (in which case `.outputSeek()` might be a better choice)
139
+ template<class Inputs>
140
+ void seek(Inputs &&inputs, int inputSamples, double playbackRate) {
141
+ tmpProcessBuffer.resize(0);
142
+ tmpProcessBuffer.resize(stft.blockSamples() + stft.defaultInterval());
143
+
144
+ int startIndex = std::max<int>(0, inputSamples - int(tmpProcessBuffer.size())); // start position in input
145
+ int padStart = int(tmpProcessBuffer.size() + startIndex) - inputSamples; // start position in tmpProcessBuffer
146
+
147
+ Sample totalEnergy = 0;
148
+ for (int c = 0; c < channels; ++c) {
149
+ auto &&inputChannel = inputs[c];
150
+ for (int i = startIndex; i < inputSamples; ++i) {
151
+ Sample s = inputChannel[i];
152
+ totalEnergy += s*s;
153
+ tmpProcessBuffer[i - startIndex + padStart] = s;
154
+ }
155
+
156
+ stft.writeInput(c, tmpProcessBuffer.size(), tmpProcessBuffer.data());
157
+ }
158
+ stft.moveInput(tmpProcessBuffer.size());
159
+ if (totalEnergy >= noiseFloor) {
160
+ silenceCounter = 0;
161
+ silenceFirst = true;
162
+ }
163
+ didSeek = true;
164
+ seekTimeFactor = (playbackRate*stft.defaultInterval() > 1) ? 1/playbackRate : stft.defaultInterval();
165
+ }
166
+ int seekLength() const {
167
+ return int(stft.blockSamples() + stft.defaultInterval());
168
+ }
169
+
170
+ // Moves the input position *and* pre-calculates some output, so that the next samples returned from `.process()` are aligned to the beginning of the sample.
171
+ // The time-stretch rate is inferred from `inputLength`, so use `.outputSeekLength()` to get a correct value for that.
172
+ template<class Inputs>
173
+ void outputSeek(Inputs &&inputs, int inputLength) {
174
+ // TODO: add fade-out parameter to avoid clicks, instead of doing a full reset
175
+ reset();
176
+ // Assume we've been handed enough surplus input to produce `outputLatency()` samples of pre-roll
177
+ int surplusInput = std::max<int>(inputLength - inputLatency(), 0);
178
+ Sample playbackRate = surplusInput/Sample(outputLatency());
179
+
180
+ // Move the input position to the start of the sound
181
+ int seekSamples = inputLength - surplusInput;
182
+ seek(inputs, seekSamples, playbackRate);
183
+
184
+ tmpPreRollBuffer.resize(outputLatency()*channels);
185
+ struct BufferOutput {
186
+ Sample *samples;
187
+ int length;
188
+
189
+ Sample * operator[](int c) {
190
+ return samples + c*length;
191
+ }
192
+ } preRollOutput{tmpPreRollBuffer.data(), outputLatency()};
193
+
194
+ // Use the surplus input to produce pre-roll output
195
+ OffsetIO<Inputs> offsetInput{inputs, seekSamples};
196
+ process(offsetInput, surplusInput, preRollOutput, preRollOutput.length);
197
+
198
+ // put the thing down, flip it and reverse it
199
+ for (auto &v : tmpPreRollBuffer) v = -v;
200
+ for (int c = 0; c < channels; ++c) {
201
+ std::reverse(preRollOutput[c], preRollOutput[c] + preRollOutput.length);
202
+ stft.addOutput(c, preRollOutput.length, preRollOutput[c]);
203
+ }
204
+ }
205
+ int outputSeekLength(Sample playbackRate) const {
206
+ return inputLatency() + playbackRate*outputLatency();
207
+ }
208
+
209
+ template<class Inputs, class Outputs>
210
+ void process(Inputs &&inputs, int inputSamples, Outputs &&outputs, int outputSamples) {
211
+ #ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_START
212
+ SIGNALSMITH_STRETCH_PROFILE_PROCESS_START(inputSamples, outputSamples);
213
+ #endif
214
+ int prevCopiedInput = 0;
215
+ auto copyInput = [&](int toIndex){
216
+
217
+ int length = std::min<int>(int(stft.blockSamples() + stft.defaultInterval()), toIndex - prevCopiedInput);
218
+ tmpProcessBuffer.resize(length);
219
+ int offset = toIndex - length;
220
+ for (int c = 0; c < channels; ++c) {
221
+ auto &&inputBuffer = inputs[c];
222
+ for (int i = 0; i < length; ++i) {
223
+ tmpProcessBuffer[i] = inputBuffer[i + offset];
224
+ }
225
+ stft.writeInput(c, length, tmpProcessBuffer.data());
226
+ }
227
+ stft.moveInput(length);
228
+ prevCopiedInput = toIndex;
229
+ };
230
+
231
+ Sample totalEnergy = 0;
232
+ for (int c = 0; c < channels; ++c) {
233
+ auto &&inputChannel = inputs[c];
234
+ for (int i = 0; i < inputSamples; ++i) {
235
+ Sample s = inputChannel[i];
236
+ totalEnergy += s*s;
237
+ }
238
+ }
239
+
240
+ if (totalEnergy < noiseFloor) {
241
+ if (silenceCounter >= 2*stft.blockSamples()) {
242
+ if (silenceFirst) { // first block of silence processing
243
+ silenceFirst = false;
244
+ //stft.reset();
245
+ blockProcess = {};
246
+ for (auto &b : _channelBands) {
247
+ b.input = b.prevInput = b.output = 0;
248
+ b.inputEnergy = 0;
249
+ }
250
+ }
251
+
252
+ if (inputSamples > 0) {
253
+ // copy from the input, wrapping around if needed
254
+ for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) {
255
+ int inputIndex = outputIndex%inputSamples;
256
+ for (int c = 0; c < channels; ++c) {
257
+ outputs[c][outputIndex] = inputs[c][inputIndex];
258
+ }
259
+ }
260
+ } else {
261
+ for (int c = 0; c < channels; ++c) {
262
+ auto &&outputChannel = outputs[c];
263
+ for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) {
264
+ outputChannel[outputIndex] = 0;
265
+ }
266
+ }
267
+ }
268
+
269
+ // Store input in history buffer
270
+ copyInput(inputSamples);
271
+ return;
272
+ } else {
273
+ silenceCounter += inputSamples;
274
+ }
275
+ } else {
276
+ silenceCounter = 0;
277
+ silenceFirst = true;
278
+ }
279
+
280
+ for (int outputIndex = 0; outputIndex < outputSamples; ++outputIndex) {
281
+ bool newBlock = blockProcess.samplesSinceLast >= stft.defaultInterval();
282
+ if (newBlock) {
283
+ blockProcess.step = 0;
284
+ blockProcess.steps = 0; // how many processing steps this block will have
285
+ blockProcess.samplesSinceLast = 0;
286
+
287
+ // Time to process a spectrum! Where should it come from in the input?
288
+ int inputOffset = static_cast<int>(std::round(outputIndex*Sample(inputSamples)/outputSamples));
289
+ int inputInterval = inputOffset - prevInputOffset;
290
+ prevInputOffset = inputOffset;
291
+
292
+ copyInput(inputOffset);
293
+ stashedInput = stft.input; // save the input state, since that's what we'll analyse later
294
+ if (_splitComputation) {
295
+ stashedOutput = stft.output; // save the current output, and read from it
296
+ stft.moveOutput(stft.defaultInterval()); // the actual input jumps forward in time by one interval, ready for the synthesis
297
+ }
298
+
299
+ blockProcess.newSpectrum = didSeek || (inputInterval > 0);
300
+ blockProcess.mappedFrequencies = customFreqMap || freqMultiplier != 1;
301
+ if (blockProcess.newSpectrum) {
302
+ // make sure the previous input is the correct distance in the past (give or take 1 sample)
303
+ blockProcess.reanalysePrev = didSeek || std::abs(inputInterval - int(stft.defaultInterval())) > 1;
304
+ if (blockProcess.reanalysePrev) blockProcess.steps += stft.analyseSteps() + 1;
305
+
306
+ // analyse a new input
307
+ blockProcess.steps += stft.analyseSteps() + 1;
308
+ }
309
+
310
+ blockProcess.processFormants = formantMultiplier != 1 || (formantCompensation && blockProcess.mappedFrequencies);
311
+
312
+ blockProcess.timeFactor = didSeek ? seekTimeFactor : stft.defaultInterval()/static_cast<Sample>(std::max(1, inputInterval));
313
+ didSeek = false;
314
+
315
+ updateProcessSpectrumSteps();
316
+ blockProcess.steps += processSpectrumSteps;
317
+
318
+ blockProcess.steps += stft.synthesiseSteps() + 1;
319
+ }
320
+
321
+ size_t processToStep = newBlock ? blockProcess.steps : 0;
322
+ if (_splitComputation) {
323
+ Sample processRatio = Sample(blockProcess.samplesSinceLast + 1)/stft.defaultInterval();
324
+ processToStep = std::min(blockProcess.steps, static_cast<size_t>((blockProcess.steps + 0.999f)*processRatio));
325
+ }
326
+
327
+ while (blockProcess.step < processToStep) {
328
+ size_t step = blockProcess.step++;
329
+ #ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_STEP
330
+ SIGNALSMITH_STRETCH_PROFILE_PROCESS_STEP(step, blockProcess.steps);
331
+ #endif
332
+ if (blockProcess.newSpectrum) {
333
+ if (blockProcess.reanalysePrev) {
334
+ // analyse past input
335
+ if (step < stft.analyseSteps()) {
336
+ stashedInput.swap(stft.input);
337
+ stft.analyseStep(step, stft.defaultInterval());
338
+ stashedInput.swap(stft.input);
339
+ continue;
340
+ }
341
+ step -= stft.analyseSteps();
342
+ if (step < 1) {
343
+ // Copy previous analysis to our band objects
344
+ for (int c = 0; c < channels; ++c) {
345
+ auto channelBands = bandsForChannel(c);
346
+ auto *spectrumBands = stft.spectrum(c);
347
+ for (int b = 0; b < bands; ++b) {
348
+ channelBands[b].prevInput = spectrumBands[b];
349
+ }
350
+ }
351
+ continue;
352
+ }
353
+ step -= 1;
354
+ }
355
+
356
+ // Analyse latest (stashed) input
357
+ if (step < stft.analyseSteps()) {
358
+ stashedInput.swap(stft.input);
359
+ stft.analyseStep(step);
360
+ stashedInput.swap(stft.input);
361
+ continue;
362
+ }
363
+ step -= stft.analyseSteps();
364
+ if (step < 1) {
365
+ // Copy analysed spectrum into our band objects
366
+ for (int c = 0; c < channels; ++c) {
367
+ auto channelBands = bandsForChannel(c);
368
+ auto *spectrumBands = stft.spectrum(c);
369
+ for (int b = 0; b < bands; ++b) {
370
+ channelBands[b].input = spectrumBands[b];
371
+ }
372
+ }
373
+ continue;
374
+ }
375
+ step -= 1;
376
+ }
377
+
378
+ if (step < processSpectrumSteps) {
379
+ processSpectrum(step);
380
+ continue;
381
+ }
382
+ step -= processSpectrumSteps;
383
+
384
+ if (step < 1) {
385
+ // Copy band objects into spectrum
386
+ for (int c = 0; c < channels; ++c) {
387
+ auto channelBands = bandsForChannel(c);
388
+ auto *spectrumBands = stft.spectrum(c);
389
+ for (int b = 0; b < bands; ++b) {
390
+ spectrumBands[b] = channelBands[b].output;
391
+ }
392
+ }
393
+ continue;
394
+ }
395
+ step -= 1;
396
+
397
+ if (step < stft.synthesiseSteps()) {
398
+ stft.synthesiseStep(step);
399
+ continue;
400
+ }
401
+ }
402
+ #ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_ENDSTEP
403
+ SIGNALSMITH_STRETCH_PROFILE_PROCESS_ENDSTEP();
404
+ #endif
405
+
406
+ ++blockProcess.samplesSinceLast;
407
+ if (_splitComputation) stashedOutput.swap(stft.output);
408
+ for (int c = 0; c < channels; ++c) {
409
+ auto &&outputChannel = outputs[c];
410
+ Sample v = 0;
411
+ stft.readOutput(c, 1, &v);
412
+ outputChannel[outputIndex] = v;
413
+ }
414
+ stft.moveOutput(1);
415
+ if (_splitComputation) stashedOutput.swap(stft.output);
416
+ }
417
+
418
+ copyInput(inputSamples);
419
+ prevInputOffset -= inputSamples;
420
+ #ifdef SIGNALSMITH_STRETCH_PROFILE_PROCESS_END
421
+ SIGNALSMITH_STRETCH_PROFILE_PROCESS_END();
422
+ #endif
423
+ }
424
+
425
+ // Read the remaining output, providing no further input. If `outputSamples` is more than one interval, it will compute additional blocks assuming a zero-valued input
426
+ template<class Outputs>
427
+ void flush(Outputs &&outputs, int outputSamples, Sample playbackRate=0) {
428
+ struct Zeros {
429
+ struct Channel {
430
+ Sample operator[](int) {
431
+ return 0;
432
+ }
433
+ };
434
+ Channel operator[](int) {
435
+ return {};
436
+ }
437
+ } zeros;
438
+ // If we're asked for more than an interval of extra output, then zero-pad the input
439
+ int outputBlock = std::max<int>(0, outputSamples - stft.defaultInterval());
440
+ if (outputBlock > 0) process(zeros, outputBlock*playbackRate, outputs, outputBlock);
441
+
442
+ int tailSamples = outputSamples - outputBlock; // at most one interval
443
+ tmpProcessBuffer.resize(tailSamples);
444
+ stft.finishOutput(1);
445
+ for (int c = 0; c < channels; ++c) {
446
+ stft.readOutput(c, tailSamples, tmpProcessBuffer.data());
447
+ auto &&outputChannel = outputs[c];
448
+ for (int i = 0; i < tailSamples; ++i) {
449
+ outputChannel[outputBlock + i] = tmpProcessBuffer[i];
450
+ }
451
+ stft.readOutput(c, tailSamples, tailSamples, tmpProcessBuffer.data());
452
+ for (int i = 0; i < tailSamples; ++i) {
453
+ outputChannel[outputBlock + tailSamples - 1 - i] -= tmpProcessBuffer[i];
454
+ }
455
+ }
456
+ stft.reset(0.1f);
457
+ // Reset the phase-vocoder stuff, so the next block gets a fresh start
458
+ for (int c = 0; c < channels; ++c) {
459
+ auto channelBands = bandsForChannel(c);
460
+ for (int b = 0; b < bands; ++b) {
461
+ channelBands[b].prevInput = channelBands[b].output = 0;
462
+ }
463
+ }
464
+ }
465
+
466
+ // Process a complete audio buffer all in one go
467
+ template<class Inputs, class Outputs>
468
+ bool exact(Inputs &&inputs, int inputSamples, Outputs &&outputs, int outputSamples) {
469
+ Sample playbackRate = inputSamples/Sample(outputSamples);
470
+ auto seekLength = outputSeekLength(playbackRate);
471
+ if (inputSamples < seekLength) {
472
+ // to short for this - zero the output just to be polite
473
+ for (int c = 0; c < channels; ++c) {
474
+ auto &&channel = outputs[c];
475
+ for (int i = 0; i < outputSamples; ++i) {
476
+ channel[i] = 0;
477
+ }
478
+ }
479
+ return false;
480
+ }
481
+
482
+ outputSeek(inputs, seekLength);
483
+
484
+ int outputIndex = outputSamples - seekLength/playbackRate;
485
+ OffsetIO<Inputs> offsetInput{inputs, seekLength};
486
+ process(offsetInput, inputSamples - seekLength, outputs, outputIndex);
487
+
488
+ OffsetIO<Outputs> offsetOutput{outputs, outputIndex};
489
+ flush(offsetOutput, outputSamples - outputIndex, playbackRate);
490
+ return true;
491
+ }
492
+
493
+ private:
494
+ bool _splitComputation = false;
495
+ struct {
496
+ size_t samplesSinceLast = std::numeric_limits<size_t>::max();
497
+ size_t steps = 0;
498
+ size_t step = 0;
499
+
500
+ bool newSpectrum = false;
501
+ bool reanalysePrev = false;
502
+ bool mappedFrequencies = false;
503
+ bool processFormants = false;
504
+ Sample timeFactor;
505
+ } blockProcess;
506
+
507
+ using Complex = std::complex<Sample>;
508
+ static constexpr Sample noiseFloor{Sample(1e-15)};
509
+ static constexpr Sample maxCleanStretch{2}; // time-stretch ratio before we start randomising phases
510
+ size_t silenceCounter = 0;
511
+ bool silenceFirst = true;
512
+
513
+ Sample freqMultiplier = 1, freqTonalityLimit = 0.5;
514
+ std::function<Sample(Sample)> customFreqMap = nullptr;
515
+
516
+ bool formantCompensation = false; // compensate for pitch/freq change
517
+ Sample formantMultiplier = 1, invFormantMultiplier = 1;
518
+
519
+ using STFT = signalsmith::linear::DynamicSTFT<Sample, false, true>;
520
+ STFT stft;
521
+ typename STFT::Input stashedInput;
522
+ typename STFT::Output stashedOutput;
523
+
524
+ std::vector<Sample> tmpProcessBuffer, tmpPreRollBuffer;
525
+
526
+ int channels = 0, bands = 0;
527
+ int prevInputOffset = -1;
528
+ bool didSeek = false;
529
+ Sample seekTimeFactor = 1;
530
+
531
+ Sample bandToFreq(Sample b) const {
532
+ return stft.binToFreq(b);
533
+ }
534
+ Sample freqToBand(Sample f) const {
535
+ return stft.freqToBin(f);
536
+ }
537
+
538
+ struct Band {
539
+ Complex input, prevInput{0};
540
+ Complex output{0};
541
+ Sample inputEnergy;
542
+ };
543
+ std::vector<Band> _channelBands;
544
+ Band * bandsForChannel(int channel) {
545
+ return _channelBands.data() + channel*bands;
546
+ }
547
+ template<Complex Band::*member>
548
+ Complex getBand(int channel, int index) {
549
+ if (index < 0 || index >= bands) return 0;
550
+ return _channelBands[index + channel*bands].*member;
551
+ }
552
+ template<Complex Band::*member>
553
+ Complex getFractional(int channel, int lowIndex, Sample fractional) {
554
+ Complex low = getBand<member>(channel, lowIndex);
555
+ Complex high = getBand<member>(channel, lowIndex + 1);
556
+ return low + (high - low)*fractional;
557
+ }
558
+ template<Complex Band::*member>
559
+ Complex getFractional(int channel, Sample inputIndex) {
560
+ int lowIndex = static_cast<int>(std::floor(inputIndex));
561
+ Sample fracIndex = inputIndex - lowIndex;
562
+ return getFractional<member>(channel, lowIndex, fracIndex);
563
+ }
564
+ template<Sample Band::*member>
565
+ Sample getBand(int channel, int index) {
566
+ if (index < 0 || index >= bands) return 0;
567
+ return _channelBands[index + channel*bands].*member;
568
+ }
569
+ template<Sample Band::*member>
570
+ Sample getFractional(int channel, int lowIndex, Sample fractional) {
571
+ Sample low = getBand<member>(channel, lowIndex);
572
+ Sample high = getBand<member>(channel, lowIndex + 1);
573
+ return low + (high - low)*fractional;
574
+ }
575
+ template<Sample Band::*member>
576
+ Sample getFractional(int channel, Sample inputIndex) {
577
+ int lowIndex = std::floor(inputIndex);
578
+ Sample fracIndex = inputIndex - lowIndex;
579
+ return getFractional<member>(channel, lowIndex, fracIndex);
580
+ }
581
+
582
+ struct Peak {
583
+ Sample input, output;
584
+ };
585
+ std::vector<Peak> peaks;
586
+ std::vector<Sample> energy, smoothedEnergy;
587
+ struct PitchMapPoint {
588
+ Sample inputBin, freqGrad;
589
+ };
590
+ std::vector<PitchMapPoint> outputMap;
591
+
592
+ struct Prediction {
593
+ Sample energy = 0;
594
+ Complex input;
595
+
596
+ Complex makeOutput(Complex phase) {
597
+ Sample phaseNorm = _impl::norm(phase);
598
+ if (phaseNorm <= noiseFloor) {
599
+ phase = input; // prediction is too weak, fall back to the input
600
+ phaseNorm = _impl::norm(input) + noiseFloor;
601
+ }
602
+ return phase*std::sqrt(energy/phaseNorm);
603
+ }
604
+ };
605
+ std::vector<Prediction> channelPredictions;
606
+ Prediction * predictionsForChannel(int c) {
607
+ return channelPredictions.data() + c*bands;
608
+ }
609
+
610
+ // If RandomEngine=void, use std::default_random_engine;
611
+ using RandomEngineImpl = typename std::conditional<
612
+ std::is_void<RandomEngine>::value,
613
+ std::default_random_engine,
614
+ RandomEngine
615
+ >::type;
616
+ RandomEngineImpl randomEngine;
617
+
618
+ size_t processSpectrumSteps = 0;
619
+ static constexpr size_t splitMainPrediction = 8; // it's just heavy, since we're blending up to 4 different phase predictions
620
+ void updateProcessSpectrumSteps() {
621
+ processSpectrumSteps = 0;
622
+ if (blockProcess.newSpectrum) processSpectrumSteps += channels;
623
+ if (blockProcess.mappedFrequencies) {
624
+ processSpectrumSteps += smoothEnergySteps;
625
+ processSpectrumSteps += 1; // findPeaks
626
+ }
627
+ processSpectrumSteps += 1; // updating the output map
628
+ processSpectrumSteps += channels; // preliminary phase-vocoder prediction
629
+ processSpectrumSteps += splitMainPrediction;
630
+ if (blockProcess.newSpectrum) processSpectrumSteps += 1; // .input -> .prevInput
631
+ if (blockProcess.processFormants) processSpectrumSteps += 3;
632
+ }
633
+ void processSpectrum(size_t step) {
634
+ Sample timeFactor = blockProcess.timeFactor;
635
+
636
+ Sample smoothingBins = Sample(stft.fftSamples())/stft.defaultInterval();
637
+ int longVerticalStep = static_cast<int>(std::round(smoothingBins));
638
+ timeFactor = std::max<Sample>(timeFactor, 1/maxCleanStretch);
639
+ bool randomTimeFactor = (timeFactor > maxCleanStretch);
640
+ std::uniform_real_distribution<Sample> timeFactorDist(maxCleanStretch*2*randomTimeFactor - timeFactor, timeFactor);
641
+
642
+ if (blockProcess.newSpectrum) {
643
+ if (step < size_t(channels)) {
644
+ int channel = int(step);
645
+ auto bins = bandsForChannel(channel);
646
+
647
+ Complex rot = std::polar(Sample(1), bandToFreq(0)*stft.defaultInterval()*Sample(2*M_PI));
648
+ Sample freqStep = bandToFreq(1) - bandToFreq(0);
649
+ Complex rotStep = std::polar(Sample(1), freqStep*stft.defaultInterval()*Sample(2*M_PI));
650
+
651
+ for (int b = 0; b < bands; ++b) {
652
+ auto &bin = bins[b];
653
+ bin.output = _impl::mul(bin.output, rot);
654
+ bin.prevInput = _impl::mul(bin.prevInput, rot);
655
+ rot = _impl::mul(rot, rotStep);
656
+ }
657
+ return;
658
+ }
659
+ step -= channels;
660
+ }
661
+ if (blockProcess.mappedFrequencies) {
662
+ if (step < smoothEnergySteps) {
663
+ smoothEnergy(step, smoothingBins);
664
+ return;
665
+ }
666
+ step -= smoothEnergySteps;
667
+ if (step-- == 0) {
668
+ findPeaks();
669
+ return;
670
+ }
671
+ }
672
+ if (step-- == 0) {
673
+ if (blockProcess.mappedFrequencies) {
674
+ updateOutputMap();
675
+ } else { // we're not pitch-shifting, so no need to find peaks etc.
676
+ for (int c = 0; c < channels; ++c) {
677
+ Band *bins = bandsForChannel(c);
678
+ for (int b = 0; b < bands; ++b) {
679
+ bins[b].inputEnergy = _impl::norm(bins[b].input);
680
+ }
681
+ }
682
+
683
+ for (int b = 0; b < bands; ++b) {
684
+ outputMap[b] = {Sample(b), 1};
685
+ }
686
+ }
687
+ return;
688
+ }
689
+ if (blockProcess.processFormants) {
690
+ if (step < 3) {
691
+ updateFormants(step);
692
+ return;
693
+ }
694
+ step -= 3;
695
+ }
696
+ // Preliminary output prediction from phase-vocoder
697
+ if (step < size_t(channels)) {
698
+ int c = int(step);
699
+ Band *bins = bandsForChannel(c);
700
+ auto *predictions = predictionsForChannel(c);
701
+ for (int b = 0; b < bands; ++b) {
702
+ auto mapPoint = outputMap[b];
703
+ int lowIndex = static_cast<int>(std::floor(mapPoint.inputBin));
704
+ Sample fracIndex = mapPoint.inputBin - lowIndex;
705
+
706
+ Prediction &prediction = predictions[b];
707
+ Sample prevEnergy = prediction.energy;
708
+ prediction.energy = getFractional<&Band::inputEnergy>(c, lowIndex, fracIndex);
709
+ prediction.energy *= std::max<Sample>(0, mapPoint.freqGrad); // scale the energy according to local stretch factor
710
+ prediction.input = getFractional<&Band::input>(c, lowIndex, fracIndex);
711
+
712
+ auto &outputBin = bins[b];
713
+ Complex prevInput = getFractional<&Band::prevInput>(c, lowIndex, fracIndex);
714
+ Complex freqTwist = _impl::mul<true>(prediction.input, prevInput);
715
+ Complex phase = _impl::mul(outputBin.output, freqTwist);
716
+ outputBin.output = phase/(std::max(prevEnergy, prediction.energy) + noiseFloor);
717
+ }
718
+ return;
719
+ }
720
+ step -= channels;
721
+
722
+ if (step < splitMainPrediction) {
723
+ // Re-predict using phase differences between frequencies
724
+ size_t chunk = step;
725
+ int startB = int(bands*chunk/splitMainPrediction);
726
+ int endB = int(bands*(chunk + 1)/splitMainPrediction);
727
+ for (int b = startB; b < endB; ++b) {
728
+ // Find maximum-energy channel and calculate that
729
+ int maxChannel = 0;
730
+ Sample maxEnergy = predictionsForChannel(0)[b].energy;
731
+ for (int c = 1; c < channels; ++c) {
732
+ Sample e = predictionsForChannel(c)[b].energy;
733
+ if (e > maxEnergy) {
734
+ maxChannel = c;
735
+ maxEnergy = e;
736
+ }
737
+ }
738
+
739
+ auto *predictions = predictionsForChannel(maxChannel);
740
+ auto &prediction = predictions[b];
741
+ auto *bins = bandsForChannel(maxChannel);
742
+ auto &outputBin = bins[b];
743
+
744
+ Complex phase = 0;
745
+ auto mapPoint = outputMap[b];
746
+
747
+ // Upwards vertical steps
748
+ if (b > 0) {
749
+ Sample binTimeFactor = randomTimeFactor ? timeFactorDist(randomEngine) : timeFactor;
750
+ Complex downInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - binTimeFactor);
751
+ Complex shortVerticalTwist = _impl::mul<true>(prediction.input, downInput);
752
+
753
+ auto &downBin = bins[b - 1];
754
+ phase += _impl::mul(downBin.output, shortVerticalTwist);
755
+
756
+ if (b >= longVerticalStep) {
757
+ Complex longDownInput = getFractional<&Band::input>(maxChannel, mapPoint.inputBin - longVerticalStep*binTimeFactor);
758
+ Complex longVerticalTwist = _impl::mul<true>(prediction.input, longDownInput);
759
+
760
+ auto &longDownBin = bins[b - longVerticalStep];
761
+ phase += _impl::mul(longDownBin.output, longVerticalTwist);
762
+ }
763
+ }
764
+ // Downwards vertical steps
765
+ if (b < bands - 1) {
766
+ auto &upPrediction = predictions[b + 1];
767
+ auto &upMapPoint = outputMap[b + 1];
768
+
769
+ Sample binTimeFactor = randomTimeFactor ? timeFactorDist(randomEngine) : timeFactor;
770
+ Complex downInput = getFractional<&Band::input>(maxChannel, upMapPoint.inputBin - binTimeFactor);
771
+ Complex shortVerticalTwist = _impl::mul<true>(upPrediction.input, downInput);
772
+
773
+ auto &upBin = bins[b + 1];
774
+ phase += _impl::mul<true>(upBin.output, shortVerticalTwist);
775
+
776
+ if (b < bands - longVerticalStep) {
777
+ auto &longUpPrediction = predictions[b + longVerticalStep];
778
+ auto &longUpMapPoint = outputMap[b + longVerticalStep];
779
+
780
+ Complex longDownInput = getFractional<&Band::input>(maxChannel, longUpMapPoint.inputBin - longVerticalStep*binTimeFactor);
781
+ Complex longVerticalTwist = _impl::mul<true>(longUpPrediction.input, longDownInput);
782
+
783
+ auto &longUpBin = bins[b + longVerticalStep];
784
+ phase += _impl::mul<true>(longUpBin.output, longVerticalTwist);
785
+ }
786
+ }
787
+
788
+ outputBin.output = prediction.makeOutput(phase);
789
+
790
+ // All other bins are locked in phase
791
+ for (int c = 0; c < channels; ++c) {
792
+ if (c != maxChannel) {
793
+ auto &channelBin = bandsForChannel(c)[b];
794
+ auto &channelPrediction = predictionsForChannel(c)[b];
795
+
796
+ Complex channelTwist = _impl::mul<true>(channelPrediction.input, prediction.input);
797
+ Complex channelPhase = _impl::mul(outputBin.output, channelTwist);
798
+ channelBin.output = channelPrediction.makeOutput(channelPhase);
799
+ }
800
+ }
801
+ }
802
+ return;
803
+ }
804
+ step -= splitMainPrediction;
805
+
806
+ if (blockProcess.newSpectrum) {
807
+ if (step-- == 0) {
808
+ for (auto &bin : _channelBands) {
809
+ bin.prevInput = bin.input;
810
+ }
811
+ }
812
+ }
813
+ }
814
+
815
+ // Produces smoothed energy across all channels
816
+ static constexpr size_t smoothEnergySteps = 3;
817
+ Sample smoothEnergyState = 0;
818
+ void smoothEnergy(size_t step, Sample smoothingBins) {
819
+ Sample smoothingSlew = 1/(1 + smoothingBins*Sample(0.5));
820
+ if (step-- == 0) {
821
+ for (auto &e : energy) e = 0;
822
+ for (int c = 0; c < channels; ++c) {
823
+ Band *bins = bandsForChannel(c);
824
+ for (int b = 0; b < bands; ++b) {
825
+ Sample e = _impl::norm(bins[b].input);
826
+ bins[b].inputEnergy = e; // Used for interpolating prediction energy
827
+ energy[b] += e;
828
+ }
829
+ }
830
+ for (int b = 0; b < bands; ++b) {
831
+ smoothedEnergy[b] = energy[b];
832
+ }
833
+ smoothEnergyState = 0;
834
+ return;
835
+ }
836
+
837
+ // The two other steps are repeated smoothing passes, down and up
838
+ Sample e = smoothEnergyState;
839
+ for (int b = bands - 1; b >= 0; --b) {
840
+ e += (smoothedEnergy[b] - e)*smoothingSlew;
841
+ smoothedEnergy[b] = e;
842
+ }
843
+ for (int b = 0; b < bands; ++b) {
844
+ e += (smoothedEnergy[b] - e)*smoothingSlew;
845
+ smoothedEnergy[b] = e;
846
+ }
847
+ smoothEnergyState = e;
848
+ }
849
+
850
+ Sample mapFreq(Sample freq) const {
851
+ if (customFreqMap) return customFreqMap(freq);
852
+ if (freq > freqTonalityLimit) {
853
+ return freq + (freqMultiplier - 1)*freqTonalityLimit;
854
+ }
855
+ return freq*freqMultiplier;
856
+ }
857
+
858
+ // Identifies spectral peaks using energy across all channels
859
+ void findPeaks() {
860
+ peaks.resize(0);
861
+
862
+ int start = 0;
863
+ while (start < bands) {
864
+ if (energy[start] > smoothedEnergy[start]) {
865
+ int end = start;
866
+ Sample bandSum = 0, energySum = 0;
867
+ while (end < bands && energy[end] > smoothedEnergy[end]) {
868
+ bandSum += end*energy[end];
869
+ energySum += energy[end];
870
+ ++end;
871
+ }
872
+ Sample avgBand = bandSum/energySum;
873
+ Sample avgFreq = bandToFreq(avgBand);
874
+ peaks.emplace_back(Peak{avgBand, freqToBand(mapFreq(avgFreq))});
875
+
876
+ start = end;
877
+ }
878
+ ++start;
879
+ }
880
+ }
881
+
882
+ void updateOutputMap() {
883
+ if (peaks.empty()) {
884
+ for (int b = 0; b < bands; ++b) {
885
+ outputMap[b] = {Sample(b), 1};
886
+ }
887
+ return;
888
+ }
889
+ Sample bottomOffset = peaks[0].input - peaks[0].output;
890
+ for (int b = 0; b < std::min(bands, static_cast<int>(std::ceil(peaks[0].output))); ++b) {
891
+ outputMap[b] = {b + bottomOffset, 1};
892
+ }
893
+ // Interpolate between points
894
+ for (size_t p = 1; p < peaks.size(); ++p) {
895
+ const Peak &prev = peaks[p - 1], &next = peaks[p];
896
+ Sample rangeScale = 1/(next.output - prev.output);
897
+ Sample outOffset = prev.input - prev.output;
898
+ Sample outScale = next.input - next.output - prev.input + prev.output;
899
+ Sample gradScale = outScale*rangeScale;
900
+ int startBin = std::max(0, static_cast<int>(std::ceil(prev.output)));
901
+ int endBin = std::min(bands, static_cast<int>(std::ceil(next.output)));
902
+ for (int b = startBin; b < endBin; ++b) {
903
+ Sample r = (b - prev.output)*rangeScale;
904
+ Sample h = r*r*(3 - 2*r);
905
+ Sample outB = b + outOffset + h*outScale;
906
+
907
+ Sample gradH = 6*r*(1 - r);
908
+ Sample gradB = 1 + gradH*gradScale;
909
+
910
+ outputMap[b] = {outB, gradB};
911
+ }
912
+ }
913
+ Sample topOffset = peaks.back().input - peaks.back().output;
914
+ for (int b = std::max(0, static_cast<int>(peaks.back().output)); b < bands; ++b) {
915
+ outputMap[b] = {b + topOffset, 1};
916
+ }
917
+ }
918
+
919
+ // If we mapped formants the same way as mapFreq(), this would be the inverse
920
+ Sample invMapFormant(Sample freq) const {
921
+ if (freq*invFormantMultiplier > freqTonalityLimit) {
922
+ return freq + (1 - formantMultiplier)*freqTonalityLimit;
923
+ }
924
+ return freq*invFormantMultiplier;
925
+ }
926
+
927
+ Sample freqEstimateWeighted = 0;
928
+ Sample freqEstimateWeight = 0;
929
+ Sample estimateFrequency() {
930
+ // 3 highest peaks in the input
931
+ std::array<int, 3> peakIndices{0, 0, 0};
932
+ for (int b = 1; b < bands - 1; ++b) {
933
+ Sample e = formantMetric[b];
934
+ // local maxima only
935
+ if (e < formantMetric[b - 1] || e <= formantMetric[b + 1]) continue;
936
+
937
+ if (e > formantMetric[peakIndices[0]]) {
938
+ if (e > formantMetric[peakIndices[1]]) {
939
+ if (e > formantMetric[peakIndices[2]]) {
940
+ peakIndices = {peakIndices[1], peakIndices[2], b};
941
+ } else {
942
+ peakIndices = {peakIndices[1], b, peakIndices[2]};
943
+ }
944
+ } else {
945
+ peakIndices[0] = b;
946
+ }
947
+ }
948
+ }
949
+
950
+ // VERY rough pitch estimation
951
+ int peakEstimate = peakIndices[2];
952
+ if (formantMetric[peakIndices[1]] > formantMetric[peakIndices[2]]*0.1) {
953
+ int diff = std::abs(peakEstimate - peakIndices[1]);
954
+ if (diff > peakEstimate/8 && diff < peakEstimate*7/8) peakEstimate = peakEstimate%diff;
955
+ if (formantMetric[peakIndices[0]] > formantMetric[peakIndices[2]]*0.01) {
956
+ diff = std::abs(peakEstimate - peakIndices[0]);
957
+ if (diff > peakEstimate/8 && diff < peakEstimate*7/8) peakEstimate = peakEstimate%diff;
958
+ }
959
+ }
960
+ Sample weight = formantMetric[peakIndices[2]];
961
+ // Smooth it out a bit
962
+ freqEstimateWeighted += (peakEstimate*weight - freqEstimateWeighted)*Sample(0.25);
963
+ freqEstimateWeight += (weight - freqEstimateWeight)*Sample(0.25);
964
+
965
+ return freqEstimateWeighted/(freqEstimateWeight + Sample(1e-30));
966
+ }
967
+
968
+ Sample freqEstimate;
969
+
970
+ std::vector<Sample> formantMetric;
971
+ Sample formantBaseFreq = 0;
972
+ void updateFormants(size_t step) {
973
+ if (step-- == 0) {
974
+ for (auto &e : formantMetric) e = 0;
975
+ for (int c = 0; c < channels; ++c) {
976
+ Band *bins = bandsForChannel(c);
977
+ for (int b = 0; b < bands; ++b) {
978
+ formantMetric[b] += bins[b].inputEnergy;
979
+ }
980
+ }
981
+
982
+ freqEstimate = freqToBand(formantBaseFreq);
983
+ if (formantBaseFreq <= 0) freqEstimate = estimateFrequency();
984
+ } else if (step-- == 0) {
985
+ Sample decay = 1 - 1/(freqEstimate*Sample(0.5) + 1);
986
+ Sample e = 0;
987
+ for (size_t repeat = 0; repeat < 2; ++repeat) {
988
+ for (int b = bands - 1; b >= 0; --b) {
989
+ e = std::max(formantMetric[b], e*decay);
990
+ formantMetric[b] = e;
991
+ }
992
+ for (int b = 0; b < bands; ++b) {
993
+ e = std::max(formantMetric[b], e*decay);
994
+ formantMetric[b] = e;
995
+ }
996
+ }
997
+ decay = 1/decay;
998
+ for (size_t repeat = 0; repeat < 2; ++repeat) {
999
+ for (int b = bands - 1; b >= 0; --b) {
1000
+ e = std::min(formantMetric[b], e*decay);
1001
+ formantMetric[b] = e;
1002
+ }
1003
+ for (int b = 0; b < bands; ++b) {
1004
+ e = std::min(formantMetric[b], e*decay);
1005
+ formantMetric[b] = e;
1006
+ }
1007
+ }
1008
+ } else {
1009
+ auto getFormant = [&](Sample band) -> Sample {
1010
+ if (band < 0) return 0;
1011
+ band = std::min(band, static_cast<Sample>(bands));
1012
+ int floorBand = std::floor(band);
1013
+ Sample fracBand = band - floorBand;
1014
+ Sample low = formantMetric[floorBand], high = formantMetric[floorBand + 1];
1015
+ return low + (high - low)*fracBand;
1016
+ };
1017
+
1018
+ for (int b = 0; b < bands; ++b) {
1019
+ Sample inputF = bandToFreq(static_cast<Sample>(b));
1020
+ Sample outputF = formantCompensation ? mapFreq(inputF) : inputF;
1021
+ outputF = invMapFormant(outputF);
1022
+
1023
+ Sample inputE = formantMetric[b];
1024
+ Sample targetE = getFormant(freqToBand(outputF));
1025
+
1026
+ Sample formantRatio = targetE/(inputE + Sample(1e-30));
1027
+ Sample energyRatio = formantRatio;
1028
+
1029
+ for (int c = 0; c < channels; ++c) {
1030
+ Band *bins = bandsForChannel(c);
1031
+ // This is what's used to decide the output energy, so this affects the output
1032
+ bins[b].inputEnergy *= energyRatio;
1033
+ }
1034
+ }
1035
+ }
1036
+ }
1037
+
1038
+ // Proxy class to avoid copying/allocating anything
1039
+ template<class Io>
1040
+ struct OffsetIO {
1041
+ Io &io;
1042
+ int offset;
1043
+
1044
+ struct Channel {
1045
+ Io &io;
1046
+ int channel;
1047
+ int offset;
1048
+
1049
+ auto operator[](int i) -> decltype(io[0][0]) {
1050
+ return io[channel][i + offset];
1051
+ }
1052
+ };
1053
+ Channel operator[](int c) {
1054
+ return {io, c, offset};
1055
+ }
1056
+ };
1057
+ };
1058
+
1059
+ }} // namespace
1060
+ #endif // include guard