Skip to content

Instantly share code, notes, and snippets.

@dndrks
Last active April 11, 2020 18:38
Show Gist options
  • Save dndrks/d99faa40f6832b1e37468ff61f773389 to your computer and use it in GitHub Desktop.
Save dndrks/d99faa40f6832b1e37468ff61f773389 to your computer and use it in GitHub Desktop.
-- physical: clock edition
-- norns study 4
--
-- grid controls arpeggio
-- midi controls root note
-- ENC1 = bpm
-- ENC2 = divisor
-- ENC3 = scale
-- KEY2 = hold
-- KEY3 = restart sequence
engine.name = 'PolyPerc'
local music = require 'musicutil'
steps = {}
position = 1
transpose = 0
active = 1
divisor = 4
local task_id = nil
mode = math.random(#music.SCALES)
scale = music.generate_scale_of_length(60,music.SCALES[mode].name,8)
function init()
for i=1,16 do
table.insert(steps,math.random(8))
end
grid_redraw()
task_id = clock.run(step)
end
function step()
while true do
clock.sync(1/divisor)
if active == 1 then
position = (position % 16) + 1
engine.hz(music.note_num_to_freq(scale[steps[position]] + transpose))
grid_redraw()
end
end
end
function key(n,z)
if n == 2 then
active = math.abs(z-1)
elseif n == 3 then
if z == 1 then
clock.cancel(task_id)
task_id = clock.run(step)
position = 0
end
end
end
function enc(n,d)
if n == 1 then
params:delta("clock_tempo",d)
elseif n == 2 then
divisor = util.clamp(divisor + d,1,8)
elseif n == 3 then
mode = util.clamp(mode + d, 1, #music.SCALES)
scale = music.generate_scale_of_length(60,music.SCALES[mode].name,8)
end
redraw()
end
function redraw()
screen.clear()
screen.level(15)
screen.move(0,30)
screen.text("bpm: "..params:get("clock_tempo"))
screen.move(0,40)
screen.text("div: 1/".. divisor*4)
screen.move(0,50)
screen.text(music.SCALES[mode].name)
screen.update()
end
g = grid.connect()
g.key = function(x,y,z)
if z == 1 then
steps[x] = y
grid_redraw()
end
end
function grid_redraw()
g:all(0)
for i=1,16 do
g:led(i,steps[i],i==position and 15 or 4)
end
g:refresh()
end
m = midi.connect()
m.event = function(data)
local d = midi.to_msg(data)
if d.type == "note_on" then
transpose = d.note - 60
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment