"""The PSG's and the FM unit's outputs brought to the host's sample rate. The chip (sn76489-python) makes one sample per 16 input clocks, 223,722 Hz on an NTSC console, each a ``(left, right)`` pair of levels 0.0-4.0. This box filter averages them down to the host's rate with an integer phase, so the long-run rate is exact, and scales each side to 0.0-1.0, which is what the frontend and :mod:`sms_python.wav` take. It is sms-python's earlier in-chip filter, moved to the board when the PSG became a core: the analogue path belongs to the board. """ from __future__ import annotations from sn76489_python import CLOCKS_PER_SAMPLE from ym2413_python import CLOCKS_PER_CYCLE, CYCLES_PER_SAMPLE class BoxFilter: """Averages the chip's samples into ``sample_rate`` samples a second.""" def __init__(self, sample_rate: int, clock: int) -> None: self.sample_rate = sample_rate self.clock = clock self._phase = 0 self._left = 0.0 self._right = 0.0 self._count = 0 def feed(self, samples: list[tuple[float, float]]) -> list[tuple[float, float]]: """The host-rate samples that ``samples`` complete; a partial one is carried.""" out = [] step = self.sample_rate * CLOCKS_PER_SAMPLE clock = self.clock for left, right in samples: self._left += left self._right += right self._count += 1 self._phase += step if self._phase >= clock: self._phase -= clock count = self._count * 4 # four channels, each 0.0-1.0 out.append((self._left / count, self._right / count)) self._left = self._right = 0.0 self._count = 0 return out #: The FM unit's level beside the PSG: one FM channel at full scale (its DAC word #: swinging about +-256 in one of the 18 slots: -255 to +255 in ym2413-python's #: ``take_outputs``) as loud as one PSG tone at full volume. #: Not measured: no recording of the two chips' levels on a Japanese Master System or #: a Mark III FM Unit is known (SMS Power's AudioControlPort page and its forum #: threads report only which chips are audible). MAME 0.285 routes both at unity. #: A PSG tone at full volume swings 0.25 of :class:`BoxFilter`'s range; an FM #: channel's mean over a sample's 18 slots swings 512/18 DAC units. FM_PER_UNIT = 0.25 / (512 / CYCLES_PER_SAMPLE) class FMBoxFilter: """Averages the YM2413's samples into ``sample_rate`` samples a second. Each chip sample is the pins' words summed over its 18 cycles (MO + RO: one DAC); their mean is what the board's analogue path integrates. Returns the level in :class:`BoxFilter`'s units, centred on zero. """ def __init__(self, sample_rate: int, clock: int) -> None: self.sample_rate = sample_rate self.clock = clock self._phase = 0 self._total = 0 self._count = 0 def feed(self, samples: list[tuple[int, int]]) -> list[float]: out = [] step = self.sample_rate * CLOCKS_PER_CYCLE * CYCLES_PER_SAMPLE clock = self.clock scale = FM_PER_UNIT / CYCLES_PER_SAMPLE # a sample's summed words to their mean for mo, ro in samples: self._total += mo + ro self._count += 1 self._phase += step if self._phase >= clock: self._phase -= clock out.append(self._total / self._count * scale) self._total = 0 self._count = 0 return out