-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpart.lua
46 lines (40 loc) · 1.05 KB
/
part.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
require("array")
Part = {}
Part.__index = Part
setmetatable(Part, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Part.new(timeSignature, nbBars, chord_events)
local self = setmetatable({}, Part)
self.timeSignature = timeSignature
self.nbBars = nbBars
local evts = Array(chord_events)
function less_than(evt1, evt2)
return evt1.time:to_time_index() < evt1.time:to_time_index()
end
evts:sort(less_than)
self.chord_events = evts
return self
end
function Part:clone()
return Part(self.timeSignature, self.nbBars, self.chord_events:clone())
end
function Part:add_chord_event(chord_evt)
local evts = self.chord_events:clone()
evts:push_tail(chord_evt)
return Part(self.timeSignature:clone(), nbBars, evts)
end
function Part:get_current_chord(time)
local time_index = time:to_time_index()
local current_chord = nil
for i,evt in ipairs(self.chord_events) do
if evt:get_time_index() <= time_index then
current_chord = evt.chord
else
return current_chord
end
end
return current_chord
end