summaryrefslogtreecommitdiff
path: root/src/mml_composemusic_mcp/synthesizer.py
blob: 42a3c11737fa2438e22a46e3acd2b421ba4fcf65 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""NES APU-style synthesizer: NoteSequence -> WAV."""

import wave
from math import floor
from pathlib import Path

import numpy as np

from .ir import (
    DutyEvent,
    ErrorCode,
    ErrorDetail,
    NoteEvent,
    NoteSequence,
    RestEvent,
    TempoEvent,
    VolumeEvent,
)

CPU_CLOCK = 1_789_772.5
SAMPLE_RATE_DEFAULT = 44100


def midi_to_freq(note_number: int) -> float:
    return 440.0 * 2.0 ** ((note_number - 69) / 12.0)


def noise_period_to_rate(period: int, mode: int, sample_rate: int) -> float:
    # NES noise: rate = CPU_CLOCK / (NTSC divider for period)
    # Approximate using pyxel mapping described in design
    dividers = [
        4,
        8,
        16,
        32,
        64,
        96,
        128,
        160,
        202,
        254,
        380,
        508,
        762,
        1016,
        2034,
        4068,
    ]
    div = dividers[period & 0x0F]
    # Long mode (0) vs short mode (1) affects LFSR length, not frequency divider
    return CPU_CLOCK / div


class ChannelSynthesizer:
    def __init__(
        self, channel_type: str, sample_rate: int = SAMPLE_RATE_DEFAULT
    ) -> None:
        self.channel_type = channel_type
        self.sample_rate = sample_rate
        self.wave = np.zeros(0, dtype=np.float64)

    def render(
        self,
        events: list,
        total_ticks: int,
        bpm: int,
        mode: str,
    ) -> np.ndarray:
        seconds = tick_to_second(total_ticks, bpm)
        length = int(seconds * self.sample_rate)
        self.wave = np.zeros(length, dtype=np.float64)
        last_velocity = 15 if self.channel_type != "triangle" else 15
        last_duty = 2
        for event in events:
            if isinstance(event, VolumeEvent):
                last_velocity = event.value
                continue
            if isinstance(event, DutyEvent):
                last_duty = event.value
                continue
            if isinstance(event, TempoEvent):
                bpm = event.bpm
                continue
            if not isinstance(event, (NoteEvent, RestEvent)):
                continue
            start_sample = int(
                tick_to_second(event.tick_position, bpm) * self.sample_rate
            )
            end_tick = event.tick_position + event.duration
            end_sample = min(
                int(tick_to_second(end_tick, bpm) * self.sample_rate), length
            )
            if start_sample >= length or end_sample <= start_sample:
                continue
            if isinstance(event, RestEvent):
                continue
            self._render_note(
                start_sample,
                end_sample,
                event,
                last_velocity,
                last_duty,
                mode,
            )
        return self.wave

    def _render_note(
        self,
        start: int,
        end: int,
        event: NoteEvent,
        velocity: int,
        duty: int,
        mode: str,
    ) -> None:
        duration_samples = end - start
        if duration_samples <= 0:
            return
        if self.channel_type == "noise":
            self._render_noise(start, end, event, velocity, mode)
            return
        if self.channel_type == "triangle":
            self._render_triangle(start, end, event, velocity, mode)
            return
        self._render_pulse(start, end, event, velocity, duty)

    def _render_pulse(
        self,
        start: int,
        end: int,
        event: NoteEvent,
        velocity: int,
        duty: int,
    ) -> None:
        freq = midi_to_freq(event.note_number)
        if freq <= 0:
            return
        phase_inc = freq / self.sample_rate
        duty_cycle = {0: 0.125, 1: 0.25, 2: 0.5, 3: 0.75}.get(duty, 0.5)
        samples = np.arange(end - start)
        phase = (samples * phase_inc) % 1.0
        waveform = np.where(phase < duty_cycle, 1.0, -1.0)
        amp = velocity / 15.0
        gate = max(0.0, min(1.0, event.gate_time))
        if gate < 1.0:
            gate_sample = int((end - start) * gate)
            if gate_sample < len(waveform):
                waveform[gate_sample:] = 0.0
        self.wave[start:end] += waveform * amp

    def _render_triangle(
        self,
        start: int,
        end: int,
        event: NoteEvent,
        velocity: int,
        mode: str,
    ) -> None:
        freq = midi_to_freq(event.note_number)
        if freq <= 0:
            return
        phase_inc = freq / self.sample_rate
        samples = np.arange(end - start)
        phase = (samples * phase_inc) % 1.0
        # 32-step triangle approximated by abs(phase*2-1)
        waveform = 2.0 * np.abs(phase * 2.0 - 1.0) - 1.0
        if mode == "pyxel":
            amp = velocity / 15.0
        else:
            amp = 1.0
        gate = max(0.0, min(1.0, event.gate_time))
        if gate < 1.0:
            gate_sample = int((end - start) * gate)
            if gate_sample < len(waveform):
                waveform[gate_sample:] = 0.0
        self.wave[start:end] += waveform * amp

    def _render_noise(
        self,
        start: int,
        end: int,
        event: NoteEvent,
        velocity: int,
        mode: str,
    ) -> None:
        if mode == "pyxel":
            period = max(0, min(15, 15 - floor(event.note_number / 8)))
            mode_flag = 1 if period >= 13 else 0
        else:
            period = 8
            mode_flag = 0
        rate = noise_period_to_rate(period, mode_flag, self.sample_rate)
        phase_inc = rate / self.sample_rate
        samples = np.arange(end - start)
        phase = (samples * phase_inc) % 1.0
        diff = np.diff(phase, prepend=0)
        transitions = diff < 0
        # LFSR-like pseudo-random toggling on each period rollover
        state = np.zeros(len(samples), dtype=np.int32)
        lfsr = 1
        for i in range(len(samples)):
            if transitions[i]:
                # feedback bit
                if mode_flag:
                    bit = (lfsr ^ (lfsr >> 6)) & 1
                    lfsr = ((lfsr >> 1) | (bit << 14)) & 0x7FFF
                else:
                    bit = (lfsr ^ (lfsr >> 1)) & 1
                    lfsr = ((lfsr >> 1) | (bit << 14)) & 0x7FFF
            state[i] = lfsr & 1
        waveform = np.where(state, 1.0, -1.0)
        amp = velocity / 15.0
        gate = max(0.0, min(1.0, event.gate_time))
        if gate < 1.0:
            gate_sample = int((end - start) * gate)
            if gate_sample < len(waveform):
                waveform[gate_sample:] = 0.0
        self.wave[start:end] += waveform * amp


def tick_to_second(tick: int, bpm: int, ticks_per_quarter: int = 192) -> float:
    quarter_seconds = 60.0 / bpm
    return tick * (quarter_seconds / ticks_per_quarter)


def synthesize(
    note_sequence: NoteSequence,
    mode: str,
    sample_rate: int = SAMPLE_RATE_DEFAULT,
    normalize: bool = True,
) -> tuple[np.ndarray, float, list[ErrorDetail]]:
    errors: list[ErrorDetail] = []
    total_ticks = max(ch.total_ticks for ch in note_sequence.channels.values())
    if total_ticks <= 0:
        return np.zeros(0, dtype=np.float64), 0.0, errors
    channels = []
    for _name, ch in note_sequence.channels.items():
        synth = ChannelSynthesizer(ch.channel_type, sample_rate)
        wave = synth.render(ch.events, total_ticks, note_sequence.bpm, mode)
        channels.append(wave)
    max_len = max(len(w) for w in channels)
    padded = [np.pad(w, (0, max_len - len(w)), mode="constant") for w in channels]
    mixed = np.mean(padded, axis=0)
    peak = np.max(np.abs(mixed))
    if normalize and peak > 0:
        mixed = mixed / peak
    mixed = np.clip(mixed, -1.0, 1.0)
    duration = max_len / sample_rate
    return mixed, duration, errors


def write_wav(path: Path, wave_data: np.ndarray, sample_rate: int) -> list[ErrorDetail]:
    errors: list[ErrorDetail] = []
    try:
        path.parent.mkdir(parents=True, exist_ok=True)
        pcm = np.int16(np.clip(wave_data * 32767, -32768, 32767))
        with wave.open(str(path), "wb") as wav:
            wav.setnchannels(1)
            wav.setsampwidth(2)
            wav.setframerate(sample_rate)
            wav.writeframes(pcm.tobytes())
    except Exception as exc:
        errors.append(
            ErrorDetail(
                code=ErrorCode.SYSTEM_WAV_WRITE_FAILED,
                line=0,
                column=0,
                message=f"WAVファイルの出力に失敗しました: {exc}",
                severity="error",
                hint="しばらく待ってから再度お試しください。",
            )
        )
    return errors


def build_channel_summary(note_sequence: NoteSequence | dict) -> list[dict]:
    if isinstance(note_sequence, dict):
        channels = note_sequence["channels"]
    else:
        channels = note_sequence.channels
    summary = []
    for name, ch in channels.items():
        events = ch["events"] if isinstance(ch, dict) else ch.events
        total_ticks = ch["total_ticks"] if isinstance(ch, dict) else ch.total_ticks
        notes = [e for e in events if isinstance(e, NoteEvent)]
        if not notes:
            notes = [
                e for e in events if isinstance(e, dict) and e.get("type") == "note"
            ]
            octaves = [e["note_number"] // 12 - 1 for e in notes]
        else:
            octaves = [e.note_number // 12 - 1 for e in notes]
        summary.append(
            {
                "channel": name,
                "note_count": len(notes),
                "octave_range": [min(octaves, default=4), max(octaves, default=4)],
                "duration_ticks": total_ticks,
            }
        )
    return summary