-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscale.lua
More file actions
243 lines (216 loc) · 6.04 KB
/
scale.lua
File metadata and controls
243 lines (216 loc) · 6.04 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
require("note")
require("array")
Scale = {}
Scale.__index = Scale
setmetatable(Scale, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Scale.new(notes)
local self = setmetatable({}, Scale)
self.notes = Array(notes)
return self
end
function Scale.from_tonic_and_mode(tonic, mode)
local c_maj = Scale.c_major()
local alterations = mode:get_alterations(Mode.IONIAN)
return c_maj:add_alterations(alterations):transpose(tonic)
end
function Scale:clone()
return Scale(self.notes:clone())
end
function Scale:get_tonic()
return self.notes[1]
end
function Scale:get_mode()
local semi_tones = Array(self:to_semitones())
local intervals = semi_tones:rotate_values(1)
:zip(semi_tones)
:map(function(e) return (e[1] - e[2]) % 12 end)
return Mode.from_intervals(intervals)
end
function Scale:transpose(new_tonic)
local transpose_distance = new_tonic.name - self.notes[1].name
local semi_tones = Array{2, 2, 1, 2, 2, 2, 1}
:rotate_values(self.notes[1].name - NotesNames.C)
local rotated_semi_tones = semi_tones:rotate_values(transpose_distance)
local rotation_induced_alterations = semi_tones
:diff_values(rotated_semi_tones)
:acc_table_values()
local self_alterations = self.notes:map(function(n) return n.alteration end)
local final_alterations = rotation_induced_alterations
:map(
function(alt)
return alt + new_tonic.alteration
end)
:push_head(new_tonic.alteration)
:pop_tail()
:zip(self_alterations)
:map(function(elt)
return elt[1] + elt[2]
end)
return Scale(self.notes
:rotate_values(transpose_distance)
:zip(final_alterations)
:map(function(elt)
return Note(elt[1].name, elt[2])
end))
end
function Scale.get_major(tonic_note)
return Scale.c_major():transpose(tonic_note)
end
function Scale.c_major()
return Scale({
Note(NotesNames.C, 0),
Note(NotesNames.D, 0),
Note(NotesNames.E, 0),
Note(NotesNames.F, 0),
Note(NotesNames.G, 0),
Note(NotesNames.A, 0),
Note(NotesNames.B, 0)
})
end
function Scale:count_alterations()
return self.notes:map(function(n) return n.alteration end):sum()
end
--[[
Warning: only works for ionian scales
]]
function Scale:to_the_left_on_circle_of_5ths(iterations)
if iterations == 0 then
return self
end
return self:rotate(4)
:add_alteration(4, -1)
:to_the_left_on_circle_of_5ths(iterations -1)
end
--[[
Warning: only works for ionian scales
]]
function Scale:to_the_right_on_circle_of_5ths(iterations)
if(iterations == 0) then
return self
end
return self:rotate(5)
:add_alteration(7, 1)
:to_the_right_on_circle_of_5ths(iterations -1)
end
--[[
Warning: only works for ionian scales
]]
function Scale:circle_of_5ths_rotate(iterations)
local total_alterations = self:count_alterations()+iterations
local rotation = ( (total_alterations + 5) % 12 ) - 5 - self:count_alterations()
if(rotation == 0) then
return self
elseif(rotation < 0) then
return self:to_the_left_on_circle_of_5ths(-rotation)
elseif(rotation > 0) then
return self:to_the_right_on_circle_of_5ths(rotation)
else
return "Error"
end
end
--[[
Returns a new scale based on self, rotated from note_offset
note_offset:
* 1 => root, no rotation
* 2 => 2nd
* 3 => 3rd
etc
]]
function Scale:rotate(note_offset)
local notes = self.notes:rotate_values(note_offset - 1)
return Scale(notes)
end
--[[
Returns a new scale based on self, adding an alteration
note_offset:
* 1 => root
* 2 => 2nd
* 3 => 3rd
etc
alteration:
* +1 note is raised by a semitone
* -1 note is lowered by a semitone
]]
function Scale:add_alteration(note_offset, alteration)
local new_scale = self:clone()
new_scale.notes[note_offset].alteration = new_scale.notes[note_offset].alteration + alteration
return new_scale
end
function Scale:add_alterations(alterations)
local new_alterations = self.notes
:map(function(n) return n.alteration end)
:zip(alterations)
:map(function(e) return e[1] + e[2] end)
local new_notes = self.notes
:zip(new_alterations)
:map(function(e) return Note(e[1].name, e[2]) end)
return Scale(new_notes)
end
function Scale:to_semitones()
local result = {}
local last = 0
local st = 0
for i = 1, 7 do
st = self.notes[i]:to_semitone()
-- we want the scale to be ascending
if st < last then
st = st + 12
end
last = st
table.insert(result, st)
end
return result
end
function Scale:get_downward_enharmonic()
return Scale(self.notes:map(function(note) return note:get_downward_enharmonic() end))
end
function Scale:get_upward_enharmonic()
return Scale(self.notes:map(function(note) return note:get_upward_enharmonic() end))
end
function Scale:tostring()
local f = function(acc, n)
return acc .. " " .. n:tostring()
end
return self.notes:fold_left("{", f) .. "}"
end
function Scale:get_distance(other_scale)
function compare_scales(ref, scale)
local f = function(acc, elt)
if elt ~= 0 then
return acc + 1
else
return acc
end
end
local alterations_diff = Array(scale.notes)
:zip(ref.notes)
:map(function(e) return e[1].alteration - e[2].alteration end)
return alterations_diff:fold_left(0, f)
end
function align_scales(ref, scale)
local rotation = (ref.notes[1].name - scale.notes[1].name + 8) % 7
return scale:rotate(rotation)
end
function align_downward(ref, scale)
local rotation = (ref.notes[1].name - scale.notes[1].name + 8) % 7
return scale:rotate(rotation)
end
local aligned = align_scales(self, other_scale)
local closest_up_or_downward = aligned
:rotate(7)
:get_upward_enharmonic()
if self.notes[1].alteration - aligned.notes[1].alteration > 0 then
closest_up_or_downward = aligned
:rotate(2)
:get_downward_enharmonic()
end
return math.min(compare_scales(self, aligned), compare_scales(self, closest_up_or_downward))
end
function Scale:get_note_from_degree(degree)
local note = self.notes[degree.name]
return Note(note.name, note.alteration + degree.alteration)
end