MML to SMF Library Demo

This page demonstrates how to use mmlabc-to-smf-wasm as a library in your web applications. The library provides a simple API to convert Music Macro Language (MML) to Standard MIDI Files (SMF).

Installation

Include the WASM module in your project:

<script type="module">
    import init, { parse_tree_json_to_smf } from './mmlabc-to-smf-wasm/pkg/mmlabc_to_smf_wasm.js';
    
    // Initialize the WASM module
    await init();
    
    // Now you can use the library functions
</script>

Basic Usage

The library requires two steps:

  1. Parse MML with web-tree-sitter to get a parse tree
  2. Convert the parse tree to MIDI with parse_tree_json_to_smf()
import { Parser, Language } from './web-tree-sitter.js';

// Initialize parser
await Parser.init();
const parser = new Parser();
const lang = await Language.load('./tree-sitter-mml/tree-sitter-mml.wasm');
parser.setLanguage(lang);

// Parse MML
const mml = "cde";
const tree = parser.parse(mml);

// Convert tree to JSON
function treeToJSON(node, source) {
    const result = { type: node.type };
    if (node.childCount === 0) {
        result.text = source.substring(node.startIndex, node.endIndex);
    }
    if (node.childCount > 0) {
        result.children = [];
        for (let i = 0; i < node.childCount; i++) {
            result.children.push(treeToJSON(node.child(i), source));
        }
    }
    return result;
}

const parseTreeJSON = JSON.stringify(treeToJSON(tree.rootNode, mml));

// Convert to MIDI
const smfData = parse_tree_json_to_smf(parseTreeJSON, mml);

// Download or use the MIDI data
const blob = new Blob([smfData], { type: 'audio/midi' });
const url = URL.createObjectURL(blob);
// ... create download link or play the MIDI

セットアップ(日本語)

GitHub から npm インストールし、Rust なしで動かす手順です。

npm install github:cat2151/mmlabc-to-smf-rust
cd node_modules/mmlabc-to-smf-rust/demo-library
npm install
npm run fetch:cdn   # GitHub Pages からビルド済み WASM / web-tree-sitter を取得(Rust不要)
npm run serve       # http://localhost:8080/demo-library/ を自動で開く

Rust / wasm-pack を使って自前でビルドしたい場合は npm run build を実行してください。

Interactive Example

Try it yourself! Enter MML code and convert it to MIDI:

API Reference

parse_tree_json_to_smf(parse_tree_json, mml_source)

Parameters:

Returns: Uint8Array containing SMF binary data

Supported MML Features

Resources