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).
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>
The library requires two steps:
web-tree-sitter to get a parse treeparse_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 を実行してください。
Try it yourself! Enter MML code and convert it to MIDI:
parse_tree_json_to_smf(parse_tree_json, mml_source)Parameters:
parse_tree_json (string): JSON representation of the parse tree from web-tree-sittermml_source (string): Original MML source text (for diagnostics)Returns: Uint8Array containing SMF binary data
c d e f g a b+ - (sharp/flat)c4 d8 e16c4. d8..o5 < >t120v100@1kt1'ceg'c;e;g