-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.lua
164 lines (146 loc) · 3.44 KB
/
notes.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
local livereload = require "livereload"
local utils = livereload "utils"
local notes = {}
--[[
Note:
{
octave (int),
steps (mod7),
accidental (-2..2),
}
Key:
{
sharps (-7..7),
root (mod7)
}
Cleff: "treble" | "bass" | "piano"
]]
local function modulo(number, mod, base)
if base == nil then
base = 0
end
while number < base do
number = number + mod
end
while number >= base + mod do
number = number - mod
end
return number
end
function notes.adjustmentForKey(key)
local sharps, root = unpack(key)
local adjustment = 4 * sharps + (root - 1)
while adjustment >= 7 do
adjustment = adjustment - 7
end
while adjustment < 0 do
adjustment = adjustment + 7
end
return adjustment
end
function notes.accidentalsForKey(key)
local sharps, root = unpack(key)
if sharps == 0 then
return {}
end
local value = 1
if sharps < 0 then
value = -1
end
local i
local result = {}
for i = value, value * math.abs(sharps), value do
note = modulo(value * ((math.abs(i) * 4) -2) + 2, 7, 1)
table.insert(result, {note, value})
end
return result
end
function notes.inKey(note, key, octave)
if octave == nil then
octave = 0
end
local sharps, root = unpack(key)
local octave, steps, accidental = unpack(note)
if accidental == nil then
accidental = 0
end
steps = steps + notes.adjustmentForKey(key)
local accidentals = {}
for _, accidental in ipairs(notes.accidentalsForKey(key)) do
local note, value = unpack(accidental)
accidentals[note] = value
end
while steps > 7 do
steps = steps - 7
octave = octave + 1
end
while steps < 0 do
steps = steps + 7
octave = octave - 1
end
accidental = accidental + (accidentals[steps] or 0)
return {octave, steps, accidental}
end
function notes.keyAccidentalPositions(key, cleff)
local sharps = unpack(key)
local result = {{}}
if cleff == "piano" then
table.insert(result, {})
end
local accidentals = notes.accidentalsForKey(key)
for _, accidental in ipairs(accidentals) do
local note, value = unpack(accidental)
staff_pos = notes.staffPosition({0, note}, cleff, {0, 1})
local base = 1
if cleff == "treble" then
base = 3
end
staff_pos = modulo(staff_pos, 7, base)
table.insert(result[1], {value, staff_pos})
if cleff == "piano" then
table.insert(result[2], {value, modulo(staff_pos, 7, 15)})
end
end
return result
end
function notes.staffPosition(note, cleff, key)
local pos = 0
if key then
pos = pos + notes.adjustmentForKey(key)
if note then
local octave, steps = unpack(note)
pos = pos + octave * 7 + steps - 1
end
end
if cleff == "treble" then
pos = pos - 2
elseif cleff == "bass" or cleff == "piano" then
pos = pos + 10
end
return pos
end
function notes.isUp(position, cleff, prev_up)
local up = true
if position == 4 or (cleff == "piano" and (position == 10 or position == 16)) then
up = prev_up
else
up = position < 4 or (cleff == "piano" and position > 10 and position < 16)
end
return up
end
function notes.areEqual(a, b)
return a[1] == b[1] and a[2] == b[2] and (a[3] or 0) == (b[3] or 0)
end
function notes.normalize(note)
local octave, step, accidental = unpack(note)
while step > 7 do
octave = octave + 1
step = step - 7
end
while step < 1 do
octave = octave - 1
step = step + 7
end
return {octave, step, accidental}
end
return notes