-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdegree.lua
More file actions
78 lines (69 loc) · 1.4 KB
/
degree.lua
File metadata and controls
78 lines (69 loc) · 1.4 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
local SHARP <const> = "♯"
local FLAT <const> = "♭"
DegreeNames = {
I = 1,
II = 2,
III = 3,
IV = 4,
V = 5,
VI = 6,
VII = 7
}
Degree = {}
Degree.__index = Degree
setmetatable(Degree, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Degree.new(name, alteration)
local self = setmetatable({}, Degree)
self.name = name
self.alteration = alteration
return self
end
function Degree:clone()
return Degree(self.name, self.alteration)
end
function Degree:offset(offset)
local deg = self:clone()
deg.name = (deg.name - 1 + offset) % 7 + 1
return deg
end
function Degree:name_tostring()
if self.name == 1 then
return "I"
elseif self.name == 2 then
return "II"
elseif self.name == 3 then
return "III"
elseif self.name == 4 then
return "IV"
elseif self.name == 5 then
return "V"
elseif self.name == 6 then
return "VI"
elseif self.name == 7 then
return "VII"
else
return "ERROR"
end
end
function Degree: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 Degree:tostring()
return self:alteration_tostring() .. self:name_tostring()
end