Created
August 23, 2021 22:29
-
-
Save chmanie/06144f4377d28580c63d3d9a3439b64d to your computer and use it in GitHub Desktop.
Just a little norns drum sequencer PoC using grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lattice = require("lattice") | |
function createGridState() | |
local grid = {} | |
for i = 1, 16 do | |
grid[i] = {} | |
for j = 1, 8 do | |
grid[i][j] = 0 | |
end | |
end | |
return grid | |
end | |
prevStep = 0 | |
step = 0 | |
g = 0 -- grid ... instance? | |
m = 0 -- midi ... instance? | |
gridState = createGridState() | |
running = false | |
function flash(x, y) | |
g:led(x, y, 0) | |
g:refresh() | |
clock.sleep(1/32) | |
g:led(x, y, 15) | |
g:refresh() | |
end | |
function sendMidiTrigger(row) | |
m:note_on(60, 127, 8 + row) | |
clock.sleep(1/16) | |
m:note_off(60, 0, 8 + row) | |
end | |
function drawPlayhead() | |
-- Remove last column only if we're at least at step 2 | |
if prevStep > 0 then | |
for i = 1, 8 do | |
if gridState[prevStep][i] == 0 then | |
g:led(prevStep, i, 0) | |
end | |
end | |
end | |
for i = 1, 8 do | |
if gridState[step][i] == 0 then | |
g:led(step, i, 1) | |
else | |
clock.run(function(t) flash(step, i) end) | |
end | |
end | |
g:refresh() | |
end | |
function removePlayhead() | |
for i = 1, 8 do | |
if gridState[step][i] == 0 then | |
g:led(step, i, 0) | |
end | |
end | |
g:refresh() | |
end | |
function next(nppqn) | |
if running == false then | |
m:start() | |
running = true | |
end | |
prevStep = step | |
step = util.wrap(step + 1, 1, 16) | |
drawPlayhead() | |
for i = 1, 8 do | |
if gridState[step][i] == 1 then | |
clock.run(function(t) sendMidiTrigger(i) end) | |
end | |
end | |
end | |
function onPressGridKey(x, y, z) | |
if z == 0 then return end | |
if gridState[x][y] == 0 then | |
gridState[x][y] = 1 | |
g:led(x, y, 15) | |
else | |
gridState[x][y] = 0 | |
g:led(x, y, 0) | |
end | |
g:refresh() | |
end | |
function init() | |
g = grid.connect() | |
m = midi.connect() | |
g.key = onPressGridKey | |
-- default lattice usage, with no arguments | |
default_lattice = lattice:new() | |
-- default lattice usage, showing default arguments | |
my_lattice = lattice:new{ | |
auto = true, | |
meter = 4, | |
ppqn = 96 | |
} | |
pattern_d = my_lattice:new_pattern{ | |
action = next, | |
division = 1/16, | |
enabled = true | |
} | |
-- start the lattice | |
my_lattice:start() | |
-- demo stuff | |
screen_dirty = true | |
redraw_clock_id = clock.run(redraw_clock) | |
end | |
function key(k, z) | |
if z == 0 then return end | |
if k == 2 then | |
my_lattice:toggle() | |
elseif k == 3 then | |
my_lattice:stop() | |
m:stop() | |
removePlayhead() | |
prevStep = 0 | |
step = 0 | |
running = false | |
end | |
end | |
function enc(e, d) | |
params:set("clock_tempo", params:get("clock_tempo") + d) | |
screen_dirty = true | |
end | |
function cleanup() | |
my_lattice:destroy() | |
end | |
-- screen stuff | |
function redraw_clock() | |
while true do | |
clock.sleep(1/15) | |
if screen_dirty then | |
redraw() | |
screen_dirty = false | |
end | |
end | |
end | |
function redraw() | |
screen.clear() | |
screen.level(15) | |
screen.aa(0) | |
screen.font_size(8) | |
screen.font_face(0) | |
screen.move(1, 8) | |
screen.text(params:get("clock_tempo") .. " BPM") | |
screen.update() | |
end | |
function cleanup() end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment