Skip to content

Audio: Fade Out BGM falls from where it is, not most of the way at once (runtime 20261130.3) - #103

Open
Xehanort88 wants to merge 3 commits into
Psychronic-Games:mainfrom
Xehanort88:pr/hold-gain
Open

Xehanort88 wants to merge 3 commits into
Psychronic-Games:mainfrom
Xehanort88:pr/hold-gain

Conversation

@Xehanort88

Copy link
Copy Markdown
Contributor

Stacked on #102 (palette pools and palette layers on the context clock). This branch holds those two commits plus one of its own, Audio: Fade Out BGM falls from where it is (runtime 20261130.3). Please review or merge that one first; once it is in, this pull request shows the one commit alone.

It depends on it for its tests, not its fix: the one-line change to WebAudio._holdGain would apply to 0.99.8 by itself, but webaudio-fade-render.test.cjs renders through the offline audio model, and the model's cancelAndHoldAtTime only follows the spec (inserting no event when no ramp is under way, which is the case this bug lives in) since the palette change.


The problem

Fade Out BGM (and BGS, ME, and the master fade a mobile page makes when hidden) did not fade from
the level the music was at. It dropped at once and then faded from there. The deeper into the
track, the bigger the drop. Measured on Chromium 157 (NW.js 0.120.0), the engine's own fades:

Case Level on the frame before the fade Level on the fade's first frame
Fade Out BGM (0.2 s), 0.3 s into a track 1.000 0.399
Master fade-out (0.25 s), 0.3 s in 1.000 0.453
fadeOut(0.05), 12,800 frames in 1.000 0.158

A 3 s Fadeout BGM a minute into a track starts at about 3 / 63 ≈ 5 % of its level: close to a cut.

A fade-out during a fade-in was fine, which is probably why nobody caught it: the case that
was measured and fixed earlier (the comment above _holdGain) is exactly the one that works.

Why

WebAudio._holdGain called cancelAndHoldAtTime(now), and the fade then wrote
linearRampToValueAtTime(0, now + d). Per the Web Audio spec, and in Chromium,
cancelAndHoldAtTime inserts an event only when a ramp is under way at that time (the ramp is
cut to end there). When the param is simply holding a value, it inserts nothing. A linear ramp
starts from the previous event, which for a track's fade stage is the setValueAtTime(1, <creation time>) made when the track started. So the ramp ran from the track's start to the end
of the fade, and at "now" it was already most of the way down.

The plan player's bus was not affected: its fade stage has no earlier event, and a ramp with no
earlier event starts when it is written.

What changed

runtime/reactor_core_io.js, WebAudio._holdGain only: it reads gain.value first and, after
cancelAndHoldAtTime (or cancelScheduledValues where that is missing), always writes
setValueAtTime(held, now). The fallback branch already did this; now both branches do. Every
caller benefits at once:

  • WebAudio.prototype.fadeOut (Fade Out BGM / BGS / ME, AudioManager.fadeOutBgm, the frame-path
    palette hand-overs, a track entry's fadeOut);
  • WebAudio._fadeOut (master, on a hidden mobile page);
  • AudioManager._fadeOutBgmPlan (already right; still right).

Fades in (_restartGain) already set their value explicitly and are unchanged.

Verification

Rendered (editor/tests/webaudio-fade-render.test.cjs, 3 tests, all pass)

The real AudioManager and WebAudio play a 1 s track at 48 kHz over the model
OfflineAudioContext, whose cancelAndHoldAtTime follows the spec since the palette change. Each
fade's output is held frame by frame to what the fade should sound like, at 1e-5. Each test also
checks that there is no step at the fade's first frame and silence after it.

  1. Fade Out BGM part-way through a track.
  2. Fade Out BGM during a fade-in.
  3. The master fade-out.

With the old _holdGain put back, tests 1 and 3 fail (worst 0.317 and 0.290 at the fade's
start). Test 2 passes either way, as explained above.

On Chromium's own OfflineAudioContext (measured)

tests/smoke/fade-render-offline, run on this branch with the NW.js 0.120.0 SDK, Chromium 157.0.8103.39. "Old" is the same smoke run against the runtime of the palette branch, before this fix:

Scenario Fixed: worst Fixed: level before → at start Old: worst Old: level before → at start
Fade Out BGM mid-track 5.1e-7 1.0000 → 1.0000 3.2e-1 1.0000 → 0.3989
During a fade-in 2.7e-5 0.4999 → 0.4999 2.4e-7 0.4999 → 0.5000
Master fade-out 4.2e-7 1.0000 → 1.0000 2.9e-1 1.0000 → 0.4534

One honest cost. During a running ramp, the anchor written is gain.value. Chromium reports
it very slightly behind the ramp, so a fade-out started mid fade-in now begins with a step of
about 3e-5 (−90 dB). Before, it was exact there. It is far below hearing, and it is the price of
anchoring at all: Web Audio offers no way to ask whether a ramp is under way.

Tests on this branch (base 0.99.8, with the palette commits below it)

  • Every bgm-*, audio-* and webaudio-* file (16 files, 216 tests), webaudio-fade-render and webaudio-fade-automation included: all pass.
  • i18n, user-visible-i18n-routing, i18n-routing-audit (40) and runtime-manifest, audit-shell-sync-runtime, project-manager, which read the revision (35): all pass. sync-runtime.cjs --check reports template/Demo/js in step with runtime/.
  • webaudio-fade-automation.test.cjs: two assertions spelled out the old call order, [cancelAndHold, linearRamp], which is the bug. They now expect [cancelAndHold, setValueAtTime(held), linearRamp]. The other four are unchanged.
  • Whole suite on 0.99.8 plus the three commits of this stack (Windows, Git Bash, all 1,046 test files, run with npm run test:changed in four slices): 25 files fail, none of them an audio test. 23 are on the known Windows list as of 0.99.7: symlinks refused without Developer Mode, no zip, GNU tar reading C: as a host, Linux-only AppImage tooling, POSIX fixture paths (database-sections, audit-3d-editor-workshop, media-surface-panel, rgss-port-screenshots, vehicles-starting-position, tactics-card-convert), and the RTP tests that read APPDATA. The other two were checked on plain 0.99.8: distribution-staging fails there in the same way, and character-helper-threads failed only under 16 workers and passes when run alone on both trees. character-3d-creatures (a time budget) failed once under load and passed on its rerun.

Limits

  • Not heard, not run in the game. Offline renders on the model and on Chromium only. The
    quickest check by ear: in a playtest, let a BGM play for a minute and use Fadeout BGM (3 s). It
    should fall smoothly from full level instead of dipping at once.
  • A plugin that writes its own automation to a track's _fadeGainNode and relies on the old order
    would see the extra setValueAtTime. None of the plugins this was checked against touches
    _holdGain or cancelAndHoldAtTime (searched).

Changelog text for the next release

No changelog/ file: docs.test.cjs ties the newest entry to the package version, and 0.99.8 is released.

Fade Out BGM fades from where the music is. A fade-out (Fadeout BGM/BGS, and the master
fade on a hidden mobile page) used to drop most of the way at once on a track that had been
playing for a while, then fade from there. It now falls smoothly from the current level.


🤖 Generated with Claude Code

Xehanort88 and others added 3 commits November 30, 2026 07:30
… may repeat (runtime 20261130.1)

Adaptive scores overlap their segments by different amounts (a 28.4s file
holding 24s of music and a 4.4s tail beside segments with 4.8s and one-shots
with none), draw with weights, and let some layers repeat. A palette had one
fade-out for every track, drew evenly and never repeated, so such a score could
only be approximated by padding the audio files with silence.

- tail on a pool track: seconds before its end that the next draw starts, the
  track fading out under it; overrides the palette fade-out for that hand-over,
  0 plays it to its end. AudioManager._bgmSequenceLayerFade decides
  _bgmSequenceLayerIsEnding, the retire fade and a single-shot advance.
- weight on a pool track or silence: its share of a Random draw (0 never
  drawn; all zero falls back to an even draw).
- repeats on a layer: a Random draw may pick the entry just played.

All three are optional and written only when they say something; a sequence
without them plays as before, and an older runtime ignores them. The editor
gets Tail (s) and Draw weight on pool rows and Allow repeats on a layer, with
the six phrases in all 17 locales (machine-assisted first pass). "Draw weight",
not "Weight": the catalog already holds "Weight" as body weight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A palette's layers used to start each track when its file had loaded and
hand over from a once-per-frame check, so stems drifted apart and joins
landed wherever a frame fell. Now, with the scheduled WebAudio API:

- every layer's first draw is decoded before any sounds, and all start on
  one context frame;
- each next draw is decoded while the one before it plays and committed
  with playAt on the frame its predecessor hands over (end less its tail
  or the palette fade-out; exactly at the end for tail 0, gapless);
- the outgoing fade and the palette's own end after `duration` are written
  on the clock (fadeOutAt + stopAt); nothing is drawn past the end frame;
- the frame loop only keeps the books: promotions, retiring tails, a
  single-shot's end, and starting what follows the palette.

Weights, repeats, order, once-entries, trims, tails, ME duck, the volume
option, Fade In/Out BGM and stop all behave as before. A WebAudio without
the scheduled API (a replacement class, a stub, no context) keeps the old
frame path, which the existing bgm-sequence-runtime tests exercise.

WebAudio gains fadeInAt and fadeOutAt. fadeOutAt always anchors its level
with setValueAtTime: Chromium's cancelAndHoldAtTime inserts no event when
no ramp is under way, so a ramp written next would start from the event
before (measured on NW.js 0.120 / Chromium 157).

The offline audio model's cancelAndHoldAtTime now follows the spec the
same way, and a ramp with no earlier event starts when it is written.
New tests: bgm-palette-clock (19), rendered at 48 kHz and held frame by
frame to the palette rules; the same scenarios on Chromium's own
OfflineAudioContext (tests/smoke/palette-clock-offline) come out within
5.5e-7, a one-frame shift 0.26-0.55.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ce (runtime 20261130.3)

WebAudio._holdGain called cancelAndHoldAtTime(now) and the fade then wrote
linearRampToValueAtTime(0, now + d). Per spec, and in Chromium,
cancelAndHoldAtTime inserts an event only when a ramp is under way; on a
param merely holding a value it adds nothing, and the ramp starts from the
event before -- for a track's fade stage, the setValueAtTime(1) made when
it started. Measured on NW.js 0.120 / Chromium 157: Fade Out BGM 0.3 s into
a track dropped from 1.0 to 0.399 on its first frame; the master fade-out
to 0.453. A 3 s fade a minute in started at about 5%.

The hold now always anchors the level it read with setValueAtTime, as its
fallback branch already did. Every fade-out through it is fixed at once:
WebAudio.prototype.fadeOut (BGM/BGS/ME), the master _fadeOut, the plan bus.

webaudio-fade-automation: two assertions spelled out the old call order;
they now expect the anchor. New webaudio-fade-render (3 tests) renders the
fades through the real AudioManager over the spec-correct offline model;
with the old hold, two of them fail. tests/smoke/fade-render-offline runs
them on Chromium's own OfflineAudioContext: 5e-7 fixed, 0.32 before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant