View on GitHub

mmlabc-to-smf-rust

mmlabc-to-smf-rust

mmlabc-to-smf-rust

Japanese English DeepWiki

Conversion library from Music Macro Language (MML) to Standard MIDI File (SMF)

Overview

This library converts Music Macro Language (MML) strings into Standard MIDI Files. It is written in Rust.

Usage

It is used as a library by cat-play-mml.

Current Status

Frequent breaking changes are being made.

The README is currently under-maintained. More MML commands are actually implemented than described here. The README will be updated later.

If you want to know which MML features are implemented, please refer to tree-sitter-mml/grammar.js first (note that this will also be subject to breaking changes in the future).

Implemented Features βœ…

Demo / How to Run

# Basic scale conversion
cargo run -- "cdefgab"

# Multi-channel
cargo run -- "c;e;g"

# Custom output file
cargo run -- "cde" -o my_song.mid

Future Outlook

Short-term Goals 🚧

Long-term Goals 🎯

References

Features

Requirements

Installation

Development Version (Current State)

git clone https://github.com/cat2151/mmlabc-to-smf-rust
cd mmlabc-to-smf-rust
cargo build --release

Direct Execution (via Cargo)

cargo run -- "cdefgab"

Usage

Basic Usage

# Convert basic scale (automatically played by cat-play-mml by default)
cargo run -- "cdefgab"

# Multi-channel (simultaneous playback)
cargo run -- "c;e;g"  # C major chord

# Custom output file
cargo run -- "cde" -o my_song.mid

# Disable auto-playback
cargo run -- "cde" --no-play

Auto-playback Feature

By default, generated MIDI files are automatically played using the cat-play-mml command. This allows for immediate sound confirmation during MML development.

Custom Player Configuration

You can configure a custom MIDI player by creating a mmlabc-to-smf-rust.toml file in the directory where the tool is executed.

Example configuration file:

# mmlabc-to-smf-rust.toml
external_smf_player = "timidity"

Common configurable MIDI players:

If the configuration file does not exist, cat-play-mml will be used by default.

Refer to mmlabc-to-smf-rust.toml.example for a sample configuration file.

Output Files

The following files are generated upon execution:

Supported MML Notation

Currently supported notation:

Examples:

cdefgab     β†’ Continuous playback of C-D-E-F-G-A-B
c;e;g       β†’ Simultaneous playback of C, E, and G notes (C major chord)

Development

Build

cargo build        # Debug build
cargo build --release  # Release build

Test

cargo test         # Run all tests (35 test cases)

Format & Lint

cargo clippy       # Code quality check
cargo fmt --check  # Format check
cargo fmt          # Apply format

tree-sitter Parser Files

The tree-sitter parser files (located under tree-sitter-mml/src/) are tracked in Git following tree-sitter best practices for reliable distribution on crates.io.

Development Workflow:

Why Commit Generated Files This follows the best practices of the tree-sitter ecosystem:

Updating the Grammar: If you modify tree-sitter-mml/grammar.js:

  1. Run cargo build - the build script will detect changes and regenerate the parser files.
  2. Commit both grammar.js and the regenerated C language files together.
  3. This ensures the grammar and parser remain synchronized.

To manually regenerate the parser files:

cd tree-sitter-mml
npm install  # if tree-sitter-cli is not yet installed
npx tree-sitter generate

Project Structure

src/
β”œβ”€β”€ main.rs              # CLI entry point
β”œβ”€β”€ lib.rs               # Library root
β”œβ”€β”€ pass1_parser.rs      # Pass 1: Token parsing
β”œβ”€β”€ pass2_ast.rs         # Pass 2: AST conversion
β”œβ”€β”€ pass3_events.rs      # Pass 3: MIDI event generation
β”œβ”€β”€ pass4_midi.rs        # Pass 4: MIDI file creation
β”œβ”€β”€ tree_sitter_mml.rs   # tree-sitter MML integration
└── types.rs             # Common type definitions

tests/
β”œβ”€β”€ integration_test.rs  # Integration tests
β”œβ”€β”€ test_channel.rs      # Channel functionality tests
β”œβ”€β”€ test_pass1.rs        # Pass 1 tests
β”œβ”€β”€ test_pass2.rs        # Pass 2 tests
β”œβ”€β”€ test_pass3.rs        # Pass 3 tests
└── test_pass4.rs        # Pass 4 tests

License

MIT License - See the LICENSE file for details.

Reference