music

    Foundational music theory library: pitches, intervals, scales, chords and rhythm. Typed, pure, in-memory, all targets.

    music
    music-theory
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    8 hours ago
    Downloads
    7

    No dependencies

    #maya0513/music

    Foundational music-theory library, written in pure MoonBit. Typed representations throughout (no stringly-typed notes): a spelled Pitch is a step, an accidental and an octave. In-memory only: no FFI, no file IO, no async, so every package builds and tests on all targets (wasm, wasm-gc, js, native).

    The scope follows Tonal's (note / interval / scale / chord / rhythm as pure functions over data), with music21 as the semantic oracle for spelling and interval arithmetic. This library is independent of maya0513/audio; in particular it owns the pitch -> MIDI -> frequency conversion (equal temperament, A4 reference tunable, default 440 Hz), and the audio library only ever receives frequencies as Double hertz values.

    Status: source published on GitHub (https://github.com/maya0513/music.mbt); not yet published on mooncakes.io.

    #Scope and non-goals

    This library is a set of foundational building blocks for music theory over neutral in-memory data: spelled pitches, intervals, scales, chords, rational durations, and byte streams. Everything is a pure function over that data: no IO, no FFI, no async, and failures are explicit typed errors rather than panics or silent coercion. Spelling is notation-grade throughout.

    The boundary, in one list:

    • In scope: pitches and pitch classes, intervals (simple and compound, with up-to-double alterations), a scale dictionary, chords, rhythm durations and tempo, key signatures as correspondence data, and an in-memory Standard MIDI File (format 0) codec.
    • Scores and event sequencing are out of scope. Note-event timelines, tracks and playback scheduling are application semantics layered over these primitives by other libraries.
    • The analysis and generation layers are out of scope: Roman-numeral analysis, chord-symbol parsing, chord identification, key detection, Euclidean rhythm generation and tempo maps are applications of these primitives, not primitives themselves.
    • The MIDI codec converts format 0 only. Format 1/2 files, multiple tracks and SMPTE division are rejected with explicit errors rather than guessed at. SysEx and meta payloads are carried as opaque bytes and are never interpreted.
    • Notation rendering and file IO are out of scope. The MIDI package converts Bytes in memory only; hosts own the files.
    • Audio processing is out of scope. This library owns pitch -> MIDI -> frequency conversion and hands plain Double hertz values to the audio side. The two libraries never import each other; composition happens in libraries built on top of both.

    #Packages

    PackageContents
    maya0513/music/pitchPitchClass (mod 12, normalization proven), spelled Pitch, parse / format ("C#4", "Eb5"), MIDI conversion, equal-temperament frequency, cents conversion and numeric n-EDO conversion.
    maya0513/music/intervalInterval (number + quality, compound numbers and double alterations), parse / format ("P5", "m3", "AA4"), inversion, addition (semitone addition proven), spelling-preserving transposition, interval between pitches, 5-limit just-intonation ratios.
    maya0513/music/scaleA scale dictionary: major, the minor forms, harmonic major, the seven church modes, pentatonics and whole tone. Generation is by degree with notation-grade spelling (Eb major is spelled Eb F G Ab Bb C D). Key signatures as ordered accidental data.
    maya0513/music/chordTriads, suspended and six chords, seventh chords and extensions up to the thirteenth, built from intervals (degree spelling, like the scale package), pitch classes, symbols, inversions.
    maya0513/music/rhythmDuration as a normalized rational number of quarter notes (canonicalization covered by proof + property tests), dots, PPQ 480 tick conversion, tempo / seconds, time signatures.
    maya0513/music/midiIn-memory Standard MIDI File conversion on Bytes: format 0 read / write with the full channel-message set, SysEx and meta events as opaque payloads, VLQ, running status on read. Format 1/2, multiple tracks and SMPTE division are rejected explicitly. No file IO.

    #Example

    let c = @pitch.Pitch::parse("C4")
    let g = @interval.transpose_pitch(c, @interval.Interval::parse("P5"))
    // g is G4, spelled as a fifth above C

    #Development

    The toolchain is pinned with mise (mise.toml): MoonBit 1.0.0-rc1 via vfox:maya0513/vfox-moonbit, plus Z3 for moon prove. All day-to-day commands are in the justfile:

    just check # moon check --deny-warn --target all just test # moon test on native, wasm, wasm-gc and js just coverage # must report no uncovered lines just prove # moon prove for the proof-enabled packages just bench # moon bench --target native just ci # everything

    Library packages keep 100% test coverage (moon coverage analyze reports no uncovered lines). Benchmarks are recorded in docs/benchmarks.md.