Skip to content

AudioOut: explicitly downmix six-channel PCM for stereo output - #96

Closed
Jpkovas wants to merge 1 commit into
seedds:mainfrom
Jpkovas:fix/audioout-six-channel-stereo
Closed

Jpkovas wants to merge 1 commit into
seedds:mainfrom
Jpkovas:fix/audioout-six-channel-stereo

Conversation

@Jpkovas

@Jpkovas Jpkovas commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Six-channel AudioOut buffers can lose dialogue and spatial effects on a stereo device. The current AudioUnit receives the six channels directly. A synthetic Apple AudioConverter probe preserves only front left/right with its default six-to-two mapping; center, LFE and rear-only inputs produce zero output, including with an explicit input layout.

This change offers stereo to the output AudioUnit and mixes FL/FR/C/LFE/BL/BR explicitly, using the existing audio renderer's Q15 coefficients. Guest queue accounting remains six-channel. A bounded stack scratch buffer keeps heap allocations out of the real-time callback; sums use int64 and saturate to PCM16.

Validation on public main ebe75c9d5d34486f714678f850f0e74bbab398ac:

  • The new callback test fails against the unchanged runtime and passes with this patch.
  • Eight synthetic cases exercise six isolated channels, positive/negative saturation and a split guest buffer; the test checks complete buffer consumption and release order.
  • The existing AudioOut lifecycle test and 100,000-operation SPSC test pass.
  • The original synthetic probe below reproduces the default device conversion using constant samples; no media or game is needed.
make -C dm/host/tests GAME=0100DCA0064A6000 build/t_audio build/t_audio_spsc
dm/host/tests/build/t_audio
dm/host/tests/build/t_audio_spsc

Separately, a local combined iPhone build containing this correction was tested with Luigi's Mansion 3 1.4.0. The player confirmed that the missing voices/SFX became audible, and callback diagnostics reported no underruns in the observed windows. That device build also contained other unrelated fixes; it is integration evidence, not an isolated device A/B.

The multichannel Vorbis fix discussed in #60 is already in main and addresses a different decoder path. This patch changes direct PCM AudioOut output. Only open-source runtime changes and synthetic regression data are included.

Asset-free AudioConverter probe

Save as coreaudio-channel-probe.c, then run:

clang -framework AudioToolbox coreaudio-channel-probe.c -o channel-probe
./channel-probe
#include <AudioToolbox/AudioToolbox.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  for (int layout=0;layout<2;layout++) {
    AudioStreamBasicDescription src={.mSampleRate=48000,.mFormatID=kAudioFormatLinearPCM,.mFormatFlags=kLinearPCMFormatFlagIsSignedInteger|kLinearPCMFormatFlagIsPacked,.mBytesPerPacket=12,.mFramesPerPacket=1,.mBytesPerFrame=12,.mChannelsPerFrame=6,.mBitsPerChannel=16};
    AudioStreamBasicDescription dst=src; dst.mChannelsPerFrame=2;dst.mBytesPerPacket=dst.mBytesPerFrame=4;
    AudioConverterRef c; OSStatus s=AudioConverterNew(&src,&dst,&c);
    if(s){printf("new %d\n",s);return 1;}
    if(layout){ AudioChannelLayout acl={.mChannelLayoutTag=kAudioChannelLayoutTag_AudioUnit_5_1};s=AudioConverterSetProperty(c,kAudioConverterInputChannelLayout,sizeof acl,&acl);printf("layout status %d\n",s); }
    for(int ch=0;ch<6;ch++){short in[6*128]={0},out[2*128]={0};for(int f=0;f<128;f++)in[6*f+ch]=10000;UInt32 n=sizeof out;s=AudioConverterConvertBuffer(c,sizeof in,in,&n,out);printf("layout %d channel %d status %d out %d %d bytes %u\n",layout,ch,s,out[100],out[101],n);}
    AudioConverterDispose(c);
  }
}

@seedds

seedds commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Merged into main as 8e8ea51 (cherry-picked onto main, authorship kept). Thanks! The only edit: the comment above device_channels is reworded in the file's comment style; the code is unchanged. The new t_audio surround case fails on the previous main (center channel silent) and passes with the change; t_audio_spsc passes.

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