Skip to main content

Module stems

Module stems 

Source
Expand description

v3.6.0 (T-AUDIO-STEMS) — per-channel audio stems, rendered offline from the APU’s own per-cycle mix trace.

A stem is one sound source rendered on its own: pulse 1, pulse 2, the triangle, the noise channel, the DMC, and the cartridge’s expansion audio (VRC6, VRC7, Namco 163, Sunsoft 5B, MMC5, FDS, or an NSF’s chips). A musician wants them as separate files that line up sample for sample with the mixed output, so they can be remixed, and a researcher wants them to hear what one channel is doing under the others.

§How they are made: one emulation pass, re-rendered offline

The emulation runs once, unchanged, with the APU’s audio provenance armed (Nes::set_audio_provenance, the v2.3.7 “Overtone” debug facility). While armed, the APU records, for every CPU cycle, the five raw channel outputs it handed the mixer and the raw expansion sample (MixRecord). StemCapture reads that trace after each frame and pushes each source, alone, through a private copy of the same output pipeline the console audio goes through: the same lookup-table mixer, a band-limited buffer built with the same sample rate and CPU clock, and the same analog filter model. Nothing is added to the APU’s per-cycle work, and nothing in the emulation changes: the stems are a function of a trace the core already knew how to record. With provenance disarmed (every other run) the core is exactly what it was; with it armed, the recording is output-only and is never read back into synthesis (see rustynes_apu::provenance).

The capture also re-renders the mix itself from the trace, through its own pipeline, and compares it with what the core produced (StemCapture::mix_mismatches). The two are bit-identical: the same values were added to buffers built the same way in the same order. That comparison is what licenses the stems — it shows the private pipeline is the console’s pipeline, rather than a lookalike.

§What “the stems sum to the mix” means, precisely

The 2A03 mixer is not linear (nesdev “APU Mixer”):

out = pulse_table[p1 + p2] + tnd_table[3*tri + 2*noise + dmc] + expansion
pulse_table[n] = 95.52 / (8128 / n + 100)
tnd_table[n]   = 163.67 / (24329 / n + 100)

Both tables are concave, so two channels sounding together come out quieter than the two alone added up. Each stem here is its channel rendered alone through that same mixer — pulse 1’s stem is pulse_table[p1], the noise’s is tnd_table[2*noise], and so on — because that is what the channel sounds like on the console with every other channel silent. The sum of the six source stems is therefore not the mix; it differs by the mixer’s interaction term

interaction = out - (pulse_table[p1] + pulse_table[p2]
                     + tnd_table[3*tri] + tnd_table[2*noise] + tnd_table[dmc]
                     + expansion)

which is zero whenever at most one channel of each group is non-zero, and never positive before the filter (concavity). The capture renders that term as a seventh track, Stem::Interaction, through the same pipeline. The band-limited buffer and the filter chain are linear, so

mix = Σ six source stems + interaction      (to f32 rounding)

and the residual is measured, not assumed: StemCapture::max_sum_error holds the largest |mix - Σ all seven| seen, which the gate in this module’s tests and in rustynes-test-harness/tests/audio_stems.rs bounds (measured at about 1e-6 against a signal near ±0.2; see docs/audio-stems.md). The expansion stem enters the mixer linearly, so it has no interaction term of its own.

§What is not split

The expansion stem is the cartridge’s whole expansion output, the value the board’s mix_audio returns. A board with one chip (every cartridge) gets one stem named after it. An NSF that enables several chips at once gets them summed into the one stem, because the trace records only their sum; splitting them would need a per-chip record in the mapper’s per-cycle path, which this design deliberately does not add. Nor are a chip’s own voices (VRC6’s two pulses and saw, N163’s eight channels) split.

Structs§

StemCapture
A running stem capture over one Nes.
StemFrame
One frame’s samples, borrowed from the capture.

Enums§

Stem
One rendered track.
StemError
Why a capture could not continue.

Constants§

STEM_COUNT
The number of tracks a StemCapture renders (Stem::ALL).

Functions§

stem_values
The per-cycle value each track receives for one mix record.