fix(audio): a frame mixed for the old device is dropped when the new one has another channel count #448
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!448
Loading…
Reference in a new issue
No description provided.
Delete branch "jarynclouatre/Longshot:fix/audio-frame-new-channel-count"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type: fix · Risk: low · Tested on: Linux x86-64 (game-free checks only) · Needs: none
Why
When Windows' default output changes between a stereo device and a 5.1 one (or the first device appears after none), one audio frame mixed for the old device is sent to the new one with the other channel count: 5 ms with the channels in the wrong places and, from stereo to 5.1, stale samples behind it. A click at the switch.
renderer_maininhost/nn_audio.cmixes, gains and mono-mixes each frame forout_channels, thenoutput_framemay open the output again (output_moveon a default change,output_open_defaultwhen no device was open;output_opensetsout_channels) and goes on to copy the same samples with the new count.Found reading
output_frameafter #2190's default-device move (Hookshot).How to reproduce
Seen on: found reading
output_frame; not seen in the game. Game-free checks on Linux x86-64.Settings and switches: defaults.
Steps: While sound plays on Windows, change the default output between a stereo device and a 5.1 one, or make the first device appear after none.
Expected: A frame mixed for the old channel count is dropped if the reopened device has another count; the next frame is mixed for the new device.
Actual: One 5 ms frame has the channels in the wrong places and, from stereo to 5.1, stale samples behind it. A click at the switch.
What changed
An audio frame is dropped if reopening the output changed its channel count after it was mixed. The next frame uses the new count, and a move with the same count keeps its frame.
host/nn_audio.c,output_frame): keeps the channel count its frame was mixed for, and when a reopen in this call changed it, returns without queueing: that frame is dropped and the next one is mixed for the new device. A move that keeps the count still queues its frame. The WAV dump at the top is unchanged (a dump keeps one layout).tests/audio_output_errors_test.candtests/test_audio_output_errors.py: three new cases over the real output section.upmix: stereo fallback, then the default moves to a device that takes 5.1; nothing of the stereo frame reaches a block (a sentinel past its end never appears), the next frame is queued with 6 channels.downmix: the reverse, 5.1 to stereo.retry: no device at the start, then one that takes 5.1. Thedefaultcase now also checks that a stereo-to-stereo move keeps its frame.Measurements
Verdict: the three new channel-count cases fail on main and pass here; the suite has no new failures.
Hardware: none used (game-free checks on a Linux x86-64 PC, gcc 15.2, Python 3.14)
Scene: none, the game was not run
Method: 2026-11-14; branch
fix/audio-frame-new-channel-count.python3 -I -B tests/run_all.py, two runs each, interleaved (main, the other branch, PR, twice). The new cases also run against main'shost/nn_audio.c(--source).How to reproduce: the commands above in a checkout of each commit
5e072fc84093570ftests/run_all.py(run 1, run 2)test_bake_portability.py,test_menu_keys.py,test_module_ids.py,test_pad_layout.pytests/test_audio_output_errors.pynew casesupmix,downmix,retryfail (the old frame is copied with the new count)test_bake_portability.pyfails here for a missing Python module (zstandard).test_menu_keys.pydoesn't build with g++ andtest_pad_layout.py's fixture lackshost/hold_toggle.h, both on main since #137 (#331 and #338 fix them).test_module_ids.pylooks for a source marker that main no longer has.Flags and settings
None.
Testing
Tested: the game-free suite on a Linux PC, as in Measurements; the test compiles the changed output section with gcc and runs it with waveOut faked.
host/nn_audio.ccompiles (-fsyntax-only -Wall -Wextra) with llvm-mingw 20261104's x86_64-w64-mingw32-clang with the same 5 warnings as main's.Not tested: a real device change in the game on Windows (a stereo headset and 5.1 speakers switched as Windows' default while sound plays).
Must be tested: None.
Risks
What could break: The output frame at a device change with another channel count.
Turning it off: Reverting the commit puts the copy back.
Compatibility: At a switch between channel counts one 5 ms frame is now silent instead of garbled.
Checklist
Everyone
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
mainand passing here.tests/that reads changed source updated in this PR, not weakened.WIP: fix(audio): a frame mixed for the old device is dropped when the new one has another channel countto fix(audio): a frame mixed for the old device is dropped when the new one has another channel countCode review only; nothing was run.
renderer_mainrenders each frame forout_channels(render_frame,audio_apply_gain,audio_mono, thenoutput_frame), all on the one mixer thread.output_framecan reopen the device (output_move,output_open_default), andoutput_openrewritesout_channels. The old code then copiedFRAME * 2 * out_channelsbytes from a buffer mixed for the old count. Takingmixedat entry and returning when the count changed fixes that. Points I checked:out_channels,woutand the fill state are only touched from the mixer thread, so the early return needs no lock.memcpyand the header handling, so none of them see a mismatched count.output_openzeroesfill_blockandfill_framesand sizeshdr[].dwBufferLengthfor the new count, so the next frame starts clean.defaultcase now checks that.woutNULL, the existingif (!wout) returnstill comes first. The stereo fallback inoutput_opencan change the count while no device is open, and that path returns as before.out_channelsonly for its own fixed layout, so it is unchanged.upmix,downmixandretrycases use a sentinel past the mixed frame's end and check that it never reaches a block. That is a real check of the over-read, not only of the count.The description matches the diff. The four failures it lists on both sides are not touched by this change. Nothing here needs a Windows device run, and one silent 5 ms frame at a channel-count switch is the intended result.
Approving.