Repository navigation
feat(audio): duck background music under narration - #1806
simply-Rahul8 wants to merge 2 commits into
Conversation
|
Thanks for the contribution. I tested the ducking change with actual encoded videos: leaving it off produced exactly the same decoded audio as main, and turning it on reduced the music to about 30% under speech and let it recover in the pause. The 191 related tests/self-tests also passed. I found two audio cases that need fixing: The gain envelope drifts over longer narration. With a 24 kHz track alternating one second of speech and one second of silence, I measured:
At 54.15 seconds in the first example, speech has already resumed, but the music gain is still about 0.975 instead of dropping toward 0.3. Later pauses are shifted too. Please carry incomplete windows across chunks and only pad the final remainder, with long-track tests that check both envelope length and speech boundaries. A stereo narration can cancel out in the detector. Averaging the channels before measuring RMS makes equal, opposite-phase channels look silent. I tested a stereo track with RMS about 0.283 in each channel; its gain stayed at 1 throughout, so there was no ducking. Please measure multichannel energy without cancelling the waveforms first, and add a stereo regression. This is an edge case for imported audio, not the usual mono TTS output. Both issues require Please also split the ducking change and I'll keep this unmerged pending the audio fixes and split. Thanks again for the work and the detailed setup notes. |
Background music is mixed at a flat bgm_volume, so it either competes with the voice or sits too quiet in the pauses. Add an opt-in bgm_duck flag (request field and --bgm-duck) that lowers the music while the narration speaks and brings it back in pauses. The gain envelope is computed from the narration track in 10 ms steps (chunked, so memory stays flat for long narrations), smoothed with a fast attack and slow release, and applied to the BGM clip before the existing final fade-out. The speech threshold is relative to the narration's own loudness, so it does not depend on voice_volume or the TTS provider. Both the resolved and provider-supplied BGM paths are covered. Off by default, so existing requests render exactly as before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…stereo - Carry the incomplete 10 ms window across iter_chunks() chunks and pad only the final remainder. Padding each chunk drifted the envelope (about 0.4 s per minute at 24 kHz), so ducking started and ended late on long narration. - Measure energy per channel instead of the mean waveform, so channels in opposite phase no longer cancel to silence and disable ducking. Adds a 120 s / 24 kHz regression (envelope length and speech boundaries) and an opposite-phase stereo regression. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
47291b2 to
ab240db
Compare
|
Thanks for the careful testing and for measuring the drift.
Locally the related suites pass (275 passed, 1 skipped) and ruff is clean. CI is running on the new head. |
Summary
Background music is mixed at a flat
bgm_volume, so it either competes with the voice or sits too quiet in the pauses. This adds an opt-inbgm_duckoption (request field and--bgm-duckCLI flag) that lowers the music while the narration speaks and brings it back in pauses. Off by default: existing requests render exactly as before.This PR is only the ducking change. The
contrib/script-to-shortpipeline that used to be in this PR now lives in its own PR (#1814), as requested in review.Update after review
Two fixes, in a second commit, for the cases found in review:
iter_chunks()chunk was padded per chunk, so the gain array grew longer than the audio. The incomplete window is now carried into the next chunk and only the final remainder is padded. For a 24 kHz track alternating 1 s of speech and 1 s of silence, the gain array now has exactly 6000 steps for 60 s and 12000 for 120 s (it was 6039 and 12100).New regression tests: a 120 s / 24 kHz alternating track (envelope length, plus speech boundaries at about 55 s and 111 s) and an opposite-phase stereo track. Both fail on the previous code and pass now. The branch is also rebased onto current
main(92e0f9a).How it works
_narration_duck_gain()(app/services/video.py) reads the narration track in chunks (iter_chunks, so a long narration is never decoded into memory at once), takes RMS per 10 ms step, and turns it into a speech level.voice_volumeor the TTS provider._duck_under_narration()applies that gain to the BGM clip withClip.transform. It handles array and scalartand mono/stereo frames, and returns full level past the end of the narration.try/except, so a failure keeps today's behaviour. Both the locally resolved BGM and the provider-suppliedbgm_file_overridepaths go through the same line.Scope
VideoParamsonly.SubtitleRequest/AudioRequestdon't mix music, and thevideo_project.pyrender path is untouched.Main.pyand every i18n file). Happy to follow up if you want it.Verification
Run in an environment built with
uv sync --frozen(moviepy 2.2.1):ruff check app cli.py main.py webui test docs/skillandcompileall: pass.test_bgm_duck.py(now 9 tests, no ffmpeg needed),test_video.py,test_cli.py,test_bgm.py,test_bgm_final_fade.py,test_finite_audio_parameters.py: 275 passed, 1 skipped.tframes, music longer than narration, wiring ingenerate_video, and the CLI flag.generate_video(8 s clip, 880 Hz music, music band isolated with a high-pass filter), measured on the first version of this PR: speech windows 0.037 off vs 0.013 on, pause window 0.036 in both. I did not repeat the render after the review fixes; the fixes are covered by the unit tests above.I did not get the full
test/suite to run locally (no Redis here), so CI is the full-suite evidence.🤖 Generated with Claude Code