"""One operator: a phase counter, an envelope, and the log-sine arithmetic. Every rule names its section of ``docs/spec.md``; ``docs/provenance-audit.md`` gives the chain behind each. PROVISIONAL marks a default no document or measurement settles; "emulator tier" means the default is ymfm's (BSD-3-Clause), named because nothing better is in an allowed source. CHOICE marks what no source speaks to. """ from __future__ import annotations from ym2612_python.tables import ( DETUNE, ENVELOPE_INCREMENTS, ENVELOPE_SHIFT, EXPONENT, LOG_SINE, ) #: Envelope phases (spec 7.1.1). ATTACK, DECAY, SUSTAIN, RELEASE = 0, 1, 2, 3 #: The envelope's quietest value: 10 bits (spec 7.1.1, measured). MAX_ATTENUATION = 0x3FF #: Sustain level 15 as a 10-bit attenuation (spec 7.4.7, contradiction C9, decision #: T6). PROVISIONAL: $3E0 is the manual's "93 dB"; Nemesis measured $3FF (p7967). SUSTAIN_LEVEL_15 = 0x3E0 #: SSG-EG works on the attenuation's top bit (spec 7.6, measured: p7967, p8025). SSG_THRESHOLD = 0x200 #: Bits of the SSG-EG register (spec 3.2: documented). SSG_ENABLE, SSG_ATTACK, SSG_ALTERNATE, SSG_HOLD = 8, 4, 2, 1 class Operator: """One of a channel's four operators. All state is plain integers. Registers (spec 3.2): ``detune`` (3 bits, bit 2 the sign), ``multiple``, ``total_level``, ``key_scale``, ``attack_rate``, ``tremolo`` (the enable bit), ``decay_rate``, ``sustain_rate``, ``sustain_level``, ``release_rate``, ``ssg``. Frequency, set by the chip from the channel's registers or, for channel 3 in its special mode, from the operator's own (spec 10.2): ``f_number``, ``block``, ``key_code``. State: ``phase`` (the 20-bit counter, spec 5.1), ``envelope_phase``, ``attenuation`` (10 bits, 0 loudest), ``ssg_flag`` (the SSG-EG inversion flag, spec 7.6.2), ``key_request`` (this operator's bit of the last $28 write), ``key_state`` (the key as last acted on). """ __slots__ = ( "attack_rate", "attenuation", "block", "decay_rate", "detune", "envelope_phase", "f_number", "key_code", "key_request", "key_scale", "key_state", "multiple", "phase", "release_rate", "ssg", "ssg_flag", "sustain_level", "sustain_rate", "total_level", "tremolo", ) # fmt: skip def __init__(self) -> None: self.detune = 0 self.multiple = 0 self.total_level = 0 self.key_scale = 0 self.attack_rate = 0 self.tremolo = 0 self.decay_rate = 0 self.sustain_rate = 0 self.sustain_level = 0 self.release_rate = 0 self.ssg = 0 self.f_number = 0 self.block = 0 self.key_code = 0 self.phase = 0 # Spec 7.5, decision T14 (PROVISIONAL, emulator tier): release at the # quietest level, key off. self.envelope_phase = RELEASE self.attenuation = MAX_ATTENUATION self.ssg_flag = 0 self.key_request = 0 self.key_state = 0 # -- phase generator (spec 5) ------------------------------------------------ def phase_increment(self, f_number: int) -> int: """The 20-bit amount added to the phase each sample (spec 5.2 to 5.8). ``f_number`` is the 11-bit F-number after vibrato. All measured by Nemesis (p6177), with the worked values of rung 1. """ # Spec 5.3: block 0 shifts right once, block 1 not at all, 2 to 7 left. increment = (f_number << self.block) >> 1 # Spec 5.6, 5.7: detune by key code, kept to 17 bits, so that a negative # detune on a small increment wraps to a very large one. magnitude = self.detune & 3 if magnitude: amount = DETUNE[magnitude][self.key_code] if self.detune & 4: increment = (increment - amount) & 0x1FFFF else: increment = (increment + amount) & 0x1FFFF # Spec 5.8: the multiple; 0 halves. Kept to 20 bits. if self.multiple: return (increment * self.multiple) & 0xFFFFF return increment >> 1 # -- envelope generator (spec 7) --------------------------------------------- def rate(self, register: int) -> int: """Spec 7.2.1, 7.2.2 (documented, Yamaha): 2R + Rks; 0 if R is 0; at most 63. Rks is the key code shifted right by 3, 2, 1, 0 for key scale 0 to 3. """ if register == 0: return 0 rate = 2 * register + (self.key_code >> (3 - self.key_scale)) return min(rate, 63) def inverted(self) -> bool: """Whether SSG-EG inverts the output now (spec 7.6.2: measured, p8025).""" ssg = self.ssg return bool( ssg & SSG_ENABLE and self.envelope_phase != RELEASE and ((ssg & SSG_ATTACK) != 0) != (self.ssg_flag != 0) ) def set_key(self, key: int) -> None: """Act on the key if it changed (spec 7.4.3, 7.4.8, 7.4.9; every sample).""" if key == self.key_state: return # spec 7.4.8: a repeated key-on or key-off changes nothing self.key_state = key if key: # Spec 7.4.3: attack from where the envelope stands; the level is not # reset (p5724, p7999). Spec 5.9: the phase counter is cleared (p7967). # Spec 7.6.2: the inversion flag is cleared (p8025). self.envelope_phase = ATTACK self.attenuation = 0x3FF self.phase = 0 self.ssg_flag = 0 # Spec 7.4.4 (measured, p7967): rates 62 and 63 go to 0 at once. if self.rate(self.attack_rate) >= 62: self.attenuation = 0 else: # Spec 7.6.5, PROVISIONAL, emulator tier (unknown U23): release goes # on from the level that was being heard. if self.inverted(): self.attenuation = (SSG_THRESHOLD - self.attenuation) & 0x3FF self.envelope_phase = RELEASE def ssg_step(self) -> None: """The SSG-EG work done every sample, before any envelope update (spec 7.6.3). Measured by Nemesis and stated in prose (p7967, p8025, p8032, p8037), except the held-silent case, which follows from the manual's eight shapes. """ ssg = self.ssg if not ssg & SSG_ENABLE or self.attenuation < SSG_THRESHOLD: return if ssg & SSG_ALTERNATE and (not ssg & SSG_HOLD or not self.ssg_flag): self.ssg_flag ^= 1 if not ssg & (SSG_ALTERNATE | SSG_HOLD): self.phase = 0 phase = self.envelope_phase if phase == ATTACK: return if phase != RELEASE and not ssg & SSG_HOLD: # The pattern repeats: attack again, at once if the rate is 62 or 63. self.envelope_phase = ATTACK if self.rate(self.attack_rate) >= 62: self.attenuation = 0 elif phase == RELEASE or not self.inverted(): # Release reaching $200 goes silent (p8025, p8032). A held pattern that # ends not inverted goes silent (shapes 001 and 111: derived). self.attenuation = MAX_ATTENUATION def envelope_step(self, counter: int) -> None: """One envelope update, every third sample (spec 7.3, 7.4).""" phase = self.envelope_phase attenuation = self.attenuation # Spec 7.4.6 (measured, p7967): attack ends at 0; decay ends when the level # reaches or passes the sustain level, without being cut back to it, and # the change is one-way. CHOICE of order: both are looked at before the # step, as ymfm does, so "the sustain phase begins on the next cycle". if phase == ATTACK and attenuation == 0: phase = self.envelope_phase = DECAY if phase == DECAY: level = self.sustain_level if attenuation >= (SUSTAIN_LEVEL_15 if level == 15 else level << 5): phase = self.envelope_phase = SUSTAIN # Spec 7.2.3 (measured, p7967): the rate is worked out at every update. if phase == ATTACK: rate = self.rate(self.attack_rate) elif phase == DECAY: rate = self.rate(self.decay_rate) elif phase == SUSTAIN: rate = self.rate(self.sustain_rate) else: # Spec 7.2.1: the 4-bit release rate doubled plus one, never 0 (p5724). rate = self.rate(2 * self.release_rate + 1) # Spec 7.3.2: the shared counter's low bits gate the update and the three # bits above them choose one of eight increments. shift = ENVELOPE_SHIFT[rate] if counter & ((1 << shift) - 1): return increment = ENVELOPE_INCREMENTS[rate][(counter >> shift) & 7] if phase == ATTACK: # Spec 7.4.5 (measured, p7967): at 62 or 63 a running attack stalls. # Spec 7.4.2, PROVISIONAL, emulator tier (unknown U22): the curve. if rate < 62: self.attenuation = attenuation + ((~attenuation * increment) >> 4) elif self.ssg & SSG_ENABLE: # Spec 7.6.1: four times the step (measured), only below $200 (derived). if attenuation < SSG_THRESHOLD: attenuation += 4 * increment self.attenuation = attenuation if attenuation < 0x400 else MAX_ATTENUATION else: # Spec 7.4.1 (measured): add, limited. attenuation += increment self.attenuation = attenuation if attenuation < 0x400 else MAX_ATTENUATION def level(self, tremolo: int) -> int: """The attenuation handed to the operator unit this sample (spec 7.1.2). Measured (p5716, p7967): SSG-EG's inversion on a copy first, then the total level, then tremolo, limited to 10 bits. """ level = self.attenuation if self.ssg & SSG_ENABLE and self.inverted(): level = (SSG_THRESHOLD - level) & 0x3FF # spec 7.6.2: $001 -> $1FF level += self.total_level << 3 if self.tremolo: level += tremolo return level if level < 0x400 else MAX_ATTENUATION # -- one sample (spec 5, 6) -------------------------------------------------- def clock(self, increment: int, modulation: int, tremolo: int) -> int: """Advance the phase and return the signed 14-bit output (spec 6). ``modulation`` is the 10-bit phase offset from other operators or from feedback (spec 9.3, 9.4). The output is worked from the advanced phase: with the counter frozen at 0 by test bit $21:3 "the phase equals the increment" (spec 17). """ phase = self.phase = (self.phase + increment) & 0xFFFFF # Spec 6.2: ten bits; bit 8 mirrors the quarter wave, bit 9 negates. angle = (phase >> 10) + modulation index = angle & 0xFF if angle & 0x100: index ^= 0xFF # Spec 6.4: the envelope's 4.6 attenuation becomes 4.8 and is added. total = LOG_SINE[index] + (self.level(tremolo) << 2) # Spec 6.5: the fraction indexes the exponent ROM; the whole part shifts. value = (EXPONENT[total & 0xFF] << 2) >> (total >> 8) return -value if angle & 0x200 else value