forked from monome/bowery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuclidean.lua
75 lines (63 loc) · 1.47 KB
/
euclidean.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
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
--- Euclidean Rhythms
-- sam wolk 2019.10.21
-- Create 4 euclidean rhythm generators
-- in1: clock
-- in2: reset
-- outs: euclidean rhythms
-- ER parameters for each channel.
lengths = {16,16,16,16}
fills = {4,5,9,12}
offsets = {0,0,0,0}
-- Non-User Variables
locations = {-1,-1,-1,-1} --
-- ER function adapted from https://gist.github.com/vrld/b1e6f4cce7a8d15e00e4
-- k > Euclidean Fill
-- n > Euclidean Length
-- s > current step (0-indexed)
function er(k,n,s)
local r = {}
for i = 1,n do
r[i] = {i <= k}
end
local function cat(i,k)
for _,v in ipairs(r[k]) do
r[i][#r[i]+1] = v
end
r[k] = nil
end
while #r > k do
for i = 1,math.min(k, #r-k) do
cat(i, #r)
end
end
while #r > 1 do
cat(#r-1, #r)
end
return r[1][s+1]
end
-- Use a trigger to advance ER counters and activate ASLs on hits
input[1].change = function(state)
for i=1,4 do
--- increment counters
locations[i] = ((locations[i]+1) % lengths[i])
-- get current location
local index = ((locations[i]+offsets[i]) % lengths[i])
-- fire asl if there is a hit
if er(fills[i],lengths[i],index) then
output[i]()
end
end
end
-- Use a trigger to reset locations
input[2].change = function(state)
for i=1,4 do
locations[i]=-1
end
end
function init()
input[1].mode('change',1,0.1,'rising')
input[2].mode('change',1,0.1,'rising')
for i=1,4 do
output[i].action = pulse(0.02,8)
end
end