# SPDX-License-Identifier: MIT """BRR decoding and Gaussian interpolation as pure functions on 15-bit samples. Written from anomie's S-DSP document ("BRR DECODING", "PITCH ADJUSTMENTS") and fullsnes ("SNES APU DSP BRR Samples", "SNES APU DSP BRR Pitch"). Samples are kept as signed 15-bit integers, the form both documents use; the DSP appends a 0 bit to make 16 bits after the envelope. """ from __future__ import annotations from snes_python.dsp.tables import GAUSS def clamp16(value: int) -> int: """Range restriction to signed 16 bits ("clamp" in anomie's terminology).""" if value > 0x7FFF: return 0x7FFF if value < -0x8000: return -0x8000 return value def wrap16(value: int) -> int: """Bit truncation to signed 16 bits, the C ``(int16_t)`` cast anomie writes.""" return ((value + 0x8000) & 0xFFFF) - 0x8000 def wrap15(value: int) -> int: """Bit truncation to signed 15 bits ("clip to 15 bits", anomie).""" return ((value + 0x4000) & 0x7FFF) - 0x4000 def nibble_sample(nibble: int, shift: int) -> int: """RD, the scaled nibble. DOC anomie "BRR DECODING": shifts 0-12 give ``(D << shift) >> 1``; 13-15 give 0 or -$800 by the nibble's sign (fullsnes: "as if shift=12 and nibble SAR 3").""" d = nibble - 16 if nibble & 8 else nibble if shift > 12: return -0x800 if d < 0 else 0 return (d << shift) >> 1 def brr_filter(rd: int, filt: int, old: int, older: int) -> int: """One decoded sample. DOC anomie "BRR DECODING" (filters 0-3, exact form; the same in fullsnes): computed wide, clamped to 16 bits, then clipped to 15 bits (anomie: "clamped to 16 bits at the end and then clipped to 15 bits"; fullsnes lists the resulting glitches).""" if filt == 0: s = rd elif filt == 1: s = rd + old + ((-old) >> 4) elif filt == 2: s = rd + (old << 1) + ((-((old << 1) + old)) >> 5) - older + (older >> 4) else: s = ( rd + (old << 1) + ((-(old + (old << 2) + (old << 3))) >> 6) - older + (((older << 1) + older) >> 4) ) return wrap15(clamp16(s)) def decode_group(header: int, byte1: int, byte2: int, old: int, older: int) -> list[int]: """Four samples from two data bytes, high nibble first (anomie's block format).""" shift = header >> 4 filt = (header >> 2) & 3 out = [] for nibble in (byte1 >> 4, byte1 & 15, byte2 >> 4, byte2 & 15): s = brr_filter(nibble_sample(nibble, shift), filt, old, older) out.append(s) older, old = old, s return out def interpolate(s0: int, s1: int, s2: int, s3: int, phase: int) -> int: """The 4-point Gaussian, oldest sample first, ``phase`` 0-255; returns 15 bits. DOC fullsnes "4-Point Gaussian Interpolation": each product ``gauss * sample SAR 10`` on the 15-bit samples; the first addition cannot overflow, the second wraps at 16 bits, the third saturates to 16 bits; the result SAR 1 is 15 bits. (anomie states the products with ``>> 11`` in a 15-bit sum, which differs in the low bits: undocumented-behavior.md C1.) """ out = (GAUSS[255 - phase] * s0) >> 10 out += (GAUSS[511 - phase] * s1) >> 10 out = wrap16(out + ((GAUSS[256 + phase] * s2) >> 10)) out = clamp16(out + ((GAUSS[phase] * s3) >> 10)) return out >> 1