Skip to content

feat(audio): duck background music under narration - #1806

Open
simply-Rahul8 wants to merge 2 commits into
harry0703:mainfrom
simply-Rahul8:feat/bgm-duck-under-narration
Open

simply-Rahul8 wants to merge 2 commits into
harry0703:mainfrom
simply-Rahul8:feat/bgm-duck-under-narration

Conversation

@simply-Rahul8

@simply-Rahul8 simply-Rahul8 commented Nov 14, 2026 •

Copy link
Copy Markdown

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-in bgm_duck option (request field and --bgm-duck CLI 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-short pipeline 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:

  • Envelope drift on long narration. The incomplete 10 ms window at the end of each 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).
  • Opposite-phase stereo. The detector averaged the channels before measuring RMS, so equal and opposite channels cancelled to silence. It now averages the per-channel energy.

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.
  • The speech threshold is relative to the narration's own loudness (90th percentile), so it does not depend on voice_volume or the TTS provider.
  • The level is smoothed with a fast attack and slow release (per-step factors 0.5 / 0.04, not seconds), and mapped to a gain of 1.0 in pauses and 0.3 under speech.
  • _duck_under_narration() applies that gain to the BGM clip with Clip.transform. It handles array and scalar t and mono/stereo frames, and returns full level past the end of the narration.
  • It is applied right after the existing BGM effects (loop, volume, final 3 s fade-out), inside the existing try/except, so a failure keeps today's behaviour. Both the locally resolved BGM and the provider-supplied bgm_file_override paths go through the same line.

Scope

  • Added to VideoParams only. SubtitleRequest / AudioRequest don't mix music, and the video_project.py render path is untouched.
  • No new dependency (numpy and moviepy are already used here).
  • Not included, to keep this reviewable: a WebUI checkbox (would touch Main.py and 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/skill and compileall: 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.
  • Covered: default off, gain dips under speech and recovers in pauses, independence from narration loudness, silent narration, mono and stereo, opposite-phase stereo, a 120 s track, scalar-t frames, music longer than narration, wiring in generate_video, and the CLI flag.
  • Real render through 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

@simply-Rahul8 simply-Rahul8 changed the title feat(audio): duck background music under narration feat: add contrib/script-to-short pipeline and opt-in BGM ducking Nov 14, 2026
@harry0703

Copy link
Copy Markdown
Owner

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. _narration_duck_gain() pads every chunk independently to a 10 ms boundary. MoviePy's iter_chunks() divides the samples into chunks whose lengths aren't necessarily multiples of that window, so the padding accumulates when the results are concatenated.

With a 24 kHz track alternating one second of speech and one second of silence, I measured:

  • 60 seconds: 6,039 gain steps instead of 6,000, adding 0.39 seconds.
  • 120 seconds: 12,100 instead of 12,000, adding 1 second.

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 bgm_duck=True; I didn't find a change to the default rendering behavior.

Please also split the ducking change and contrib/script-to-short into separate PRs, as they are independent features. That will let us review the optional pipeline's installation, assets and publishing flow separately. I ran its self-tests and produced a valid 1080×1920, 4.10-second local render using a synthetic character, previously generated narration and fixture word timings. Fresh Edge synthesis failed on the available network, including a retry through the local proxy, so that wasn't a successful live TTS end-to-end run. Rhubarb and OAuth publishing were not exercised.

I'll keep this unmerged pending the audio fixes and split. Thanks again for the work and the detailed setup notes.

simply-Rahul8 and others added 2 commits November 18, 2026 16:31
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>
@simply-Rahul8
simply-Rahul8 force-pushed the feat/bgm-duck-under-narration branch from 47291b2 to ab240db Compare November 18, 2026 11:04
@simply-Rahul8 simply-Rahul8 changed the title feat: add contrib/script-to-short pipeline and opt-in BGM ducking feat(audio): duck background music under narration Nov 18, 2026
@simply-Rahul8

Copy link
Copy Markdown
Author

Thanks for the careful testing and for measuring the drift.

  • Envelope drift: fixed. The incomplete 10 ms window is now carried into the next chunk and only the final remainder is padded. With your 24 kHz alternating track the gain array has 6000 steps for 60 s and 12000 for 120 s. New test: a 120 s / 24 kHz track checking the envelope length and that speech boundaries line up at about 55 s and 111 s.
  • Opposite-phase stereo: fixed. The detector now averages per-channel energy instead of the mean waveform. New test with equal, opposite-phase channels. Both new tests fail on the previous code and pass now.
  • Split: done. This PR is ducking only (branch force-pushed, also rebased onto current main). The pipeline is now feat(contrib): add script-to-short talking-character Shorts pipeline #1814, with its latest changes from my public repo, including an optional Chatterbox voice engine and an Instagram video_url route. I called out the parts that need the closest look in that PR's description.

Locally the related suites pass (275 passed, 1 skipped) and ruff is clean. CI is running on the new head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants