"""Tests for the deterministic, pure discrete-sound approximation.""" from itertools import pairwise from math import sqrt from struct import unpack from galemu.devices import SoundEvent from galemu.synth import ( SAMPLE_RATE, SAMPLES_PER_FRAME, SoundState, synthesize_frame, ) def _samples(pcm: bytes) -> tuple[int, ...]: return unpack(f"<{SAMPLES_PER_FRAME}h", pcm) def _peak(pcm: bytes) -> int: return max(abs(sample) for sample in _samples(pcm)) def _rms(pcm: bytes) -> float: samples = _samples(pcm) return sqrt(sum(sample * sample for sample in samples) / len(samples)) def _zero_crossings(pcm: bytes) -> int: samples = _samples(pcm) return sum(1 for previous, current in pairwise(samples) if (previous < 0) != (current < 0)) def test_synthesizer_emits_exactly_one_integral_audio_frame() -> None: state, pcm = synthesize_frame(SoundState(), ()) assert SAMPLE_RATE == 48_000 assert len(pcm) == SAMPLES_PER_FRAME * 2 assert pcm == b"\0" * len(pcm) assert state.sample_position == SAMPLES_PER_FRAME def test_continuous_tone_latches_persist_across_empty_frames() -> None: state, first_pcm = synthesize_frame( SoundState(), (SoundEvent("sound_tone_enable", 1, 0), SoundEvent("sound_volume", 1, 1)), ) state, second_pcm = synthesize_frame(state, ()) assert first_pcm != b"\0" * len(first_pcm) assert second_pcm != b"\0" * len(second_pcm) assert state.tones == (True, False, False) def test_background_resistor_latches_are_silent_before_the_gameplay_tone_gate() -> None: _state, pcm = synthesize_frame(SoundState(), (SoundEvent("sound_background_resistor", 1, 0),)) assert pcm == b"\0" * len(pcm) def test_gameplay_tone_gate_restores_the_drone_when_volume_latches_are_clear() -> None: _state, pcm = synthesize_frame( SoundState(), ( SoundEvent("sound_background_resistor", 1, 0), SoundEvent("sound_tone_enable", 1, 0), ), ) assert pcm != b"\0" * len(pcm) def test_background_drone_glides_down_and_resets_each_lfo_cycle() -> None: state, pcm = synthesize_frame(SoundState(), (SoundEvent("sound_tone_enable", 1, 0),)) crossings = [] for _ in range(13): crossings.append(_zero_crossings(pcm)) state, pcm = synthesize_frame(state, ()) # At the default DAC latch the LFO ramp takes ~10.7 frames: the tone must # glide down in frequency along the ramp and snap back up at the reset. early = crossings[0] + crossings[1] late = crossings[8] + crossings[9] after_reset = crossings[11] + crossings[12] assert early >= late + 4 assert after_reset >= late + 4 def test_fire_sweeps_upward_while_held_and_fades_after_release() -> None: state, first_pcm = synthesize_frame(SoundState(), (SoundEvent("sound_fire", 1),)) late_pcm = first_pcm for _ in range(17): state, late_pcm = synthesize_frame(state, ()) state, release_pcm = synthesize_frame(state, (SoundEvent("sound_fire", 0),)) faded_pcm = release_pcm for _ in range(30): state, faded_pcm = synthesize_frame(state, ()) assert first_pcm != b"\0" * len(first_pcm) assert release_pcm != b"\0" * len(release_pcm) # The 555 control voltage decays while FIRE is held, so the missile swoop # must rise in frequency, then fade out over the ~100 ms release path. assert _zero_crossings(late_pcm) > _zero_crossings(first_pcm) * 3 // 2 assert _peak(faded_pcm) < _peak(release_pcm) // 10 def test_hit_and_fire_mix_deterministically_without_pcm_overflow() -> None: events = (SoundEvent("sound_hit", 1), SoundEvent("sound_fire", 1)) first_state, first_pcm = synthesize_frame(SoundState(), events) second_state, second_pcm = synthesize_frame(SoundState(), events) assert (first_state, first_pcm) == (second_state, second_pcm) assert all( -32_768 <= sample <= 32_767 for sample in unpack(f"<{SAMPLES_PER_FRAME}h", first_pcm) ) def test_hit_noise_is_loud_while_held_and_decays_after_release() -> None: state, attack_pcm = synthesize_frame(SoundState(), (SoundEvent("sound_hit", 1),)) sustain_pcm = attack_pcm for _ in range(5): state, sustain_pcm = synthesize_frame(state, ()) state, release_pcm = synthesize_frame(state, (SoundEvent("sound_hit", 0),)) tail_pcm = release_pcm for _ in range(11): state, tail_pcm = synthesize_frame(state, ()) quiet_pcm = tail_pcm for _ in range(60): state, quiet_pcm = synthesize_frame(state, ()) assert _peak(attack_pcm) > 2_000 # The explosion is full-amplitude filtered noise while HIT is held — not # just an attack transient — and must stay clearly audible. assert _rms(sustain_pcm) > 4_000 # After release the stored level decays through the ~378 ms RC tail. assert _rms(sustain_pcm) > _rms(tail_pcm) > _rms(quiet_pcm) assert _rms(quiet_pcm) < 500 assert state.hit_gate is False