Skip to content

Commit

Permalink
pattern time overdub (#1127)
Browse files Browse the repository at this point in the history
* typed but not tested

* forgotten line

* pattern time overdub (#1126)

* debugging

- include `overdub` in the constructor
- `overdub_event`: insert both `.time` and `.event`
- `overdub_event`: calculates previous step's time adjustment so total time after overdub remains the same

* force step increase after overdub event

this keeps the overdub entry from overriding total loop time

Co-authored-by: dan derks <[email protected]>
  • Loading branch information
tehn and dndrks authored May 29, 2020
1 parent 805a2a9 commit 127cb8b
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions lua/lib/pattern_time.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function pattern.new()
setmetatable(i, pattern)
i.rec = 0
i.play = 0
i.overdub = 0
i.prev_time = 0
i.event = {}
i.time = {}
Expand All @@ -29,6 +30,7 @@ function pattern:clear()
self.metro:stop()
self.rec = 0
self.play = 0
self.overdub = 0
self.prev_time = 0
self.event = {}
self.time = {}
Expand All @@ -54,22 +56,24 @@ function pattern:rec_stop()
if self.rec == 1 then
self.rec = 0
if self.count ~= 0 then
print("count "..self.count)
--print("count "..self.count)
local t = self.prev_time
self.prev_time = util.time()
self.time[self.count] = self.prev_time - t
--tab.print(self.time)
else
print("no events recorded")
print("pattern_time: no events recorded")
end
else print("not recording")
else print("pattern_time: not recording")
end
end

--- watch
function pattern:watch(e)
if self.rec == 1 then
self:rec_event(e)
elseif self.overdub == 1 then
self:overdub_event(e)
end
end

Expand All @@ -78,21 +82,33 @@ function pattern:rec_event(e)
local c = self.count + 1
if c == 1 then
self.prev_time = util.time()
--print("first event")
else
local t = self.prev_time
self.prev_time = util.time()
self.time[c-1] = self.prev_time - t
--print(self.time[c-1])
end
self.count = c
self.event[c] = e
end

--- add overdub event
function pattern:overdub_event(e)
local c = self.step + 1
local t = self.prev_time
self.prev_time = util.time()
local a = self.time[c-1]
self.time[c-1] = self.prev_time - t
table.insert(self.time, c, a - self.time[c-1])
table.insert(self.event, c, e)
self.step = self.step + 1
self.count = self.count + 1
end

--- start this pattern
function pattern:start()
if self.count > 0 then
print("start pattern ")
--print("start pattern ")
self.prev_time = util.time()
self.process(self.event[1])
self.play = 1
self.step = 1
Expand All @@ -103,6 +119,7 @@ end

--- process next event
function pattern:next_event()
self.prev_time = util.time()
if self.step == self.count then self.step = 1
else self.step = self.step + 1 end
--print("next step "..self.step)
Expand All @@ -116,10 +133,19 @@ end
--- stop this pattern
function pattern:stop()
if self.play == 1 then
print("stop pattern ")
self.play = 0
self.overdub = 0
self.metro:stop()
else print("not playing") end
else print("pattern_time: not playing") end
end

--- set overdub
function pattern:set_overdub(s)
if s==1 and self.play == 1 and self.rec == 0 then
self.overdub = 1
else
self.overdub = 0
end
end

return pattern

0 comments on commit 127cb8b

Please sign in to comment.