-
Notifications
You must be signed in to change notification settings - Fork 3
/
study2.lua
115 lines (101 loc) · 3.2 KB
/
study2.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
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
-- nest_ study 2
-- the grid & multiples
--
-- 1-16
-- 1 : page select/play
-- 2-6: note/octave
-- 3: gate
-- 4: step
include 'lib/nest/core'
include 'lib/nest/norns'
include 'lib/nest/grid'
engine.name = "PolyPerc"
scale = { 0, 2, 4, 7, 9 } -- scale degrees in semitones
root = 440 * 2^(5/12) -- the d above middle a
seq = nest_ {
tab = _grid.number {
x = { 1, 2 },
y = 1,
level = { 4, 15 },
},
play = _grid.toggle {
x = 16,
y = 1,
level = { 4, 15 },
value = 1,
},
pages = nest_ {
nest_ {
notes = nest_(16):each(function(i)
return _grid.number {
x = i,
y = { 2, 6 },
value = math.random(1, 5), -- initialize every note with a random number
-- adjust the brightness level based on step & gates
level = function(self)
if seq.gates.value[i] == 0 then return 0 -- if this step's gate is off set brightness low
elseif seq.step.value == i then return 15 -- if it's the current step set level high
else return 4 end -- otherwise set dim
end,
}
end),
enabled = function(self)
return (seq.tab.value == self.key)
end
},
nest_ {
octaves = nest_(16):each(function(i)
return _grid.number {
x = i,
y = { 2, 6 },
value = 3,
level = function(self)
if seq.gates.value[i] == 0 then return 0
elseif seq.step.value == i then return 15
else return 4 end
end
}
end),
enabled = function(self)
return (seq.tab.value == self.key)
end
}
},
gates = _grid.toggle {
x = { 1, 16 },
y = 7,
level = 4,
value = 1 -- a shortcut, toggle knows to set all the toggle values to 1
},
step = _grid.number {
x = { 1, 16 },
y = 8
}
}
-- sequencer counter
count = function()
while true do -- loop forever
if seq.play.value == 1 then -- if playing
local step = seq.step.value -- the current step
if seq.play.value == 1 and seq.gates.value[step] == 1 then -- if the current gate is high
-- find note frequency
local note = scale[seq.pages[1].notes[step].value]
local octave = seq.pages[2].octaves[step].value - 4
local hz = root * 2^octave * 2^(note/12)
engine.hz(hz) -- send a note to the engine
end
seq.step.value = step % 16 + 1 -- incriment & wrap step
seq.step:update()
end
clock.sync(1/4) -- wait for the next quarter note
end
end
-- connect the nest to a grid device
seq:connect {
g = grid.connect()
}
-- initialize the nest, start counting
function init()
seq:init()
clock.run(count)
end