summaryrefslogtreecommitdiff
path: root/tests/test_synthesizer.py
blob: 3d7b61e20ca49a7fed9cee543d80fb70aa077166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""Tests for the retro APU synthesizer engine."""

import wave

import numpy as np
import pytest

from mml_composemusic_mcp.ir import (
    NoteEvent,
    NoteSequence,
    TempoEvent,
    VolumeEnvelopeEvent,
    VolumeEvent,
)
from mml_composemusic_mcp.synthesizer import (
    NOISE_PERIODS,
    PULSE_SEQUENCES,
    TRIANGLE_SEQUENCE,
    ChannelSynthesizer,
    TempoMap,
    apu_mix,
    build_channel_summary,
    build_tempo_map,
    frequency_to_timer,
    midi_to_freq,
    noise_period_to_rate,
    synthesize,
    tick_to_second,
    timer_to_frequency,
    write_wav,
)

# --- Utility functions ---


def test_midi_to_freq_a4():
    assert midi_to_freq(69) == pytest.approx(440.0)


def test_midi_to_freq_c4():
    assert midi_to_freq(60) == pytest.approx(261.63, rel=1e-2)


def test_midi_to_freq_high():
    assert midi_to_freq(127) > 8000


def test_noise_period_to_rate():
    rate = noise_period_to_rate(8, 0, 44100)
    assert rate > 0


def test_ntsc_noise_period_table():
    assert NOISE_PERIODS == (
        4,
        8,
        16,
        32,
        64,
        96,
        128,
        160,
        202,
        254,
        380,
        508,
        762,
        1016,
        2034,
        4068,
    )


def test_apu_step_sequences():
    assert [sum(sequence) for sequence in PULSE_SEQUENCES] == [1, 2, 4, 6]
    assert TRIANGLE_SEQUENCE[:16] == tuple(range(15, -1, -1))
    assert TRIANGLE_SEQUENCE[16:] == tuple(range(16))


def test_timer_quantization_matches_2a03_formula():
    timer = frequency_to_timer(440.0, "pulse")
    assert timer == round(1_789_772.5 / (16 * 440.0) - 1)
    assert timer_to_frequency(timer, "pulse") == pytest.approx(440.0, rel=0.003)


def test_nonlinear_apu_mixer():
    one = np.array([15.0])
    zero = np.array([0.0])
    single = apu_mix(one, zero, zero, zero)[0]
    double = apu_mix(one, one, zero, zero)[0]
    assert 0 < single < double < single * 2


def test_tempo_map_integrates_mid_song_change():
    tempo = TempoMap([(0, 120), (192, 60)])
    assert tempo.tick_to_second(192) == pytest.approx(0.5)
    assert tempo.tick_to_second(384) == pytest.approx(1.5)


def test_note_sequence_tempo_map_keeps_default_before_late_change():
    ns = NoteSequence(bpm=120)
    ns.channels["Pulse1"].events.append(TempoEvent(tick_position=192, bpm=60))
    tempo = build_tempo_map(ns)
    assert tempo.tick_to_second(192) == pytest.approx(0.5)
    assert tempo.tick_to_second(384) == pytest.approx(1.5)


def test_tick_to_second():
    assert tick_to_second(192, 120) == pytest.approx(0.5, rel=1e-6)


def test_tick_to_second_zero():
    assert tick_to_second(0, 120) == 0.0


# --- Channel synthesizer ---


def test_pulse_render():
    synth = ChannelSynthesizer("pulse", 44100)
    events = [NoteEvent(tick_position=0, duration=192, note_number=69, velocity=15)]
    wave_out = synth.render(events, 192, 120, "ppmck")
    assert len(wave_out) > 0
    assert np.any(wave_out != 0)


def test_triangle_render_ppmck():
    synth = ChannelSynthesizer("triangle", 44100)
    events = [NoteEvent(tick_position=0, duration=192, note_number=69, velocity=7)]
    wave_out = synth.render(events, 192, 120, "ppmck")
    assert len(wave_out) > 0
    assert np.any(wave_out != 0)
    # Raw channel output is the 2A03's four-bit DAC value.
    peak = np.max(np.abs(wave_out))
    assert peak == 15


def test_triangle_render_pyxel():
    synth = ChannelSynthesizer("triangle", 44100)
    events = [
        VolumeEvent(tick_position=0, value=7),
        NoteEvent(tick_position=0, duration=192, note_number=69, velocity=7),
    ]
    wave_out = synth.render(events, 192, 120, "pyxel")
    assert len(wave_out) > 0
    assert np.any(wave_out != 0)
    # Triangle volume is not controlled by the 2A03 DAC.
    peak = np.max(np.abs(wave_out))
    assert peak == 15


def test_noise_render_ppmck():
    synth = ChannelSynthesizer("noise", 44100)
    events = [NoteEvent(tick_position=0, duration=192, note_number=0, velocity=10)]
    wave_out = synth.render(events, 192, 120, "ppmck")
    assert len(wave_out) > 0
    assert np.any(wave_out != 0)


def test_noise_render_pyxel():
    synth = ChannelSynthesizer("noise", 44100)
    events = [NoteEvent(tick_position=0, duration=192, note_number=48, velocity=10)]
    wave_out = synth.render(events, 192, 120, "pyxel")
    assert len(wave_out) > 0
    assert np.any(wave_out != 0)


def test_noise_short_mode_uses_different_lfsr_tap():
    events = [NoteEvent(tick_position=0, duration=192, note_number=48, velocity=15)]
    long_wave = ChannelSynthesizer("noise", 8000, noise_mode=0).render(
        events, 192, 120, "pyxel"
    )
    short_wave = ChannelSynthesizer("noise", 8000, noise_mode=1).render(
        events, 192, 120, "pyxel"
    )
    assert not np.array_equal(long_wave, short_wave)


# --- Full synthesize ---


def test_synthesize_basic():
    ns = NoteSequence(bpm=120)
    ns.channels["Pulse1"].events.append(
        NoteEvent(tick_position=0, duration=192, note_number=60, velocity=15)
    )
    ns.channels["Pulse1"].total_ticks = 192
    wave_data, duration, errors = synthesize(ns, "ppmck", 44100, True)
    assert len(wave_data) > 0
    assert duration > 0
    assert errors == []


def test_synthesize_empty():
    ns = NoteSequence(bpm=120)
    wave_data, duration, errors = synthesize(ns, "ppmck", 44100, True)
    assert len(wave_data) == 0
    assert duration == 0


def test_synthesize_normalize():
    ns = NoteSequence(bpm=120)
    ns.channels["Pulse1"].events.append(
        NoteEvent(tick_position=0, duration=192, note_number=60, velocity=15)
    )
    ns.channels["Pulse1"].total_ticks = 192
    on_data, _, _ = synthesize(ns, "ppmck", 44100, True)
    off_data, _, _ = synthesize(ns, "ppmck", 44100, False)
    # Normalized should have peak ~1.0
    assert np.max(np.abs(on_data)) == pytest.approx(1.0, rel=1e-2)


def test_volume_envelope_changes_apu_dac_at_48_ticks():
    definitions = {"volume_envelopes": {1: {"points": [15, 0], "loop_points": []}}}
    events = [
        VolumeEnvelopeEvent(tick_position=0, slot=1),
        NoteEvent(tick_position=0, duration=96, note_number=69, velocity=15),
    ]
    wave_out = ChannelSynthesizer("pulse", 8000).render(
        events, 96, 120, "ppmck", definitions
    )
    midpoint = len(wave_out) // 2
    assert np.max(wave_out[:midpoint]) == 15
    assert np.max(wave_out[midpoint:]) == 0


# --- WAV output ---


def test_write_wav_success(tmp_path):
    path = tmp_path / "test.wav"
    data = np.array([0.0, 0.5, -0.5, 0.0], dtype=np.float64)
    errors = write_wav(path, data, 44100)
    assert not errors
    assert path.exists()
    with wave.open(str(path), "rb") as wav:
        assert wav.getnchannels() == 1
        assert wav.getsampwidth() == 2
        assert wav.getframerate() == 44100


def test_write_wav_failure(tmp_path):
    blocker = tmp_path / "blocker"
    blocker.write_text("x")
    bad_path = blocker / "output.wav"
    data = np.array([0.0, 0.1], dtype=np.float64)
    errors = write_wav(bad_path, data, 44100)
    if errors:
        from mml_composemusic_mcp.ir import ErrorCode

        assert errors[0].code == ErrorCode.RUNTIME_WAV_WRITE_FAILED


# --- Channel summary ---


def test_build_channel_summary():
    ns = NoteSequence(bpm=120)
    ns.channels["Pulse1"].events.append(
        NoteEvent(tick_position=0, duration=192, note_number=60, velocity=15)
    )
    ns.channels["Pulse1"].events.append(
        NoteEvent(tick_position=192, duration=192, note_number=72, velocity=15)
    )
    ns.channels["Pulse1"].total_ticks = 384
    summary = build_channel_summary(ns)
    pulse1 = [s for s in summary if s["channel"] == "Pulse1"][0]
    assert pulse1["note_count"] == 2
    assert pulse1["duration_ticks"] == 384


def test_build_channel_summary_from_dict():
    ns = NoteSequence(bpm=120)
    ns.channels["Pulse1"].events.append(
        NoteEvent(tick_position=0, duration=192, note_number=60, velocity=15)
    )
    ns.channels["Pulse1"].total_ticks = 192
    ns_dict = ns.to_dict()
    summary = build_channel_summary(ns_dict)
    assert summary
    pulse1 = [s for s in summary if s["channel"] == "Pulse1"][0]
    assert pulse1["note_count"] == 1