目录

moonbit-music-theory

A lightweight, zero-dependency, pure algebraic and computational music theory library written in MoonBit. It is designed for composing, education, and music application development.

Features

  • Notes & Pitch: Parsing of note names (e.g. C4, F#3, Bb-1) and bidirectionally converting them to MIDI pitch numbers.
  • Intervals: Algebraic addition, subtraction, and note transposition.
  • Scales & Modes: Diatonic scale generation for Major, Minor, Dorian, Phrygian, Lydian, Mixolydian, and Locrian scales, as well as Pentatonic scales.
  • Chords: Generation of triads and seventh chords from a root note, and parsing standard chord symbols (e.g. Cmaj7, Ab5m7).
  • Roman Numeral Analysis: Chromatic degree analysis under a key signature (e.g. G7 under C Major is analyzed as V7, Eb is bIII).
  • JSON Serialization: Full serialization/deserialization for core models (Note, Interval, Key).
  • CLI query utility: Command-line tool to query chords, scales, transposition, and Roman numeral degree analysis.

Usage & Examples

Here is a literate programming example showing core library usages:

test "Readme examples" {
  // 1. Parsing and transposing notes
  let c4 = @theory.Note::parse("C4").unwrap()
  let m3 = @theory.Interval::parse("M3").unwrap()
  let e4 = c4.transpose(m3)
  inspect(e4.to_string(), content="E4")

  // 2. Generating chords
  let (root, chord_type) = @theory.parse_chord_symbol("Cmaj7").unwrap()
  let notes = @theory.generate_chord(root, chord_type)
  let notes_strs = notes.map(fn(n) { n.to_string() })
  inspect(notes_strs, content="[\"C4\", \"E4\", \"G4\", \"B4\"]")

  // 3. Roman Numeral analysis
  let c_major = @theory.Key::new(NoteLetter::C, Accidental::Natural, ScaleType::Major)
  let result = c_major.analyze_chord("G7").unwrap()
  inspect(result, content="V7")
}

CLI Commands

You can run the query CLI tool directly:

# Query chord notes
moon run src/cli -- chord Cmaj7

# Generate scale notes
moon run src/cli -- scale C major

# Transpose a note by an interval
moon run src/cli -- transpose C4 M3

# Roman numeral degree analysis
moon run src/cli -- roman C major G7
关于

面向作曲、教育、音乐应用开发的基础理论库。

160.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号