API reference¶
Only names exported directly from scoda are part of the supported public API.
Musical values and transformations¶
Immutable musical values and transformations used by S-Coda.
Note
dataclass
¶
A half-open MIDI note interval [start, end) in sequence ticks.
TimeSignature
dataclass
¶
A MIDI time-signature event at an absolute sequence tick.
KeySignature
dataclass
¶
A MIDI key-signature event at an absolute sequence tick.
Tempo
dataclass
¶
A MIDI tempo event expressed as microseconds per quarter note.
ControlChange
dataclass
¶
A channel-specific MIDI controller update at an absolute tick.
ProgramChange
dataclass
¶
A channel-specific MIDI program update at an absolute tick.
BarSpan
dataclass
¶
An absolute bar interval with the musical context active at its start.
Sequence
dataclass
¶
Canonical immutable collection of notes, timed events, and duration.
similarity ¶
Return multiset overlap of note intervals and selected attributes.
cutoff ¶
Shorten sequences above maximum_length to the selected target.
filter_events ¶
Return a copy containing only events accepted by predicate.
transpose ¶
Transpose notes and key signatures by an integer number of semitones.
quantise ¶
Move note boundaries and event times to the nearest allowed grid point.
quantise_note_lengths ¶
Replace note durations with the nearest allowed note value.
quantise_and_normalise ¶
Quantise positions and durations while retaining canonical invariants.
resample ¶
Return this sequence expressed at a different tick resolution.
slice ¶
Return a clipped half-open tick window [start, end) with local timing.
Crossing notes are deliberately clipped. This is a self-contained
excerpt, not a lossless partition: concatenation cannot recover ties.
Use :func:bar_spans with the original notes for lossless bar views.
concatenate
classmethod
¶
Join sequences end to end without changing their tick resolution.
SequenceBuilder ¶
Mutable construction helper that produces an immutable :class:Sequence.
set_duration ¶
Set a duration that contains every value currently in the builder.
bar_spans ¶
Return bar intervals through sequence.duration_ticks in one pass.
split_bars ¶
Return clipped bar excerpts, optionally carrying active MIDI context.
Notes crossing boundaries become independent fragments. Tokenise the original sequences, not concatenated excerpts, to preserve articulation.
Duration policies¶
A duration palette, optionally repeated at a fixed tick interval.
With extensions enabled the domain is n * duration_extension_ticks + v
for non-negative n and a configured base value v. The extension
interval equals the largest base value, making decomposition unique and
exact multiples representable without a zero-duration note.
Music theory¶
Bases: StrEnum
A standard MIDI major or minor key signature.
Enum values use the spellings accepted by MIDI files. Enharmonic keys are preserved when parsed, while transposition returns one canonical spelling for each pitch class and mode.
MIDI¶
Deterministic conversion between Mido objects and immutable S-Coda values.
MidiDiagnostic
dataclass
¶
One deterministic MIDI import observation or repair.
MidiImportReport
dataclass
¶
MidiImportReport(
source_ticks_per_quarter,
target_ticks_per_quarter,
tracks_read,
notes_created,
diagnostics=(),
)
Immutable summary of a MIDI import and every emitted diagnostic.
MidiLoadResult
dataclass
¶
Canonical imported sequences together with their complete report.
load_midi ¶
load_midi(
source,
*,
track_groups=None,
meta_track_indices=None,
meta_target=0,
target_ticks_per_quarter=None,
mode="repair",
unclosed_note_policy="close_at_track_end",
)
Load MIDI into canonical sequences and return all repairs as diagnostics.
to_mido ¶
Convert canonical sequences to a deterministic Mido file.
save_midi ¶
Write canonical sequences to a deterministic standard MIDI file.
Tokenisation¶
Canonical notelike tokenisation for immutable S-Coda sequences.
NotelikeConfig
dataclass
¶
NotelikeConfig(
ticks_per_quarter=24,
num_tracks=1,
pitch_range=(21, 108),
note_values=(
4,
6,
8,
9,
12,
16,
18,
24,
32,
36,
48,
64,
72,
96,
),
velocity_bins=1,
include_time_signatures=False,
max_bar_quarters=4,
duration_extension_ticks=None,
)
Immutable configuration for :class:NotelikeTokeniser.
NotelikeTokeniser ¶
Bases: _IncrementalTokeniser
Canonical notelike grammar with explicit immutable configuration.
__reduce__ ¶
Reconstruct the immutable codec from its config when pickled.
The derived lookup tables intentionally use read-only mapping proxies,
which Python's default pickler cannot serialise. The complete codec is
deterministic from :attr:config, so multiprocessing workers can
safely and compactly rebuild it instead of serialising derived state.
next_completion_token_id ¶
Return the next canonical tail token without introducing an attack.
At an onset horizon existing notes may still sound. Advance through
that tail in the current meter, then emit sto. Dangling track or
extension prefixes require a note and cannot be completed this way.
completion_token_ids ¶
Return a deterministic attack-free suffix, including terminal stop.
tokenise ¶
Return a complete framed canonical stream for synchronised sequences.
tokenise_body ¶
Return the canonical, deliberately lossy stream body without framing.
detokenise ¶
Validate and decode a complete canonical stream into normalised tracks.
body_metadata ¶
Return metadata aligned one-to-one with an unframed token body.
prefix_metadata ¶
Return metadata aligned one-to-one with a valid framed-stream prefix.
metadata ¶
Return metadata aligned one-to-one with a complete token stream.
TokeniserState
dataclass
¶
TokeniserState(
started=False,
ended=False,
active_track=None,
bar_count=0,
bar_position=0,
bar_capacity=96,
pending_note_ticks=0,
duration_extension_ticks=0,
meter_numerator=4,
meter_denominator=4,
meter_declared=False,
position_track_floor=-1,
last_note_key=None,
valid=True,
phase="initial",
)
Immutable grammar state used for incremental constrained decoding.
TokenMetadata
dataclass
¶
TokenMetadata(
token_index,
token_index_in_bar,
tick,
tick_in_bar,
track_index,
pitch,
circle_of_fifths,
)
Per-token structural and musical metadata aligned with a token stream.
VocabularyManifest
dataclass
¶
create_tokeniser ¶
Construct a tokeniser from a persisted codec identifier and configuration.
Errors¶
Public exception hierarchy for S-Coda.
ScodaError ¶
Bases: Exception
Base class for all library-defined errors.
ValidationError ¶
Bases: ScodaError, ValueError
Raised when a public value violates a core invariant.
SequenceError ¶
Bases: ScodaError
Raised when a sequence transformation cannot be performed safely.
MidiError ¶
Bases: ScodaError
Base class for MIDI conversion errors.
MidiImportError ¶
TokenisationError ¶
Bases: ScodaError
Raised for invalid tokens, configurations, or token streams.