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.
- package/CHANGELOG.md +134 -0
- package/LICENSE +20 -0
- package/LICENSES/Apache-2.0.txt +175 -0
- package/NOTICE +68 -0
- package/README.md +313 -2
- package/android/build.gradle +137 -0
- package/android/consumer-rules.pro +29 -0
- package/android/src/main/AndroidManifest.xml +9 -0
- package/android/src/main/cpp/CMakeLists.txt +48 -0
- package/android/src/main/cpp/gx_dfn_engine.c +332 -0
- package/android/src/main/cpp/gx_fx_jni.cpp +34 -0
- package/android/src/main/cpp/gx_ns_jni.c +162 -0
- package/android/src/main/cpp/third_party/onnxruntime/LICENSE +21 -0
- package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_c_api.h +9227 -0
- package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_ep_c_api.h +3315 -0
- package/android/src/main/cpp/third_party/onnxruntime/include/onnxruntime_error_code.h +98 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/GravixAudioHooks.kt +144 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/GravixAudioModule.kt +105 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/GravixNativeAudio.kt +310 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtc.kt +134 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtcModule.kt +176 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/GravixRtcPackage.kt +39 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioDeviceKind.java +44 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioManagerUtils.kt +120 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioSwitchManager.java +270 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/audio/AudioType.kt +50 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicCaptureRouter.kt +375 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicDecoder.kt +339 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicInterruptionWatcher.kt +82 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMicRelease.kt +117 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMixKernel.kt +191 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/music/MusicMixerEngine.kt +617 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/AudioRoutePolicy.kt +137 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GravixCaptureProcessing.kt +67 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxCaptureDump.kt +143 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxCaptureSwitcher.kt +196 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNoiseProcessor.kt +795 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsLogic.kt +180 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsNative.kt +83 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxNsStudio.kt +372 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxRecordStartGuard.kt +76 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxRecorderSeam.kt +434 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxSingerSource.kt +37 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/GxVoiceFx.kt +160 -0
- package/android/src/main/java/com/gravitycompile/gravixrtc/processing/OtherAppAudioPolicy.kt +143 -0
- package/android/src/main/java/org/webrtc/audio/WebRtcAudioTrackHelper.kt +43 -0
- package/docs/ANDROID_NATIVE_HOOKS.md +102 -0
- package/docs/FEATURE_MATRIX.md +192 -0
- package/docs/IOS_NATIVE_HOOKS.md +190 -0
- package/gravix-rtc-react-native.podspec +49 -0
- package/ios/GravixRtc.h +7 -0
- package/ios/GravixRtc.mm +273 -0
- package/ios/GravixRtcSetup.h +29 -0
- package/ios/GravixRtcSetup.m +52 -0
- package/ios/audio/GravixRtcIosAudio.m +157 -0
- package/ios/audio/GxAudioDevice.h +62 -0
- package/ios/audio/GxAudioDevice.m +857 -0
- package/ios/audio/GxAudioSetup.h +34 -0
- package/ios/audio/GxAudioSetup.m +70 -0
- package/ios/audio/GxCompat.swift +53 -0
- package/ios/audio/GxIosAudio.swift +99 -0
- package/ios/audio/GxMusicController.swift +150 -0
- package/ios/audio/GxMusicPost.swift +362 -0
- package/ios/audio/GxNoiseProcessor.swift +718 -0
- package/ios/audio/GxNsDfn3CoreML.swift +195 -0
- package/ios/audio/GxNsStudioEngine.swift +237 -0
- package/ios/audio/GxNsStudioPack.swift +212 -0
- package/ios/audio/GxVoiceFx.swift +161 -0
- package/ios/audio/gx_adm_state.h +153 -0
- package/ios/gxcore/apple/gx_music_post.c +312 -0
- package/ios/gxcore/apple/gx_pcm.c +30 -0
- package/ios/gxcore/apple/gxns_build.h +19 -0
- package/ios/gxcore/apple/gxns_ios.c +17 -0
- package/ios/gxcore/apple/include/gx_fx.h +3 -0
- package/ios/gxcore/apple/include/gx_music_post.h +88 -0
- package/ios/gxcore/apple/include/gx_ns.h +3 -0
- package/ios/gxcore/apple/include/gx_pcm.h +33 -0
- package/ios/gxcore/apple/include/gxns_ios.h +18 -0
- package/ios/gxcore/apple/include/module.modulemap +10 -0
- package/ios/gxcore/apple/shim/rnnoise.h +2 -0
- package/ios/gxcore/apple/shim/speex_resampler.h +2 -0
- package/ios/gxcore/apple/units/gx_fx_forward.cpp +3 -0
- package/ios/gxcore/apple/units/gx_ns.c +7 -0
- package/ios/gxcore/apple/units/gx_ns_timer.c +7 -0
- package/ios/gxcore/apple/units/rnnoise_celt_lpc.c +7 -0
- package/ios/gxcore/apple/units/rnnoise_denoise.c +7 -0
- package/ios/gxcore/apple/units/rnnoise_kiss_fft.c +7 -0
- package/ios/gxcore/apple/units/rnnoise_pitch.c +7 -0
- package/ios/gxcore/apple/units/rnnoise_rnn.c +7 -0
- package/ios/gxcore/apple/units/rnnoise_rnn_data.c +7 -0
- package/ios/gxcore/apple/units/speexdsp_resample.c +7 -0
- package/ios/resources/ns/DFN3-NOTICE.txt +10 -0
- package/ios/resources/ns/DeepFilterNet-LICENSE-APACHE +201 -0
- package/ios/resources/ns/DeepFilterNet-LICENSE-MIT +19 -0
- package/ios/resources/ns/gxns_dfn3.mlmodelc/analytics/coremldata.bin +0 -0
- package/ios/resources/ns/gxns_dfn3.mlmodelc/coremldata.bin +0 -0
- package/ios/resources/ns/gxns_dfn3.mlmodelc/metadata.json +338 -0
- package/ios/resources/ns/gxns_dfn3.mlmodelc/model.mil +927 -0
- package/ios/resources/ns/gxns_dfn3.mlmodelc/weights/weight.bin +0 -0
- package/lib/module/android/bindings.js +49 -0
- package/lib/module/android/bindings.js.map +1 -0
- package/lib/module/android/index.js +13 -0
- package/lib/module/android/index.js.map +1 -0
- package/lib/module/android/micMute.js +341 -0
- package/lib/module/android/micMute.js.map +1 -0
- package/lib/module/android/nativeAudio.js +47 -0
- package/lib/module/android/nativeAudio.js.map +1 -0
- package/lib/module/android/noiseSuppression.js +170 -0
- package/lib/module/android/noiseSuppression.js.map +1 -0
- package/lib/module/android/roomMusic.js +142 -0
- package/lib/module/android/roomMusic.js.map +1 -0
- package/lib/module/android/singerMode.js +31 -0
- package/lib/module/android/singerMode.js.map +1 -0
- package/lib/module/android/voiceEffects.js +65 -0
- package/lib/module/android/voiceEffects.js.map +1 -0
- package/lib/module/audio/AudioManager.js +79 -0
- package/lib/module/audio/AudioManager.js.map +1 -0
- package/lib/module/audio/AudioSession.js +90 -0
- package/lib/module/audio/AudioSession.js.map +1 -0
- package/lib/module/audio/remoteVolume.js +54 -0
- package/lib/module/audio/remoteVolume.js.map +1 -0
- package/lib/module/components/VideoView.js +109 -0
- package/lib/module/components/VideoView.js.map +1 -0
- package/lib/module/features/audioScenario.js +53 -0
- package/lib/module/features/audioScenario.js.map +1 -0
- package/lib/module/features/beauty.js +828 -0
- package/lib/module/features/beauty.js.map +1 -0
- package/lib/module/features/micRelease.js +89 -0
- package/lib/module/features/micRelease.js.map +1 -0
- package/lib/module/features/noiseSuppression.js +213 -0
- package/lib/module/features/noiseSuppression.js.map +1 -0
- package/lib/module/features/roomMusic.js +90 -0
- package/lib/module/features/roomMusic.js.map +1 -0
- package/lib/module/features/roomMusicAndroid.js +294 -0
- package/lib/module/features/roomMusicAndroid.js.map +1 -0
- package/lib/module/features/roomMusicIos.js +250 -0
- package/lib/module/features/roomMusicIos.js.map +1 -0
- package/lib/module/features/roomMusicTypes.js +53 -0
- package/lib/module/features/roomMusicTypes.js.map +1 -0
- package/lib/module/features/screenShare.js +22 -0
- package/lib/module/features/screenShare.js.map +1 -0
- package/lib/module/features/singerMode.js +73 -0
- package/lib/module/features/singerMode.js.map +1 -0
- package/lib/module/features/voiceEffects.js +148 -0
- package/lib/module/features/voiceEffects.js.map +1 -0
- package/lib/module/hooks/context.js +34 -0
- package/lib/module/hooks/context.js.map +1 -0
- package/lib/module/hooks/hooks.js +145 -0
- package/lib/module/hooks/hooks.js.map +1 -0
- package/lib/module/hooks/types.js +4 -0
- package/lib/module/hooks/types.js.map +1 -0
- package/lib/module/hooks/useRoomEvents.js +30 -0
- package/lib/module/hooks/useRoomEvents.js.map +1 -0
- package/lib/module/index.js +43 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/ios/autoAttach.js +14 -0
- package/lib/module/ios/autoAttach.js.map +1 -0
- package/lib/module/ios/iosAudio.js +149 -0
- package/lib/module/ios/iosAudio.js.map +1 -0
- package/lib/module/join/appStateLifecycle.js +54 -0
- package/lib/module/join/appStateLifecycle.js.map +1 -0
- package/lib/module/micPolicy.js +249 -0
- package/lib/module/micPolicy.js.map +1 -0
- package/lib/module/native/NativeGravixAudio.js +18 -0
- package/lib/module/native/NativeGravixAudio.js.map +1 -0
- package/lib/module/native/NativeGravixRtc.js +13 -0
- package/lib/module/native/NativeGravixRtc.js.map +1 -0
- package/lib/module/native/module.js +23 -0
- package/lib/module/native/module.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/polyfills/DOMException.js +22 -0
- package/lib/module/polyfills/DOMException.js.map +1 -0
- package/lib/module/polyfills/early.js +18 -0
- package/lib/module/polyfills/early.js.map +1 -0
- package/lib/module/polyfills/event.js +71 -0
- package/lib/module/polyfills/event.js.map +1 -0
- package/lib/module/polyfills/sha256.js +92 -0
- package/lib/module/polyfills/sha256.js.map +1 -0
- package/lib/module/polyfills/subtleHmac.js +91 -0
- package/lib/module/polyfills/subtleHmac.js.map +1 -0
- package/lib/module/polyfills/textCoding.js +164 -0
- package/lib/module/polyfills/textCoding.js.map +1 -0
- package/lib/module/registerGlobals.js +135 -0
- package/lib/module/registerGlobals.js.map +1 -0
- package/lib/module/unsupported.js +90 -0
- package/lib/module/unsupported.js.map +1 -0
- package/lib/module/version.js +7 -0
- package/lib/module/version.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/android/bindings.d.ts +14 -0
- package/lib/typescript/src/android/bindings.d.ts.map +1 -0
- package/lib/typescript/src/android/index.d.ts +9 -0
- package/lib/typescript/src/android/index.d.ts.map +1 -0
- package/lib/typescript/src/android/micMute.d.ts +124 -0
- package/lib/typescript/src/android/micMute.d.ts.map +1 -0
- package/lib/typescript/src/android/nativeAudio.d.ts +40 -0
- package/lib/typescript/src/android/nativeAudio.d.ts.map +1 -0
- package/lib/typescript/src/android/noiseSuppression.d.ts +48 -0
- package/lib/typescript/src/android/noiseSuppression.d.ts.map +1 -0
- package/lib/typescript/src/android/roomMusic.d.ts +80 -0
- package/lib/typescript/src/android/roomMusic.d.ts.map +1 -0
- package/lib/typescript/src/android/singerMode.d.ts +27 -0
- package/lib/typescript/src/android/singerMode.d.ts.map +1 -0
- package/lib/typescript/src/android/voiceEffects.d.ts +30 -0
- package/lib/typescript/src/android/voiceEffects.d.ts.map +1 -0
- package/lib/typescript/src/audio/AudioManager.d.ts +32 -0
- package/lib/typescript/src/audio/AudioManager.d.ts.map +1 -0
- package/lib/typescript/src/audio/AudioSession.d.ts +70 -0
- package/lib/typescript/src/audio/AudioSession.d.ts.map +1 -0
- package/lib/typescript/src/audio/remoteVolume.d.ts +8 -0
- package/lib/typescript/src/audio/remoteVolume.d.ts.map +1 -0
- package/lib/typescript/src/components/VideoView.d.ts +44 -0
- package/lib/typescript/src/components/VideoView.d.ts.map +1 -0
- package/lib/typescript/src/features/audioScenario.d.ts +24 -0
- package/lib/typescript/src/features/audioScenario.d.ts.map +1 -0
- package/lib/typescript/src/features/beauty.d.ts +377 -0
- package/lib/typescript/src/features/beauty.d.ts.map +1 -0
- package/lib/typescript/src/features/micRelease.d.ts +28 -0
- package/lib/typescript/src/features/micRelease.d.ts.map +1 -0
- package/lib/typescript/src/features/noiseSuppression.d.ts +64 -0
- package/lib/typescript/src/features/noiseSuppression.d.ts.map +1 -0
- package/lib/typescript/src/features/roomMusic.d.ts +41 -0
- package/lib/typescript/src/features/roomMusic.d.ts.map +1 -0
- package/lib/typescript/src/features/roomMusicAndroid.d.ts +60 -0
- package/lib/typescript/src/features/roomMusicAndroid.d.ts.map +1 -0
- package/lib/typescript/src/features/roomMusicIos.d.ts +56 -0
- package/lib/typescript/src/features/roomMusicIos.d.ts.map +1 -0
- package/lib/typescript/src/features/roomMusicTypes.d.ts +76 -0
- package/lib/typescript/src/features/roomMusicTypes.d.ts.map +1 -0
- package/lib/typescript/src/features/screenShare.d.ts +12 -0
- package/lib/typescript/src/features/screenShare.d.ts.map +1 -0
- package/lib/typescript/src/features/singerMode.d.ts +20 -0
- package/lib/typescript/src/features/singerMode.d.ts.map +1 -0
- package/lib/typescript/src/features/voiceEffects.d.ts +46 -0
- package/lib/typescript/src/features/voiceEffects.d.ts.map +1 -0
- package/lib/typescript/src/hooks/context.d.ts +12 -0
- package/lib/typescript/src/hooks/context.d.ts.map +1 -0
- package/lib/typescript/src/hooks/hooks.d.ts +54 -0
- package/lib/typescript/src/hooks/hooks.d.ts.map +1 -0
- package/lib/typescript/src/hooks/types.d.ts +8 -0
- package/lib/typescript/src/hooks/types.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useRoomEvents.d.ts +13 -0
- package/lib/typescript/src/hooks/useRoomEvents.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +37 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/ios/autoAttach.d.ts +2 -0
- package/lib/typescript/src/ios/autoAttach.d.ts.map +1 -0
- package/lib/typescript/src/ios/iosAudio.d.ts +96 -0
- package/lib/typescript/src/ios/iosAudio.d.ts.map +1 -0
- package/lib/typescript/src/join/appStateLifecycle.d.ts +14 -0
- package/lib/typescript/src/join/appStateLifecycle.d.ts.map +1 -0
- package/lib/typescript/src/micPolicy.d.ts +104 -0
- package/lib/typescript/src/micPolicy.d.ts.map +1 -0
- package/lib/typescript/src/native/NativeGravixAudio.d.ts +21 -0
- package/lib/typescript/src/native/NativeGravixAudio.d.ts.map +1 -0
- package/lib/typescript/src/native/NativeGravixRtc.d.ts +31 -0
- package/lib/typescript/src/native/NativeGravixRtc.d.ts.map +1 -0
- package/lib/typescript/src/native/module.d.ts +7 -0
- package/lib/typescript/src/native/module.d.ts.map +1 -0
- package/lib/typescript/src/polyfills/DOMException.d.ts +3 -0
- package/lib/typescript/src/polyfills/DOMException.d.ts.map +1 -0
- package/lib/typescript/src/polyfills/early.d.ts +2 -0
- package/lib/typescript/src/polyfills/early.d.ts.map +1 -0
- package/lib/typescript/src/polyfills/event.d.ts +2 -0
- package/lib/typescript/src/polyfills/event.d.ts.map +1 -0
- package/lib/typescript/src/polyfills/sha256.d.ts +3 -0
- package/lib/typescript/src/polyfills/sha256.d.ts.map +1 -0
- package/lib/typescript/src/polyfills/subtleHmac.d.ts +37 -0
- package/lib/typescript/src/polyfills/subtleHmac.d.ts.map +1 -0
- package/lib/typescript/src/polyfills/textCoding.d.ts +36 -0
- package/lib/typescript/src/polyfills/textCoding.d.ts.map +1 -0
- package/lib/typescript/src/registerGlobals.d.ts +42 -0
- package/lib/typescript/src/registerGlobals.d.ts.map +1 -0
- package/lib/typescript/src/unsupported.d.ts +42 -0
- package/lib/typescript/src/unsupported.d.ts.map +1 -0
- package/lib/typescript/src/version.d.ts +3 -0
- package/lib/typescript/src/version.d.ts.map +1 -0
- package/metro/index.js +39 -0
- package/metro/noise-stub.js +10 -0
- package/native/gxfx/include/gx_fx.h +82 -0
- package/native/gxfx/src/gx_fx.cpp +527 -0
- package/native/gxfx/third_party/README.md +4 -0
- package/native/gxfx/third_party/signalsmith-linear/LICENSE.txt +21 -0
- package/native/gxfx/third_party/signalsmith-linear/approx.h +191 -0
- package/native/gxfx/third_party/signalsmith-linear/fft.h +1386 -0
- package/native/gxfx/third_party/signalsmith-linear/linear.h +1129 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/approx-cmsisdsp.h +40 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/basic-fill-warnings.h +61 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/fft-accelerate.h +340 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/fft-cmsisdsp.h +307 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/fft-ipp.h +532 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/fft-linear.h +213 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/fft-pffft-double.h +221 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/fft-pffft.h +221 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/linear-accelerate.h +593 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/linear-cmsisdsp.h +713 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/linear-xsimd-dispatch.cpp +6 -0
- package/native/gxfx/third_party/signalsmith-linear/platform/linear-xsimd.h +818 -0
- package/native/gxfx/third_party/signalsmith-linear/stft.h +700 -0
- package/native/gxfx/third_party/signalsmith-stretch-LICENSE.txt +21 -0
- package/native/gxfx/third_party/signalsmith-stretch.h +1060 -0
- package/native/gxns/CMakeLists.txt +67 -0
- package/native/gxns/include/gx_ns.h +285 -0
- package/native/gxns/src/gx_ns.c +1518 -0
- package/native/gxns/src/gx_ns_timer.c +84 -0
- package/native/gxns/src/gx_ns_timer.h +29 -0
- package/native/gxns/third_party/VENDOR.md +43 -0
- package/native/gxns/third_party/rnnoise/AUTHORS +1 -0
- package/native/gxns/third_party/rnnoise/COPYING +31 -0
- package/native/gxns/third_party/rnnoise/include/rnnoise.h +117 -0
- package/native/gxns/third_party/rnnoise/src/_kiss_fft_guts.h +182 -0
- package/native/gxns/third_party/rnnoise/src/arch.h +261 -0
- package/native/gxns/third_party/rnnoise/src/celt_lpc.c +172 -0
- package/native/gxns/third_party/rnnoise/src/celt_lpc.h +45 -0
- package/native/gxns/third_party/rnnoise/src/common.h +48 -0
- package/native/gxns/third_party/rnnoise/src/denoise.c +664 -0
- package/native/gxns/third_party/rnnoise/src/kiss_fft.c +601 -0
- package/native/gxns/third_party/rnnoise/src/kiss_fft.h +203 -0
- package/native/gxns/third_party/rnnoise/src/opus_types.h +159 -0
- package/native/gxns/third_party/rnnoise/src/pitch.c +523 -0
- package/native/gxns/third_party/rnnoise/src/pitch.h +149 -0
- package/native/gxns/third_party/rnnoise/src/rnn.c +178 -0
- package/native/gxns/third_party/rnnoise/src/rnn.h +65 -0
- package/native/gxns/third_party/rnnoise/src/rnn_data.c +11051 -0
- package/native/gxns/third_party/rnnoise/src/rnn_data.h +34 -0
- package/native/gxns/third_party/rnnoise/src/tansig_table.h +45 -0
- package/native/gxns/third_party/speexdsp/AUTHORS +18 -0
- package/native/gxns/third_party/speexdsp/COPYING +35 -0
- package/native/gxns/third_party/speexdsp/arch.h +232 -0
- package/native/gxns/third_party/speexdsp/resample.c +1239 -0
- package/native/gxns/third_party/speexdsp/resample_neon.h +339 -0
- package/native/gxns/third_party/speexdsp/resample_sse.h +128 -0
- package/native/gxns/third_party/speexdsp/speex_resampler.h +343 -0
- package/package.json +186 -4
- package/src/android/bindings.ts +61 -0
- package/src/android/index.ts +10 -0
- package/src/android/micMute.ts +422 -0
- package/src/android/nativeAudio.ts +94 -0
- package/src/android/noiseSuppression.ts +209 -0
- package/src/android/roomMusic.ts +193 -0
- package/src/android/singerMode.ts +53 -0
- package/src/android/voiceEffects.ts +126 -0
- package/src/audio/AudioManager.ts +111 -0
- package/src/audio/AudioSession.ts +190 -0
- package/src/audio/remoteVolume.ts +58 -0
- package/src/components/VideoView.tsx +152 -0
- package/src/features/audioScenario.ts +58 -0
- package/src/features/beauty.ts +1057 -0
- package/src/features/micRelease.ts +108 -0
- package/src/features/noiseSuppression.ts +251 -0
- package/src/features/roomMusic.ts +106 -0
- package/src/features/roomMusicAndroid.ts +310 -0
- package/src/features/roomMusicIos.ts +300 -0
- package/src/features/roomMusicTypes.ts +97 -0
- package/src/features/screenShare.ts +19 -0
- package/src/features/singerMode.ts +70 -0
- package/src/features/voiceEffects.ts +245 -0
- package/src/hooks/context.tsx +38 -0
- package/src/hooks/hooks.ts +247 -0
- package/src/hooks/types.ts +10 -0
- package/src/hooks/useRoomEvents.ts +35 -0
- package/src/index.tsx +134 -0
- package/src/ios/autoAttach.ts +11 -0
- package/src/ios/iosAudio.ts +202 -0
- package/src/join/appStateLifecycle.ts +55 -0
- package/src/micPolicy.ts +312 -0
- package/src/native/NativeGravixAudio.ts +30 -0
- package/src/native/NativeGravixRtc.ts +38 -0
- package/src/native/module.ts +22 -0
- package/src/polyfills/DOMException.ts +20 -0
- package/src/polyfills/early.ts +16 -0
- package/src/polyfills/event.ts +88 -0
- package/src/polyfills/sha256.ts +112 -0
- package/src/polyfills/subtleHmac.ts +124 -0
- package/src/polyfills/textCoding.ts +197 -0
- package/src/registerGlobals.ts +201 -0
- package/src/unsupported.ts +139 -0
- package/src/version.ts +4 -0
|
@@ -0,0 +1,1518 @@
|
|
|
1
|
+
/* Copyright (c) 2024-2026 Gravity Compile. MIT License; see LICENSE. */
|
|
2
|
+
#include "gx_ns.h"
|
|
3
|
+
|
|
4
|
+
#include <math.h>
|
|
5
|
+
#include <stdatomic.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
#include "gx_ns_timer.h"
|
|
10
|
+
#include "rnnoise.h"
|
|
11
|
+
#include "speex_resampler.h"
|
|
12
|
+
|
|
13
|
+
#define NB_RATES 6
|
|
14
|
+
#define GX_PI 3.14159265358979323846
|
|
15
|
+
#define GX_NS_TALK_WIN 100 /* talk detector: 1 s of post-NS block levels */
|
|
16
|
+
#define GX_NS_TALK_WARM 200 /* the first 2 s (RNNoise's output on noise still rising) ... */
|
|
17
|
+
#define GX_NS_TALK_WIN0 30 /* ... compare with the last 300 ms only */
|
|
18
|
+
static const int k_rates[NB_RATES] = {8000, 16000, 24000, 32000, 44100, 48000};
|
|
19
|
+
|
|
20
|
+
#define SRC_DRY 0
|
|
21
|
+
#define SRC_RNN 1
|
|
22
|
+
#define SRC_EXT 2
|
|
23
|
+
|
|
24
|
+
#define DELAY_FADE 240 /* 5 ms at 48 kHz */
|
|
25
|
+
#define DELAY_FORCE_BLOCKS 200 /* change the delay after 2 s even if never quiet */
|
|
26
|
+
#define RESAMPLE_SLACK 8 /* samples of FIFO headroom against resampler jitter */
|
|
27
|
+
|
|
28
|
+
/* ---------- small ring FIFO ---------- */
|
|
29
|
+
typedef struct {
|
|
30
|
+
float *buf;
|
|
31
|
+
int cap, r, count;
|
|
32
|
+
} fifo;
|
|
33
|
+
|
|
34
|
+
static int fifo_alloc(fifo *f, int cap) {
|
|
35
|
+
f->buf = (float *)calloc((size_t)cap, sizeof(float));
|
|
36
|
+
f->cap = cap;
|
|
37
|
+
f->r = f->count = 0;
|
|
38
|
+
return f->buf ? 0 : -1;
|
|
39
|
+
}
|
|
40
|
+
static void fifo_clear(fifo *f) { f->r = f->count = 0; }
|
|
41
|
+
static int fifo_push(fifo *f, const float *x, int n) {
|
|
42
|
+
int i, w;
|
|
43
|
+
if (n > f->cap - f->count) n = f->cap - f->count; /* never happens with our sizing */
|
|
44
|
+
w = f->r + f->count;
|
|
45
|
+
for (i = 0; i < n; i++) f->buf[(w + i) % f->cap] = x ? x[i] : 0.f;
|
|
46
|
+
f->count += n;
|
|
47
|
+
return n;
|
|
48
|
+
}
|
|
49
|
+
static int fifo_pop(fifo *f, float *x, int n) {
|
|
50
|
+
int i;
|
|
51
|
+
if (n > f->count) n = f->count;
|
|
52
|
+
for (i = 0; i < n; i++) x[i] = f->buf[(f->r + i) % f->cap];
|
|
53
|
+
f->r = (f->r + n) % f->cap;
|
|
54
|
+
f->count -= n;
|
|
55
|
+
return n;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/* ---------- delay line (48 kHz, block-wise) ---------- */
|
|
59
|
+
typedef struct {
|
|
60
|
+
float *buf;
|
|
61
|
+
int cap, w; /* w = next write position */
|
|
62
|
+
} delay_line;
|
|
63
|
+
|
|
64
|
+
static int dl_alloc(delay_line *d, int cap) {
|
|
65
|
+
d->buf = (float *)calloc((size_t)cap, sizeof(float));
|
|
66
|
+
d->cap = cap;
|
|
67
|
+
d->w = 0;
|
|
68
|
+
return d->buf ? 0 : -1;
|
|
69
|
+
}
|
|
70
|
+
static void dl_clear(delay_line *d) {
|
|
71
|
+
memset(d->buf, 0, sizeof(float) * (size_t)d->cap);
|
|
72
|
+
d->w = 0;
|
|
73
|
+
}
|
|
74
|
+
static void dl_write(delay_line *d, const float *x, int n) {
|
|
75
|
+
int i;
|
|
76
|
+
for (i = 0; i < n; i++) d->buf[(d->w + i) % d->cap] = x[i];
|
|
77
|
+
d->w = (d->w + n) % d->cap;
|
|
78
|
+
}
|
|
79
|
+
/* The n samples written last, delayed by `delay` samples. */
|
|
80
|
+
static void dl_read(const delay_line *d, float *out, int n, int delay) {
|
|
81
|
+
int i, start = d->w - n - delay;
|
|
82
|
+
while (start < 0) start += d->cap;
|
|
83
|
+
for (i = 0; i < n; i++) out[i] = d->buf[(start + i) % d->cap];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/* ---------- per-rate resamplers (pre-allocated) ---------- */
|
|
87
|
+
/* auto music detector (2026-11-20) */
|
|
88
|
+
#define GX_MU_N 1024 /* FFT size at 48 kHz (21 ms window, 10 ms hop) */
|
|
89
|
+
#define GX_MU_K0 4 /* analysed bins: ~190 Hz ... */
|
|
90
|
+
#define GX_MU_K1 110 /* ... ~5.2 kHz (46.9 Hz per bin) */
|
|
91
|
+
#define GX_MU_WIN 300 /* decision window: 3 s of blocks */
|
|
92
|
+
#define GX_MU_PEAKWIN 20 /* dip test: under the max of the last 200 ms ... */
|
|
93
|
+
#ifndef GX_MU_DIP_DB
|
|
94
|
+
#define GX_MU_DIP_DB 15.f /* ... by this much (speech pauses between words) */
|
|
95
|
+
#endif
|
|
96
|
+
#ifndef GX_MU_HOLD
|
|
97
|
+
#define GX_MU_HOLD 15 /* a spectral peak held 150 ms is a note */
|
|
98
|
+
#endif
|
|
99
|
+
#ifndef GX_MU_PROM_DB
|
|
100
|
+
#define GX_MU_PROM_DB 5.f /* peak prominence over its neighbourhood (8 missed dense mixes) */
|
|
101
|
+
#endif
|
|
102
|
+
#ifndef GX_MU_ENTER_DIP
|
|
103
|
+
#define GX_MU_ENTER_DIP 0.04f
|
|
104
|
+
#endif
|
|
105
|
+
#ifndef GX_MU_ENTER_TONAL
|
|
106
|
+
#define GX_MU_ENTER_TONAL 0.5f
|
|
107
|
+
#endif
|
|
108
|
+
#ifndef GX_MU_ENTER_ONSETS
|
|
109
|
+
#define GX_MU_ENTER_ONSETS 20
|
|
110
|
+
#endif
|
|
111
|
+
#ifndef GX_MU_STAY_DIP
|
|
112
|
+
#define GX_MU_STAY_DIP 0.12f
|
|
113
|
+
#endif
|
|
114
|
+
#ifndef GX_MU_STAY_TONAL
|
|
115
|
+
#define GX_MU_STAY_TONAL 0.12f
|
|
116
|
+
#endif
|
|
117
|
+
#ifndef GX_MU_STAY_ONSETS
|
|
118
|
+
#define GX_MU_STAY_ONSETS 10
|
|
119
|
+
#endif
|
|
120
|
+
#define GX_MU_ENTER_RUN 30 /* the enter test holds 300 ms (on top of the 3 s window) */
|
|
121
|
+
#define GX_MU_LEAVE_RUN 1500 /* the stay test fails for 15 s (0.11.4: was 3 s; gapped songs dropped out mid-song) */
|
|
122
|
+
#define GX_MU_RAMP 30 /* 300 ms fades */
|
|
123
|
+
|
|
124
|
+
typedef struct {
|
|
125
|
+
int rate, block; /* block = samples per 10 ms at this rate */
|
|
126
|
+
SpeexResamplerState *up, *down;
|
|
127
|
+
int up_lat, down_lat; /* in samples at `rate` */
|
|
128
|
+
} rate_ctx;
|
|
129
|
+
|
|
130
|
+
struct gx_ns {
|
|
131
|
+
gx_ns_config cfg;
|
|
132
|
+
|
|
133
|
+
/* control (any thread) */
|
|
134
|
+
_Atomic int req_engine, req_bypass, req_agc, req_agc_order, req_p95_budget, req_pregain;
|
|
135
|
+
_Atomic float req_atten_db;
|
|
136
|
+
_Atomic uint32_t req_reset, req_stats_reset;
|
|
137
|
+
_Atomic(gx_ns_external_engine *) ext_ptr;
|
|
138
|
+
gx_ns_external_engine ext_slots[2];
|
|
139
|
+
int ext_slot_next;
|
|
140
|
+
_Atomic uint32_t ext_gen;
|
|
141
|
+
_Atomic uint32_t blocks;
|
|
142
|
+
|
|
143
|
+
/* status published for readers */
|
|
144
|
+
_Atomic int pub_active, pub_xfade, pub_reason, pub_rate, pub_latency;
|
|
145
|
+
_Atomic float pub_vad, pub_gain_db, pub_pregain_db;
|
|
146
|
+
_Atomic float pub_pg_applied_db, pub_agc_applied_db, pub_pause_att_db;
|
|
147
|
+
atomic_int pub_talk;
|
|
148
|
+
|
|
149
|
+
/* stream (audio thread only) */
|
|
150
|
+
rate_ctx rates[NB_RATES];
|
|
151
|
+
rate_ctx *rc;
|
|
152
|
+
int need_reprime;
|
|
153
|
+
int prefill;
|
|
154
|
+
fifo in48, out_r;
|
|
155
|
+
float *scratch_in48, *scratch_out_r;
|
|
156
|
+
int scratch48_cap, scratch_r_cap;
|
|
157
|
+
|
|
158
|
+
/* engines */
|
|
159
|
+
DenoiseState *rnn;
|
|
160
|
+
int rnn_running;
|
|
161
|
+
float vad;
|
|
162
|
+
uint32_t reset_seen, stats_reset_seen, ext_gen_seen;
|
|
163
|
+
int ext_failed;
|
|
164
|
+
int ext_slow; /* rolling p95 over budget: studio demoted until the next attach */
|
|
165
|
+
uint16_t ext_us[GX_NS_EXT_P95_WINDOW_MAX], ext_sel[GX_NS_EXT_P95_WINDOW_MAX];
|
|
166
|
+
int ext_us_n, ext_us_w, ext_us_since;
|
|
167
|
+
_Atomic uint32_t pub_ext_p95, pub_ext_slow, pub_ext_suppress;
|
|
168
|
+
int ext_suppress; /* output speech collapsed vs input: studio demoted until the next attach */
|
|
169
|
+
int sup_run; /* consecutive speech blocks with the collapse */
|
|
170
|
+
float sup_in, sup_out; /* speech-gated power EMAs of the engine's input / output ([-1,1] scale) */
|
|
171
|
+
float sup_rnn; /* ... and of RNNoise's (its shadow) output on the same blocks */
|
|
172
|
+
int reset_pending;
|
|
173
|
+
int ext_preroll;
|
|
174
|
+
|
|
175
|
+
/* delay compensation */
|
|
176
|
+
int L; /* output alignment latency at 48 kHz */
|
|
177
|
+
delay_line dl_dry, dl_rnn, dl_ext;
|
|
178
|
+
int quiet_wait;
|
|
179
|
+
int delay_fade_in; /* samples left of the 5 ms fade-in after a delay change */
|
|
180
|
+
|
|
181
|
+
/* crossfade */
|
|
182
|
+
int cur, next, xf_pos, xf_len;
|
|
183
|
+
int fade_in_pos; /* reset/rate fade-in progress, == xf_len when idle */
|
|
184
|
+
gx_ns_reason reason;
|
|
185
|
+
|
|
186
|
+
/* AGC + limiter */
|
|
187
|
+
int have_level;
|
|
188
|
+
float level_db, gain_db, prev_gain_lin;
|
|
189
|
+
float nf_db; /* energy VAD noise floor */
|
|
190
|
+
int nf_have; /* the floor starts at the first non-silent block's level */
|
|
191
|
+
/* Talk detector + pause gate (DTX, 2026-10-21). "Talk" is judged on what the
|
|
192
|
+
* NS leaves (post-NS level, pre-gain taken out, before our AGC) against its
|
|
193
|
+
* minimum over the last second: speech dips between syllables, what RNNoise
|
|
194
|
+
* leaves of noise does not. RNNoise's VAD alone calls swelling traffic
|
|
195
|
+
* noise speech (~100% of blocks), and a slow noise floor follows the swells
|
|
196
|
+
* too late. Out of talk (after a hangover) the pre-gain and AGC boosts ramp
|
|
197
|
+
* to unity (values held), so neither lifts the noise over Opus' DTX
|
|
198
|
+
* threshold; talk opens the gate at once. */
|
|
199
|
+
float post_hist[GX_NS_TALK_WIN]; /* post-NS block levels, ring */
|
|
200
|
+
int post_pos;
|
|
201
|
+
float in_hist[3]; /* the last input block levels (the NS output lags them) */
|
|
202
|
+
float exp_lvl_db, exp_prev_lin; /* pause expander: output level EMA, last gain */
|
|
203
|
+
int exp_quiet; /* blocks since the last talk block (the expander waits for a real pause) */
|
|
204
|
+
int pnf_have; /* blocks with signal seen, up to GX_NS_TALK_WARM */
|
|
205
|
+
int talk_now; /* this / the last block was talk */
|
|
206
|
+
int talk_hang; /* blocks of hangover left */
|
|
207
|
+
/* Sustained talk (singer report 2026-11-20): a held sung note (or a legato
|
|
208
|
+
* phrase) never rises 15 dB over its own 1 s minimum, so it used to stop
|
|
209
|
+
* counting as talk after ~1 s and the pause expander cut it by 15 dB. Talk
|
|
210
|
+
* that is going on stays talk while it holds near the level it started at
|
|
211
|
+
* and well over the level of the last real pause. */
|
|
212
|
+
float talk_lvl_db; /* post-NS level of talk onsets (EMA over rise-detected talk blocks) */
|
|
213
|
+
float quiet_db; /* post-NS level of the last pause (EMA over pause blocks) */
|
|
214
|
+
float in_quiet_db; /* input level of the last pause (an NS that erased loud input is no pause) */
|
|
215
|
+
int quiet_have; /* a pause has been seen (quiet_db is valid) */
|
|
216
|
+
int sus_run; /* blocks of sustained talk since the last rise-detected one */
|
|
217
|
+
float lvl_jit_db; /* EMA of the block-to-block change of the post-NS level: a voice
|
|
218
|
+
* (shimmer, vibrato) moves, a mains buzz / a fan's hum is flat */
|
|
219
|
+
float prev_post_db;
|
|
220
|
+
float pg_applied_db; /* pre-gain actually applied to the last block */
|
|
221
|
+
float gate;
|
|
222
|
+
float lim_env, lim_rel, lim_ceil;
|
|
223
|
+
|
|
224
|
+
/* pre-gain (before NS) */
|
|
225
|
+
int pg_have;
|
|
226
|
+
float pg_level_db, pg_gain_db, pg_prev_lin;
|
|
227
|
+
|
|
228
|
+
/* block buffers */
|
|
229
|
+
float blk[GX_NS_BLOCK], b_rnn[GX_NS_BLOCK], b_ext[GX_NS_BLOCK], b_tmp[GX_NS_BLOCK];
|
|
230
|
+
float s_dry[GX_NS_BLOCK], s_rnn[GX_NS_BLOCK], s_ext[GX_NS_BLOCK];
|
|
231
|
+
|
|
232
|
+
/* auto music (2026-11-20): detector on the input block before the pre-gain */
|
|
233
|
+
_Atomic int req_auto_music;
|
|
234
|
+
_Atomic uint32_t req_mu_reset;
|
|
235
|
+
uint32_t mu_reset_seen;
|
|
236
|
+
_Atomic float req_makeup_db;
|
|
237
|
+
float mk_lin, mk_db; /* bypass makeup gain applied to the last block */
|
|
238
|
+
/* singer level normaliser (0.11.4), audio thread; requests below */
|
|
239
|
+
_Atomic float req_sl_target, req_sl_max, req_sl_rise;
|
|
240
|
+
float sl_level_db, sl_gain_db;
|
|
241
|
+
int sl_have, sl_loud; /* sl_loud: consecutive blocks 10 dB over the estimate */
|
|
242
|
+
int sl_quiet; /* 0.11.5: blocks in a row with nothing over the floor (pause guard) */
|
|
243
|
+
int sl_act; /* 0.11.5: blocks in a row with signal */
|
|
244
|
+
float sl_duck_db; /* 0.11.5: part of sl_gain_db taken off in a pause */
|
|
245
|
+
/* level meters (0.11.4): ~1 s windows, published as dBFS */
|
|
246
|
+
double lm_pre_acc, lm_post_acc;
|
|
247
|
+
float lm_pre_pk, lm_post_pk;
|
|
248
|
+
int lm_n;
|
|
249
|
+
_Atomic float pub_pre_rms, pub_pre_pk, pub_post_rms, pub_post_pk, pub_mk_db, pub_sl_gain, pub_sl_level;
|
|
250
|
+
atomic_int pub_music;
|
|
251
|
+
_Atomic float pub_mu_dbg[7];
|
|
252
|
+
float mu_buf[GX_MU_N]; /* the last GX_MU_N input samples */
|
|
253
|
+
float mu_hann[GX_MU_N], mu_re[GX_MU_N], mu_im[GX_MU_N];
|
|
254
|
+
float mu_twr[GX_MU_N / 2], mu_twi[GX_MU_N / 2];
|
|
255
|
+
uint16_t mu_rev[GX_MU_N];
|
|
256
|
+
float mu_db[GX_MU_K1 + 4]; /* power spectrum (dB) up to the last analysed bin + 3 */
|
|
257
|
+
uint8_t mu_cnt[GX_MU_K1 + 1]; /* blocks a spectral peak has held (+-1 bin) */
|
|
258
|
+
uint8_t mu_cnt2[GX_MU_K1 + 1];
|
|
259
|
+
uint8_t mu_fl[GX_MU_WIN]; /* per block: bit0 dip, bit1 tonal */
|
|
260
|
+
uint8_t mu_on[GX_MU_WIN]; /* per block: note onsets */
|
|
261
|
+
int mu_pos, mu_fill, mu_sdip, mu_ston, mu_sons;
|
|
262
|
+
float mu_lv[GX_MU_PEAKWIN]; /* block levels for the dip test */
|
|
263
|
+
int mu_lpos;
|
|
264
|
+
int mu_state, mu_run; /* detected; blocks the enter / leave test has held */
|
|
265
|
+
float mu_m; /* relax amount, ramps 0..1 over GX_MU_RAMP blocks */
|
|
266
|
+
float mu_lvl_db; /* music level (EMA of block levels while detected) */
|
|
267
|
+
int mu_have_lvl;
|
|
268
|
+
|
|
269
|
+
gx_timer timer;
|
|
270
|
+
_Atomic uint32_t underflows, ext_errors, switches;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
typedef struct gx_ns gx_ns;
|
|
274
|
+
|
|
275
|
+
static float db_to_lin(float db) { return powf(10.f, db / 20.f); }
|
|
276
|
+
|
|
277
|
+
static float block_rms_db(const float *x, int n) {
|
|
278
|
+
double acc = 0;
|
|
279
|
+
int i;
|
|
280
|
+
for (i = 0; i < n; i++) acc += (double)x[i] * x[i];
|
|
281
|
+
acc /= n;
|
|
282
|
+
if (acc < 1e-3) return -120.f;
|
|
283
|
+
return (float)(10.0 * log10(acc / (32768.0 * 32768.0)));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
void gx_ns_default_config(gx_ns_config *c) {
|
|
287
|
+
memset(c, 0, sizeof(*c));
|
|
288
|
+
c->max_frame_samples = 2048;
|
|
289
|
+
c->crossfade_ms = 20;
|
|
290
|
+
c->resampler_quality = 5;
|
|
291
|
+
c->initial_engine = GX_NS_ENGINE_RNNOISE;
|
|
292
|
+
c->agc_enabled = 1;
|
|
293
|
+
c->agc_order = GX_NS_DEFAULT_AGC_ORDER;
|
|
294
|
+
c->agc_target_dbfs = (float)GX_NS_DEFAULT_AGC_TARGET_DBFS;
|
|
295
|
+
c->agc_max_gain_db = (float)GX_NS_DEFAULT_AGC_MAX_GAIN_DB;
|
|
296
|
+
c->agc_min_gain_db = -10.f;
|
|
297
|
+
c->limiter_dbfs = -1.f;
|
|
298
|
+
c->watchdog_us = 8000;
|
|
299
|
+
c->max_external_latency = 960;
|
|
300
|
+
c->ext_atten_lim_db = (float)GX_NS_DEFAULT_ATTEN_LIM_DB;
|
|
301
|
+
c->ext_p95_budget_us = GX_NS_DEFAULT_EXT_P95_BUDGET_US;
|
|
302
|
+
c->ext_p95_window = GX_NS_DEFAULT_EXT_P95_WINDOW;
|
|
303
|
+
c->pregain_enabled = 1;
|
|
304
|
+
c->pregain_target_dbfs = (float)GX_NS_DEFAULT_PREGAIN_TARGET_DBFS;
|
|
305
|
+
c->pregain_max_db = (float)GX_NS_DEFAULT_PREGAIN_MAX_DB;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
float gx_ns_pregain_for_level(float speech_dbfs) {
|
|
309
|
+
float g = (float)GX_NS_DEFAULT_PREGAIN_TARGET_DBFS - speech_dbfs;
|
|
310
|
+
if (g > (float)GX_NS_DEFAULT_PREGAIN_MAX_DB) g = (float)GX_NS_DEFAULT_PREGAIN_MAX_DB;
|
|
311
|
+
return g < 0.f ? 0.f : g;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const char *gx_ns_engine_name(gx_ns_engine e) {
|
|
315
|
+
switch (e) {
|
|
316
|
+
case GX_NS_ENGINE_RNNOISE: return "rnnoise";
|
|
317
|
+
case GX_NS_ENGINE_DFN3: return "dfn3";
|
|
318
|
+
default: return "off";
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const char *gx_ns_reason_name(gx_ns_reason r) {
|
|
323
|
+
switch (r) {
|
|
324
|
+
case GX_NS_REASON_BYPASS: return "music_bypass";
|
|
325
|
+
case GX_NS_REASON_NO_EXTERNAL: return "studio_unavailable";
|
|
326
|
+
case GX_NS_REASON_EXT_ERROR: return "studio_error";
|
|
327
|
+
case GX_NS_REASON_DELAY_PENDING: return "delay_pending";
|
|
328
|
+
case GX_NS_REASON_EXT_SLOW: return "studio_slow";
|
|
329
|
+
case GX_NS_REASON_EXT_SUPPRESS: return "studio_suppress";
|
|
330
|
+
default: return "ok";
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static gx_ns_engine src_engine(int src) {
|
|
335
|
+
return src == SRC_RNN ? GX_NS_ENGINE_RNNOISE : src == SRC_EXT ? GX_NS_ENGINE_DFN3 : GX_NS_ENGINE_OFF;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
void gx_ns_destroy(gx_ns *st) {
|
|
339
|
+
int i;
|
|
340
|
+
if (!st) return;
|
|
341
|
+
for (i = 0; i < NB_RATES; i++) {
|
|
342
|
+
if (st->rates[i].up) gx_resampler_destroy(st->rates[i].up);
|
|
343
|
+
if (st->rates[i].down) gx_resampler_destroy(st->rates[i].down);
|
|
344
|
+
}
|
|
345
|
+
if (st->rnn) rnnoise_destroy(st->rnn);
|
|
346
|
+
free(st->in48.buf);
|
|
347
|
+
free(st->out_r.buf);
|
|
348
|
+
free(st->scratch_in48);
|
|
349
|
+
free(st->scratch_out_r);
|
|
350
|
+
free(st->dl_dry.buf);
|
|
351
|
+
free(st->dl_rnn.buf);
|
|
352
|
+
free(st->dl_ext.buf);
|
|
353
|
+
free(st);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
/* ---------- auto music detector (2026-11-20) ----------
|
|
358
|
+
* Music played into the mic (field case: a guest's song from a speaker)
|
|
359
|
+
* looked like noise to the chain: RNNoise erased the gaps between notes, the
|
|
360
|
+
* pause gate dropped the boosts and the expander cut the rest (dips of
|
|
361
|
+
* 15-25 dB every few seconds). Speech pauses between words, noise is
|
|
362
|
+
* stationary; music is continuous AND holds tonal peaks that change (notes).
|
|
363
|
+
* Per block on the input (before the pre-gain, so relaxing the chain never
|
|
364
|
+
* changes what the detector sees):
|
|
365
|
+
* dip : the level falls GX_MU_DIP_DB under the max of the last 200 ms
|
|
366
|
+
* tonal : >= 2 spectral peaks (190 Hz - 5.2 kHz) held >= 150 ms
|
|
367
|
+
* onsets : peaks that just reached 150 ms (a new note)
|
|
368
|
+
* Over 3 s: enter when dips are rare, most blocks tonal and notes keep
|
|
369
|
+
* starting; leave when that fails for 3 s (looser thresholds). */
|
|
370
|
+
static void mu_reset(gx_ns *st) {
|
|
371
|
+
int i;
|
|
372
|
+
for (i = 0; i < GX_MU_PEAKWIN; i++) st->mu_lv[i] = -200.f; /* no level yet: no dips */
|
|
373
|
+
memset(st->mu_cnt, 0, sizeof(st->mu_cnt));
|
|
374
|
+
memset(st->mu_fl, 0, sizeof(st->mu_fl));
|
|
375
|
+
memset(st->mu_on, 0, sizeof(st->mu_on));
|
|
376
|
+
st->mu_pos = st->mu_fill = st->mu_sdip = st->mu_ston = st->mu_sons = 0;
|
|
377
|
+
st->mu_state = st->mu_run = 0;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
static void mu_init(gx_ns *st) {
|
|
381
|
+
int i, j, bits = 0;
|
|
382
|
+
while ((1 << bits) < GX_MU_N) bits++;
|
|
383
|
+
for (i = 0; i < GX_MU_N; i++) {
|
|
384
|
+
int r = 0;
|
|
385
|
+
for (j = 0; j < bits; j++) if (i & (1 << j)) r |= 1 << (bits - 1 - j);
|
|
386
|
+
st->mu_rev[i] = (uint16_t)r;
|
|
387
|
+
st->mu_hann[i] = (float)(0.5 - 0.5 * cos(2.0 * GX_PI * i / GX_MU_N));
|
|
388
|
+
}
|
|
389
|
+
mu_reset(st);
|
|
390
|
+
for (i = 0; i < GX_MU_N / 2; i++) {
|
|
391
|
+
st->mu_twr[i] = (float)cos(-2.0 * GX_PI * i / GX_MU_N);
|
|
392
|
+
st->mu_twi[i] = (float)sin(-2.0 * GX_PI * i / GX_MU_N);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
/* in-place radix-2 complex FFT of mu_re/mu_im (GX_MU_N points) */
|
|
398
|
+
static void mu_fft(gx_ns *st) {
|
|
399
|
+
int i, len, k;
|
|
400
|
+
float *re = st->mu_re, *im = st->mu_im;
|
|
401
|
+
for (i = 0; i < GX_MU_N; i++) {
|
|
402
|
+
int r = st->mu_rev[i];
|
|
403
|
+
if (r > i) { float t = re[i]; re[i] = re[r]; re[r] = t; t = im[i]; im[i] = im[r]; im[r] = t; }
|
|
404
|
+
}
|
|
405
|
+
for (len = 2; len <= GX_MU_N; len <<= 1) {
|
|
406
|
+
int half = len >> 1, step = GX_MU_N / len;
|
|
407
|
+
for (i = 0; i < GX_MU_N; i += len) {
|
|
408
|
+
for (k = 0; k < half; k++) {
|
|
409
|
+
float wr = st->mu_twr[k * step], wi = st->mu_twi[k * step];
|
|
410
|
+
int a = i + k, b = a + half;
|
|
411
|
+
float xr = re[b] * wr - im[b] * wi, xi = re[b] * wi + im[b] * wr;
|
|
412
|
+
re[b] = re[a] - xr; im[b] = im[a] - xi;
|
|
413
|
+
re[a] += xr; im[a] += xi;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/* One input block (48 kHz, before the pre-gain); updates mu_state. */
|
|
420
|
+
static void mu_block(gx_ns *st, const float *x, float lvl_db) {
|
|
421
|
+
int i, k, stable = 0, onsets = 0, dip, tonal, p;
|
|
422
|
+
float mx = -200.f, fmax = -200.f;
|
|
423
|
+
memmove(st->mu_buf, st->mu_buf + GX_NS_BLOCK, sizeof(float) * (GX_MU_N - GX_NS_BLOCK));
|
|
424
|
+
memcpy(st->mu_buf + GX_MU_N - GX_NS_BLOCK, x, sizeof(float) * GX_NS_BLOCK);
|
|
425
|
+
for (i = 0; i < GX_MU_N; i++) { st->mu_re[i] = st->mu_buf[i] * st->mu_hann[i]; st->mu_im[i] = 0.f; }
|
|
426
|
+
mu_fft(st);
|
|
427
|
+
for (k = 0; k <= GX_MU_K1 + 3; k++) {
|
|
428
|
+
float pw = st->mu_re[k] * st->mu_re[k] + st->mu_im[k] * st->mu_im[k];
|
|
429
|
+
st->mu_db[k] = 10.f * log10f(pw + 1e-3f);
|
|
430
|
+
if (k >= GX_MU_K0 && st->mu_db[k] > fmax) fmax = st->mu_db[k];
|
|
431
|
+
}
|
|
432
|
+
/* held peaks: a bin that is a prominent local max carries the longest hold
|
|
433
|
+
* of itself and its neighbours (a note may wander by a bin: vibrato) */
|
|
434
|
+
for (k = GX_MU_K0; k <= GX_MU_K1; k++) {
|
|
435
|
+
float d = st->mu_db[k], nb = 0.f;
|
|
436
|
+
int peak = 0;
|
|
437
|
+
if (k >= GX_MU_K0 && d > st->mu_db[k - 1] && d >= st->mu_db[k + 1] && d > fmax - 50.f && lvl_db > -65.f) {
|
|
438
|
+
for (i = 2; i <= 3; i++) nb += st->mu_db[k - i] + st->mu_db[k + i];
|
|
439
|
+
peak = d - nb * 0.25f >= GX_MU_PROM_DB;
|
|
440
|
+
}
|
|
441
|
+
if (peak) {
|
|
442
|
+
/* the same bin grows the hold; a neighbour's hold is carried without
|
|
443
|
+
* growing (vibrato wanders back and forth, a gliding voice keeps moving) */
|
|
444
|
+
int own = st->mu_cnt[k] ? st->mu_cnt[k] + 1 : 1, c = own;
|
|
445
|
+
if (k > GX_MU_K0 && st->mu_cnt[k - 1] > c) c = st->mu_cnt[k - 1];
|
|
446
|
+
if (k < GX_MU_K1 && st->mu_cnt[k + 1] > c) c = st->mu_cnt[k + 1];
|
|
447
|
+
if (c > 250) c = 250;
|
|
448
|
+
st->mu_cnt2[k] = (uint8_t)c;
|
|
449
|
+
if (c >= GX_MU_HOLD) stable++;
|
|
450
|
+
if (own == GX_MU_HOLD && c == own) onsets++; /* a note reached the hold in its own bin (not a hop) */
|
|
451
|
+
} else {
|
|
452
|
+
st->mu_cnt2[k] = 0;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
memcpy(st->mu_cnt + GX_MU_K0, st->mu_cnt2 + GX_MU_K0, (size_t)(GX_MU_K1 - GX_MU_K0 + 1));
|
|
456
|
+
/* dip: under the max of the last 200 ms */
|
|
457
|
+
for (i = 0; i < GX_MU_PEAKWIN; i++) if (st->mu_lv[i] > mx) mx = st->mu_lv[i];
|
|
458
|
+
st->mu_lv[st->mu_lpos] = lvl_db;
|
|
459
|
+
st->mu_lpos = (st->mu_lpos + 1) % GX_MU_PEAKWIN;
|
|
460
|
+
dip = lvl_db < mx - GX_MU_DIP_DB || lvl_db < -65.f;
|
|
461
|
+
tonal = stable >= 2;
|
|
462
|
+
/* 3 s window sums */
|
|
463
|
+
p = st->mu_pos;
|
|
464
|
+
if (st->mu_fill == GX_MU_WIN) {
|
|
465
|
+
st->mu_sdip -= st->mu_fl[p] & 1;
|
|
466
|
+
st->mu_ston -= (st->mu_fl[p] >> 1) & 1;
|
|
467
|
+
st->mu_sons -= st->mu_on[p];
|
|
468
|
+
} else {
|
|
469
|
+
st->mu_fill++;
|
|
470
|
+
}
|
|
471
|
+
st->mu_fl[p] = (uint8_t)(dip | (tonal << 1));
|
|
472
|
+
st->mu_on[p] = (uint8_t)(onsets > 255 ? 255 : onsets);
|
|
473
|
+
st->mu_sdip += dip; st->mu_ston += tonal; st->mu_sons += st->mu_on[p];
|
|
474
|
+
st->mu_pos = (p + 1) % GX_MU_WIN;
|
|
475
|
+
if (st->mu_fill == GX_MU_WIN) {
|
|
476
|
+
float fd = (float)st->mu_sdip / GX_MU_WIN, ft = (float)st->mu_ston / GX_MU_WIN;
|
|
477
|
+
if (!st->mu_state) {
|
|
478
|
+
int ok = fd <= GX_MU_ENTER_DIP && ft >= GX_MU_ENTER_TONAL && st->mu_sons >= GX_MU_ENTER_ONSETS;
|
|
479
|
+
st->mu_run = ok ? st->mu_run + 1 : 0;
|
|
480
|
+
if (st->mu_run >= GX_MU_ENTER_RUN) { st->mu_state = 1; st->mu_run = 0; }
|
|
481
|
+
} else {
|
|
482
|
+
int ok = fd <= GX_MU_STAY_DIP && ft >= GX_MU_STAY_TONAL && st->mu_sons >= GX_MU_STAY_ONSETS;
|
|
483
|
+
st->mu_run = ok ? 0 : st->mu_run + 1;
|
|
484
|
+
if (st->mu_run >= GX_MU_LEAVE_RUN) { st->mu_state = 0; st->mu_run = 0; }
|
|
485
|
+
}
|
|
486
|
+
atomic_store_explicit(&st->pub_mu_dbg[0], fd, memory_order_relaxed);
|
|
487
|
+
atomic_store_explicit(&st->pub_mu_dbg[1], ft, memory_order_relaxed);
|
|
488
|
+
atomic_store_explicit(&st->pub_mu_dbg[2], (float)st->mu_sons, memory_order_relaxed);
|
|
489
|
+
}
|
|
490
|
+
atomic_store_explicit(&st->pub_mu_dbg[3], (float)stable, memory_order_relaxed);
|
|
491
|
+
atomic_store_explicit(&st->pub_mu_dbg[4], (float)st->mu_state, memory_order_relaxed);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
gx_ns *gx_ns_create(const gx_ns_config *cfg) {
|
|
495
|
+
gx_ns *st;
|
|
496
|
+
int i, err = 0, maxf, dl_cap;
|
|
497
|
+
st = (gx_ns *)calloc(1, sizeof(*st));
|
|
498
|
+
if (!st) return NULL;
|
|
499
|
+
if (cfg) st->cfg = *cfg; else gx_ns_default_config(&st->cfg);
|
|
500
|
+
if (st->cfg.max_frame_samples < 64) st->cfg.max_frame_samples = 64;
|
|
501
|
+
if (st->cfg.crossfade_ms < 1) st->cfg.crossfade_ms = 1;
|
|
502
|
+
if (st->cfg.max_external_latency < GX_NS_RNNOISE_LATENCY) st->cfg.max_external_latency = GX_NS_RNNOISE_LATENCY;
|
|
503
|
+
maxf = st->cfg.max_frame_samples;
|
|
504
|
+
|
|
505
|
+
for (i = 0; i < NB_RATES; i++) {
|
|
506
|
+
rate_ctx *r = &st->rates[i];
|
|
507
|
+
r->rate = k_rates[i];
|
|
508
|
+
r->block = k_rates[i] / 100;
|
|
509
|
+
if (r->rate != GX_NS_ENGINE_RATE) {
|
|
510
|
+
r->up = gx_resampler_init(1, (spx_uint32_t)r->rate, GX_NS_ENGINE_RATE, st->cfg.resampler_quality, &err);
|
|
511
|
+
r->down = gx_resampler_init(1, GX_NS_ENGINE_RATE, (spx_uint32_t)r->rate, st->cfg.resampler_quality, &err);
|
|
512
|
+
if (!r->up || !r->down) goto fail;
|
|
513
|
+
gx_resampler_reset_mem(r->up);
|
|
514
|
+
gx_resampler_reset_mem(r->down);
|
|
515
|
+
r->up_lat = gx_resampler_get_input_latency(r->up);
|
|
516
|
+
r->down_lat = gx_resampler_get_output_latency(r->down);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/* 48 kHz side: one call can bring maxf * 6 samples (8 kHz input) + a block. */
|
|
520
|
+
st->scratch48_cap = maxf * 6 + 2 * GX_NS_BLOCK + 64;
|
|
521
|
+
st->scratch_r_cap = maxf + 2 * GX_NS_BLOCK + 64;
|
|
522
|
+
if (fifo_alloc(&st->in48, st->scratch48_cap) || fifo_alloc(&st->out_r, 2 * st->scratch_r_cap)) goto fail;
|
|
523
|
+
st->scratch_in48 = (float *)calloc((size_t)st->scratch48_cap, sizeof(float));
|
|
524
|
+
st->scratch_out_r = (float *)calloc((size_t)st->scratch_r_cap, sizeof(float));
|
|
525
|
+
if (!st->scratch_in48 || !st->scratch_out_r) goto fail;
|
|
526
|
+
dl_cap = st->cfg.max_external_latency + 3 * GX_NS_BLOCK; /* + RNNoise priming history */
|
|
527
|
+
if (dl_alloc(&st->dl_dry, dl_cap) || dl_alloc(&st->dl_rnn, dl_cap) || dl_alloc(&st->dl_ext, dl_cap)) goto fail;
|
|
528
|
+
|
|
529
|
+
st->rnn = rnnoise_create(NULL);
|
|
530
|
+
if (!st->rnn) goto fail;
|
|
531
|
+
/* Warm-up: RNNoise allocates its FFT tables lazily on the first frame. */
|
|
532
|
+
memset(st->blk, 0, sizeof(st->blk));
|
|
533
|
+
rnnoise_process_frame(st->rnn, st->b_tmp, st->blk);
|
|
534
|
+
rnnoise_reset(st->rnn);
|
|
535
|
+
(void)gx_timer_now_ns(); /* Apple: fills the timebase outside the audio thread */
|
|
536
|
+
|
|
537
|
+
mu_init(st);
|
|
538
|
+
atomic_init(&st->req_auto_music, st->cfg.auto_music ? 1 : 0);
|
|
539
|
+
atomic_init(&st->pub_music, 0);
|
|
540
|
+
atomic_init(&st->req_mu_reset, 0);
|
|
541
|
+
atomic_init(&st->req_makeup_db, 0.f);
|
|
542
|
+
atomic_init(&st->req_sl_target, (float)GX_NS_SINGER_TARGET_DBFS);
|
|
543
|
+
atomic_init(&st->req_sl_max, 0.f);
|
|
544
|
+
atomic_init(&st->req_sl_rise, (float)GX_NS_SINGER_RISE_DB_S / 100.f);
|
|
545
|
+
st->sl_level_db = -120.f;
|
|
546
|
+
atomic_init(&st->pub_pre_rms, -120.f); atomic_init(&st->pub_pre_pk, -120.f);
|
|
547
|
+
atomic_init(&st->pub_post_rms, -120.f); atomic_init(&st->pub_post_pk, -120.f);
|
|
548
|
+
atomic_init(&st->pub_mk_db, 0.f); atomic_init(&st->pub_sl_gain, 0.f); atomic_init(&st->pub_sl_level, -120.f);
|
|
549
|
+
st->mk_lin = 1.f;
|
|
550
|
+
for (i = 0; i < 7; i++) atomic_init(&st->pub_mu_dbg[i], 0.f);
|
|
551
|
+
|
|
552
|
+
gx_timer_init(&st->timer, (uint32_t)st->cfg.watchdog_us);
|
|
553
|
+
atomic_init(&st->req_engine, (int)st->cfg.initial_engine);
|
|
554
|
+
atomic_init(&st->req_bypass, 0);
|
|
555
|
+
atomic_init(&st->req_agc, st->cfg.agc_enabled ? 1 : 0);
|
|
556
|
+
atomic_init(&st->req_agc_order, (int)st->cfg.agc_order);
|
|
557
|
+
atomic_init(&st->req_reset, 0);
|
|
558
|
+
atomic_init(&st->req_stats_reset, 0);
|
|
559
|
+
atomic_init(&st->ext_ptr, NULL);
|
|
560
|
+
atomic_init(&st->ext_gen, 0);
|
|
561
|
+
atomic_init(&st->blocks, 0);
|
|
562
|
+
atomic_init(&st->underflows, 0);
|
|
563
|
+
atomic_init(&st->ext_errors, 0);
|
|
564
|
+
atomic_init(&st->switches, 0);
|
|
565
|
+
atomic_init(&st->pub_rate, 0);
|
|
566
|
+
atomic_init(&st->pub_latency, 0);
|
|
567
|
+
atomic_init(&st->pub_vad, 0.f);
|
|
568
|
+
atomic_init(&st->pub_gain_db, 0.f);
|
|
569
|
+
atomic_init(&st->pub_pregain_db, 0.f);
|
|
570
|
+
atomic_init(&st->pub_pg_applied_db, 0.f);
|
|
571
|
+
atomic_init(&st->pub_agc_applied_db, 0.f);
|
|
572
|
+
atomic_init(&st->pub_pause_att_db, 0.f);
|
|
573
|
+
atomic_init(&st->pub_talk, 0);
|
|
574
|
+
atomic_init(&st->req_pregain, st->cfg.pregain_enabled ? 1 : 0);
|
|
575
|
+
atomic_init(&st->pub_ext_suppress, 0);
|
|
576
|
+
st->pg_prev_lin = 1.f;
|
|
577
|
+
|
|
578
|
+
st->xf_len = st->cfg.crossfade_ms * GX_NS_ENGINE_RATE / 1000;
|
|
579
|
+
st->L = GX_NS_RNNOISE_LATENCY;
|
|
580
|
+
st->cur = st->next = (st->cfg.initial_engine == GX_NS_ENGINE_OFF) ? SRC_DRY : SRC_RNN;
|
|
581
|
+
st->xf_pos = 0;
|
|
582
|
+
st->fade_in_pos = st->xf_len; /* first stream start does its own fade-in only on reset */
|
|
583
|
+
st->rc = NULL;
|
|
584
|
+
st->need_reprime = 1;
|
|
585
|
+
st->prev_gain_lin = 1.f;
|
|
586
|
+
st->exp_prev_lin = 1.f;
|
|
587
|
+
st->exp_lvl_db = -100.f;
|
|
588
|
+
st->nf_db = -60.f;
|
|
589
|
+
st->lim_ceil = 32768.f * db_to_lin(st->cfg.limiter_dbfs);
|
|
590
|
+
st->lim_rel = expf(-1.f / (0.05f * GX_NS_ENGINE_RATE));
|
|
591
|
+
atomic_init(&st->pub_active, st->cur);
|
|
592
|
+
atomic_init(&st->pub_xfade, 0);
|
|
593
|
+
atomic_init(&st->pub_reason, 0);
|
|
594
|
+
if (st->cfg.ext_p95_window < 20) st->cfg.ext_p95_window = 20;
|
|
595
|
+
if (st->cfg.ext_p95_window > GX_NS_EXT_P95_WINDOW_MAX) st->cfg.ext_p95_window = GX_NS_EXT_P95_WINDOW_MAX;
|
|
596
|
+
atomic_init(&st->req_atten_db, st->cfg.ext_atten_lim_db > 0.f ? st->cfg.ext_atten_lim_db : 0.f);
|
|
597
|
+
atomic_init(&st->req_p95_budget, st->cfg.ext_p95_budget_us > 0 ? st->cfg.ext_p95_budget_us : 0);
|
|
598
|
+
atomic_init(&st->pub_ext_p95, 0);
|
|
599
|
+
atomic_init(&st->pub_ext_slow, 0);
|
|
600
|
+
return st;
|
|
601
|
+
fail:
|
|
602
|
+
gx_ns_destroy(st);
|
|
603
|
+
return NULL;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/* ---------- control ---------- */
|
|
607
|
+
void gx_ns_set_engine(gx_ns *st, gx_ns_engine e) { atomic_store(&st->req_engine, (int)e); }
|
|
608
|
+
void gx_ns_set_bypass(gx_ns *st, int b) { atomic_store(&st->req_bypass, b ? 1 : 0); }
|
|
609
|
+
void gx_ns_set_agc(gx_ns *st, int on) { atomic_store(&st->req_agc, on ? 1 : 0); }
|
|
610
|
+
void gx_ns_set_agc_order(gx_ns *st, gx_ns_agc_order o) { atomic_store(&st->req_agc_order, (int)o); }
|
|
611
|
+
void gx_ns_request_reset(gx_ns *st) { atomic_fetch_add(&st->req_reset, 1); }
|
|
612
|
+
void gx_ns_set_atten_limit(gx_ns *st, float db) { atomic_store(&st->req_atten_db, db > 0.f ? db : 0.f); }
|
|
613
|
+
void gx_ns_set_ext_p95_budget(gx_ns *st, int us) { atomic_store(&st->req_p95_budget, us > 0 ? us : 0); }
|
|
614
|
+
void gx_ns_reset_stats(gx_ns *st) { atomic_fetch_add(&st->req_stats_reset, 1); }
|
|
615
|
+
void gx_ns_set_pregain(gx_ns *st, int on) { atomic_store(&st->req_pregain, on ? 1 : 0); }
|
|
616
|
+
void gx_ns_set_auto_music(gx_ns *st, int on) { atomic_store(&st->req_auto_music, on ? 1 : 0); }
|
|
617
|
+
void gx_ns_reset_music(gx_ns *st) { atomic_fetch_add(&st->req_mu_reset, 1); }
|
|
618
|
+
void gx_ns_set_bypass_makeup(gx_ns *st, float db) {
|
|
619
|
+
atomic_store(&st->req_makeup_db, db < 0.f ? 0.f : db > (float)GX_NS_MAKEUP_MAX_DB ? (float)GX_NS_MAKEUP_MAX_DB : db);
|
|
620
|
+
}
|
|
621
|
+
void gx_ns_set_singer_level(gx_ns *st, float target_dbfs, float max_db, float rise_db_per_s) {
|
|
622
|
+
if (!(target_dbfs <= 0.f) || target_dbfs < -60.f) target_dbfs = (float)GX_NS_SINGER_TARGET_DBFS;
|
|
623
|
+
if (!(max_db > 0.f)) max_db = 0.f;
|
|
624
|
+
if (max_db > (float)GX_NS_MAKEUP_MAX_DB) max_db = (float)GX_NS_MAKEUP_MAX_DB;
|
|
625
|
+
if (!(rise_db_per_s > 0.f)) rise_db_per_s = (float)GX_NS_SINGER_RISE_DB_S;
|
|
626
|
+
if (rise_db_per_s > 30.f) rise_db_per_s = 30.f;
|
|
627
|
+
atomic_store(&st->req_sl_target, target_dbfs);
|
|
628
|
+
atomic_store(&st->req_sl_rise, rise_db_per_s / 100.f); /* per 10 ms block */
|
|
629
|
+
atomic_store(&st->req_sl_max, max_db);
|
|
630
|
+
}
|
|
631
|
+
void gx_ns_get_music_debug(gx_ns *st, float out[7]) {
|
|
632
|
+
int i;
|
|
633
|
+
for (i = 0; i < 7; i++) out[i] = atomic_load_explicit(&st->pub_mu_dbg[i], memory_order_relaxed);
|
|
634
|
+
}
|
|
635
|
+
uint32_t gx_ns_blocks_processed(gx_ns *st) { return atomic_load(&st->blocks); }
|
|
636
|
+
|
|
637
|
+
int gx_ns_set_external_engine(gx_ns *st, const gx_ns_external_engine *eng) {
|
|
638
|
+
gx_ns_external_engine *slot = NULL;
|
|
639
|
+
if (eng) {
|
|
640
|
+
if (!eng->process || eng->latency_samples < 0 || eng->latency_samples > st->cfg.max_external_latency) return -1;
|
|
641
|
+
slot = &st->ext_slots[st->ext_slot_next];
|
|
642
|
+
st->ext_slot_next ^= 1;
|
|
643
|
+
*slot = *eng;
|
|
644
|
+
}
|
|
645
|
+
atomic_store(&st->ext_ptr, slot);
|
|
646
|
+
atomic_fetch_add(&st->ext_gen, 1);
|
|
647
|
+
return 0;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
static rate_ctx *find_rate(gx_ns *st, int rate) {
|
|
651
|
+
int i;
|
|
652
|
+
for (i = 0; i < NB_RATES; i++)
|
|
653
|
+
if (st->rates[i].rate == rate) return &st->rates[i];
|
|
654
|
+
return NULL;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
static int latency_for(gx_ns *st, const rate_ctx *r, int prefill) {
|
|
658
|
+
if (r->rate == GX_NS_ENGINE_RATE) return prefill + st->L;
|
|
659
|
+
return prefill + r->up_lat + r->down_lat + (int)((long)st->L * r->rate / GX_NS_ENGINE_RATE);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
int gx_ns_latency_samples(gx_ns *st, int sample_rate) {
|
|
663
|
+
rate_ctx *r = find_rate(st, sample_rate);
|
|
664
|
+
if (!r) return -1;
|
|
665
|
+
if (st->rc == r && !st->need_reprime) return latency_for(st, r, st->prefill);
|
|
666
|
+
return latency_for(st, r, r->rate == GX_NS_ENGINE_RATE ? 0 : r->block + RESAMPLE_SLACK);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/* ---------- stream reset (no allocation) ---------- */
|
|
670
|
+
static void stream_reset(gx_ns *st, rate_ctx *r, int n, const gx_ns_external_engine *ext) {
|
|
671
|
+
st->rc = r;
|
|
672
|
+
atomic_fetch_add_explicit(&st->req_mu_reset, 1, memory_order_relaxed); /* auto music starts over */
|
|
673
|
+
fifo_clear(&st->in48);
|
|
674
|
+
fifo_clear(&st->out_r);
|
|
675
|
+
if (r->up) {
|
|
676
|
+
gx_resampler_reset_mem(r->up);
|
|
677
|
+
gx_resampler_reset_mem(r->down);
|
|
678
|
+
st->prefill = r->block + RESAMPLE_SLACK;
|
|
679
|
+
} else {
|
|
680
|
+
st->prefill = (n % GX_NS_BLOCK == 0) ? 0 : GX_NS_BLOCK;
|
|
681
|
+
}
|
|
682
|
+
fifo_push(&st->out_r, NULL, st->prefill);
|
|
683
|
+
dl_clear(&st->dl_dry);
|
|
684
|
+
dl_clear(&st->dl_rnn);
|
|
685
|
+
dl_clear(&st->dl_ext);
|
|
686
|
+
rnnoise_reset(st->rnn);
|
|
687
|
+
if (ext && ext->reset) ext->reset(ext->user);
|
|
688
|
+
if (st->cur != st->next) { st->cur = st->next; st->xf_pos = 0; }
|
|
689
|
+
st->fade_in_pos = 0; /* buffered audio was dropped: 20 ms fade-in from silence */
|
|
690
|
+
st->lim_env = 0.f;
|
|
691
|
+
st->need_reprime = 0;
|
|
692
|
+
atomic_store_explicit(&st->pub_rate, r->rate, memory_order_relaxed);
|
|
693
|
+
atomic_store_explicit(&st->pub_latency, latency_for(st, r, st->prefill), memory_order_relaxed);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/* ---------- AGC ---------- */
|
|
697
|
+
#define GX_NS_TALK_OVER_DB 15.f /* post-NS level over its 1 s minimum that counts as talk */
|
|
698
|
+
#define GX_NS_TALK_MIN_DB (-80.f) /* ... and over this absolute level (gain taken out; as low as the pre-gain
|
|
699
|
+
* floor: a -66 dBFS phone mic must still count as talk, review 2026-10-21) */
|
|
700
|
+
#define GX_NS_TALK_KEEP_DB 8.f /* ... and the NS kept most of it (noise loses more) */
|
|
701
|
+
#ifndef GX_NS_TALK_RECENT
|
|
702
|
+
#define GX_NS_TALK_RECENT 60 /* within 600 ms of talk, a talk test passes with the lower VAD */
|
|
703
|
+
#endif
|
|
704
|
+
#ifndef GX_NS_TALK_RISE_DB
|
|
705
|
+
#define GX_NS_TALK_RISE_DB 20.f /* a rise this far over the input floor: a word start, the lower VAD will do
|
|
706
|
+
* (RNNoise's VAD lags at word starts; device 2026-10-21: 6-12 dB onset dips) */
|
|
707
|
+
#endif
|
|
708
|
+
#ifndef GX_NS_TALK_VAD_HOLD
|
|
709
|
+
#define GX_NS_TALK_VAD_HOLD 0.5f /* ... (hysteresis: talk going on needs less) */
|
|
710
|
+
#endif
|
|
711
|
+
#ifndef GX_NS_TALK_VAD
|
|
712
|
+
#define GX_NS_TALK_VAD 0.9f /* ... and RNNoise is sure it is voice (2026-10-21 fan room:
|
|
713
|
+
* sounds RNNoise is unsure of opened the gate 43% of the time) */
|
|
714
|
+
#endif
|
|
715
|
+
#ifndef GX_NS_SUS_OVER_DB
|
|
716
|
+
#define GX_NS_SUS_OVER_DB 10.f /* sustained talk: this far over the last pause ... */
|
|
717
|
+
#endif
|
|
718
|
+
#ifndef GX_NS_SUS_DROP_DB
|
|
719
|
+
#define GX_NS_SUS_DROP_DB 15.f /* ... and within this of the onset level ... */
|
|
720
|
+
#endif
|
|
721
|
+
#ifndef GX_NS_SUS_JITTER_DB
|
|
722
|
+
#define GX_NS_SUS_JITTER_DB 0.1f /* ... and its level moves (mean block-to-block change; a held
|
|
723
|
+
* vowel: 0.3-1 dB, a 100 Hz buzz: ~0.02 dB, test_sustain) ... */
|
|
724
|
+
#endif
|
|
725
|
+
#ifndef GX_NS_SUS_MAX
|
|
726
|
+
#define GX_NS_SUS_MAX 1500 /* ... for at most 15 s without a new onset (a steady noise that starts
|
|
727
|
+
* while someone talks cannot hold the gate open for longer) */
|
|
728
|
+
#endif
|
|
729
|
+
#define GX_NS_PAUSE_TARGET_DB (-56.f) /* pause expander: what the NS leaves of loud noise ... */
|
|
730
|
+
#define GX_NS_PAUSE_MAX_ATT_DB 15.f /* ... is pulled down to here, by at most this */
|
|
731
|
+
#ifndef GX_NS_EXP_RISE
|
|
732
|
+
#define GX_NS_EXP_RISE 0.03f /* ... its level estimate rises slowly (~0.3 s), falls fast */
|
|
733
|
+
#endif
|
|
734
|
+
#ifndef GX_NS_EXP_WAIT
|
|
735
|
+
#define GX_NS_EXP_WAIT 40 /* ... after this many blocks without talk (400 ms), over 100 ms */
|
|
736
|
+
#endif
|
|
737
|
+
#define GX_NS_PAUSE_HANG 10 /* 100 ms of no talk before the gate closes ... */
|
|
738
|
+
#define GX_NS_PAUSE_RAMP 0.1f /* ... then 100 ms to unity (a word's next syllable reopens it at once) */
|
|
739
|
+
|
|
740
|
+
/* A boost scaled by the pause gate; an attenuation always applies. */
|
|
741
|
+
static float gated_db(const gx_ns *st, float db) { return db > 0.f ? db * st->gate : db; }
|
|
742
|
+
|
|
743
|
+
/* Auto music: a boost held open (not gated) but capped at `cap`, blended in by
|
|
744
|
+
* the relax amount. mu_m == 0 is exactly gated_db. */
|
|
745
|
+
static float music_db(const gx_ns *st, float db, float cap) {
|
|
746
|
+
float g = gated_db(st, db), h;
|
|
747
|
+
if (st->mu_m <= 0.f) return g;
|
|
748
|
+
h = db > 0.f ? (db < cap ? db : cap) : db;
|
|
749
|
+
return g + st->mu_m * (h - g);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/* Auto music: the boost room left before the music passes GX_NS_MUSIC_MAX_DBFS. */
|
|
753
|
+
static float music_cap(const gx_ns *st, float already_db) {
|
|
754
|
+
float c = (float)GX_NS_MUSIC_MAX_DBFS - (st->mu_lvl_db + already_db);
|
|
755
|
+
return c > 0.f ? c : 0.f;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
static float energy_vad(gx_ns *st, float rms_db) {
|
|
759
|
+
if (!st->nf_have) {
|
|
760
|
+
if (rms_db < -90.f) return 0.f; /* digital silence (a mic starting up) is no floor */
|
|
761
|
+
st->nf_db = rms_db; st->nf_have = 1;
|
|
762
|
+
}
|
|
763
|
+
if (rms_db < st->nf_db) st->nf_db = 0.5f * st->nf_db + 0.5f * rms_db;
|
|
764
|
+
else st->nf_db += 0.02f; /* 2 dB/s upward drift */
|
|
765
|
+
if (st->nf_db < -90.f) st->nf_db = -90.f;
|
|
766
|
+
return (rms_db > st->nf_db + 10.f && rms_db > -60.f) ? 1.f : 0.f;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/* Auto music (0.11.4): may the gains learn on this block? Only on the music
|
|
770
|
+
* itself: the stay test passing (not counting down the leave hold) and the
|
|
771
|
+
* block near the music level (not a pause or the room after the song). */
|
|
772
|
+
static int mu_learn_ok(const gx_ns *st, float rms_db) {
|
|
773
|
+
return st->mu_state && st->mu_run < 50 && st->mu_have_lvl && rms_db > st->mu_lvl_db - 10.f;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/* Measures x (pre-gain) and applies the smoothed gain in place. */
|
|
777
|
+
static void agc_block(gx_ns *st, float *x, float vad, int gated) {
|
|
778
|
+
int i;
|
|
779
|
+
float rms_db = block_rms_db(x, GX_NS_BLOCK), g_lin, step;
|
|
780
|
+
if (st->mu_m > 0.f) {
|
|
781
|
+
/* auto music (0.11.4): learns slowly on the music, boost-only (<= 1 dB/s
|
|
782
|
+
* up, never down); the applied boost stays capped (music_cap) */
|
|
783
|
+
if (rms_db > -65.f && mu_learn_ok(st, rms_db)) {
|
|
784
|
+
float want;
|
|
785
|
+
if (!st->have_level) { st->level_db = rms_db; st->have_level = 1; }
|
|
786
|
+
else st->level_db += 0.01f * (rms_db - st->level_db);
|
|
787
|
+
want = st->cfg.agc_target_dbfs - st->level_db;
|
|
788
|
+
if (want > st->cfg.agc_max_gain_db) want = st->cfg.agc_max_gain_db;
|
|
789
|
+
if (want > st->gain_db) st->gain_db = want - st->gain_db < 0.01f ? want : st->gain_db + 0.01f;
|
|
790
|
+
}
|
|
791
|
+
} else if (vad >= 0.5f && rms_db > -65.f) {
|
|
792
|
+
if (!st->have_level) { st->level_db = rms_db; st->have_level = 1; }
|
|
793
|
+
else st->level_db += 0.05f * (rms_db - st->level_db);
|
|
794
|
+
{
|
|
795
|
+
float want = st->cfg.agc_target_dbfs - st->level_db;
|
|
796
|
+
if (want > st->cfg.agc_max_gain_db) want = st->cfg.agc_max_gain_db;
|
|
797
|
+
if (want < st->cfg.agc_min_gain_db) want = st->cfg.agc_min_gain_db;
|
|
798
|
+
if (want > st->gain_db) { step = 0.1f; st->gain_db = (want - st->gain_db < step) ? want : st->gain_db + step; } /* 10 dB/s up */
|
|
799
|
+
else { step = 0.2f; st->gain_db = (st->gain_db - want < step) ? want : st->gain_db - step; } /* 20 dB/s down */
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
g_lin = db_to_lin(gated ? music_db(st, st->gain_db, music_cap(st, st->pg_applied_db)) : st->gain_db);
|
|
803
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
804
|
+
float a = (float)(i + 1) / GX_NS_BLOCK;
|
|
805
|
+
x[i] *= st->prev_gain_lin + (g_lin - st->prev_gain_lin) * a;
|
|
806
|
+
}
|
|
807
|
+
st->prev_gain_lin = g_lin;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/* Pre-gain: boost-only speech-level normaliser ahead of every engine. Level
|
|
811
|
+
* tracking on speech blocks only (previous block's RNNoise VAD + an energy check
|
|
812
|
+
* against the noise floor), fast attack (louder speech pulls the gain down at
|
|
813
|
+
* once) / slow release (gain rises at <= 15 dB/s); held in pauses, so noise is
|
|
814
|
+
* never pumped up. The first speech block sets the gain directly (a quiet
|
|
815
|
+
* phone mic must reach DFN3 at a usable level from the first words). Soft
|
|
816
|
+
* limiter (tanh knee above -6 dBFS) after the gain. `on` = 0 ramps to unity. */
|
|
817
|
+
static void pregain_block(gx_ns *st, float *x, float rms_db, int on) {
|
|
818
|
+
int i;
|
|
819
|
+
float g_lin;
|
|
820
|
+
if (!on) {
|
|
821
|
+
st->pg_gain_db = 0.f;
|
|
822
|
+
} else {
|
|
823
|
+
/* learns on talk (post-NS detector, last block) that RNNoise also calls voice */
|
|
824
|
+
int speech = st->talk_now && st->vad >= 0.5f && rms_db > -80.f && st->mu_m <= 0.f;
|
|
825
|
+
if (st->mu_m > 0.f) {
|
|
826
|
+
if (st->pg_have && rms_db > -80.f && mu_learn_ok(st, rms_db)) {
|
|
827
|
+
/* auto music (0.11.4): slow boost-only learning on the music (<= 1 dB/s
|
|
828
|
+
* up, never down; the applied boost stays capped by music_cap) */
|
|
829
|
+
float want;
|
|
830
|
+
st->pg_level_db += 0.01f * (rms_db - st->pg_level_db);
|
|
831
|
+
want = st->cfg.pregain_target_dbfs - st->pg_level_db;
|
|
832
|
+
if (want > st->cfg.pregain_max_db) want = st->cfg.pregain_max_db;
|
|
833
|
+
if (want > st->pg_gain_db) st->pg_gain_db = want - st->pg_gain_db < 0.01f ? want : st->pg_gain_db + 0.01f;
|
|
834
|
+
}
|
|
835
|
+
} else if (speech) {
|
|
836
|
+
float want;
|
|
837
|
+
int first = !st->pg_have;
|
|
838
|
+
if (first) { st->pg_level_db = rms_db; st->pg_have = 1; }
|
|
839
|
+
else if (rms_db > st->pg_level_db) st->pg_level_db += 0.3f * (rms_db - st->pg_level_db); /* attack */
|
|
840
|
+
else st->pg_level_db += 0.02f * (rms_db - st->pg_level_db); /* release */
|
|
841
|
+
want = st->cfg.pregain_target_dbfs - st->pg_level_db;
|
|
842
|
+
if (want > st->cfg.pregain_max_db) want = st->cfg.pregain_max_db;
|
|
843
|
+
if (want < 0.f) want = 0.f;
|
|
844
|
+
if (first) st->pg_gain_db = want; /* first speech: settle at once (ramped over the block) */
|
|
845
|
+
else if (want < st->pg_gain_db) st->pg_gain_db = (st->pg_gain_db - want < 1.f) ? want : st->pg_gain_db - 1.f; /* <= 100 dB/s down */
|
|
846
|
+
else st->pg_gain_db = (want - st->pg_gain_db < 0.15f) ? want : st->pg_gain_db + 0.15f; /* <= 15 dB/s up */
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
st->pg_applied_db = music_db(st, st->pg_gain_db, music_cap(st, 0.f));
|
|
850
|
+
g_lin = db_to_lin(st->pg_applied_db);
|
|
851
|
+
if (g_lin == 1.f && st->pg_prev_lin == 1.f) return;
|
|
852
|
+
{
|
|
853
|
+
const float T = 16384.f, C = 32000.f; /* knee -6 dBFS, ceiling ~-0.2 dBFS */
|
|
854
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
855
|
+
float a = (float)(i + 1) / GX_NS_BLOCK, y = x[i] * (st->pg_prev_lin + (g_lin - st->pg_prev_lin) * a), m = fabsf(y);
|
|
856
|
+
if (m > T) y = (y > 0.f ? 1.f : -1.f) * (T + (C - T) * tanhf((m - T) / (C - T)));
|
|
857
|
+
x[i] = y;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
st->pg_prev_lin = g_lin;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/* Studio guard: speech-gated power EMAs of the engine's input and output; a
|
|
864
|
+
* collapse (output >= GX_NS_EXT_SUPPRESS_DB under input) over
|
|
865
|
+
* GX_NS_EXT_SUPPRESS_BLOCKS consecutive speech blocks demotes the engine. */
|
|
866
|
+
static void ext_suppress_check(gx_ns *st, const float *in, const float *out, const float *rnn, float vad) {
|
|
867
|
+
double pi = 0, po = 0, pr = 0;
|
|
868
|
+
int i;
|
|
869
|
+
float ratio_db, vs_rnn_db;
|
|
870
|
+
if (vad < 0.5f || st->ext_suppress) return;
|
|
871
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
872
|
+
pi += (double)in[i] * in[i]; po += (double)out[i] * out[i];
|
|
873
|
+
pr += (double)rnn[i] * rnn[i] * (1.0 / (32768.0 * 32768.0)); /* RNNoise runs on the int16 scale */
|
|
874
|
+
}
|
|
875
|
+
pi /= GX_NS_BLOCK; po /= GX_NS_BLOCK; pr /= GX_NS_BLOCK;
|
|
876
|
+
if (pi < 1e-9) return; /* below -90 dBFS: nothing to judge */
|
|
877
|
+
st->sup_in += 0.05f * ((float)pi - st->sup_in);
|
|
878
|
+
st->sup_out += 0.05f * ((float)po - st->sup_out);
|
|
879
|
+
st->sup_rnn += 0.05f * ((float)pr - st->sup_rnn);
|
|
880
|
+
ratio_db = 10.f * log10f((st->sup_out + 1e-12f) / (st->sup_in + 1e-12f));
|
|
881
|
+
/* 2026-10-21 iPhone quiet mic: DFN3 took ~19 dB off the speech, just short of
|
|
882
|
+
* the 20 dB vs-input test, for up to 57 s, while RNNoise (its shadow, same
|
|
883
|
+
* blocks) kept it. Far below RNNoise on speech = DFN3 erases it. */
|
|
884
|
+
vs_rnn_db = 10.f * log10f((st->sup_out + 1e-12f) / (st->sup_rnn + 1e-12f));
|
|
885
|
+
if (ratio_db < -(float)GX_NS_EXT_SUPPRESS_DB || vs_rnn_db < -(float)GX_NS_EXT_VS_RNN_DB) st->sup_run++;
|
|
886
|
+
else st->sup_run = 0;
|
|
887
|
+
if (st->sup_run >= GX_NS_EXT_SUPPRESS_BLOCKS) {
|
|
888
|
+
st->ext_suppress = 1;
|
|
889
|
+
atomic_store_explicit(&st->pub_ext_suppress, 1, memory_order_relaxed);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
static void limiter_block(gx_ns *st, float *x) {
|
|
894
|
+
int i;
|
|
895
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
896
|
+
float a = fabsf(x[i]);
|
|
897
|
+
float decayed = st->lim_env * st->lim_rel;
|
|
898
|
+
st->lim_env = a > decayed ? a : decayed; /* env >= |x|, so |x| * ceil / env <= ceil */
|
|
899
|
+
if (st->lim_env > st->lim_ceil) x[i] *= st->lim_ceil / st->lim_env;
|
|
900
|
+
if (x[i] > 32767.f) x[i] = 32767.f;
|
|
901
|
+
else if (x[i] < -32768.f) x[i] = -32768.f;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/* k-th smallest of a[0..n) (quickselect, in place, no allocation). */
|
|
906
|
+
static uint16_t select_kth(uint16_t *a, int n, int k) {
|
|
907
|
+
int lo = 0, hi = n - 1;
|
|
908
|
+
while (lo < hi) {
|
|
909
|
+
uint16_t piv = a[(lo + hi) / 2], t;
|
|
910
|
+
int i = lo, j = hi;
|
|
911
|
+
while (i <= j) {
|
|
912
|
+
while (a[i] < piv) i++;
|
|
913
|
+
while (a[j] > piv) j--;
|
|
914
|
+
if (i <= j) { t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; }
|
|
915
|
+
}
|
|
916
|
+
if (k <= j) hi = j; else if (k >= i) lo = i; else return a[k];
|
|
917
|
+
}
|
|
918
|
+
return a[k];
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/* Studio frame time -> rolling p95 over the window; every 25 blocks once full. */
|
|
922
|
+
static void ext_time_add(gx_ns *st, uint64_t ns) {
|
|
923
|
+
int win = st->cfg.ext_p95_window;
|
|
924
|
+
uint32_t us = (uint32_t)(ns / 1000u), budget;
|
|
925
|
+
st->ext_us[st->ext_us_w] = (uint16_t)(us > 65535u ? 65535u : us);
|
|
926
|
+
st->ext_us_w = (st->ext_us_w + 1) % win;
|
|
927
|
+
if (st->ext_us_n < win) st->ext_us_n++;
|
|
928
|
+
if (++st->ext_us_since < 25 || st->ext_us_n < win) return;
|
|
929
|
+
st->ext_us_since = 0;
|
|
930
|
+
memcpy(st->ext_sel, st->ext_us, sizeof(uint16_t) * (size_t)win);
|
|
931
|
+
us = select_kth(st->ext_sel, win, (int)((win * 95 + 99) / 100) - 1);
|
|
932
|
+
atomic_store_explicit(&st->pub_ext_p95, us, memory_order_relaxed);
|
|
933
|
+
budget = (uint32_t)atomic_load_explicit(&st->req_p95_budget, memory_order_relaxed);
|
|
934
|
+
if (budget && us > budget && !st->ext_slow) {
|
|
935
|
+
st->ext_slow = 1;
|
|
936
|
+
atomic_store_explicit(&st->pub_ext_slow, 1, memory_order_relaxed);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/* ---------- one 10 ms block at 48 kHz, in place ---------- */
|
|
941
|
+
static void process_block(gx_ns *st, float *blk) {
|
|
942
|
+
const float *srcs[3];
|
|
943
|
+
gx_ns_external_engine *ext;
|
|
944
|
+
uint32_t g;
|
|
945
|
+
int req, bypass, agc_on, agc_order, desired, i, want_L, run_rnn, run_ext, pg_on;
|
|
946
|
+
float vad = 0.f, in_rms_db;
|
|
947
|
+
|
|
948
|
+
/* control snapshot */
|
|
949
|
+
req = atomic_load_explicit(&st->req_engine, memory_order_relaxed);
|
|
950
|
+
bypass = atomic_load_explicit(&st->req_bypass, memory_order_relaxed);
|
|
951
|
+
agc_on = atomic_load_explicit(&st->req_agc, memory_order_relaxed);
|
|
952
|
+
agc_order = atomic_load_explicit(&st->req_agc_order, memory_order_relaxed);
|
|
953
|
+
ext = atomic_load_explicit(&st->ext_ptr, memory_order_acquire);
|
|
954
|
+
g = atomic_load_explicit(&st->ext_gen, memory_order_relaxed);
|
|
955
|
+
if (g != st->ext_gen_seen) {
|
|
956
|
+
st->ext_gen_seen = g; st->ext_failed = 0; st->ext_preroll = -1;
|
|
957
|
+
st->ext_slow = 0; st->ext_us_n = st->ext_us_w = st->ext_us_since = 0; /* fresh engine, fresh window */
|
|
958
|
+
atomic_store_explicit(&st->pub_ext_slow, 0, memory_order_relaxed);
|
|
959
|
+
atomic_store_explicit(&st->pub_ext_p95, 0, memory_order_relaxed);
|
|
960
|
+
st->ext_suppress = 0; st->sup_run = 0; st->sup_in = st->sup_out = st->sup_rnn = 0.f;
|
|
961
|
+
atomic_store_explicit(&st->pub_ext_suppress, 0, memory_order_relaxed);
|
|
962
|
+
}
|
|
963
|
+
g = atomic_load_explicit(&st->req_reset, memory_order_relaxed);
|
|
964
|
+
if (g != st->reset_seen) { st->reset_seen = g; st->reset_pending = 1; }
|
|
965
|
+
/* Engine vanished under us (detached without switching away first): hard cut to RNNoise. */
|
|
966
|
+
if (!ext && (st->cur == SRC_EXT || st->next == SRC_EXT)) {
|
|
967
|
+
if (st->cur == SRC_EXT) st->cur = SRC_RNN;
|
|
968
|
+
if (st->next == SRC_EXT) st->next = st->cur;
|
|
969
|
+
st->xf_pos = 0;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/* delay compensation target */
|
|
973
|
+
want_L = GX_NS_RNNOISE_LATENCY;
|
|
974
|
+
if (ext && ext->latency_samples > want_L) want_L = ext->latency_samples;
|
|
975
|
+
|
|
976
|
+
/* desired source */
|
|
977
|
+
st->reason = GX_NS_REASON_OK;
|
|
978
|
+
if (st->reset_pending) desired = SRC_DRY; /* fade out the old engine state first */
|
|
979
|
+
else if (bypass) { desired = SRC_DRY; st->reason = GX_NS_REASON_BYPASS; }
|
|
980
|
+
else if (req == GX_NS_ENGINE_OFF) desired = SRC_DRY;
|
|
981
|
+
else if (req == GX_NS_ENGINE_DFN3) {
|
|
982
|
+
if (!ext) { desired = SRC_RNN; st->reason = GX_NS_REASON_NO_EXTERNAL; }
|
|
983
|
+
else if (st->ext_failed) { desired = SRC_RNN; st->reason = GX_NS_REASON_EXT_ERROR; }
|
|
984
|
+
else if (st->ext_slow) { desired = SRC_RNN; st->reason = GX_NS_REASON_EXT_SLOW; }
|
|
985
|
+
else if (st->ext_suppress) { desired = SRC_RNN; st->reason = GX_NS_REASON_EXT_SUPPRESS; }
|
|
986
|
+
else if (st->L != want_L) { desired = SRC_RNN; st->reason = GX_NS_REASON_DELAY_PENDING; }
|
|
987
|
+
else {
|
|
988
|
+
if (st->ext_preroll < 0) /* fill its delay line + let it settle before it is heard */
|
|
989
|
+
st->ext_preroll = 2 + (st->L - ext->latency_samples + GX_NS_BLOCK - 1) / GX_NS_BLOCK;
|
|
990
|
+
desired = st->ext_preroll > 0 ? st->next : SRC_EXT; /* keep what plays during the pre-roll */
|
|
991
|
+
}
|
|
992
|
+
} else desired = SRC_RNN;
|
|
993
|
+
|
|
994
|
+
if (st->cur == st->next) {
|
|
995
|
+
if (desired != st->cur) { st->next = desired; st->xf_pos = 0; }
|
|
996
|
+
} else if (desired == st->cur) { /* reverse a running fade */
|
|
997
|
+
int t = st->cur; st->cur = st->next; st->next = t;
|
|
998
|
+
st->xf_pos = st->xf_len - st->xf_pos;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/* Music bypass takes the WHOLE chain out (NS and our AGC; owner 2026-10-21):
|
|
1002
|
+
* the gain ramps back to unity like "AGC off" and returns after the bypass.
|
|
1003
|
+
* The peak limiter (-1 dBFS) stays: it only touches near-full-scale peaks.
|
|
1004
|
+
* Engine "off" (the SDK's off and kill switch) does the same, like pre-gain:
|
|
1005
|
+
* WebRTC's own AGC is back on there and two AGCs must never stack. */
|
|
1006
|
+
if (bypass || req == GX_NS_ENGINE_OFF) agc_on = 0;
|
|
1007
|
+
|
|
1008
|
+
/* pre-gain (all engines), then the probe-only AGC before NS */
|
|
1009
|
+
in_rms_db = block_rms_db(blk, GX_NS_BLOCK);
|
|
1010
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
1011
|
+
float a = fabsf(blk[i]);
|
|
1012
|
+
st->lm_pre_acc += (double)blk[i] * blk[i];
|
|
1013
|
+
if (a > st->lm_pre_pk) st->lm_pre_pk = a;
|
|
1014
|
+
}
|
|
1015
|
+
/* auto music: a new capture stream (gx_ns_reset_music / route reset / rate
|
|
1016
|
+
* change) starts from scratch, not relaxed */
|
|
1017
|
+
g = atomic_load_explicit(&st->req_mu_reset, memory_order_relaxed);
|
|
1018
|
+
if (g != st->mu_reset_seen || st->reset_pending) {
|
|
1019
|
+
st->mu_reset_seen = g;
|
|
1020
|
+
if (st->reset_pending) { st->sl_have = 0; st->sl_level_db = -120.f; st->sl_gain_db = 0.f; st->sl_loud = 0; st->sl_quiet = 0; st->sl_act = 0; st->sl_duck_db = 0.f; } /* a new route: its own level */
|
|
1021
|
+
mu_reset(st);
|
|
1022
|
+
st->mu_m = 0.f;
|
|
1023
|
+
st->mu_have_lvl = 0;
|
|
1024
|
+
}
|
|
1025
|
+
/* auto music: judged on the input (before the pre-gain); rests while the
|
|
1026
|
+
* music bypass or engine off already take the chain out */
|
|
1027
|
+
if (atomic_load_explicit(&st->req_auto_music, memory_order_relaxed) && !bypass && req != GX_NS_ENGINE_OFF) {
|
|
1028
|
+
mu_block(st, blk, in_rms_db);
|
|
1029
|
+
} else if (st->mu_fill || st->mu_state) {
|
|
1030
|
+
mu_reset(st);
|
|
1031
|
+
}
|
|
1032
|
+
atomic_store_explicit(&st->pub_music, st->mu_state, memory_order_relaxed);
|
|
1033
|
+
if (st->mu_state) {
|
|
1034
|
+
if (in_rms_db > -60.f) {
|
|
1035
|
+
if (!st->mu_have_lvl) {
|
|
1036
|
+
/* seed from the loudest block of the last 200 ms, never a quiet instant
|
|
1037
|
+
* (a low seed would let the held boosts lift the music for ~1 s) */
|
|
1038
|
+
int q;
|
|
1039
|
+
float mx = in_rms_db;
|
|
1040
|
+
for (q = 0; q < GX_MU_PEAKWIN; q++) if (st->mu_lv[q] > mx) mx = st->mu_lv[q];
|
|
1041
|
+
st->mu_lvl_db = mx;
|
|
1042
|
+
st->mu_have_lvl = 1;
|
|
1043
|
+
}
|
|
1044
|
+
else if (st->mu_run < 50 && in_rms_db > st->mu_lvl_db - 15.f)
|
|
1045
|
+
st->mu_lvl_db += 0.01f * (in_rms_db - st->mu_lvl_db); /* ~1 s; 0.11.4: not from pauses / the room after the song */
|
|
1046
|
+
}
|
|
1047
|
+
st->mu_m = st->mu_m + 1.f / GX_MU_RAMP < 1.f ? st->mu_m + 1.f / GX_MU_RAMP : 1.f;
|
|
1048
|
+
} else {
|
|
1049
|
+
st->mu_m = st->mu_m - 1.f / GX_MU_RAMP > 0.f ? st->mu_m - 1.f / GX_MU_RAMP : 0.f;
|
|
1050
|
+
if (st->mu_m <= 0.f) st->mu_have_lvl = 0;
|
|
1051
|
+
}
|
|
1052
|
+
atomic_store_explicit(&st->pub_mu_dbg[5], st->mu_m, memory_order_relaxed);
|
|
1053
|
+
atomic_store_explicit(&st->pub_mu_dbg[6], st->mu_lvl_db, memory_order_relaxed);
|
|
1054
|
+
pg_on = atomic_load_explicit(&st->req_pregain, memory_order_relaxed) && !bypass && req != GX_NS_ENGINE_OFF;
|
|
1055
|
+
pregain_block(st, blk, in_rms_db, pg_on);
|
|
1056
|
+
|
|
1057
|
+
if (agc_on && agc_order == GX_NS_AGC_BEFORE_NS) agc_block(st, blk, st->talk_now ? st->vad : 0.f, 0); /* probe-only order: not gated */
|
|
1058
|
+
|
|
1059
|
+
dl_write(&st->dl_dry, blk, GX_NS_BLOCK);
|
|
1060
|
+
|
|
1061
|
+
/* RNNoise: whenever audible, fading, wanted, or as the shadow of DFN3. */
|
|
1062
|
+
run_rnn = st->cur == SRC_RNN || st->next == SRC_RNN || desired == SRC_RNN || st->cur == SRC_EXT || st->next == SRC_EXT;
|
|
1063
|
+
if (run_rnn) {
|
|
1064
|
+
if (!st->rnn_running) {
|
|
1065
|
+
/* (Re)start from a clean state, primed with the two previous input
|
|
1066
|
+
* blocks so the overlap-add memory is filled: without it the first two
|
|
1067
|
+
* output blocks have a seam that clicks in the crossfade. */
|
|
1068
|
+
rnnoise_reset(st->rnn);
|
|
1069
|
+
dl_read(&st->dl_dry, st->b_tmp, GX_NS_BLOCK, 2 * GX_NS_BLOCK);
|
|
1070
|
+
rnnoise_process_frame(st->rnn, st->b_rnn, st->b_tmp);
|
|
1071
|
+
dl_read(&st->dl_dry, st->b_tmp, GX_NS_BLOCK, GX_NS_BLOCK);
|
|
1072
|
+
rnnoise_process_frame(st->rnn, st->b_rnn, st->b_tmp);
|
|
1073
|
+
st->rnn_running = 1;
|
|
1074
|
+
}
|
|
1075
|
+
vad = rnnoise_process_frame(st->rnn, st->b_rnn, blk);
|
|
1076
|
+
dl_write(&st->dl_rnn, st->b_rnn, GX_NS_BLOCK);
|
|
1077
|
+
} else {
|
|
1078
|
+
st->rnn_running = 0;
|
|
1079
|
+
vad = energy_vad(st, in_rms_db);
|
|
1080
|
+
dl_write(&st->dl_rnn, blk, GX_NS_BLOCK); /* keeps the line time-aligned; never audible */
|
|
1081
|
+
}
|
|
1082
|
+
if (run_rnn) (void)energy_vad(st, in_rms_db); /* keep the noise floor tracking */
|
|
1083
|
+
|
|
1084
|
+
/* External engine ([-1, 1] scale). */
|
|
1085
|
+
run_ext = ext && !st->ext_failed && (st->cur == SRC_EXT || st->next == SRC_EXT || (st->ext_preroll > 0 && !st->ext_slow && !st->ext_suppress));
|
|
1086
|
+
if (run_ext) {
|
|
1087
|
+
const float k = 1.f / 32768.f;
|
|
1088
|
+
uint64_t te;
|
|
1089
|
+
int rc;
|
|
1090
|
+
for (i = 0; i < GX_NS_BLOCK; i++) st->b_tmp[i] = blk[i] * k;
|
|
1091
|
+
te = gx_timer_now_ns();
|
|
1092
|
+
rc = ext->process(ext->user, st->b_ext, st->b_tmp, GX_NS_BLOCK);
|
|
1093
|
+
ext_time_add(st, gx_timer_now_ns() - te);
|
|
1094
|
+
if (rc != 0) {
|
|
1095
|
+
st->ext_failed = 1;
|
|
1096
|
+
atomic_fetch_add_explicit(&st->ext_errors, 1, memory_order_relaxed);
|
|
1097
|
+
if (st->cur == SRC_EXT) st->cur = SRC_RNN;
|
|
1098
|
+
if (st->next == SRC_EXT) st->next = st->cur;
|
|
1099
|
+
st->xf_pos = 0;
|
|
1100
|
+
dl_write(&st->dl_ext, blk, GX_NS_BLOCK);
|
|
1101
|
+
} else {
|
|
1102
|
+
ext_suppress_check(st, st->b_tmp, st->b_ext, st->b_rnn, vad);
|
|
1103
|
+
for (i = 0; i < GX_NS_BLOCK; i++) st->b_ext[i] *= 32768.f;
|
|
1104
|
+
dl_write(&st->dl_ext, st->b_ext, GX_NS_BLOCK);
|
|
1105
|
+
}
|
|
1106
|
+
if (st->ext_preroll > 0) st->ext_preroll--;
|
|
1107
|
+
} else {
|
|
1108
|
+
dl_write(&st->dl_ext, blk, GX_NS_BLOCK);
|
|
1109
|
+
st->ext_preroll = -1; /* not running: its delay line is stale, pre-roll again before use */
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/* aligned sources */
|
|
1113
|
+
dl_read(&st->dl_dry, st->s_dry, GX_NS_BLOCK, st->L);
|
|
1114
|
+
dl_read(&st->dl_rnn, st->s_rnn, GX_NS_BLOCK, st->L - GX_NS_RNNOISE_LATENCY);
|
|
1115
|
+
dl_read(&st->dl_ext, st->s_ext, GX_NS_BLOCK, ext ? st->L - ext->latency_samples : 0);
|
|
1116
|
+
/* Studio attenuation limit: mix back the delay-aligned dry signal (equals
|
|
1117
|
+
* DeepFilterNet's spectral atten_lim mix: STFT/ISTFT are linear). */
|
|
1118
|
+
{
|
|
1119
|
+
float lim = atomic_load_explicit(&st->req_atten_db, memory_order_relaxed);
|
|
1120
|
+
if (ext && lim > 0.f) {
|
|
1121
|
+
const float gd = db_to_lin(-lim), ge = 1.f - gd;
|
|
1122
|
+
for (i = 0; i < GX_NS_BLOCK; i++) st->s_ext[i] = ge * st->s_ext[i] + gd * st->s_dry[i];
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
srcs[SRC_DRY] = st->s_dry;
|
|
1126
|
+
srcs[SRC_RNN] = st->s_rnn;
|
|
1127
|
+
srcs[SRC_EXT] = st->s_ext;
|
|
1128
|
+
|
|
1129
|
+
/* complementary linear crossfade */
|
|
1130
|
+
if (st->cur == st->next) {
|
|
1131
|
+
memcpy(blk, srcs[st->cur], sizeof(float) * GX_NS_BLOCK);
|
|
1132
|
+
} else {
|
|
1133
|
+
const float *a = srcs[st->cur], *b = srcs[st->next];
|
|
1134
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
1135
|
+
float w = st->xf_pos >= st->xf_len ? 1.f : (float)st->xf_pos / (float)st->xf_len;
|
|
1136
|
+
blk[i] = a[i] + (b[i] - a[i]) * w;
|
|
1137
|
+
if (st->xf_pos < st->xf_len) st->xf_pos++;
|
|
1138
|
+
}
|
|
1139
|
+
if (st->xf_pos >= st->xf_len) {
|
|
1140
|
+
st->cur = st->next;
|
|
1141
|
+
st->xf_pos = 0;
|
|
1142
|
+
atomic_fetch_add_explicit(&st->switches, 1, memory_order_relaxed);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
/* Auto music: the NS keeps at least GX_NS_MUSIC_FLOOR_DB of the input
|
|
1147
|
+
* (dry mix, same alignment: L does not change) */
|
|
1148
|
+
if (st->mu_m > 0.f && !(st->cur == SRC_DRY && st->next == SRC_DRY)) {
|
|
1149
|
+
const float g = st->mu_m * db_to_lin(-(float)GX_NS_MUSIC_FLOOR_DB);
|
|
1150
|
+
for (i = 0; i < GX_NS_BLOCK; i++) blk[i] += g * (st->s_dry[i] - blk[i]);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
/* Route change: the old engine has faded out to dry (continuous signal);
|
|
1154
|
+
* reset it now, it fades back in from the next block. ~40 ms in total. */
|
|
1155
|
+
if (st->reset_pending && st->cur == SRC_DRY && st->next == SRC_DRY) {
|
|
1156
|
+
st->rnn_running = 0; /* restarts primed on the next block */
|
|
1157
|
+
if (ext && ext->reset) ext->reset(ext->user);
|
|
1158
|
+
st->reset_pending = 0;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
/* talk detector + pause gate on what the NS left (pre-gain taken out). With
|
|
1162
|
+
* DFN3 audible it judges the louder of DFN3 and RNNoise's shadow (same
|
|
1163
|
+
* alignment): DFN3 erases speech under ~-45 dBFS, so judged on its own output
|
|
1164
|
+
* a quiet mic never counted as talk and the pre-gain that would lift it to
|
|
1165
|
+
* DFN3 never learnt (iPhone 2026-10-21: up to 57 s of erased speech). Both
|
|
1166
|
+
* suppress noise, so the louder one stays low on noise. */
|
|
1167
|
+
{
|
|
1168
|
+
float post_db = block_rms_db(blk, GX_NS_BLOCK);
|
|
1169
|
+
if (run_rnn) {
|
|
1170
|
+
float rnn_db = block_rms_db(st->s_rnn, GX_NS_BLOCK);
|
|
1171
|
+
if (rnn_db > post_db) post_db = rnn_db;
|
|
1172
|
+
}
|
|
1173
|
+
post_db -= st->pg_applied_db;
|
|
1174
|
+
float mn = 1e9f;
|
|
1175
|
+
int k;
|
|
1176
|
+
if (post_db < -100.f) post_db = -100.f;
|
|
1177
|
+
{
|
|
1178
|
+
int w = st->pnf_have < GX_NS_TALK_WARM ? GX_NS_TALK_WIN0 : GX_NS_TALK_WIN;
|
|
1179
|
+
for (k = 1; k <= w; k++) {
|
|
1180
|
+
float v = st->post_hist[(st->post_pos + GX_NS_TALK_WIN - k) % GX_NS_TALK_WIN];
|
|
1181
|
+
if (v < mn) mn = v;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
{
|
|
1185
|
+
float in_ref = in_rms_db;
|
|
1186
|
+
for (k = 0; k < 3; k++) if (st->in_hist[k] > in_ref) in_ref = st->in_hist[k];
|
|
1187
|
+
st->in_hist[st->post_pos % 3] = in_rms_db;
|
|
1188
|
+
int was_talk = st->talk_now || st->talk_hang > 0;
|
|
1189
|
+
{
|
|
1190
|
+
float d = fabsf(post_db - st->prev_post_db);
|
|
1191
|
+
st->lvl_jit_db += 0.05f * ((d > 6.f ? 6.f : d) - st->lvl_jit_db);
|
|
1192
|
+
st->prev_post_db = post_db;
|
|
1193
|
+
}
|
|
1194
|
+
int rise = st->pnf_have >= GX_NS_TALK_WIN0 && post_db > mn + GX_NS_TALK_OVER_DB;
|
|
1195
|
+
/* talk going on (this block or the hangover) that holds: near its onset
|
|
1196
|
+
* level and well over the last pause, at the NS output and at the input
|
|
1197
|
+
* (a held note, a legato line). The input check: when RNNoise erased a
|
|
1198
|
+
* loud input (a note over a mains buzz) the "pause" it left is no pause,
|
|
1199
|
+
* and the buzz coming back must not ride on it (pre-gain + AGC lifted it
|
|
1200
|
+
* 26 dB in the first cut of this, test_sustain) */
|
|
1201
|
+
int sustain = !rise && was_talk && st->quiet_have && st->sus_run < GX_NS_SUS_MAX &&
|
|
1202
|
+
post_db > st->quiet_db + GX_NS_SUS_OVER_DB && in_rms_db > st->in_quiet_db + GX_NS_SUS_OVER_DB &&
|
|
1203
|
+
post_db > st->talk_lvl_db - GX_NS_SUS_DROP_DB && st->lvl_jit_db > GX_NS_SUS_JITTER_DB;
|
|
1204
|
+
st->talk_now = (rise || sustain) && post_db > GX_NS_TALK_MIN_DB &&
|
|
1205
|
+
post_db > in_ref - GX_NS_TALK_KEEP_DB &&
|
|
1206
|
+
vad >= ((st->exp_quiet < GX_NS_TALK_RECENT || in_rms_db > st->nf_db + GX_NS_TALK_RISE_DB)
|
|
1207
|
+
? GX_NS_TALK_VAD_HOLD : GX_NS_TALK_VAD);
|
|
1208
|
+
if (st->talk_now && rise) {
|
|
1209
|
+
st->talk_lvl_db = st->sus_run || !was_talk ? post_db : st->talk_lvl_db + 0.1f * (post_db - st->talk_lvl_db);
|
|
1210
|
+
st->sus_run = 0;
|
|
1211
|
+
} else if (st->talk_now) {
|
|
1212
|
+
st->sus_run++;
|
|
1213
|
+
} else if (!was_talk) {
|
|
1214
|
+
/* a pause block (also the warm-up): the level the next talk must stand out from */
|
|
1215
|
+
if (!st->quiet_have) { st->quiet_db = post_db; st->in_quiet_db = in_rms_db; st->quiet_have = 1; }
|
|
1216
|
+
else {
|
|
1217
|
+
st->quiet_db += 0.05f * (post_db - st->quiet_db);
|
|
1218
|
+
st->in_quiet_db += 0.05f * (in_rms_db - st->in_quiet_db);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
st->post_hist[st->post_pos] = post_db;
|
|
1223
|
+
st->post_pos = (st->post_pos + 1) % GX_NS_TALK_WIN;
|
|
1224
|
+
if (st->pnf_have < GX_NS_TALK_WARM && in_rms_db > -90.f) st->pnf_have++; /* the mic's digital silence (start-up) does not count */
|
|
1225
|
+
st->exp_quiet = st->talk_now ? 0 : (st->exp_quiet < 1000 ? st->exp_quiet + 1 : 1000);
|
|
1226
|
+
if (st->talk_now) {
|
|
1227
|
+
st->talk_hang = GX_NS_PAUSE_HANG;
|
|
1228
|
+
st->gate = 1.f;
|
|
1229
|
+
} else if (st->talk_hang > 0) {
|
|
1230
|
+
st->talk_hang--;
|
|
1231
|
+
} else {
|
|
1232
|
+
st->gate = st->gate > GX_NS_PAUSE_RAMP ? st->gate - GX_NS_PAUSE_RAMP : 0.f;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
if (agc_on && agc_order == GX_NS_AGC_AFTER_NS) agc_block(st, blk, st->talk_now ? vad : 0.f, 1);
|
|
1237
|
+
else if (!agc_on) {
|
|
1238
|
+
/* keep the gain neutral and smooth when AGC goes off */
|
|
1239
|
+
st->gain_db = 0.f;
|
|
1240
|
+
if (st->prev_gain_lin != 1.f) {
|
|
1241
|
+
for (i = 0; i < GX_NS_BLOCK; i++) blk[i] *= st->prev_gain_lin + (1.f - st->prev_gain_lin) * (float)(i + 1) / GX_NS_BLOCK;
|
|
1242
|
+
st->prev_gain_lin = 1.f;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
/* Pause expander (DTX), part of our AGC stage (runs with it: off, the kill
|
|
1247
|
+
* switch, music bypass and webrtcBeforeNs leave the level alone): in pauses
|
|
1248
|
+
* (gate closed) what the NS leaves of loud noise is pulled down to
|
|
1249
|
+
* GX_NS_PAUSE_TARGET_DB, so Opus' DTX engages; talk reopens at once. Quiet
|
|
1250
|
+
* rooms are left alone (nothing over the target). */
|
|
1251
|
+
{
|
|
1252
|
+
float lvl = block_rms_db(blk, GX_NS_BLOCK), att = 0.f, g_lin;
|
|
1253
|
+
/* slow to rise (a word's onset must not pull itself down), quick to fall */
|
|
1254
|
+
st->exp_lvl_db += (lvl > st->exp_lvl_db ? GX_NS_EXP_RISE : 0.1f) * (lvl - st->exp_lvl_db);
|
|
1255
|
+
if (agc_on) {
|
|
1256
|
+
att = st->exp_lvl_db - GX_NS_PAUSE_TARGET_DB;
|
|
1257
|
+
if (att > GX_NS_PAUSE_MAX_ATT_DB) att = GX_NS_PAUSE_MAX_ATT_DB;
|
|
1258
|
+
if (att < 0.f) att = 0.f;
|
|
1259
|
+
att *= 1.f - st->gate;
|
|
1260
|
+
att *= 1.f - st->mu_m; /* auto music: off */
|
|
1261
|
+
/* only in a real pause: never on a word's first syllable after a short gap */
|
|
1262
|
+
if (st->exp_quiet < GX_NS_EXP_WAIT) att = 0.f;
|
|
1263
|
+
else if (st->exp_quiet < GX_NS_EXP_WAIT + 10) att *= (float)(st->exp_quiet - GX_NS_EXP_WAIT) / 10.f;
|
|
1264
|
+
}
|
|
1265
|
+
atomic_store_explicit(&st->pub_pause_att_db, att, memory_order_relaxed);
|
|
1266
|
+
g_lin = db_to_lin(-att);
|
|
1267
|
+
if (g_lin != 1.f || st->exp_prev_lin != 1.f) {
|
|
1268
|
+
for (i = 0; i < GX_NS_BLOCK; i++) blk[i] *= st->exp_prev_lin + (g_lin - st->exp_prev_lin) * (float)(i + 1) / GX_NS_BLOCK;
|
|
1269
|
+
st->exp_prev_lin = g_lin;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
/* reset / rate-change fade-in from silence */
|
|
1274
|
+
if (st->fade_in_pos < st->xf_len) {
|
|
1275
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
1276
|
+
float w = st->fade_in_pos >= st->xf_len ? 1.f : (float)st->fade_in_pos / (float)st->xf_len;
|
|
1277
|
+
blk[i] *= w;
|
|
1278
|
+
if (st->fade_in_pos < st->xf_len) st->fade_in_pos++;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/* delay-change fade-in (second half of a delay change) */
|
|
1283
|
+
if (st->delay_fade_in > 0) {
|
|
1284
|
+
for (i = 0; i < DELAY_FADE; i++) blk[i] *= (float)i / DELAY_FADE;
|
|
1285
|
+
st->delay_fade_in = 0;
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/* apply a pending delay change in a quiet block, never while fading */
|
|
1289
|
+
if (st->L == want_L) {
|
|
1290
|
+
st->quiet_wait = 0;
|
|
1291
|
+
} else if (st->cur == st->next && st->cur != SRC_EXT) {
|
|
1292
|
+
st->quiet_wait++;
|
|
1293
|
+
if ((vad < 0.3f && in_rms_db < -40.f) || st->quiet_wait >= DELAY_FORCE_BLOCKS) {
|
|
1294
|
+
for (i = 0; i < DELAY_FADE; i++) blk[GX_NS_BLOCK - DELAY_FADE + i] *= 1.f - (float)(i + 1) / DELAY_FADE;
|
|
1295
|
+
st->L = want_L;
|
|
1296
|
+
st->quiet_wait = 0;
|
|
1297
|
+
st->delay_fade_in = 1;
|
|
1298
|
+
if (st->rc)
|
|
1299
|
+
atomic_store_explicit(&st->pub_latency, latency_for(st, st->rc, st->prefill), memory_order_relaxed);
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
/* singer level normaliser (0.11.4, see gx_ns_set_singer_level) + the fixed
|
|
1304
|
+
* bypass makeup: <= 1.5 dB per block toward the sum, ramped inside the block */
|
|
1305
|
+
{
|
|
1306
|
+
float sl_max = bypass ? atomic_load_explicit(&st->req_sl_max, memory_order_relaxed) : 0.f;
|
|
1307
|
+
float want, g_lin;
|
|
1308
|
+
int sl_release = 0;
|
|
1309
|
+
if (bypass && sl_max > 0.f) {
|
|
1310
|
+
/* voiced: over the noise floor, and not far under the sung level (a
|
|
1311
|
+
* quieter echo / room sound must not pull the estimate down) */
|
|
1312
|
+
int active = in_rms_db > st->nf_db + 10.f && in_rms_db > -65.f;
|
|
1313
|
+
int voiced = active && (!st->sl_have || in_rms_db > st->sl_level_db - 12.f);
|
|
1314
|
+
/* pause guard (0.11.5): only a real pause (nothing over the noise floor;
|
|
1315
|
+
* soft singing under the estimate is not one) for GX_NS_SINGER_PAUSE_MS
|
|
1316
|
+
* eases the level boost out (the room / hiss under the singer is not
|
|
1317
|
+
* raised by up to 24 dB), never under the fixed makeup, so the output is
|
|
1318
|
+
* never quieter than the input. The held sl_gain_db is back at once
|
|
1319
|
+
* after 2 blocks (20 ms) of singing; other signal (a room burst, a soft
|
|
1320
|
+
* line) brings it back gradually after 3 blocks, so short room bursts in
|
|
1321
|
+
* a pause do not pump it. */
|
|
1322
|
+
if (active) {
|
|
1323
|
+
st->sl_quiet = 0;
|
|
1324
|
+
if (st->sl_act < 1000) st->sl_act++;
|
|
1325
|
+
if (st->sl_duck_db > 0.f) {
|
|
1326
|
+
if (voiced && st->sl_act >= 2) { st->sl_duck_db = 0.f; sl_release = 1; }
|
|
1327
|
+
else if (st->sl_act >= 3) st->sl_duck_db = st->sl_duck_db > 1.5f ? st->sl_duck_db - 1.5f : 0.f;
|
|
1328
|
+
}
|
|
1329
|
+
} else {
|
|
1330
|
+
st->sl_act = 0;
|
|
1331
|
+
if (st->sl_quiet < GX_NS_SINGER_PAUSE_MS / 10) st->sl_quiet++;
|
|
1332
|
+
else if (st->sl_duck_db < st->sl_gain_db)
|
|
1333
|
+
st->sl_duck_db = st->sl_gain_db - st->sl_duck_db > (float)GX_NS_SINGER_PAUSE_FALL_DB_S / 100.f
|
|
1334
|
+
? st->sl_duck_db + (float)GX_NS_SINGER_PAUSE_FALL_DB_S / 100.f
|
|
1335
|
+
: st->sl_gain_db;
|
|
1336
|
+
}
|
|
1337
|
+
if (voiced) {
|
|
1338
|
+
float t, w;
|
|
1339
|
+
if (!st->sl_have) { st->sl_level_db = in_rms_db; st->sl_have = 1; }
|
|
1340
|
+
else {
|
|
1341
|
+
/* ~2 s average (dB); 10 dB louder for 0.5 s: ~0.2 s (a short bump or
|
|
1342
|
+
* clap must not lift the estimate out of reach of the singing) */
|
|
1343
|
+
st->sl_loud = in_rms_db > st->sl_level_db + 10.f ? st->sl_loud + 1 : st->sl_loud > 0 ? st->sl_loud - 1 : 0; /* net loud blocks */
|
|
1344
|
+
st->sl_level_db += (st->sl_loud >= 50 ? 0.05f : 0.005f) * (in_rms_db - st->sl_level_db);
|
|
1345
|
+
}
|
|
1346
|
+
t = atomic_load_explicit(&st->req_sl_target, memory_order_relaxed);
|
|
1347
|
+
w = t - st->sl_level_db;
|
|
1348
|
+
if (w > sl_max) w = sl_max;
|
|
1349
|
+
if (w < 0.f) w = 0.f;
|
|
1350
|
+
if (w > st->sl_gain_db) {
|
|
1351
|
+
float r = atomic_load_explicit(&st->req_sl_rise, memory_order_relaxed);
|
|
1352
|
+
st->sl_gain_db = w - st->sl_gain_db > r ? st->sl_gain_db + r : w;
|
|
1353
|
+
} else {
|
|
1354
|
+
st->sl_gain_db = st->sl_gain_db - w > 1.f ? st->sl_gain_db - 1.f : w;
|
|
1355
|
+
}
|
|
1356
|
+
} else if (st->sl_gain_db > sl_max) {
|
|
1357
|
+
st->sl_gain_db = sl_max; /* the cap was lowered */
|
|
1358
|
+
}
|
|
1359
|
+
if (st->sl_duck_db > st->sl_gain_db) st->sl_duck_db = st->sl_gain_db;
|
|
1360
|
+
} else {
|
|
1361
|
+
st->sl_quiet = 0;
|
|
1362
|
+
st->sl_act = 0;
|
|
1363
|
+
st->sl_duck_db = st->sl_duck_db > 1.f ? st->sl_duck_db - 1.f : 0.f;
|
|
1364
|
+
if (st->sl_gain_db > 0.f)
|
|
1365
|
+
st->sl_gain_db = st->sl_gain_db > 1.f ? st->sl_gain_db - 1.f : 0.f; /* off: fades in 10 ms steps */
|
|
1366
|
+
if (st->sl_duck_db > st->sl_gain_db) st->sl_duck_db = st->sl_gain_db;
|
|
1367
|
+
}
|
|
1368
|
+
want = (bypass ? atomic_load_explicit(&st->req_makeup_db, memory_order_relaxed) : 0.f) + st->sl_gain_db - st->sl_duck_db;
|
|
1369
|
+
if (want > (float)GX_NS_MAKEUP_MAX_DB) want = (float)GX_NS_MAKEUP_MAX_DB;
|
|
1370
|
+
if (sl_release && st->mk_db < want) st->mk_db = want; /* back within this 10 ms block (ramped) */
|
|
1371
|
+
else if (st->mk_db < want) st->mk_db = want - st->mk_db > 1.5f ? st->mk_db + 1.5f : want;
|
|
1372
|
+
else if (st->mk_db > want) st->mk_db = st->mk_db - want > 1.5f ? st->mk_db - 1.5f : want;
|
|
1373
|
+
g_lin = st->mk_db == 0.f ? 1.f : db_to_lin(st->mk_db);
|
|
1374
|
+
if (g_lin != 1.f || st->mk_lin != 1.f)
|
|
1375
|
+
for (i = 0; i < GX_NS_BLOCK; i++) blk[i] *= st->mk_lin + (g_lin - st->mk_lin) * (float)(i + 1) / GX_NS_BLOCK;
|
|
1376
|
+
st->mk_lin = g_lin;
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
limiter_block(st, blk);
|
|
1380
|
+
|
|
1381
|
+
/* level meters: ~1 s windows */
|
|
1382
|
+
for (i = 0; i < GX_NS_BLOCK; i++) {
|
|
1383
|
+
float a = fabsf(blk[i]);
|
|
1384
|
+
st->lm_post_acc += (double)blk[i] * blk[i];
|
|
1385
|
+
if (a > st->lm_post_pk) st->lm_post_pk = a;
|
|
1386
|
+
}
|
|
1387
|
+
if (++st->lm_n >= 100) {
|
|
1388
|
+
const double fs2 = 32768.0 * 32768.0, n = 100.0 * GX_NS_BLOCK;
|
|
1389
|
+
atomic_store_explicit(&st->pub_pre_rms, st->lm_pre_acc / n < 1e-3 ? -120.f : (float)(10.0 * log10(st->lm_pre_acc / n / fs2)), memory_order_relaxed);
|
|
1390
|
+
atomic_store_explicit(&st->pub_post_rms, st->lm_post_acc / n < 1e-3 ? -120.f : (float)(10.0 * log10(st->lm_post_acc / n / fs2)), memory_order_relaxed);
|
|
1391
|
+
atomic_store_explicit(&st->pub_pre_pk, st->lm_pre_pk < 0.03f ? -120.f : 20.f * log10f(st->lm_pre_pk / 32768.f), memory_order_relaxed);
|
|
1392
|
+
atomic_store_explicit(&st->pub_post_pk, st->lm_post_pk < 0.03f ? -120.f : 20.f * log10f(st->lm_post_pk / 32768.f), memory_order_relaxed);
|
|
1393
|
+
st->lm_pre_acc = st->lm_post_acc = 0.0;
|
|
1394
|
+
st->lm_pre_pk = st->lm_post_pk = 0.f;
|
|
1395
|
+
st->lm_n = 0;
|
|
1396
|
+
}
|
|
1397
|
+
atomic_store_explicit(&st->pub_mk_db, st->mk_db, memory_order_relaxed);
|
|
1398
|
+
atomic_store_explicit(&st->pub_sl_gain, st->sl_gain_db, memory_order_relaxed);
|
|
1399
|
+
atomic_store_explicit(&st->pub_sl_level, st->sl_have ? st->sl_level_db : -120.f, memory_order_relaxed);
|
|
1400
|
+
|
|
1401
|
+
st->vad = vad;
|
|
1402
|
+
atomic_store_explicit(&st->pub_vad, vad, memory_order_relaxed);
|
|
1403
|
+
atomic_store_explicit(&st->pub_gain_db, st->gain_db, memory_order_relaxed);
|
|
1404
|
+
atomic_store_explicit(&st->pub_pregain_db, st->pg_gain_db, memory_order_relaxed);
|
|
1405
|
+
atomic_store_explicit(&st->pub_pg_applied_db, st->pg_applied_db, memory_order_relaxed);
|
|
1406
|
+
atomic_store_explicit(&st->pub_agc_applied_db, !agc_on ? 0.f : agc_order == GX_NS_AGC_AFTER_NS ? music_db(st, st->gain_db, music_cap(st, st->pg_applied_db)) : st->gain_db, memory_order_relaxed);
|
|
1407
|
+
atomic_store_explicit(&st->pub_talk, st->talk_now || st->talk_hang > 0, memory_order_relaxed);
|
|
1408
|
+
atomic_store_explicit(&st->pub_active, st->cur, memory_order_relaxed);
|
|
1409
|
+
atomic_store_explicit(&st->pub_xfade, st->cur != st->next, memory_order_relaxed);
|
|
1410
|
+
atomic_store_explicit(&st->pub_reason, (int)st->reason, memory_order_relaxed);
|
|
1411
|
+
atomic_fetch_add_explicit(&st->blocks, 1, memory_order_release);
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
static void process_chunk(gx_ns *st, float *x, int n) {
|
|
1415
|
+
rate_ctx *r = st->rc;
|
|
1416
|
+
int got;
|
|
1417
|
+
if (!r->up) {
|
|
1418
|
+
fifo_push(&st->in48, x, n);
|
|
1419
|
+
} else {
|
|
1420
|
+
spx_uint32_t in_len = (spx_uint32_t)n, out_len = (spx_uint32_t)st->scratch48_cap;
|
|
1421
|
+
gx_resampler_process_float(r->up, 0, x, &in_len, st->scratch_in48, &out_len);
|
|
1422
|
+
fifo_push(&st->in48, st->scratch_in48, (int)out_len);
|
|
1423
|
+
}
|
|
1424
|
+
while (st->in48.count >= GX_NS_BLOCK) {
|
|
1425
|
+
fifo_pop(&st->in48, st->blk, GX_NS_BLOCK);
|
|
1426
|
+
process_block(st, st->blk);
|
|
1427
|
+
if (!r->up) {
|
|
1428
|
+
fifo_push(&st->out_r, st->blk, GX_NS_BLOCK);
|
|
1429
|
+
} else {
|
|
1430
|
+
spx_uint32_t in_len = GX_NS_BLOCK, out_len = (spx_uint32_t)st->scratch_r_cap;
|
|
1431
|
+
gx_resampler_process_float(r->down, 0, st->blk, &in_len, st->scratch_out_r, &out_len);
|
|
1432
|
+
fifo_push(&st->out_r, st->scratch_out_r, (int)out_len);
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
got = fifo_pop(&st->out_r, x, n);
|
|
1436
|
+
if (got < n) {
|
|
1437
|
+
memset(x + got, 0, sizeof(float) * (size_t)(n - got));
|
|
1438
|
+
atomic_fetch_add_explicit(&st->underflows, 1, memory_order_relaxed);
|
|
1439
|
+
st->need_reprime = 1; /* next call re-primes with a larger prefill and fades in */
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
int gx_ns_process(gx_ns *st, float *samples, int n, int sample_rate) {
|
|
1444
|
+
uint64_t t0 = gx_timer_now_ns();
|
|
1445
|
+
rate_ctx *r;
|
|
1446
|
+
uint32_t sr;
|
|
1447
|
+
if (n <= 0) return 0;
|
|
1448
|
+
r = (st->rc && st->rc->rate == sample_rate) ? st->rc : find_rate(st, sample_rate);
|
|
1449
|
+
if (!r) return -1;
|
|
1450
|
+
sr = atomic_load_explicit(&st->req_stats_reset, memory_order_relaxed);
|
|
1451
|
+
if (sr != st->stats_reset_seen) {
|
|
1452
|
+
st->stats_reset_seen = sr;
|
|
1453
|
+
gx_timer_clear(&st->timer);
|
|
1454
|
+
atomic_store_explicit(&st->underflows, 0, memory_order_relaxed);
|
|
1455
|
+
atomic_store_explicit(&st->ext_errors, 0, memory_order_relaxed);
|
|
1456
|
+
atomic_store_explicit(&st->switches, 0, memory_order_relaxed);
|
|
1457
|
+
}
|
|
1458
|
+
if (r != st->rc || st->need_reprime) {
|
|
1459
|
+
int underflowed = (r == st->rc);
|
|
1460
|
+
stream_reset(st, r, n, atomic_load_explicit(&st->ext_ptr, memory_order_acquire));
|
|
1461
|
+
if (underflowed && !r->up && st->prefill == 0) {
|
|
1462
|
+
/* frame size changed to a non-multiple of 10 ms at 48 kHz */
|
|
1463
|
+
fifo_push(&st->out_r, NULL, GX_NS_BLOCK);
|
|
1464
|
+
st->prefill = GX_NS_BLOCK;
|
|
1465
|
+
atomic_store_explicit(&st->pub_latency, latency_for(st, r, st->prefill), memory_order_relaxed);
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
while (n > 0) {
|
|
1469
|
+
int c = n < st->cfg.max_frame_samples ? n : st->cfg.max_frame_samples;
|
|
1470
|
+
process_chunk(st, samples, c);
|
|
1471
|
+
samples += c;
|
|
1472
|
+
n -= c;
|
|
1473
|
+
}
|
|
1474
|
+
gx_timer_add(&st->timer, (uint32_t)((gx_timer_now_ns() - t0) / 1000u));
|
|
1475
|
+
return 0;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
void gx_ns_get_stats(gx_ns *st, gx_ns_stats *o) {
|
|
1479
|
+
memset(o, 0, sizeof(*o));
|
|
1480
|
+
o->frames = atomic_load_explicit(&st->timer.count, memory_order_relaxed);
|
|
1481
|
+
o->p50_us = gx_timer_percentile(&st->timer, 0.50);
|
|
1482
|
+
o->p95_us = gx_timer_percentile(&st->timer, 0.95);
|
|
1483
|
+
o->p99_us = gx_timer_percentile(&st->timer, 0.99);
|
|
1484
|
+
o->max_us = atomic_load_explicit(&st->timer.max_us, memory_order_relaxed);
|
|
1485
|
+
o->over_budget = atomic_load_explicit(&st->timer.over, memory_order_relaxed);
|
|
1486
|
+
o->consecutive_over = atomic_load_explicit(&st->timer.consecutive, memory_order_relaxed);
|
|
1487
|
+
o->underflows = atomic_load_explicit(&st->underflows, memory_order_relaxed);
|
|
1488
|
+
o->ext_errors = atomic_load_explicit(&st->ext_errors, memory_order_relaxed);
|
|
1489
|
+
o->engine_switches = atomic_load_explicit(&st->switches, memory_order_relaxed);
|
|
1490
|
+
o->ext_p95_us = atomic_load_explicit(&st->pub_ext_p95, memory_order_relaxed);
|
|
1491
|
+
o->ext_slow = atomic_load_explicit(&st->pub_ext_slow, memory_order_relaxed);
|
|
1492
|
+
o->ext_suppress = atomic_load_explicit(&st->pub_ext_suppress, memory_order_relaxed);
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
void gx_ns_get_status(gx_ns *st, gx_ns_status *o) {
|
|
1496
|
+
memset(o, 0, sizeof(*o));
|
|
1497
|
+
o->requested = (gx_ns_engine)atomic_load(&st->req_engine);
|
|
1498
|
+
o->active = src_engine(atomic_load_explicit(&st->pub_active, memory_order_relaxed));
|
|
1499
|
+
o->crossfading = atomic_load_explicit(&st->pub_xfade, memory_order_relaxed);
|
|
1500
|
+
o->reason = (gx_ns_reason)atomic_load_explicit(&st->pub_reason, memory_order_relaxed);
|
|
1501
|
+
o->rate = atomic_load_explicit(&st->pub_rate, memory_order_relaxed);
|
|
1502
|
+
o->latency_samples = atomic_load_explicit(&st->pub_latency, memory_order_relaxed);
|
|
1503
|
+
o->vad = atomic_load_explicit(&st->pub_vad, memory_order_relaxed);
|
|
1504
|
+
o->agc_gain_db = atomic_load_explicit(&st->pub_gain_db, memory_order_relaxed);
|
|
1505
|
+
o->pregain_db = atomic_load_explicit(&st->pub_pregain_db, memory_order_relaxed);
|
|
1506
|
+
o->pregain_applied_db = atomic_load_explicit(&st->pub_pg_applied_db, memory_order_relaxed);
|
|
1507
|
+
o->agc_applied_db = atomic_load_explicit(&st->pub_agc_applied_db, memory_order_relaxed);
|
|
1508
|
+
o->pause_att_db = atomic_load_explicit(&st->pub_pause_att_db, memory_order_relaxed);
|
|
1509
|
+
o->talk = atomic_load_explicit(&st->pub_talk, memory_order_relaxed);
|
|
1510
|
+
o->music_detected = atomic_load_explicit(&st->pub_music, memory_order_relaxed);
|
|
1511
|
+
o->pre_rms_db = atomic_load_explicit(&st->pub_pre_rms, memory_order_relaxed);
|
|
1512
|
+
o->pre_peak_db = atomic_load_explicit(&st->pub_pre_pk, memory_order_relaxed);
|
|
1513
|
+
o->post_rms_db = atomic_load_explicit(&st->pub_post_rms, memory_order_relaxed);
|
|
1514
|
+
o->post_peak_db = atomic_load_explicit(&st->pub_post_pk, memory_order_relaxed);
|
|
1515
|
+
o->makeup_db = atomic_load_explicit(&st->pub_mk_db, memory_order_relaxed);
|
|
1516
|
+
o->singer_gain_db = atomic_load_explicit(&st->pub_sl_gain, memory_order_relaxed);
|
|
1517
|
+
o->singer_level_db = atomic_load_explicit(&st->pub_sl_level, memory_order_relaxed);
|
|
1518
|
+
}
|