-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnote.lua
More file actions
177 lines (164 loc) · 4.59 KB
/
note.lua
File metadata and controls
177 lines (164 loc) · 4.59 KB
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
NotesNames = {
C = 1,
D = 2,
E = 3,
F = 4,
G = 5,
A = 6,
B = 7
}
local SHARP <const> = "♯"
local FLAT <const> = "♭"
Note = {}
Note.__index = Note
setmetatable(Note, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Note.new(name, alteration)
local self = setmetatable({}, Note)
self.name = name
self.alteration = alteration
return self
end
function Note:clone()
return Note(self.name, self.alteration)
end
function Note.from_semitone(st, prec)
mod_st = st % 12
if prec ~= nil then
local new_note_name = (prec.name % 7) + 1
local alteration = mod_st - Note(new_note_name, 0):to_semitone()
return Note(new_note_name, alteration)
else
if mod_st == 0 then
return Note(NotesNames.C, 0)
elseif mod_st == 1 then
return Note(NotesNames.C, 1)
elseif mod_st == 2 then
return Note(NotesNames.D, 0)
elseif mod_st == 3 then
return Note(NotesNames.D, 1)
elseif mod_st == 4 then
return Note(NotesNames.E, 0)
elseif mod_st == 5 then
return Note(NotesNames.F, 0)
elseif mod_st == 6 then
return Note(NotesNames.F, 1)
elseif mod_st == 7 then
return Note(NotesNames.G, 0)
elseif mod_st == 8 then
return Note(NotesNames.G, 1)
elseif mod_st == 9 then
return Note(NotesNames.A, 0)
elseif mod_st == 10 then
return Note(NotesNames.A, 1)
elseif mod_st == 11 then
return Note(NotesNames.B, 0)
end
end
end
function Note:offset(note_offset, semitones)
local note_name = (self.name + note_offset - 1) % 7 + 1
local note = Note(note_name, 0)
local semitones1 = self:to_semitone()
local semitones2 = note:to_semitone()
local diff = (semitones2 - semitones1 + 12) % 12
note.alteration = semitones - diff
return note
end
function Note:to_semitone()
if self.name == NotesNames.C then
return 0 + self.alteration
elseif self.name == NotesNames.D then
return 2 + self.alteration
elseif self.name == NotesNames.E then
return 4 + self.alteration
elseif self.name == NotesNames.F then
return 5 + self.alteration
elseif self.name == NotesNames.G then
return 7 + self.alteration
elseif self.name == NotesNames.A then
return 9 + self.alteration
elseif self.name == NotesNames.B then
return 11 + self.alteration
else
return math.mininteger
end
end
function Note:name_tostring()
if self.name == NotesNames.C then
return "C"
elseif self.name == NotesNames.D then
return "D"
elseif self.name == NotesNames.E then
return "E"
elseif self.name == NotesNames.F then
return "F"
elseif self.name == NotesNames.G then
return "G"
elseif self.name == NotesNames.A then
return "A"
elseif self.name == NotesNames.B then
return "B"
else
return "ERROR"
end
end
function Note:alteration_tostring()
if self.alteration == -2 then
return FLAT .. FLAT
elseif self.alteration == -1 then
return FLAT
elseif self.alteration == 0 then
return ""
elseif self.alteration == 1 then
return SHARP
elseif self.alteration == 2 then
return SHARP .. SHARP
else
return "ERROR"
end
end
function Note:tostring()
return self:name_tostring() .. self:alteration_tostring()
end
function Note:get_downward_enharmonic()
if self.name == NotesNames.C then
return Note(NotesNames.B, self.alteration + 1)
elseif self.name == NotesNames.D then
return Note(NotesNames.C, self.alteration + 2)
elseif self.name == NotesNames.E then
return Note(NotesNames.D, self.alteration + 2)
elseif self.name == NotesNames.F then
return Note(NotesNames.E, self.alteration + 1)
elseif self.name == NotesNames.G then
return Note(NotesNames.F, self.alteration + 2)
elseif self.name == NotesNames.A then
return Note(NotesNames.G, self.alteration + 2)
elseif self.name == NotesNames.B then
return Note(NotesNames.A, self.alteration + 2)
else
return "ERROR"
end
end
function Note:get_upward_enharmonic()
if self.name == NotesNames.C then
return Note(NotesNames.D, self.alteration - 2)
elseif self.name == NotesNames.D then
return Note(NotesNames.E, self.alteration - 2)
elseif self.name == NotesNames.E then
return Note(NotesNames.F, self.alteration - 1)
elseif self.name == NotesNames.F then
return Note(NotesNames.G, self.alteration - 2)
elseif self.name == NotesNames.G then
return Note(NotesNames.A, self.alteration - 2)
elseif self.name == NotesNames.A then
return Note(NotesNames.B, self.alteration - 2)
elseif self.name == NotesNames.B then
return Note(NotesNames.C, self.alteration - 1)
else
return "ERROR"
end
end