-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmml.rb
56 lines (55 loc) · 1.61 KB
/
mml.rb
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
# Attempt at a MML parser and player in Sonic Pi
define :mml do |txt, vol_scale=100|
octave = 5
length = 4
txt = txt.downcase.gsub(/^mml@| |;$/, '').gsub(/#/, '+').gsub(/w/, 'r')
with_bpm 120 do
until txt.empty?
r = txt.scan(/^(.)([+-]?)(\d*)(\.?)(.*)$/)
cmd, opt, num, dot, txt = r[0]
#print cmd, opt, num
case cmd
when 'o'
if not num.empty? then
octave = num.to_i
end
when 'l'
if not num.empty? then
length = num.to_i
end
when 't'
if not num.empty? then
use_bpm num.to_i
end
when 'v'
if not num.empty? then
set_volume! num.to_f / vol_scale
end
when '>'
octave += 1
when '<'
octave -= 1
when 'a'..'g', 'r'
a = if opt == '+' then 's' elsif opt == '-' then 'b' else '' end
n = cmd
if n != 'r' then
n += a + octave.to_s
end
t = (if dot.empty? then 4.0 else 6.0 end) / (if num.empty? then length else num.to_i end)
r = txt.scan(/^(\^|&[a-gr][+-]?)(\d*)(\.?)(.*)$/)
until r.empty?
cmd, num, dot, txt = r[0]
t += (if dot.empty? then 4.0 else 6.0 end) / (if num.empty? then length else num.to_i end)
r = txt.scan(/^(\^|&[a-gr][+-]?)(\d*)(\.?)(.*)$/)
end
play_pattern_timed [n.to_sym], [t], release: t
when 'n'
n = num.to_i + 12
t = (if dot.empty? then 4.0 else 6.0 end) / length
play_pattern_timed [n], [t], release: t
else
print "Unrecognised:", cmd + opt + num + dot
end
end
end
end