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
|
"""Intermediate representation for MML NoteSequence and errors."""
from dataclasses import dataclass, field
from enum import Enum
from typing import Any
class ErrorPhase(Enum):
"""Phase where an error occurred."""
LEXER = "lexer"
SYNTAX = "syntax"
SEMANTIC = "semantic"
RUNTIME = "runtime"
API = "api"
class ErrorCode(Enum):
"""Machine-readable error codes.
Errors are classified by phase:
- SYNTAX_*: token sequence violates the BNF grammar.
- SEMANTIC_*: syntactically valid but semantically invalid (range,
channel mismatch, undefined reference, etc.).
- RUNTIME_*: failure during synthesis or WAV output.
- VALIDATION_*: API-level parameter validation errors.
"""
# --- Syntax errors (Parser phase) ---
SYNTAX_INVALID_TOKEN = "SYNTAX_INVALID_TOKEN"
SYNTAX_INVALID_NUMBER = "SYNTAX_INVALID_NUMBER"
SYNTAX_UNEXPECTED_TOKEN = "SYNTAX_UNEXPECTED_TOKEN"
SYNTAX_UNTERMINATED_REPEAT = "SYNTAX_UNTERMINATED_REPEAT"
SYNTAX_UNMATCHED_REPEAT_END = "SYNTAX_UNMATCHED_REPEAT_END"
SYNTAX_UNTERMINATED_TIE = "SYNTAX_UNTERMINATED_TIE"
SYNTAX_INVALID_TRACK_HEADER = "SYNTAX_INVALID_TRACK_HEADER"
SYNTAX_DUPLICATE_TRACK = "SYNTAX_DUPLICATE_TRACK"
SYNTAX_UNTERMINATED_HEADER = "SYNTAX_UNTERMINATED_HEADER"
# --- Semantic errors (SemanticAnalyzer phase) ---
SEMANTIC_VALUE_OUT_OF_RANGE = "SEMANTIC_VALUE_OUT_OF_RANGE"
SEMANTIC_NOTE_OUT_OF_RANGE = "SEMANTIC_NOTE_OUT_OF_RANGE"
SEMANTIC_CHANNEL_MISMATCH = "SEMANTIC_CHANNEL_MISMATCH"
SEMANTIC_EMPTY_TRACK = "SEMANTIC_EMPTY_TRACK"
SEMANTIC_OUTSIDE_TRACK = "SEMANTIC_OUTSIDE_TRACK"
SEMANTIC_UNDEFINED_REFERENCE = "SEMANTIC_UNDEFINED_REFERENCE"
SEMANTIC_UNSUPPORTED_FEATURE = "SEMANTIC_UNSUPPORTED_FEATURE"
# --- Runtime errors (Synthesizer phase) ---
RUNTIME_SYNTHESIS_FAILED = "RUNTIME_SYNTHESIS_FAILED"
RUNTIME_WAV_WRITE_FAILED = "RUNTIME_WAV_WRITE_FAILED"
RUNTIME_INTERNAL_ERROR = "RUNTIME_INTERNAL_ERROR"
# --- API-level errors (parameter validation, not MML syntax) ---
VALIDATION_MISSING_PARAMETER = "VALIDATION_MISSING_PARAMETER"
VALIDATION_INVALID_MODE = "VALIDATION_INVALID_MODE"
VALIDATION_INVALID_ACTION = "VALIDATION_INVALID_ACTION"
@dataclass
class ErrorDetail:
"""Structured error/warning detail."""
code: ErrorCode
phase: ErrorPhase
line: int
column: int
message: str
severity: str # "error" | "warning"
hint: str = ""
context: str = ""
def to_dict(self) -> dict[str, Any]:
return {
"code": self.code.value,
"phase": self.phase.value,
"line": self.line,
"column": self.column,
"message": self.message,
"severity": self.severity,
"hint": self.hint,
"context": self.context,
}
@dataclass
class NoteEvent:
type: str = "note"
tick_position: int = 0
duration: int = 0
note_number: int = 69
velocity: int = 15
duty: int = 2
gate_time: float = 1.0
detune_cents: float = 0.0
@dataclass
class RestEvent:
type: str = "rest"
tick_position: int = 0
duration: int = 0
@dataclass
class VolumeEvent:
type: str = "volume"
tick_position: int = 0
value: int = 15
@dataclass
class DutyEvent:
type: str = "duty"
tick_position: int = 0
value: int = 2
@dataclass
class TempoEvent:
type: str = "tempo"
tick_position: int = 0
bpm: int = 120
@dataclass
class RepeatEvent:
type: str = "repeat"
tick_position: int = 0
start_tick: int = 0
end_tick: int = 0
repeat_count: int = 1
duration: int = 0
@dataclass
class EnvelopeEvent:
type: str = "envelope"
tick_position: int = 0
slot: int = 0
points: list[dict[str, int]] = field(default_factory=list)
@dataclass
class VibratoEvent:
type: str = "vibrato"
tick_position: int = 0
slot: int = 0
params: dict[str, int] = field(default_factory=dict)
@dataclass
class GlideEvent:
type: str = "glide"
tick_position: int = 0
slot: int = 0
params: dict[str, int] = field(default_factory=dict)
Event = (
NoteEvent
| RestEvent
| VolumeEvent
| DutyEvent
| TempoEvent
| RepeatEvent
| EnvelopeEvent
| VibratoEvent
| GlideEvent
)
@dataclass
class ChannelSequence:
channel_type: str = "pulse"
events: list[Event] = field(default_factory=list)
total_ticks: int = 0
def to_dict(self) -> dict[str, Any]:
return {
"channel_type": self.channel_type,
"events": [event_to_dict(e) for e in self.events],
"total_ticks": self.total_ticks,
}
@dataclass
class NoteSequence:
version: str = "1.0"
bpm: int = 120
ticks_per_quarter: int = 192
channels: dict[str, ChannelSequence] = field(
default_factory=lambda: {
"Pulse1": ChannelSequence("pulse"),
"Pulse2": ChannelSequence("pulse"),
"Triangle": ChannelSequence("triangle"),
"Noise": ChannelSequence("noise"),
}
)
def to_dict(self) -> dict[str, Any]:
return {
"version": self.version,
"bpm": self.bpm,
"ticks_per_quarter": self.ticks_per_quarter,
"channels": {k: v.to_dict() for k, v in self.channels.items()},
}
def event_to_dict(event: Event) -> dict[str, Any]:
if isinstance(
event, (NoteEvent, RestEvent, VolumeEvent, DutyEvent, TempoEvent, RepeatEvent)
):
return event.__dict__
if isinstance(event, (EnvelopeEvent, VibratoEvent, GlideEvent)):
return event.__dict__
raise TypeError(f"Unsupported event type: {type(event)}")
|