# SPDX-License-Identifier: MIT AND ISC """Fixed tables of the S-DSP: the Gaussian interpolation table, the counter rates, the schedule. ``gaussian_table`` evaluates ares's formula (ISC; notice below). Everything else here is written from anomie's S-DSP document (r1212, with jwdonal's updates), fullsnes ("SNES APU DSP ...") and the SNESdev wiki; each entry names its section. The Gaussian formula is taken from ares (``ares/sfc/dsp/gaussian.cpp``, pinned 9408cb43): ares - Copyright (c) 2004-2025 ares team, Near et al Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ from __future__ import annotations import math # SHA-256 of the 512 entries packed as little-endian int16. DOC: the same 512 values are printed # in anomie's document ("PITCH ADJUSTMENTS") and in fullsnes ("SNES APU DSP BRR Pitch"); ares's # formula reproduces them all (tests/dsp/test_tables.py checks both). GAUSSIAN_SHA256 = "0acd5077942f6cc6676829bc1954a05aeaad51be716f62d9ef4e8e7c94685af2" def gaussian_table() -> tuple[int, ...]: """ares's formula (ISC, notice above): a windowed sinc, each phase's four taps scaled to 2048.""" raw = [0.0] * 512 for n in range(512): k = 0.5 + n s = math.sin(math.pi * k * 1.280 / 1024) t = (math.cos(math.pi * k * 2.000 / 1023) - 1) * 0.50 u = (math.cos(math.pi * k * 4.000 / 1023) - 1) * 0.08 raw[511 - n] = s * (t + u + 1.0) / k table = [0] * 512 for phase in range(128): taps = (phase, phase + 256, 511 - phase, 255 - phase) scale = 2048.0 / sum(raw[i] for i in taps) for i in taps: table[i] = int(raw[i] * scale + 0.5) return tuple(table) GAUSS = gaussian_table() # DOC anomie "COUNTERS"; the same periods in fullsnes "ADSR/Gain (and Noise) Rates" and the SNESdev # wiki "DSP envelopes" period table. Samples per event for rate R; rate 0 never fires. COUNTER_RATES = ( 0, 2048, 1536, 1280, 1024, 768, 640, 512, 384, 320, 256, 192, 160, 128, 96, 80, 64, 48, 40, 32, 24, 20, 16, 12, 10, 8, 6, 5, 4, 3, 2, 1, ) # fmt: skip # DOC anomie "COUNTERS": the offset that aligns rates sharing a period. COUNTER_OFFSETS = ( 0, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 536, 0, 1040, 0, 0, ) # fmt: skip # DOC anomie "COUNTERS": counts from $77FF down to 0, decremented once per sample; 0 at reset. COUNTER_RANGE = 0x7800 # The step on which each voice operation runs, for voices 0-7. DOC anomie's per-register # "accessed during ... cycles" lists (S-DSP REGISTERS) and his sample loop (SOUND GENERATION); # fullsnes "SNES APU Low Level Timings" gives the same chart one step later in its numbering. STEP_S1 = (17, 20, 31, 2, 5, 8, 11, 14) STEP_S2 = (21, 0, 3, 6, 9, 12, 15, 18) STEP_S3A = (22, 1, 4, 7, 10, 13, 16, 19) STEP_S3B = (25, 1, 4, 7, 10, 13, 16, 19) STEP_S3C = (30, 1, 4, 7, 10, 13, 16, 19) STEP_S4 = (31, 2, 5, 8, 11, 14, 17, 20) STEP_S5 = (0, 3, 6, 9, 12, 15, 18, 21) STEP_S6 = (1, 4, 7, 10, 13, 16, 19, 22) STEP_S7 = (2, 5, 8, 11, 14, 17, 20, 23) STEP_S8 = (3, 6, 9, 12, 15, 18, 21, 24) STEP_S9 = (4, 7, 10, 13, 16, 19, 22, 25)