From 127cb8b6039390f693e274de6598175b81247c12 Mon Sep 17 00:00:00 2001 From: brian crabtree Date: Fri, 29 May 2020 09:25:59 -0400 Subject: [PATCH] pattern time overdub (#1127) * 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 --- lua/lib/pattern_time.lua | 42 ++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lua/lib/pattern_time.lua b/lua/lib/pattern_time.lua index c1a7f43f0..97f5d4f45 100644 --- a/lua/lib/pattern_time.lua +++ b/lua/lib/pattern_time.lua @@ -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 = {} @@ -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 = {} @@ -54,15 +56,15 @@ 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 @@ -70,6 +72,8 @@ end function pattern:watch(e) if self.rec == 1 then self:rec_event(e) + elseif self.overdub == 1 then + self:overdub_event(e) end end @@ -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 @@ -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) @@ -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