Skip to content

Tokenisation

NotelikeTokeniser maps synchronised sequences to one canonical token stream. Its immutable NotelikeConfig controls the PPQN, number of tracks, pitch range, allowed note values, velocity bins, meter vocabulary, and maximum bar length.

"""Executable tokenisation example used by the documentation."""

from scoda import Note, NotelikeConfig, NotelikeTokeniser, Sequence


def main() -> None:
    config = NotelikeConfig(
        num_tracks=2,
        pitch_range=(48, 84),
        note_values=(6, 12, 24),
        velocity_bins=127,
    )
    tokeniser = NotelikeTokeniser(config)
    tracks = (
        Sequence((Note(0, 12, 60, 80),), (), 96, 24),
        Sequence((Note(24, 48, 67, 100, 1),), (), 96, 24),
    )

    body = tokeniser.tokenise_body(tracks)
    complete = tokeniser.frame(body)
    assert tokeniser.unframe(complete) == body
    assert tokeniser.detokenise(complete) == tracks

    state = tokeniser.initial_state()
    for token_id in tokeniser.encode(complete):
        assert token_id in tokeniser.allowed_token_ids(state)
        state = tokeniser.advance(state, token_id)
    assert state.ended
    assert tokeniser.manifest.codec_id == "notelike"


if __name__ == "__main__":
    main()

Representation policy

The codec is deliberately lossy. Note durations and velocities are mapped to the nearest configured values, with ties resolved towards the lower value. Track position is encoded instead of the original MIDI channel; detokenisation uses track_index % 16 as the note channel and release velocity 0.

At the default resolution of 24 ticks per quarter, the default duration vocabulary contains the common straight, triplet, and dotted values between a sixteenth-note triplet and a whole note:

4, 6, 8, 9, 12, 16, 18, 24, 32, 36, 48, 64, 72, 96

This library default is intended as a general-purpose starting point. Machine-learning corpora should explicitly pin the smallest musically justified note_values set so preprocessing and vocabulary construction remain reproducible.

Sustained notes

Enable duration_extension_ticks to represent long durations without adding a separate long-duration token for every pitch. The extension interval must equal the largest base duration. At PPQN 24, an interval of 96 is a whole note, independently of the current meter:

values = (4, 6, 8, 12, 16, 18, 24, 32, 36, 48, 72, 96)
config = NotelikeConfig(note_values=values, duration_extension_ticks=96)
tokeniser = NotelikeTokeniser(config)
sequence = sequence.quantise_note_lengths(values, duration_extension_ticks=96)

The codec adds one ext_096 token. After selecting the onset position and track, each such token extends the following fused note by 96 ticks. For example, ext_096 pit_060-val_24-vel_064 encodes one 120-tick note, and ext_096 pit_060-val_96-vel_064 encodes one 192-tick note. Extension markers do not advance time or create attacks. They must be followed immediately by another extension or their note, never by a position, track, bar, or stop.

The shared NoteDurationPolicy supplies nearest-duration quantisation, the largest representable duration fitting a gap (floor), and unique (extension_count, base_value) decomposition. Quantisation still rounds to the configured domain; extensions preserve long sustains, not arbitrary expressive MIDI timing. Without extensions, durations above the largest configured value are capped at that value. With extensions, durations beyond the supported numeric range are rejected rather than silently capped.

velocity_bins divides the positive MIDI range 1..127 into equal-width categories and names each category by its centre value. Consequently, the default single category is vel_064: it means "the only velocity category", not that the input note necessarily had velocity 64. Detokenisation reconstructs that representative value, so a corpus that normalises every input note to velocity 127 retains one stable model category but exports velocity 64. Use all 127 bins or preserve velocity separately when exact reconstructed loudness matters.

Bar boundaries

bar is an end-of-bar boundary token. A complete 4/4 bar at 24 ticks per quarter ends in bar; it does not also emit the redundant pos_096. The boundary both closes the current bar and precedes the next bar's content. A partial final bar instead ends with its exact position followed by sto, so complete and incomplete endings remain distinguishable. The running track remains active across the boundary, avoiding a redundant track token when the next note stays on the same track; position and note-order state reset for the new bar.

Notes may continue sounding across any number of boundaries. Tokenise the original intervals: split_bars() and Sequence.slice() deliberately clip notes into independent excerpts and cannot be losslessly reassembled to infer ties. bar_spans() provides bar boundaries without changing the original notes.

complete bars:    sta ... bar ... bar sto
partial last bar: sta ... bar ... pos_048 sto

Position tokens range from 1 through one tick before the configured maximum bar capacity. With a larger configured meter capacity, a position such as pos_096 may still be a legitimate position inside that longer bar; it is never used as the endpoint of a complete 4/4 bar.

When include_time_signatures=True, numerator and denominator changes from the first sequence are encoded. Their auxiliary MIDI payload and all key, tempo, program, and controller events are not represented. Preserve that information alongside tokens when an experiment needs it. detokenise(tokenise(sequences)) therefore returns the codec-normalised representation, not necessarily the original sequences.

Framing

tokenise() returns a complete sta ... sto stream. tokenise_body() returns the unframed body used by pipelines that store boundary tokens separately. frame() and unframe() convert between the forms and reject nested or missing boundaries.

Vocabulary

vocabulary is an immutable tuple whose index is the token ID. token_to_id is the inverse read-only mapping, and vocabulary_size is their shared size. encode() and decode() validate every value and reject unknown tokens or IDs.

The manifest records the codec ID, normalised configuration JSON, ordered vocabulary, size, and SHA-256 fingerprint. Changing any vocabulary-shaping configuration changes the fingerprint.

Configurations whose vocabulary could exceed 250,000 entries are rejected before allocation. Incremental grammar masks use a bounded cache sized according to the vocabulary, so long-running generation services cannot retain an unbounded number of large masks. Configured note values are also bounded by the standard MIDI variable-length tick limit of 268,435,455, preventing pathological token text while remaining far above ordinary musical durations.

Metadata

metadata() returns immutable TokenMetadata aligned with a complete stream, prefix_metadata() aligns with a valid framed-stream prefix during autoregressive generation, and body_metadata() aligns with an unframed body. It includes token indices, absolute and within-bar ticks, active track indices, pitch, and circle-of-fifths positions. Tokens without pitch-derived values use NaN unless impute_pitch=True requests carry-forward values; before the first note, imputation uses the neutral reference pitch A4 (69).

Extension markers have the current onset time and known active track, but no pitch of their own. Imputation carries the preceding pitch; metadata never looks ahead to the following note. Their token indices advance normally.

Constrained generation

Start with initial_state(). For each generated token, obtain allowed_token_ids(state) and update the state with advance(state, token_id). inspect_prefix() validates and reconstructs state from an existing ID prefix. These methods share the same grammar used by detokenisation, so model constraints cannot drift from the decoder.

At a fixed onset horizon, stop introducing notes and use completion_token_ids(state) to obtain a deterministic attack-free sounding tail followed by sto. Pass max_tokens to bound suffix allocation and reject tails exceeding a generation safety budget. next_completion_token_id(state) supplies the same suffix one token at a time. The suffix may contain additional full bars and a partial terminal position; those serialize existing notes, not additional generated content. Dangling track or extension prefixes cannot be completed without a note.