Spaces:
Sleeping
Sleeping
File size: 15,158 Bytes
8648e23 | 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | """
Core data structures for musical representation in the Bach-style generator.
This module provides fundamental classes for representing musical elements with
precise temporal and harmonic information. All temporal values use Fraction for
exact representation, avoiding floating-point errors.
"""
from dataclasses import dataclass, field
from fractions import Fraction
from typing import Optional, List, Dict, Any
from enum import Enum
class ProlongationLevel(Enum):
"""Which structural level a note belongs to in Schenkerian analysis."""
FOREGROUND = "foreground"
MIDDLEGROUND = "middleground"
BACKGROUND = "background"
class HarmonicFunction(Enum):
"""Harmonic function labels for chord classification."""
TONIC = "tonic"
PREDOMINANT = "predominant"
DOMINANT = "dominant"
AMBIGUOUS = "ambiguous"
class Accidental(Enum):
"""Musical accidentals."""
DOUBLE_FLAT = -2
FLAT = -1
NATURAL = 0
SHARP = 1
DOUBLE_SHARP = 2
@dataclass(frozen=True)
class TimeSignature:
"""
Represents a musical time signature.
Attributes:
numerator: Number of beats per measure
denominator: Note value that represents one beat (4 = quarter note, 8 = eighth note)
"""
numerator: int
denominator: int
def __post_init__(self):
if self.numerator <= 0:
raise ValueError(f"Time signature numerator must be positive, got {self.numerator}")
if self.denominator not in [1, 2, 4, 8, 16, 32]:
raise ValueError(f"Invalid time signature denominator: {self.denominator}")
@property
def is_compound(self) -> bool:
"""Returns True if this is a compound meter (numerator divisible by 3, > 3)."""
return self.numerator > 3 and self.numerator % 3 == 0
@property
def beats_per_measure(self) -> int:
"""Returns the number of beats per measure."""
if self.is_compound:
return self.numerator // 3
return self.numerator
@property
def beat_duration(self) -> Fraction:
"""Returns the duration of one beat in quarter notes."""
if self.is_compound:
# In 6/8, one beat = dotted quarter = 3/8 = 3/2 quarter notes
return Fraction(3 * 4, self.denominator)
return Fraction(4, self.denominator)
def __str__(self) -> str:
return f"{self.numerator}/{self.denominator}"
@dataclass
class MusicalEvent:
"""
Represents a single musical event (note or rest) with complete temporal context.
Attributes:
pitch: MIDI pitch number (0-127), or None for rests
onset: Start time in quarter notes from beginning of score
duration: Length in quarter notes
velocity: MIDI velocity (0-127), representing dynamics
measure: Measure number (1-indexed)
beat: Beat position within the measure (0-indexed)
beat_strength: Metric weight (0.0 = weakest, 1.0 = downbeat)
voice: Voice number for multi-voice scores (0-indexed)
is_rest: Whether this event is a rest
"""
pitch: Optional[int]
onset: Fraction
duration: Fraction
velocity: int = 64
measure: int = 1
beat: Fraction = Fraction(0)
beat_strength: float = 1.0
voice: int = 0
is_rest: bool = False
def __post_init__(self):
"""Validate event data."""
if not self.is_rest and (self.pitch is None or not 0 <= self.pitch <= 127):
raise ValueError(f"Invalid MIDI pitch: {self.pitch}")
if not 0 <= self.velocity <= 127:
raise ValueError(f"Invalid velocity: {self.velocity}")
if self.duration <= 0:
raise ValueError(f"Duration must be positive, got {self.duration}")
if not 0.0 <= self.beat_strength <= 1.0:
raise ValueError(f"Beat strength must be in [0.0, 1.0], got {self.beat_strength}")
if self.is_rest and self.pitch is not None:
raise ValueError("Rest cannot have a pitch")
@property
def offset(self) -> Fraction:
"""End time of the event in quarter notes."""
return self.onset + self.duration
@property
def pitch_class(self) -> Optional[int]:
"""Returns pitch class (0-11) or None for rests."""
return self.pitch % 12 if self.pitch is not None else None
def transpose(self, semitones: int) -> 'MusicalEvent':
"""Return a new event transposed by the given number of semitones."""
if self.is_rest:
return self
new_pitch = max(0, min(127, self.pitch + semitones))
return MusicalEvent(
pitch=new_pitch,
onset=self.onset,
duration=self.duration,
velocity=self.velocity,
measure=self.measure,
beat=self.beat,
beat_strength=self.beat_strength,
voice=self.voice,
is_rest=False
)
@dataclass
class Chord:
"""
Represents a detected chord with harmonic analysis.
Attributes:
onset: Start time in quarter notes
duration: Length in quarter notes
root: Root pitch class (0-11)
quality: Chord quality (e.g., 'major', 'minor', 'diminished')
pitches: Set of pitch classes in the chord
bass: Bass note pitch class (may differ from root for inversions)
inversion: Inversion number (0 = root position, 1 = first inversion, etc.)
roman_numeral: Roman numeral analysis in current key
"""
onset: Fraction
duration: Fraction
root: int
quality: str
pitches: frozenset[int]
bass: int
inversion: int = 0
roman_numeral: Optional[str] = None
def __post_init__(self):
"""Validate chord data."""
if not 0 <= self.root <= 11:
raise ValueError(f"Root must be 0-11, got {self.root}")
if not 0 <= self.bass <= 11:
raise ValueError(f"Bass must be 0-11, got {self.bass}")
if self.duration <= 0:
raise ValueError(f"Duration must be positive, got {self.duration}")
@property
def offset(self) -> Fraction:
"""End time of the chord."""
return self.onset + self.duration
@dataclass
class HarmonicEvent:
"""
Bundles a Chord with its RomanNumeral for prolongation analysis.
Provides a unified onset/duration interface so prolongation rules can treat
a (chord, roman_numeral) pair as a single addressable event.
"""
chord: 'Chord'
roman_numeral: 'RomanNumeral'
@property
def onset(self) -> Fraction:
"""Start time of the underlying chord."""
return self.chord.onset
@property
def duration(self) -> Fraction:
"""Duration of the underlying chord."""
return self.chord.duration
_DEGREE_LABELS_MAJOR = {1: 'I', 2: 'ii', 3: 'iii', 4: 'IV', 5: 'V', 6: 'vi', 7: 'viio'}
_DEGREE_LABELS_MINOR = {1: 'i', 2: 'iio', 3: 'III', 4: 'iv', 5: 'V', 6: 'VI', 7: 'viio'}
_QUALITY_SUFFIXES = {
'major': '', 'minor': '', 'diminished': 'o', 'augmented': '+',
'dominant7': '7', 'dim7': 'o7', 'half-dim7': 'ø7',
'major7': 'M7', 'minor7': '7',
}
_INVERSION_FIGURES = {
0: '', 1: '6', 2: '64', # triads
}
_INVERSION_FIGURES_7TH = {
0: '7', 1: '65', 2: '43', 3: '42',
}
_SEVENTH_QUALITY_MARKS = {
'dominant7': '', # V7 — '7' already in figure
'dim7': 'o', # viio7
'half-dim7': 'ø', # viiø7
'major7': 'M', # IM7
'minor7': '', # ii7
}
_PITCH_CLASS_NAMES = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B']
@dataclass(frozen=True)
class RomanNumeral:
"""
Represents a Roman numeral analysis of a chord within a key.
Attributes:
degree: Scale degree (1-7)
quality: Chord quality string
inversion: Inversion number
local_key_tonic: Pitch class of the local key (0-11)
local_key_mode: 'major' or 'minor'
is_secondary: Whether this is a secondary dominant/function (e.g., V/V)
secondary_target: Scale degree of the secondary target
function: Harmonic function classification
"""
degree: int
quality: str
inversion: int
local_key_tonic: int
local_key_mode: str
is_secondary: bool = False
secondary_target: Optional[int] = None
function: Optional[HarmonicFunction] = None
@property
def label(self) -> str:
"""Render standard Roman numeral notation (e.g. 'V7/V', 'viio6')."""
if self.local_key_mode == 'minor':
base = _DEGREE_LABELS_MINOR.get(self.degree, str(self.degree))
else:
base = _DEGREE_LABELS_MAJOR.get(self.degree, str(self.degree))
# Override case based on actual quality
if self.quality in ('major', 'augmented', 'dominant7', 'major7'):
base = base.upper().rstrip('O')
elif self.quality in ('minor', 'diminished', 'dim7', 'half-dim7', 'minor7'):
base = base.lower().rstrip('o')
is_seventh = '7' in self.quality
if is_seventh:
mark = _SEVENTH_QUALITY_MARKS.get(self.quality, '')
figures = _INVERSION_FIGURES_7TH.get(self.inversion, str(self.inversion))
result = base + mark + figures
else:
suffix = _QUALITY_SUFFIXES.get(self.quality, '')
figures = _INVERSION_FIGURES.get(self.inversion, str(self.inversion))
result = base + suffix + figures
if self.is_secondary and self.secondary_target is not None:
target_labels = (
_DEGREE_LABELS_MINOR if self.local_key_mode == 'minor'
else _DEGREE_LABELS_MAJOR
)
target = target_labels.get(self.secondary_target, str(self.secondary_target))
result += f"/{target}"
return result
@dataclass(frozen=True)
class KeyEstimate:
"""
Represents an estimated key at a point in the score.
Attributes:
tonic: Pitch class of the tonic (0-11)
mode: 'major' or 'minor'
confidence: Confidence score (0.0-1.0)
onset: Position in the score where this key estimate applies
"""
tonic: int
mode: str
confidence: float
onset: Fraction
def __post_init__(self):
if not 0 <= self.tonic <= 11:
raise ValueError(f"Tonic must be 0-11, got {self.tonic}")
if self.mode not in ('major', 'minor'):
raise ValueError(f"Mode must be 'major' or 'minor', got {self.mode}")
if not 0.0 <= self.confidence <= 1.0:
raise ValueError(f"Confidence must be 0.0-1.0, got {self.confidence}")
@property
def label(self) -> str:
"""Human-readable key label (e.g. 'G major', 'D minor')."""
return f"{_PITCH_CLASS_NAMES[self.tonic]} {self.mode}"
@dataclass
class HarmonicSegment:
"""
Represents a tonal section with consistent key and chord progression.
Attributes:
start: Start time in quarter notes
end: End time in quarter notes
key_tonic: Tonic pitch class of the key (0-11)
key_mode: Mode ('major' or 'minor')
chords: List of chords in this segment
"""
start: Fraction
end: Fraction
key_tonic: int
key_mode: str
chords: List[Chord] = field(default_factory=list)
roman_numerals: List[RomanNumeral] = field(default_factory=list)
prolongation_level: Optional[ProlongationLevel] = None
def __post_init__(self):
"""Validate segment data."""
if not 0 <= self.key_tonic <= 11:
raise ValueError(f"Key tonic must be 0-11, got {self.key_tonic}")
if self.key_mode not in ['major', 'minor']:
raise ValueError(f"Key mode must be 'major' or 'minor', got {self.key_mode}")
if self.end <= self.start:
raise ValueError(f"End time must be after start time")
@property
def duration(self) -> Fraction:
"""Duration of the segment."""
return self.end - self.start
@dataclass
class Score:
"""
Complete score representation with all musical events and metadata.
Attributes:
events: List of all musical events in chronological order
time_signatures: List of (onset, TimeSignature) tuples for time signature changes
title: Score title
composer: Composer name
tempo: Tempo in BPM (beats per minute)
metadata: Additional metadata dictionary
"""
events: List[MusicalEvent]
time_signatures: List[tuple[Fraction, TimeSignature]] = field(default_factory=list)
title: Optional[str] = None
composer: Optional[str] = None
tempo: Optional[int] = None
metadata: Dict[str, Any] = field(default_factory=dict)
def __post_init__(self):
"""Validate and sort score data."""
# Sort events by onset time
self.events.sort(key=lambda e: (e.onset, e.voice))
# Sort time signatures by onset
self.time_signatures.sort(key=lambda ts: ts[0])
# Ensure at least one time signature
if not self.time_signatures:
self.time_signatures.append((Fraction(0), TimeSignature(4, 4)))
def get_time_signature_at(self, onset: Fraction) -> TimeSignature:
"""
Get the time signature in effect at the given onset time.
Args:
onset: Time position in quarter notes
Returns:
TimeSignature in effect at that time
"""
current_ts = self.time_signatures[0][1]
for ts_onset, ts in self.time_signatures:
if ts_onset > onset:
break
current_ts = ts
return current_ts
@property
def duration(self) -> Fraction:
"""Total duration of the score in quarter notes."""
if not self.events:
return Fraction(0)
return max(event.offset for event in self.events)
@property
def num_voices(self) -> int:
"""Number of distinct voices in the score."""
if not self.events:
return 0
return max(event.voice for event in self.events) + 1
@property
def pitch_range(self) -> tuple[Optional[int], Optional[int]]:
"""Returns (lowest_pitch, highest_pitch) or (None, None) if no notes."""
pitches = [e.pitch for e in self.events if not e.is_rest]
if not pitches:
return (None, None)
return (min(pitches), max(pitches))
def get_events_in_range(self, start: Fraction, end: Fraction) -> List[MusicalEvent]:
"""
Get all events that overlap with the given time range.
Args:
start: Start time in quarter notes
end: End time in quarter notes
Returns:
List of events that overlap with [start, end)
"""
return [
event for event in self.events
if event.onset < end and event.offset > start
]
def get_events_by_voice(self, voice: int) -> List[MusicalEvent]:
"""Get all events for a specific voice."""
return [event for event in self.events if event.voice == voice] |