Skip to content

NesEnvelope ​

Struct

Source: AudioTypes.h

Description ​

NES APU envelope unit state for PULSE 1, PULSE 2, and NOISE.

Mirrors the $4000 / $4004 / $400C write side effects (loop flag, constant volume flag, VVVV) and the $4003 / $4007 / $400F length load side effect (start flag). Clocks at quarter-frame rate (240 Hz NTSC mode 0) via the M3 tickAllNesEnvelopes() dispatcher.

The loop flag is SHARED with the length counter (halt bit): tickAllNesEnvelopes() reads lengthCounter->halt to decide whether the decay level should wrap to 15 on underflow (loop) or silence the channel (!loop). The lengthCounter pointer is bound in AudioChannel::reset() so the two sub-units see the same bit.

When envelopeEnabled == true on a PULSE or NOISE voice, the envelope's output (0..15) REPLACES the ADSR envelope.currentLevel as the per-sample volume source. When the envelope reaches 0 and loop is clear, the voice is silenced directly (matches the NES hardware, which gates the channel on the envelope output).

Default state: envelopeEnabled == false on construction, so the quarter-frame dispatch is a no-op and the canonical track PCM remains byte-for-byte identical. Hito 2 M7.

Source: https://www.nesdev.org/wiki/APU_Envelope

Properties ​

NameTypeDescription
envelopeEnabledboolPer-voice opt-in. False = quarter-clock dispatch is a no-op for this voice.
startFlagboolSet by $4003 / $4007 / $400F write; cleared on the next quarter-clock.
constVolumeboolC bit (constant volume flag). True = use VVVV directly as output.
volumeuint8_tVVVV (4 bits) — constant volume (C=1) or divider reload value (C=0).
dividerCounteruint8_tCurrent divider counter, decremented each quarter-clock while > 0.
decayLeveluint8_t0..15. Decremented on each divider wrap; wraps to 15 on loop.
outputuint8_t0..15, computed output = constVolume ? volume : decayLevel.
lengthCounterNesLengthCounter*Bound in AudioChannel::reset() to share the halt/loop flag with the length counter.

Released under the MIT License.