Repository navigation
Audio: scheduled playback on the context clock, and a BGM plan player (runtime 20261129.3) - #100
Merged
Psychronic-Games merged 1 commit intoNov 30, 2026
Conversation
… (runtime 20261129.3) Music assembled from many short files has to join on the frame: sections, layers and phrases whose starts, release tails and gains a music tool has already worked out. WebAudio could only start a sound now, and everything after that ran on the frame loop, so stems drifted and joins landed wherever a frame fell. WebAudio (reactor_core_io.js): - scheduleVoice: one run through the decoded file into a node of the caller's, at a context time, from an offset, until a cut, with a loop region of its own and pitch; decoder chunks laid end to end. Any number per object, and play()/stop() state is left alone. - scheduleClip: a voice positioned in frames with its envelope written once on the clock: fade-in, a release tail (linear, sqrt, sin, square, none) cut short rather than squeezed, a gain per source channel with gain automation, stereo-pair folding. It can be entered part-way, the source position, fade and tail carrying on from that frame. - playAt / stopAt: play() and stop() on the clock through the object's own volume, fade and pan stages, for the sequence code to move onto. - frameTime / frameClock place an event a millionth of a frame before its frame, so floating point cannot push it to the next one. - isDecoded / addDecodeListener: the whole file, not a first part. A WAV read by the PCM fallback now records its decoded size too. AudioManager (reactor_managers.js): playBgmPlan(folder, options) plays audio/<folder>/plan.json (format "reactor/music-plan", version 1, read by its shape whatever its format names). Every file decodes first; then each update commits what starts within two seconds, from Scene_Base.update and a timer of its own, so the frame rate has no say. A loop plays to its end, then replays its window forever, clips entering part-way and everything cut at the end. Beside the BGM slot like a sequence: stop, fade-out, fade-in, the ME duck, the BGM volume option, Save/Replay BGM and save files (which restart it from its first frame). bgmPlanInfo reports decoded lengths and memory. Palette playback is unchanged. Tests: bgm-plan-player.test.cjs renders ten scenarios at 48 kHz over an OfflineAudioContext model and holds them to the mixing rules frame by frame (synthetic fixtures only); tests/smoke/plan-player-offline runs the same scenarios on Chromium's own OfflineAudioContext (NW.js 0.120, Chromium 157): every one within 4.5e-7. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
Adaptive game music is assembled from many short files. Sections, layers and phrases are chosen and joined while the game runs, and every join has to land on the frame. A music tool can work all of that out ahead of time and export it as a plan: a list of clips, each a stretch of one file with a start, an offset, a length, a release tail and gains.
Reactor could not play one.
WebAudio.play()starts a sound now, and everything after that is decided on the frame loop. That is how BGM sequences work: palette layers start when their file has loaded and hand over from a once-per-frame check, so stems drift and joins land wherever a frame falls.What changed
runtime/reactor_core_io.js: scheduled playback inWebAudioThis is new code beside the old.
play,_startPlayingand_startSourceNodeare unchanged, and a test asserts that they do not use the new path.scheduleVoice(spec): one run through the decoded file into a node of the caller's. It starts at a context time, from an offset, and is cut at a given time. It can loop a region of the caller's (overriding the file's tags) and take a pitch. Several voices can play from one object at once, and itsplay()/stop()state is left alone. A single buffer loops natively; decoder chunks are laid end to end. A start that has already passed enters as far in as the time gone, so a late voice stays where it should be.scheduleClip(spec): a voice positioned in frames, with its envelope written once on the clock:logicalthat reaches silencetail_full − 1frames later, cut short and never squeezed, in five shapes (linear, sqrt, sin, square, none);volume;A clip can be entered part-way at clip frame k. The source position (wrapped at the file's loop), the fade-in, the tail and the gain points all carry on from k. Each stage owns one param.
playAt(when, offset, {loop, duration})andstopAt(time):play()andstop()on the clock, through the object's own volume, fade and pan stages, with its stop listeners. They exist so the palette code can move onto the clock in a later change.frameTimeandframeClock: Web Audio starts an event at the first frame at or after its time.frame / ratecan land a hair past the frame in floating point, which would push the event a whole frame late, so times are placed 2⁻²⁰ of a frame before their frame.isDecoded()andaddDecodeListener(): "the whole file is decoded", whichisReady()is not;isReady()turns true on the first streamed part. The PCM-WAV fallback now records its decoded size, as the native path already did.runtime/reactor_managers.js:AudioManager.playBgmPlan(folder, { volume, fadeIn, data })This plays
audio/<folder>/plan.json. The format id isreactor/music-plan, version 1, and every position is in frames atrate:formatstring still plays if its shape checks out, with a console warning. The check covers version and rate, paths that stay inside the plan's folder, file rate equal to plan rate,[start, end)loops, and clips that name real files. A refused or missingplan.jsongoes throughcheckErrorslike a missing track.BGM_PLAN_LOOKAHEAD(2 s) to the audio clock. Updates come fromScene_Base.updateand from a 250 ms timer, because scenes stop updating while the window is unfocused.loop.end, everything cut there. Then it replays[loop.start, loop.end)forever. Clips that started earlier enter part-way, and everything is cut atloop.end.bgmPlanInfo()reports the context rate, decoded lengths against the plan'sframes, and decoded bytes in memory.There is no editor UI and there are no new user-visible strings. Palette playback is unchanged.
Verification
editor/tests/bgm-plan-player.test.cjs, 18 tests, all pass. Node has noOfflineAudioContext, so the ten rendering scenarios run at 48 kHz ontests/helpers/offline-audio-model.cjs. That is a frame-by-frame model of the Web Audio subset used: sub-frame starts, a-rate automation includingsetValueCurveAtTimeandcancelAndHoldAtTime, splitters and mergers, a centred panner, andsuspend/resume. Each scenario is held to a sample-by-sample reference mixer at a tolerance of 1e-5, using synthetic fixtures only. The scenarios cover:playAt/stopAt.OfflineAudioContext(tests/smoke/plan-player-offline, NW.js 0.120.0, Chromium 157) came out within 4.5e-7 of the reference, which is float32 rounding.OfflineAudioContextand null-tested against the tool's own reference mix. With pieces decoded the tool's way (16-bit ÷ 32768), the worst residuals are −134 to −154 dBFS, which is float32 rounding.decodeAudioDatathe floor is −97 to −109 dBFS. Chromium decodes 16-bit PCM WAV asymmetrically: negative samples ÷ 32768, positive ÷ 32767. This was measured on a synthetic WAV and is a 0.00026 dB difference on positive half-waves.npm test(Git Bash, Windows): the failing-test names are identical to themainbaseline. Those are the known environmental and POSIX-path failures, plusthree-version, which fails in a worktree whosenode_modulesis a junction. Thebgm-sequence-*,audio-*andwebaudio-*files all pass. On this branch, which is cut fromupstream/main, the audio, docs and i18n test files pass: 218 of 218.Limits
_createGainNode/_createPannerNode(reverb, echo).Changelog entry
This was not added as a version file, because
docs.test.cjsties the newest entry topackage.json's version.🤖 Generated with Claude Code