startup_stubs/audio_sink: add multi-channel downmix and linear resampling #1446

Merged
tedin7 merged 3 commits from upstream-pr/1453 into main 2026-11-24 12:37:28 +00:00
tedin7 commented 2026-11-24 11:49:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:49:41Z.


Summary

Extend x2w_audio::convert_block and AudioSink::submit in src/startup_stubs/audio_sink.{h,cpp} to support:

  • Multi-channel downmix: 5.1 (6ch) and 7.1 (8ch) to stereo or mono using ITU-R BS.775-1 fold-down coefficients (1/sqrt(2) for center and surround/side channels, LFE omitted) with [-1.0, 1.0] clamping, plus stereo to mono (0.5 * (L + R)).
  • Linear sample-rate resampling when in.rate != out.rate (e.g. 24 kHz / 44.1 kHz to 48 kHz), with positive sample-rate validation (1000..384000 Hz) and Submit::Unsupported for invalid rates, unsupported channel counts, or empty/null blocks.
  • Bit-for-bit preservation of matching-rate 1:1 and mono-to-N paths, and reuse of a member scratch buffer in AudioSink::submit to avoid per-submit heap allocations.
  • Updated docs/startup-runtime.md and extended tests/startup_audio_pure_check.h and tests/startup_audio_output.cpp.

Verification

  • Linux pure checks (g++ -std=c++17 -Wall -Wextra -Werror -O2 with small/near/far/min/max macro collision test): PASS
  • python3 tests/docs_links_check.py: PASS (16 Markdown files, 325 links)
  • python3 tests/bidi_controls_check.py: PASS (1190 tracked files)
  • git diff --check origin/main..HEAD: PASS

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6581093929 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:49:41Z. --- ## Summary Extend `x2w_audio::convert_block` and `AudioSink::submit` in `src/startup_stubs/audio_sink.{h,cpp}` to support: - Multi-channel downmix: 5.1 (6ch) and 7.1 (8ch) to stereo or mono using ITU-R BS.775-1 fold-down coefficients (`1/sqrt(2)` for center and surround/side channels, LFE omitted) with `[-1.0, 1.0]` clamping, plus stereo to mono (`0.5 * (L + R)`). - Linear sample-rate resampling when `in.rate != out.rate` (e.g. 24 kHz / 44.1 kHz to 48 kHz), with positive sample-rate validation (`1000..384000` Hz) and `Submit::Unsupported` for invalid rates, unsupported channel counts, or empty/null blocks. - Bit-for-bit preservation of matching-rate 1:1 and mono-to-N paths, and reuse of a member scratch buffer in `AudioSink::submit` to avoid per-submit heap allocations. - Updated `docs/startup-runtime.md` and extended `tests/startup_audio_pure_check.h` and `tests/startup_audio_output.cpp`. ## Verification - Linux pure checks (`g++ -std=c++17 -Wall -Wextra -Werror -O2` with `small`/`near`/`far`/`min`/`max` macro collision test): PASS - `python3 tests/docs_links_check.py`: PASS (16 Markdown files, 325 links) - `python3 tests/bidi_controls_check.py`: PASS (1190 tracked files) - `git diff --check origin/main..HEAD`: PASS AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-24 12:03:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:59:43Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fb2a4c0848

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038784984 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#pullrequestreview-6038784984). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:59:43Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `fb2a4c0848` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
tedin7 commented 2026-11-24 12:03:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:53:01Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit fb2a4c08486325b4404b3a6398c7d8e344edc6df:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target startup_audio_output startup_audio_pacing -- -j 6): 4.99s (configure 1.72s)
    • Test (ctest --preset dev -R '^startup_audio_' --parallel 4 --no-tests=error --output-on-failure): 0/3 passed (2.00s: startup_audio_output 0.04s, startup_audio_pacing 0.03s, startup_audio_provider 1.90s); skipped (capability, no default render endpoint on server): startup_audio_output, startup_audio_pacing, startup_audio_provider
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target startup_audio_output startup_audio_pacing -- -j 6): 2.61s (configure 1.69s)
    • Test (ctest --preset release -R '^startup_audio_' --parallel 4 --no-tests=error --output-on-failure): 0/3 passed (1.98s: startup_audio_output 0.03s, startup_audio_pacing 0.03s, startup_audio_provider 1.89s); skipped (capability, no default render endpoint on server): startup_audio_output, startup_audio_pacing, startup_audio_provider
  • sccache: 0/8 hits; wall 20.7s

Note: startup_audio_output runs check_pure() before probing the WASAPI endpoint, so all pure conversion/downmix/resampling/negative-control assertions compiled and executed on MSVC Debug & Release /O2 prior to the capability skip (exit 77).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7130673964 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#issuecomment-7130673964). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:53:01Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `fb2a4c08486325b4404b3a6398c7d8e344edc6df`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target startup_audio_output startup_audio_pacing -- -j 6`): `4.99s` (configure `1.72s`) - Test (`ctest --preset dev -R '^startup_audio_' --parallel 4 --no-tests=error --output-on-failure`): **0/3 passed** (`2.00s`: `startup_audio_output` `0.04s`, `startup_audio_pacing` `0.03s`, `startup_audio_provider` `1.90s`); skipped (capability, no default render endpoint on server): `startup_audio_output`, `startup_audio_pacing`, `startup_audio_provider` - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target startup_audio_output startup_audio_pacing -- -j 6`): `2.61s` (configure `1.69s`) - Test (`ctest --preset release -R '^startup_audio_' --parallel 4 --no-tests=error --output-on-failure`): **0/3 passed** (`1.98s`: `startup_audio_output` `0.03s`, `startup_audio_pacing` `0.03s`, `startup_audio_provider` `1.89s`); skipped (capability, no default render endpoint on server): `startup_audio_output`, `startup_audio_pacing`, `startup_audio_provider` - `sccache`: 0/8 hits; wall `20.7s` Note: `startup_audio_output` runs `check_pure()` before probing the WASAPI endpoint, so all pure conversion/downmix/resampling/negative-control assertions compiled and executed on MSVC Debug & Release `/O2` prior to the capability skip (exit 77).
chatgpt-codex-connector[bot commented 2026-11-24 12:03:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:53:09Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T11:59:45.619388Z fb2a4c0 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7130676935 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#issuecomment-7130676935). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:53:09Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"fb2a4c08486325b4404b3a6398c7d8e344edc6df","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T11:59:45.619388Z">2026-11-24T11:59:45.619388Z</relative-time> | `fb2a4c0` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
chatgpt-codex-connector[bot commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:59:43Z.

Code review location: src/startup_stubs/audio_sink.h; line 156

@@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s)
 // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block.
 inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames)
 {
+    if (!data || frames == 0 || f.channels == 0) return 0.0f;
     float peak = 0.0f;
-    for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i)));
+    for (size_t i = 0; i < size_t(frames) * f.channels; ++i)
+        peak = (std::max)(peak, std::fabs(read_sample(f, data, i)));
     return peak;
 }
 
-// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every
-// channel, otherwise channel counts must match. Returns false for any other combination.
+inline bool is_valid_sample_rate(uint32_t rate)
+{
+    return rate >= 1000u && rate <= 384000u;
+}
+
+inline bool is_supported_channel_count(uint32_t channels)
+{
+    return channels == 1u || channels == 2u || channels == 6u || channels == 8u;
+}
+
+inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch)
+{
+    if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false;
+    if (in_ch == out_ch || in_ch == 1u) return true;
+    if (in_ch == 2u && out_ch == 1u) return true;
+    if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true;
+    return false;
+}
+
+// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels.
+inline constexpr float kItuSurroundCoeff = 0.7071067811865475f;
+
+inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch,
+                                        uint32_t out_channels)
+{
+    const size_t base = frame * in.channels;
+    if (in.channels == out_channels) return read_sample(in, src, base + out_ch);
+    if (in.channels == 1u) return read_sample(in, src, base);
+    if (in.channels == 2u && out_channels == 1u) {
+        const float left = read_sample(in, src, base);
+        const float right = read_sample(in, src, base + 1);
+        return std::clamp(0.5f * (left + right), -1.0f, 1.0f);
+    }
+    // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono.
+    const float fl = read_sample(in, src, base + 0);
+    const float fr = read_sample(in, src, base + 1);
+    const float fc = read_sample(in, src, base + 2);
+    const float bl = read_sample(in, src, base + 4);
+    const float br = read_sample(in, src, base + 5);
+    float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl;
+    float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br;
+    if (in.channels == 8u) {
+        const float sl = read_sample(in, src, base + 6);
+        const float sr = read_sample(in, src, base + 7);
+        left += kItuSurroundCoeff * sl;
+        right += kItuSurroundCoeff * sr;
+    }
+    left = std::clamp(left, -1.0f, 1.0f);
+    right = std::clamp(right, -1.0f, 1.0f);
+    if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f);
+    return out_ch == 0u ? left : right;
+}
+
+inline uint32_t resampled_frame_count(uint32_t frames, uint32_t in_rate, uint32_t out_rate)
+{
+    if (frames == 0 || !is_valid_sample_rate(in_rate) || !is_valid_sample_rate(out_rate)) return 0;
+    if (in_rate == out_rate) return frames;
+    const uint64_t num = static_cast<uint64_t>(frames) * out_rate + (in_rate / 2u);
+    const uint64_t scaled = (std::max)(uint64_t{1}, num / in_rate);
+    if (scaled > (std::numeric_limits<uint32_t>::max)()) return 0;
+    return static_cast<uint32_t>(scaled);
+}
+
+// Converts a submitted block to the endpoint format, supporting mono/stereo/5.1/7.1 channel mapping
+// (ITU-R BS.775-1 fold-down with clamping) and linear sample-rate resampling.
+// Preserves exact bit-for-bit behavior for matching-rate 1:1 and mono-to-N paths.
 inline bool convert_block(const BlockFormat& in, const void* src, uint32_t frames, const BlockFormat& out,
                           std::vector<uint8_t>& dst)
 {
-    if (in.rate != out.rate || in.channels == 0 || out.channels == 0) return false;
-    if (in.channels != out.channels && in.channels != 1) return false;
-    dst.assign(size_t(frames) * out.frame_bytes(), 0);
-    for (size_t f = 0; f < frames; ++f)
-        for (size_t c = 0; c < out.channels; ++c)
-            write_sample(out, dst.data(), f * out.channels + c, read_sample(in, src, f * in.channels + (in.channels == 1 ? 0 : c)));
+    if (!src || frames == 0) return false;
+    if (!is_valid_sample_rate(in.rate) || !is_valid_sample_rate(out.rate)) return false;
+    if (!is_supported_channel_conversion(in.channels, out.channels)) return false;
+
+    const uint32_t out_frames = resampled_frame_count(frames, in.rate, out.rate);
+    if (out_frames == 0) return false;
+    const uint64_t total_bytes = static_cast<uint64_t>(out_frames) * out.frame_bytes();
+    if (total_bytes > (std::numeric_limits<size_t>::max)()) return false;
+    dst.resize(static_cast<size_t>(total_bytes));
+
+    if (in.rate == out.rate) {
+        if (in.channels == out.channels || in.channels == 1u) {
+            for (size_t f = 0; f < frames; ++f)
+                for (size_t c = 0; c < out.channels; ++c)
+                    write_sample(out, dst.data(), f * out.channels + c,
+                                 read_sample(in, src, f * in.channels + (in.channels == 1u ? 0 : c)));
+            return true;
+        }
+        for (size_t f = 0; f < frames; ++f)
+            for (uint32_t c = 0; c < out.channels; ++c)
+                write_sample(out, dst.data(), f * out.channels + c,
+                             read_channel_mapped_sample(in, src, f, c, out.channels));
+        return true;
+    }
+
+    const double step = static_cast<double>(in.rate) / static_cast<double>(out.rate);
+    for (uint32_t f = 0; f < out_frames; ++f) {
+        const double pos = static_cast<double>(f) * step;
+        const uint32_t i0 = (std::min)(static_cast<uint32_t>(pos), frames - 1u);
+        const uint32_t i1 = (std::min)(i0 + 1u, frames - 1u);

P2 Badge Preserve resampling state between accepted blocks

When consecutive blocks are submitted at a different sample rate, AudioSink::submit invokes this conversion afresh, restarting pos at zero and independently rounding each output length. The pure converter produces 139,000 output frames for 1,000 128-frame blocks at 44.1-to-48 kHz, versus 139,320 when converting the same input as one block; a split 24-to-48-kHz ramp also loses the interpolation sample at the boundary. Thus packetization changes both playback timing and samples. Retain fractional phase and boundary history across accepted submissions rather than restarting the resampler for every block.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679653067 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#discussion_r4679653067). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:59:43Z. **Code review location:** `src/startup_stubs/audio_sink.h`; line 156 ~~~~diff @@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s) // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block. inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames) { + if (!data || frames == 0 || f.channels == 0) return 0.0f; float peak = 0.0f; - for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i))); + for (size_t i = 0; i < size_t(frames) * f.channels; ++i) + peak = (std::max)(peak, std::fabs(read_sample(f, data, i))); return peak; } -// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every -// channel, otherwise channel counts must match. Returns false for any other combination. +inline bool is_valid_sample_rate(uint32_t rate) +{ + return rate >= 1000u && rate <= 384000u; +} + +inline bool is_supported_channel_count(uint32_t channels) +{ + return channels == 1u || channels == 2u || channels == 6u || channels == 8u; +} + +inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch) +{ + if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false; + if (in_ch == out_ch || in_ch == 1u) return true; + if (in_ch == 2u && out_ch == 1u) return true; + if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true; + return false; +} + +// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels. +inline constexpr float kItuSurroundCoeff = 0.7071067811865475f; + +inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch, + uint32_t out_channels) +{ + const size_t base = frame * in.channels; + if (in.channels == out_channels) return read_sample(in, src, base + out_ch); + if (in.channels == 1u) return read_sample(in, src, base); + if (in.channels == 2u && out_channels == 1u) { + const float left = read_sample(in, src, base); + const float right = read_sample(in, src, base + 1); + return std::clamp(0.5f * (left + right), -1.0f, 1.0f); + } + // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono. + const float fl = read_sample(in, src, base + 0); + const float fr = read_sample(in, src, base + 1); + const float fc = read_sample(in, src, base + 2); + const float bl = read_sample(in, src, base + 4); + const float br = read_sample(in, src, base + 5); + float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl; + float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br; + if (in.channels == 8u) { + const float sl = read_sample(in, src, base + 6); + const float sr = read_sample(in, src, base + 7); + left += kItuSurroundCoeff * sl; + right += kItuSurroundCoeff * sr; + } + left = std::clamp(left, -1.0f, 1.0f); + right = std::clamp(right, -1.0f, 1.0f); + if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f); + return out_ch == 0u ? left : right; +} + +inline uint32_t resampled_frame_count(uint32_t frames, uint32_t in_rate, uint32_t out_rate) +{ + if (frames == 0 || !is_valid_sample_rate(in_rate) || !is_valid_sample_rate(out_rate)) return 0; + if (in_rate == out_rate) return frames; + const uint64_t num = static_cast<uint64_t>(frames) * out_rate + (in_rate / 2u); + const uint64_t scaled = (std::max)(uint64_t{1}, num / in_rate); + if (scaled > (std::numeric_limits<uint32_t>::max)()) return 0; + return static_cast<uint32_t>(scaled); +} + +// Converts a submitted block to the endpoint format, supporting mono/stereo/5.1/7.1 channel mapping +// (ITU-R BS.775-1 fold-down with clamping) and linear sample-rate resampling. +// Preserves exact bit-for-bit behavior for matching-rate 1:1 and mono-to-N paths. inline bool convert_block(const BlockFormat& in, const void* src, uint32_t frames, const BlockFormat& out, std::vector<uint8_t>& dst) { - if (in.rate != out.rate || in.channels == 0 || out.channels == 0) return false; - if (in.channels != out.channels && in.channels != 1) return false; - dst.assign(size_t(frames) * out.frame_bytes(), 0); - for (size_t f = 0; f < frames; ++f) - for (size_t c = 0; c < out.channels; ++c) - write_sample(out, dst.data(), f * out.channels + c, read_sample(in, src, f * in.channels + (in.channels == 1 ? 0 : c))); + if (!src || frames == 0) return false; + if (!is_valid_sample_rate(in.rate) || !is_valid_sample_rate(out.rate)) return false; + if (!is_supported_channel_conversion(in.channels, out.channels)) return false; + + const uint32_t out_frames = resampled_frame_count(frames, in.rate, out.rate); + if (out_frames == 0) return false; + const uint64_t total_bytes = static_cast<uint64_t>(out_frames) * out.frame_bytes(); + if (total_bytes > (std::numeric_limits<size_t>::max)()) return false; + dst.resize(static_cast<size_t>(total_bytes)); + + if (in.rate == out.rate) { + if (in.channels == out.channels || in.channels == 1u) { + for (size_t f = 0; f < frames; ++f) + for (size_t c = 0; c < out.channels; ++c) + write_sample(out, dst.data(), f * out.channels + c, + read_sample(in, src, f * in.channels + (in.channels == 1u ? 0 : c))); + return true; + } + for (size_t f = 0; f < frames; ++f) + for (uint32_t c = 0; c < out.channels; ++c) + write_sample(out, dst.data(), f * out.channels + c, + read_channel_mapped_sample(in, src, f, c, out.channels)); + return true; + } + + const double step = static_cast<double>(in.rate) / static_cast<double>(out.rate); + for (uint32_t f = 0; f < out_frames; ++f) { + const double pos = static_cast<double>(f) * step; + const uint32_t i0 = (std::min)(static_cast<uint32_t>(pos), frames - 1u); + const uint32_t i1 = (std::min)(i0 + 1u, frames - 1u); ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve resampling state between accepted blocks** When consecutive blocks are submitted at a different sample rate, `AudioSink::submit` invokes this conversion afresh, restarting `pos` at zero and independently rounding each output length. The pure converter produces 139,000 output frames for 1,000 128-frame blocks at 44.1-to-48 kHz, versus 139,320 when converting the same input as one block; a split 24-to-48-kHz ramp also loses the interpolation sample at the boundary. Thus packetization changes both playback timing and samples. Retain fractional phase and boundary history across accepted submissions rather than restarting the resampler for every block. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:59:43Z.

Code review location: src/startup_stubs/audio_sink.h; line 107

@@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s)
 // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block.
 inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames)
 {
+    if (!data || frames == 0 || f.channels == 0) return 0.0f;
     float peak = 0.0f;
-    for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i)));
+    for (size_t i = 0; i < size_t(frames) * f.channels; ++i)
+        peak = (std::max)(peak, std::fabs(read_sample(f, data, i)));
     return peak;
 }
 
-// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every
-// channel, otherwise channel counts must match. Returns false for any other combination.
+inline bool is_valid_sample_rate(uint32_t rate)
+{
+    return rate >= 1000u && rate <= 384000u;
+}
+
+inline bool is_supported_channel_count(uint32_t channels)
+{
+    return channels == 1u || channels == 2u || channels == 6u || channels == 8u;
+}
+
+inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch)
+{
+    if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false;
+    if (in_ch == out_ch || in_ch == 1u) return true;
+    if (in_ch == 2u && out_ch == 1u) return true;
+    if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true;
+    return false;
+}
+
+// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels.
+inline constexpr float kItuSurroundCoeff = 0.7071067811865475f;
+
+inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch,
+                                        uint32_t out_channels)
+{
+    const size_t base = frame * in.channels;
+    if (in.channels == out_channels) return read_sample(in, src, base + out_ch);
+    if (in.channels == 1u) return read_sample(in, src, base);
+    if (in.channels == 2u && out_channels == 1u) {
+        const float left = read_sample(in, src, base);
+        const float right = read_sample(in, src, base + 1);
+        return std::clamp(0.5f * (left + right), -1.0f, 1.0f);
+    }
+    // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono.
+    const float fl = read_sample(in, src, base + 0);
+    const float fr = read_sample(in, src, base + 1);
+    const float fc = read_sample(in, src, base + 2);
+    const float bl = read_sample(in, src, base + 4);
+    const float br = read_sample(in, src, base + 5);
+    float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl;
+    float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br;
+    if (in.channels == 8u) {
+        const float sl = read_sample(in, src, base + 6);
+        const float sr = read_sample(in, src, base + 7);
+        left += kItuSurroundCoeff * sl;
+        right += kItuSurroundCoeff * sr;
+    }
+    left = std::clamp(left, -1.0f, 1.0f);
+    right = std::clamp(right, -1.0f, 1.0f);
+    if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f);

P2 Badge Clamp only after the final mono fold-down

For 5.1/7.1-to-mono conversion, clipping left and right before averaging distorts the mix even when the final mono sample would be within range. A valid 5.1 frame with FL=1, FR=-1, FC=1 and all other channels zero should produce approximately 0.7071, but the pure converter outputs 0.3536 because only the left intermediate clips. This attenuates the center contribution unnecessarily. For a mono target, average the unclipped stereo sums and then clamp the final mono value.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679653076 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#discussion_r4679653076). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:59:43Z. **Code review location:** `src/startup_stubs/audio_sink.h`; line 107 ~~~~diff @@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s) // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block. inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames) { + if (!data || frames == 0 || f.channels == 0) return 0.0f; float peak = 0.0f; - for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i))); + for (size_t i = 0; i < size_t(frames) * f.channels; ++i) + peak = (std::max)(peak, std::fabs(read_sample(f, data, i))); return peak; } -// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every -// channel, otherwise channel counts must match. Returns false for any other combination. +inline bool is_valid_sample_rate(uint32_t rate) +{ + return rate >= 1000u && rate <= 384000u; +} + +inline bool is_supported_channel_count(uint32_t channels) +{ + return channels == 1u || channels == 2u || channels == 6u || channels == 8u; +} + +inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch) +{ + if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false; + if (in_ch == out_ch || in_ch == 1u) return true; + if (in_ch == 2u && out_ch == 1u) return true; + if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true; + return false; +} + +// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels. +inline constexpr float kItuSurroundCoeff = 0.7071067811865475f; + +inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch, + uint32_t out_channels) +{ + const size_t base = frame * in.channels; + if (in.channels == out_channels) return read_sample(in, src, base + out_ch); + if (in.channels == 1u) return read_sample(in, src, base); + if (in.channels == 2u && out_channels == 1u) { + const float left = read_sample(in, src, base); + const float right = read_sample(in, src, base + 1); + return std::clamp(0.5f * (left + right), -1.0f, 1.0f); + } + // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono. + const float fl = read_sample(in, src, base + 0); + const float fr = read_sample(in, src, base + 1); + const float fc = read_sample(in, src, base + 2); + const float bl = read_sample(in, src, base + 4); + const float br = read_sample(in, src, base + 5); + float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl; + float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br; + if (in.channels == 8u) { + const float sl = read_sample(in, src, base + 6); + const float sr = read_sample(in, src, base + 7); + left += kItuSurroundCoeff * sl; + right += kItuSurroundCoeff * sr; + } + left = std::clamp(left, -1.0f, 1.0f); + right = std::clamp(right, -1.0f, 1.0f); + if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f); ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Clamp only after the final mono fold-down** For 5.1/7.1-to-mono conversion, clipping `left` and `right` before averaging distorts the mix even when the final mono sample would be within range. A valid 5.1 frame with `FL=1`, `FR=-1`, `FC=1` and all other channels zero should produce approximately `0.7071`, but the pure converter outputs `0.3536` because only the left intermediate clips. This attenuates the center contribution unnecessarily. For a mono target, average the unclipped stereo sums and then clamp the final mono value. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
upstream-sync closed this pull request 2026-11-24 13:00:32 +00:00
tedin7 commented 2026-11-24 13:00:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T12:08:58Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038922237 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#pullrequestreview-6038922237). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T12:08:58Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-24 13:00:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T12:09:02Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038923000 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#pullrequestreview-6038923000). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T12:09:02Z. **Review:** COMMENTED --- _No description provided._
R0Y00 commented 2026-11-24 13:00:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T12:28:46Z.


Maintainer update at b1d43427 preserves equal-channel playback and mono fan-out for four-channel endpoints, and clears interpolation state when reopening the endpoint. Windows Release startup_audio_output passed, including the new pure checks and the endpoint path.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7131496688 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#issuecomment-7131496688). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T12:28:46Z. --- Maintainer update at b1d43427 preserves equal-channel playback and mono fan-out for four-channel endpoints, and clears interpolation state when reopening the endpoint. Windows Release startup_audio_output passed, including the new pure checks and the endpoint path.
tedin7 commented 2026-11-24 13:00:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T12:08:58Z.

Code review location: src/startup_stubs/audio_sink.h; line 156

@@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s)
 // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block.
 inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames)
 {
+    if (!data || frames == 0 || f.channels == 0) return 0.0f;
     float peak = 0.0f;
-    for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i)));
+    for (size_t i = 0; i < size_t(frames) * f.channels; ++i)
+        peak = (std::max)(peak, std::fabs(read_sample(f, data, i)));
     return peak;
 }
 
-// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every
-// channel, otherwise channel counts must match. Returns false for any other combination.
+inline bool is_valid_sample_rate(uint32_t rate)
+{
+    return rate >= 1000u && rate <= 384000u;
+}
+
+inline bool is_supported_channel_count(uint32_t channels)
+{
+    return channels == 1u || channels == 2u || channels == 6u || channels == 8u;
+}
+
+inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch)
+{
+    if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false;
+    if (in_ch == out_ch || in_ch == 1u) return true;
+    if (in_ch == 2u && out_ch == 1u) return true;
+    if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true;
+    return false;
+}
+
+// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels.
+inline constexpr float kItuSurroundCoeff = 0.7071067811865475f;
+
+inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch,
+                                        uint32_t out_channels)
+{
+    const size_t base = frame * in.channels;
+    if (in.channels == out_channels) return read_sample(in, src, base + out_ch);
+    if (in.channels == 1u) return read_sample(in, src, base);
+    if (in.channels == 2u && out_channels == 1u) {
+        const float left = read_sample(in, src, base);
+        const float right = read_sample(in, src, base + 1);
+        return std::clamp(0.5f * (left + right), -1.0f, 1.0f);
+    }
+    // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono.
+    const float fl = read_sample(in, src, base + 0);
+    const float fr = read_sample(in, src, base + 1);
+    const float fc = read_sample(in, src, base + 2);
+    const float bl = read_sample(in, src, base + 4);
+    const float br = read_sample(in, src, base + 5);
+    float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl;
+    float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br;
+    if (in.channels == 8u) {
+        const float sl = read_sample(in, src, base + 6);
+        const float sr = read_sample(in, src, base + 7);
+        left += kItuSurroundCoeff * sl;
+        right += kItuSurroundCoeff * sr;
+    }
+    left = std::clamp(left, -1.0f, 1.0f);
+    right = std::clamp(right, -1.0f, 1.0f);
+    if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f);
+    return out_ch == 0u ? left : right;
+}
+
+inline uint32_t resampled_frame_count(uint32_t frames, uint32_t in_rate, uint32_t out_rate)
+{
+    if (frames == 0 || !is_valid_sample_rate(in_rate) || !is_valid_sample_rate(out_rate)) return 0;
+    if (in_rate == out_rate) return frames;
+    const uint64_t num = static_cast<uint64_t>(frames) * out_rate + (in_rate / 2u);
+    const uint64_t scaled = (std::max)(uint64_t{1}, num / in_rate);
+    if (scaled > (std::numeric_limits<uint32_t>::max)()) return 0;
+    return static_cast<uint32_t>(scaled);
+}
+
+// Converts a submitted block to the endpoint format, supporting mono/stereo/5.1/7.1 channel mapping
+// (ITU-R BS.775-1 fold-down with clamping) and linear sample-rate resampling.
+// Preserves exact bit-for-bit behavior for matching-rate 1:1 and mono-to-N paths.
 inline bool convert_block(const BlockFormat& in, const void* src, uint32_t frames, const BlockFormat& out,
                           std::vector<uint8_t>& dst)
 {
-    if (in.rate != out.rate || in.channels == 0 || out.channels == 0) return false;
-    if (in.channels != out.channels && in.channels != 1) return false;
-    dst.assign(size_t(frames) * out.frame_bytes(), 0);
-    for (size_t f = 0; f < frames; ++f)
-        for (size_t c = 0; c < out.channels; ++c)
-            write_sample(out, dst.data(), f * out.channels + c, read_sample(in, src, f * in.channels + (in.channels == 1 ? 0 : c)));
+    if (!src || frames == 0) return false;
+    if (!is_valid_sample_rate(in.rate) || !is_valid_sample_rate(out.rate)) return false;
+    if (!is_supported_channel_conversion(in.channels, out.channels)) return false;
+
+    const uint32_t out_frames = resampled_frame_count(frames, in.rate, out.rate);
+    if (out_frames == 0) return false;
+    const uint64_t total_bytes = static_cast<uint64_t>(out_frames) * out.frame_bytes();
+    if (total_bytes > (std::numeric_limits<size_t>::max)()) return false;
+    dst.resize(static_cast<size_t>(total_bytes));
+
+    if (in.rate == out.rate) {
+        if (in.channels == out.channels || in.channels == 1u) {
+            for (size_t f = 0; f < frames; ++f)
+                for (size_t c = 0; c < out.channels; ++c)
+                    write_sample(out, dst.data(), f * out.channels + c,
+                                 read_sample(in, src, f * in.channels + (in.channels == 1u ? 0 : c)));
+            return true;
+        }
+        for (size_t f = 0; f < frames; ++f)
+            for (uint32_t c = 0; c < out.channels; ++c)
+                write_sample(out, dst.data(), f * out.channels + c,
+                             read_channel_mapped_sample(in, src, f, c, out.channels));
+        return true;
+    }
+
+    const double step = static_cast<double>(in.rate) / static_cast<double>(out.rate);
+    for (uint32_t f = 0; f < out_frames; ++f) {
+        const double pos = static_cast<double>(f) * step;
+        const uint32_t i0 = (std::min)(static_cast<uint32_t>(pos), frames - 1u);
+        const uint32_t i1 = (std::min)(i0 + 1u, frames - 1u);

Fixed in 66677341: Resampler phase and the boundary frame now carry across accepted blocks (ResampleState, reset on format change; committed only when a block is accepted), so output no longer depends on block splitting. Covered by new pure checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679759028 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#discussion_r4679759028). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T12:08:58Z. **Code review location:** `src/startup_stubs/audio_sink.h`; line 156 ~~~~diff @@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s) // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block. inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames) { + if (!data || frames == 0 || f.channels == 0) return 0.0f; float peak = 0.0f; - for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i))); + for (size_t i = 0; i < size_t(frames) * f.channels; ++i) + peak = (std::max)(peak, std::fabs(read_sample(f, data, i))); return peak; } -// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every -// channel, otherwise channel counts must match. Returns false for any other combination. +inline bool is_valid_sample_rate(uint32_t rate) +{ + return rate >= 1000u && rate <= 384000u; +} + +inline bool is_supported_channel_count(uint32_t channels) +{ + return channels == 1u || channels == 2u || channels == 6u || channels == 8u; +} + +inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch) +{ + if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false; + if (in_ch == out_ch || in_ch == 1u) return true; + if (in_ch == 2u && out_ch == 1u) return true; + if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true; + return false; +} + +// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels. +inline constexpr float kItuSurroundCoeff = 0.7071067811865475f; + +inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch, + uint32_t out_channels) +{ + const size_t base = frame * in.channels; + if (in.channels == out_channels) return read_sample(in, src, base + out_ch); + if (in.channels == 1u) return read_sample(in, src, base); + if (in.channels == 2u && out_channels == 1u) { + const float left = read_sample(in, src, base); + const float right = read_sample(in, src, base + 1); + return std::clamp(0.5f * (left + right), -1.0f, 1.0f); + } + // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono. + const float fl = read_sample(in, src, base + 0); + const float fr = read_sample(in, src, base + 1); + const float fc = read_sample(in, src, base + 2); + const float bl = read_sample(in, src, base + 4); + const float br = read_sample(in, src, base + 5); + float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl; + float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br; + if (in.channels == 8u) { + const float sl = read_sample(in, src, base + 6); + const float sr = read_sample(in, src, base + 7); + left += kItuSurroundCoeff * sl; + right += kItuSurroundCoeff * sr; + } + left = std::clamp(left, -1.0f, 1.0f); + right = std::clamp(right, -1.0f, 1.0f); + if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f); + return out_ch == 0u ? left : right; +} + +inline uint32_t resampled_frame_count(uint32_t frames, uint32_t in_rate, uint32_t out_rate) +{ + if (frames == 0 || !is_valid_sample_rate(in_rate) || !is_valid_sample_rate(out_rate)) return 0; + if (in_rate == out_rate) return frames; + const uint64_t num = static_cast<uint64_t>(frames) * out_rate + (in_rate / 2u); + const uint64_t scaled = (std::max)(uint64_t{1}, num / in_rate); + if (scaled > (std::numeric_limits<uint32_t>::max)()) return 0; + return static_cast<uint32_t>(scaled); +} + +// Converts a submitted block to the endpoint format, supporting mono/stereo/5.1/7.1 channel mapping +// (ITU-R BS.775-1 fold-down with clamping) and linear sample-rate resampling. +// Preserves exact bit-for-bit behavior for matching-rate 1:1 and mono-to-N paths. inline bool convert_block(const BlockFormat& in, const void* src, uint32_t frames, const BlockFormat& out, std::vector<uint8_t>& dst) { - if (in.rate != out.rate || in.channels == 0 || out.channels == 0) return false; - if (in.channels != out.channels && in.channels != 1) return false; - dst.assign(size_t(frames) * out.frame_bytes(), 0); - for (size_t f = 0; f < frames; ++f) - for (size_t c = 0; c < out.channels; ++c) - write_sample(out, dst.data(), f * out.channels + c, read_sample(in, src, f * in.channels + (in.channels == 1 ? 0 : c))); + if (!src || frames == 0) return false; + if (!is_valid_sample_rate(in.rate) || !is_valid_sample_rate(out.rate)) return false; + if (!is_supported_channel_conversion(in.channels, out.channels)) return false; + + const uint32_t out_frames = resampled_frame_count(frames, in.rate, out.rate); + if (out_frames == 0) return false; + const uint64_t total_bytes = static_cast<uint64_t>(out_frames) * out.frame_bytes(); + if (total_bytes > (std::numeric_limits<size_t>::max)()) return false; + dst.resize(static_cast<size_t>(total_bytes)); + + if (in.rate == out.rate) { + if (in.channels == out.channels || in.channels == 1u) { + for (size_t f = 0; f < frames; ++f) + for (size_t c = 0; c < out.channels; ++c) + write_sample(out, dst.data(), f * out.channels + c, + read_sample(in, src, f * in.channels + (in.channels == 1u ? 0 : c))); + return true; + } + for (size_t f = 0; f < frames; ++f) + for (uint32_t c = 0; c < out.channels; ++c) + write_sample(out, dst.data(), f * out.channels + c, + read_channel_mapped_sample(in, src, f, c, out.channels)); + return true; + } + + const double step = static_cast<double>(in.rate) / static_cast<double>(out.rate); + for (uint32_t f = 0; f < out_frames; ++f) { + const double pos = static_cast<double>(f) * step; + const uint32_t i0 = (std::min)(static_cast<uint32_t>(pos), frames - 1u); + const uint32_t i1 = (std::min)(i0 + 1u, frames - 1u); ~~~~ --- Fixed in 66677341: Resampler phase and the boundary frame now carry across accepted blocks (ResampleState, reset on format change; committed only when a block is accepted), so output no longer depends on block splitting. Covered by new pure checks.
tedin7 commented 2026-11-24 13:00:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T12:09:02Z.

Code review location: src/startup_stubs/audio_sink.h; line 107

@@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s)
 // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block.
 inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames)
 {
+    if (!data || frames == 0 || f.channels == 0) return 0.0f;
     float peak = 0.0f;
-    for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i)));
+    for (size_t i = 0; i < size_t(frames) * f.channels; ++i)
+        peak = (std::max)(peak, std::fabs(read_sample(f, data, i)));
     return peak;
 }
 
-// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every
-// channel, otherwise channel counts must match. Returns false for any other combination.
+inline bool is_valid_sample_rate(uint32_t rate)
+{
+    return rate >= 1000u && rate <= 384000u;
+}
+
+inline bool is_supported_channel_count(uint32_t channels)
+{
+    return channels == 1u || channels == 2u || channels == 6u || channels == 8u;
+}
+
+inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch)
+{
+    if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false;
+    if (in_ch == out_ch || in_ch == 1u) return true;
+    if (in_ch == 2u && out_ch == 1u) return true;
+    if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true;
+    return false;
+}
+
+// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels.
+inline constexpr float kItuSurroundCoeff = 0.7071067811865475f;
+
+inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch,
+                                        uint32_t out_channels)
+{
+    const size_t base = frame * in.channels;
+    if (in.channels == out_channels) return read_sample(in, src, base + out_ch);
+    if (in.channels == 1u) return read_sample(in, src, base);
+    if (in.channels == 2u && out_channels == 1u) {
+        const float left = read_sample(in, src, base);
+        const float right = read_sample(in, src, base + 1);
+        return std::clamp(0.5f * (left + right), -1.0f, 1.0f);
+    }
+    // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono.
+    const float fl = read_sample(in, src, base + 0);
+    const float fr = read_sample(in, src, base + 1);
+    const float fc = read_sample(in, src, base + 2);
+    const float bl = read_sample(in, src, base + 4);
+    const float br = read_sample(in, src, base + 5);
+    float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl;
+    float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br;
+    if (in.channels == 8u) {
+        const float sl = read_sample(in, src, base + 6);
+        const float sr = read_sample(in, src, base + 7);
+        left += kItuSurroundCoeff * sl;
+        right += kItuSurroundCoeff * sr;
+    }
+    left = std::clamp(left, -1.0f, 1.0f);
+    right = std::clamp(right, -1.0f, 1.0f);
+    if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f);

Fixed in 66677341: 5.1/7.1 to mono now averages the unclipped stereo sums and clamps only the final value (FL=1, FR=-1, FC=1 gives 0.7071); stereo targets still clamp per channel. Covered by new pure checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679759654 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1453#discussion_r4679759654). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T12:09:02Z. **Code review location:** `src/startup_stubs/audio_sink.h`; line 107 ~~~~diff @@ -47,22 +48,120 @@ inline void write_sample(const BlockFormat& f, void* dst, size_t i, float s) // Largest absolute sample in 0..1. Exactly 0 means every sample is zero: a valid silent block. inline float block_peak(const BlockFormat& f, const void* data, uint32_t frames) { + if (!data || frames == 0 || f.channels == 0) return 0.0f; float peak = 0.0f; - for (size_t i = 0; i < size_t(frames) * f.channels; ++i) peak = std::max(peak, std::fabs(read_sample(f, data, i))); + for (size_t i = 0; i < size_t(frames) * f.channels; ++i) + peak = (std::max)(peak, std::fabs(read_sample(f, data, i))); return peak; } -// Converts a submitted block to the endpoint format. Same rate required; mono is copied to every -// channel, otherwise channel counts must match. Returns false for any other combination. +inline bool is_valid_sample_rate(uint32_t rate) +{ + return rate >= 1000u && rate <= 384000u; +} + +inline bool is_supported_channel_count(uint32_t channels) +{ + return channels == 1u || channels == 2u || channels == 6u || channels == 8u; +} + +inline bool is_supported_channel_conversion(uint32_t in_ch, uint32_t out_ch) +{ + if (!is_supported_channel_count(in_ch) || !is_supported_channel_count(out_ch)) return false; + if (in_ch == out_ch || in_ch == 1u) return true; + if (in_ch == 2u && out_ch == 1u) return true; + if ((in_ch == 6u || in_ch == 8u) && (out_ch == 1u || out_ch == 2u)) return true; + return false; +} + +// ITU-R BS.775-1 fold-down coefficient (1 / sqrt(2)) for center and surround channels. +inline constexpr float kItuSurroundCoeff = 0.7071067811865475f; + +inline float read_channel_mapped_sample(const BlockFormat& in, const void* src, size_t frame, uint32_t out_ch, + uint32_t out_channels) +{ + const size_t base = frame * in.channels; + if (in.channels == out_channels) return read_sample(in, src, base + out_ch); + if (in.channels == 1u) return read_sample(in, src, base); + if (in.channels == 2u && out_channels == 1u) { + const float left = read_sample(in, src, base); + const float right = read_sample(in, src, base + 1); + return std::clamp(0.5f * (left + right), -1.0f, 1.0f); + } + // 5.1 (FL, FR, FC, LFE, BL, BR) or 7.1 (FL, FR, FC, LFE, BL, BR, SL, SR) -> stereo / mono. + const float fl = read_sample(in, src, base + 0); + const float fr = read_sample(in, src, base + 1); + const float fc = read_sample(in, src, base + 2); + const float bl = read_sample(in, src, base + 4); + const float br = read_sample(in, src, base + 5); + float left = fl + kItuSurroundCoeff * fc + kItuSurroundCoeff * bl; + float right = fr + kItuSurroundCoeff * fc + kItuSurroundCoeff * br; + if (in.channels == 8u) { + const float sl = read_sample(in, src, base + 6); + const float sr = read_sample(in, src, base + 7); + left += kItuSurroundCoeff * sl; + right += kItuSurroundCoeff * sr; + } + left = std::clamp(left, -1.0f, 1.0f); + right = std::clamp(right, -1.0f, 1.0f); + if (out_channels == 1u) return std::clamp(0.5f * (left + right), -1.0f, 1.0f); ~~~~ --- Fixed in 66677341: 5.1/7.1 to mono now averages the unclipped stereo sums and clamps only the final value (FL=1, FR=-1, FC=1 gives 0.7071); stereo targets still clamp per channel. Covered by new pure checks.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1446
No description provided.