-
Notifications
You must be signed in to change notification settings - Fork 0
/
mml.lua
200 lines (163 loc) · 5.41 KB
/
mml.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
local mml = {
_VERSION = "Lua MML",
_DESCRIPTION = "A Music Macro Language interpreter.",
_URL = "https://github.com/legospacy/lua-mml",
_LICENSE = [[
Copyright (c) 2014-2015 Andrew Abbott
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
}
-- Using A as a base note, these are how many
-- semitones/steps away a note on the same octave is.
local steps = {
a = 0,
b = 2,
c = -9,
d = -7,
e = -5,
f = -4,
g = -2
}
local REF_FREQ = 440 -- A4
local REF_OCTAVE = 4
local ROOT_MULT = 2^(1/12) -- A constant: the twelfth root of two.
-- See http://www.phy.mtu.edu/~suits/NoteFreqCalcs.html
-- for information on calculating note frequencies.
local function calculateNoteFrequency(n)
return REF_FREQ * (ROOT_MULT ^ n)
end
local function calculateNoteSteps(str)
local note, sharp, octave = string.match(str, "(%a)(#?)(%d)")
return (octave - REF_OCTAVE)*12 + steps[note] + (sharp == "" and 0 or 1)
end
-- Calculates how long a note is in seconds given a note fraction
-- (quarter note = 4, half note = 2, etc.) and a tempo (in beats per minute).
local function calculateNoteTime(notefrac, bpm)
return (240/notefrac) / bpm
end
local function calculateNote(note, outputType)
local steps = calculateNoteSteps(note)
if outputType == "frequency" then
return calculateNoteFrequency(steps)
elseif outputType == "steps" then
return steps
elseif outputType == "multiplier" then
return ROOT_MULT ^ steps
end
end
--[[
Phrases:
Note: n[#][l]
n is the note (a-g)
a # or + makes the note sharp and a - makes it flat
l is the length of the note
4 is quarter note (1/4), 2 is half note (1/2), etc.
Excluding this uses the default length, set with "l"
Rest: r[l]
l is the length of the rest, specified the same way as note length
Commands:
t[n] - set tempo to n
o[n] - set octave to n
l[n] - set default note length to n
v[n] - set volume to v
> - increment octave by one
< - decrement octave by one
]]
-- Receives a string of MML and returns a player.
-- When resumed, the player yields with the note (output set by outputType),
-- the time in seconds the note is to be played and the volume it should be played at.
-- It also yields for rests, with nil as the note and for the volume.
-- When the player reaches the end of the song, it will raise an error which
-- will be caught by coroutine.resume.
-- outputType can be:
-- "steps", outputs the number of semitones away from A 440 the note is.
-- "frequency", outputs the frequency of the note.
-- "multiplier", outputs frequency/440.
function mml.newPlayer(str, outputType)
local out_table = {}
local octave = 4
local tempo = 60
local notelength = 4
local volume = 10
local pos = 1
repeat
local c, args, newpos = string.match(
string.sub(str, pos),
"^([%a<>])(%A-)%s-()[%a<>]"
)
if not c then -- Might be the last command in the string.
c, args = string.match(
string.sub(str, pos),
"^([%a<>])(%A-)"
)
newpos = 0
end
if not c then -- Probably bad syntax.
error("Malformed MML")
end
pos = pos + (newpos - 1)
if c == "o" then -- Set octave
octave = tonumber(args)
elseif c == "t" then -- Set tempo
tempo = tonumber(args)
elseif c == "v" then -- Set volume
volume = tonumber(args)
elseif c == "r" then -- Rest
local delay
if args ~= "" then
delay = calculateNoteTime( tonumber(args), tempo )
else
delay = calculateNoteTime(notelength, tempo)
end
-- coroutine.yield(nil, delay, nil)
table.insert(out_table, {output=nil, notetime=delay, volume=nil})
elseif c == "l" then -- Set note length
notelength = tonumber(args)
elseif c == ">" then -- Increase octave
octave = octave + 1
elseif c == "<" then -- Decrease octave
octave = octave - 1
elseif c:find("[a-g]") then -- Play note
local note
local mod = string.match(args, "[+#-]")
if mod then
if mod == "#" or mod == "+" then
note = c .. "#" .. octave
elseif mod == "-" then
note = c .. "-" .. octave
end
else
note = c .. octave
end
local notetime
local len = string.match(args, "%d+")
if len then
notetime = calculateNoteTime(tonumber(len), tempo)
else
notetime = calculateNoteTime(notelength, tempo)
end
-- Dotted notes
if string.find(args, "%.") then
notetime = notetime * 1.5
end
local output = calculateNote(note, outputType)
table.insert(out_table, {output=output, notetime=notetime, volume=volume})
end
until newpos == 0
return out_table
end
return mml