Last active
October 15, 2016 20:28
-
-
Save HertzDevil/1fc9c6509309cd5756eaa293ed4ab2ef to your computer and use it in GitHub Desktop.
how to use the MML compiler framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- MGCInts library | |
-- this imports most modules except those in util | |
local MGCInts = require "mgcints" | |
-- create a default engine with 4 channels | |
local engine = MGCInts.Default.Engine(4, "test driver") | |
local macros = engine:getCommandTable() | |
local builder = MGCInts.MML.CmdBuilder() | |
-- build some dummy commands | |
for k, v in pairs {C = 60, D = 62, E = 64, F = 65, G = 67, A = 69, B = 71} do | |
local cmd = builder:setHandler(function (ch) | |
ch:addChunk((" Note: %d (%s)\n"):format(v, k)) | |
end):make() | |
macros:addCommand(k, cmd) | |
macros:addCommand(k:lower(), cmd) | |
end | |
-- volume command | |
builder:setTable(macros) | |
builder:setHandler(function (ch, v) | |
ch:addChunk((" Volume: %d\n"):format(v)) | |
end):param "Uint8":optional "100":make "v" | |
-- channel header / footer | |
local ch_cls = engine:getChannelClass() | |
function ch_cls:before () | |
self:addChunk("Channel " .. self.id .. ": \n") | |
end | |
function ch_cls:after () | |
self:addChunk "End of data\n\n" | |
end | |
-- test inserter, we will use standard output | |
engine:setInserter(function (rom, song, track) | |
rom:write("MGCInts test\nTrack: " .. track .. "\n\n") | |
song:doAll(function (ch) | |
rom:write(ch.stream:build()) | |
end) | |
end) | |
-- compile an MML string | |
local mmlstr = [[ | |
!13 v 72 | |
!2 v108 | |
!4 v | |
!1 cd !23 eF | |
!1234 gAb | |
]] | |
MGCInts.Music.Compiler():processFile(engine, mmlstr, io.stdout, 1) | |
--[[ output | |
MGCInts test | |
Track: 1 | |
Channel 1: | |
Volume: 72 | |
Note: 60 (C) | |
Note: 62 (D) | |
Note: 67 (G) | |
Note: 69 (A) | |
Note: 71 (B) | |
End of data | |
Channel 2: | |
Volume: 108 | |
Note: 64 (E) | |
Note: 65 (F) | |
Note: 67 (G) | |
Note: 69 (A) | |
Note: 71 (B) | |
End of data | |
Channel 3: | |
Volume: 72 | |
Note: 64 (E) | |
Note: 65 (F) | |
Note: 67 (G) | |
Note: 69 (A) | |
Note: 71 (B) | |
End of data | |
Channel 4: | |
Volume: 100 | |
Note: 67 (G) | |
Note: 69 (A) | |
Note: 71 (B) | |
End of data | |
]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment