Skip to content

Hot Path Audio Linear Resampling

github-actions[bot] edited this page Sep 14, 2026 · 3 revisions

Linear Audio Resampling

Lifecycle: Performance - Unproven · Type: Approximate shortcut
State: Source-backed; runtime value is not established.

Status: Applicable approximation candidate; not implemented, not benchmarked, and not a hardware-accuracy claim.

Mechanism class: Approximate shortcut / cheaper interpolation algorithm.

Reviewed source: 322986f4c5502b3fa887a9605ac8cd88e9b2c579

Subsystem: MCPX APU voice processing.

Summary

Current xemu creates a persistent two-channel libsamplerate callback converter using SRC_SINC_FASTEST. The proposed shortcut creates selected voice resamplers with SRC_LINEAR instead, leaving the callback, channel count, source progression, and surrounding voice logic unchanged for the first experiment.

This bypasses the more expensive bandlimited sinc interpolation algorithm. It also deliberately changes output samples and frequency response. The correct acceptance model is an optional audio-quality tradeoff, not output equivalence.

Current flow

flowchart TD
    A[Voice needs pitch conversion] --> B[Create persistent callback resampler]
    B --> C[SRC_SINC_FASTEST, 2 channels]
    C --> D[Fetch source frames through callback]
    D --> E[Bandlimited sinc interpolation]
    E --> F[Filtering, HRTF, routing and mix bins]
Loading

The source comment notes that the hardware’s real interpolation may be simpler, but xemu currently retains sinc because its quality is good. That comment does not establish what the Xbox hardware did.

Candidate flow

flowchart TD
    A[Voice/session policy resolved] --> B{Linear approximation selected?}
    B -->|No| C[Create SRC_SINC_FASTEST converter]
    B -->|Yes| D[Create SRC_LINEAR converter]
    C --> E[Use existing callback and voice flow]
    D --> E
    E --> F[Filtering, HRTF, routing and mix bins]
Loading

What disappears

The shortcut removes sinc-filter computation from admitted voices and substitutes linear interpolation. It does not remove:

  • source fetching;
  • resampler callback and persistent state;
  • pitch-ratio handling;
  • loop and completion processing;
  • filtering, HRTF, routing, or mixing;
  • the current two-channel representation.

Mono specialization is a separate proposal documented at Mono Voice Resampling. Combining the two would make attribution and quality analysis much harder.

Apply boundary

The converter type is selected when src_callback_new() creates persistent state. A safe first implementation should therefore be:

  • startup-latched; or
  • latched when a new voice resampler is created; or
  • applied only after a defined voice reset that already discards current resampler history.

Changing converter type live by destroying an active SRC_STATE can alter buffered input, phase, output latency, and completion timing. A menu toggle that claims immediate application without defining that transition is misleading.

Emulation boundary

The guest can observe:

  • produced audio samples;
  • source consumption and playback progression;
  • completion/loop timing;
  • buffer state and notifications;
  • underruns or missed audio deadlines.

The host algorithm is not itself guest-visible, but its numerical and buffering consequences are. Therefore the shortcut is approximate even when gameplay remains functional.

Why it may help

The opportunity is largest when:

  • many active voices require resampling;
  • pitch ratios vary over time;
  • audio-worker CPU is material;
  • the worker approaches its real-time deadline;
  • the cheaper converter reduces callback time enough to prevent queue starvation.

The official libsamplerate converter set explicitly trades quality for computation cost. SRC_LINEAR is described as very fast but poor quality and non-bandlimited, while SRC_SINC_FASTEST remains a bandlimited sinc converter.

Source and ownership map

Area Role
hw/xbox/mcpx/apu/vp/vp.c voice_resample(), callback creation, source fetching, persistent per-voice state
Per-voice filter state Owns the SRC_STATE and resample buffer
Voice reset/off paths Reset or retire persistent converter state
APU worker Executes resampling and downstream processing
Snapshot/lifecycle code Must preserve or explicitly rebuild selected converter state

Signal tests

Different output is expected, so a simple hash comparison is insufficient. Required material includes:

  • silence and DC;
  • impulse response;
  • low-, mid-, and high-frequency tones;
  • logarithmic frequency sweep;
  • white/pink noise;
  • speech and music;
  • fixed ratios such as 0.5, 1.0, and 2.0;
  • continuously changing pitch ratios.

Measure:

  • frequency response and alias products;
  • sample discontinuities;
  • output latency;
  • frames consumed/generated;
  • peak and RMS error relative to the current route;
  • underruns and callback tails.

Playback-state tests

Exercise:

  • loop boundaries;
  • stream starvation and recovery;
  • voice stop/start and pause/resume;
  • pitch changes during active playback;
  • completion notifications;
  • snapshot save/restore with active converter history;
  • renderer/audio device teardown and restart.

A converter that produces acceptable sound but changes guest-visible source progression or completion timing has not preserved the current playback contract.

Seen in xemu

The source-confirmed opportunity is the unconditional creation of a two-channel SRC_SINC_FASTEST converter for resampled voices. No current profile establishes that sinc interpolation is a top APU cost or that linear conversion improves whole-emulator performance.

The first diagnostic must measure voice-resampling time separately from fetching, filters, HRTF, DSP work, and SDL queue pacing.

Counters and evidence

resampler_instances_sinc
resampler_instances_linear
resampler_frames_requested
resampler_input_frames_consumed
resampler_output_frames_generated
resampler_cpu_ns
resampler_callback_p95_p99
apu_deadline_misses
audio_queue_underruns
voice_completion_delta
quality_metric_summary

Use the same executable where possible, but treat converter selection as restart/voice-lifetime state. Record the effective route rather than only the requested setting.

Risks

  • Audible aliasing, especially when downsampling.
  • Harsh high-frequency content or unstable pitch transitions.
  • Different internal buffering and source-consumption timing.
  • Loop or completion timing changes.
  • Snapshot state that does not record/recreate the selected converter.
  • Lower local CPU without meaningful whole-system benefit.
  • Incorrectly describing the result as more Xbox-accurate without hardware evidence.

Qualification

  1. Add a non-persistent diagnostic selection first.
  2. Run signal and playback-state tests before retail performance tests.
  3. Capture APU-worker Release profiles and bounded counters.
  4. Run representative audio-heavy titles and compare guest progress, frame tails, APU CPU, queue health, and listening samples.
  5. Use level-matched randomized listening comparisons.
  6. Define an acceptable quality/error budget before interpreting results.
  7. Keep SRC_SINC_FASTEST as the maintained exact-current-behavior route.
  8. Expose a user option only if the tradeoff is repeatable and understandable.

Disposition

Applicable as an explicit optional approximation. It should not be merged as an invisible optimization and should not be bundled with mono-channel specialization.

Related pages

Full technical reference

Primary reference: This subject page is the design and research reference for Linear Audio Resampling. Pull requests are implementation/test history only.
Lifecycle: Performance - Unproven · Mechanism: Explicit quality/performance approximation
Reviewed source: 322986f4c5502b3fa887a9605ac8cd88e9b2c579 · Acceptance class: intended exact or behavior-preserving for its admitted scope

1. Scope

Use libsamplerate's linear converter instead of the current sinc converter under a clearly labeled audio-quality policy.

This entry remains Unproven: the source path and opportunity are concrete, but no accepted xemu runtime result has established that the candidate improves representative workloads. The page defines what must be bypassed, what remains mandatory, and how the hypothesis can be rejected.

2. Source seams

File / function Role
hw/xbox/mcpx/apu/vp/vp.c voice_resample(), converter creation, callback and persistent resampler state

3. Current execution path

flowchart TD
    N0["Fetch source frames"]
    N1["Persistent SRC_SINC_FASTEST converter"]
    N0 --> N1
    N2["Band-limited interpolation/history"]
    N1 --> N2
    N3["Filter/HRTF/mix"]
    N2 --> N3
    N4["Queue host audio"]
    N3 --> N4
Loading

4. Candidate execution path

flowchart TD
    N0["Fetch source frames"]
    N1["Persistent SRC_LINEAR converter"]
    N0 --> N1
    N2["Cheaper interpolation/history"]
    N1 --> N2
    N3["Unchanged filter/HRTF/mix"]
    N2 --> N3
    N4["Queue host audio"]
    N3 --> N4
Loading

5. Why it may help

The current converter is intentionally higher quality than a simple interpolator. A lower-cost converter may reduce APU worker load or deadline misses in voice-heavy scenes, but it changes samples and potentially buffering/latency. This is not an equivalence optimization and must never be described as one without hardware measurements.

6. Work removed

  • Sinc kernel evaluation and its larger interpolation history
  • Some converter CPU time and potentially converter latency

7. Work that remains mandatory

  • Source fetch and decode
  • Pitch/envelope progression
  • Voice completion and looping
  • Filter/HRTF/mixing
  • Converter state management

8. Implementation sketch

int converter_type = fast_audio_policy
    ? SRC_LINEAR
    : SRC_SINC_FASTEST;

filter->resampler = src_callback_new(
    voice_resample_callback,
    converter_type,
    2,
    &error,
    filter);

The sketch defines the intended boundary; it is not a complete patch and does not replace source review, ownership design, or focused tests.

9. Correctness contract

  • The effective policy must be recorded with evidence; a requested fast mode that failed to create must not masquerade as a comparison.
  • Converter replacement cannot silently discard active phase/history unless the experiment explicitly restarts/recreates the voice.
  • Guest-visible completion, looping, and starvation behavior must remain within the stated compatibility contract.

10. State, ownership, invalidation, and fallback

The converter is persistent per voice/filter, so mode selection should be startup- or voice-creation-latched for the initial experiment. Live switching requires a defined state-transfer or restart boundary and should be treated as a separate problem.

The maintained reference path remains the fallback whenever admission, capability, lifetime, or validation is uncertain. A fast result with an unproven dependency is not acceptable as an exact optimization.

11. Advantages

  • Small implementation change
  • Clear maintained reference route
  • May reduce worker CPU and audio underruns
  • Easy to test with signal analysis and listening trials

12. Costs and failure modes

  • Expected numerical and audible differences
  • Converter latency/input-consumption behavior may differ
  • Could introduce aliasing at high frequencies
  • Cannot be accepted solely from FPS improvement

13. Evidence counters

Counters must be cheap, subsystem-owned, and snapshotted outside the hot loop. Record at least:

  • resampler_input_frames
  • resampler_output_frames
  • converter_cpu_us
  • callback_count
  • audio_queue_depth
  • audio_deadline_misses
  • underruns
  • effective_converter_type

A local reduction is not enough. Evidence must connect route reachability and removed work to fixed guest progress, CPU/GPU time where applicable, and p95/p99 behavior.

14. Qualification matrix

  • Impulse and silence
  • Low/high-frequency tones
  • Log sweep and noise
  • Speech and music clips
  • Ratios 0.5, 1.0, 2.0 and changing pitch
  • Loop, stop/start, starvation, snapshot restore
  • Level-matched randomized listening comparison

Correctness runs may use validation layers, detailed comparisons, and fault injection. Performance acceptance must use optimized Release builds with matching symbols and minimal instrumentation.

15. Acceptance rule

Accept only as an optional approximation when it delivers repeatable CPU/deadline benefit and measured artifacts remain inside a predefined quality target.

16. Rejection rule

Reject as a default or exact optimization if artifacts are audible/unbounded, buffering changes guest behavior, or the APU worker was not actually CPU-bound.

17. Related architecture and history

MCPX-APU-and-Audio · Performance-Unproven · Hot-Path-Audio-Mono-Resampling

PR pages may be linked as supporting evidence, but they are not substitutes for this reference.

Clone this wiki locally