Skip to content

Instantly share code, notes, and snippets.

@okyeron
Created June 1, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okyeron/a67b2405c64823cfe4528fa4bde5b32f to your computer and use it in GitHub Desktop.
Save okyeron/a67b2405c64823cfe4528fa4bde5b32f to your computer and use it in GitHub Desktop.
-- clock test
--
-- ...
-- .
engine.name = 'PolyPerc'
local dividers = { 1, 4, 8 }
local led_states = { false, false, false }
local tasks = { nil, nil, nil }
local beat_timestamp = 0
local last_beat = 0
m = midi.connect()
m.event = function(data)
local d = midi.to_msg(data)
if d.type ~= "clock" then
--print(d.type)
if d.type=="start" then
print("start")
clock.resume(tasks[3])
elseif d.type=="stop" then
print("stop")
clock.stop(tasks[3])
elseif d.type=="continue" then
print("continue")
clock.resume(tasks[3])
end
-- else
--print(clock.get_time_beats())
--clock.sync(1/8)
end
end
function blink_generator(x)
return function()
led_states[x] = true
redraw()
clock.sleep(0.1)
led_states[x] = false
redraw()
end
end
function tick_generator(x, hz)
return function()
while true do
clock.sync(1/dividers[x])
clock.run(blink_generator(x))
engine.hz(hz)
end
end
end
function init()
bpm = 60
--_clock_internal_set_tempo = bpm
--print(_clock_internal_set_tempo)
_clock_internal_set_tempo(bpm)
engine.release(0.125)
clock.run(function()
while true do
redraw()
clock.sleep(1/60)
end
end)
params:add_option("clock", "clock", {"internal", "midi"}, 1)
params:set_action("clock", function(x) start_midi_clock(x, 3) end )
-- params:set_action("clock", function(x) start_midi_clock(x, 3) end )
-- params:add_number("tempo", "tempo", 1, 480, bpm)
-- params:set_action("tempo", function(x) bpm_change(x) end)
end
function start_midi_clock(x, y)
clock.set_source(x)
tasks[y] = clock.run(tick_generator(y, 220))
end
function enc(n, z)
if n == 1 then
newbpm = bpm + z
_clock_internal_set_tempo(newbpm)
bpm = newbpm
end
if n == 2 then
dividers[1] = math.max(1, dividers[1] + z)
-- redraw()
elseif n == 3 then
dividers[2] = math.max(1, dividers[2] + z)
-- redraw()
end
end
function key(n, z)
if z == 1 then
if n == 2 then
if tasks[1] then
clock.stop(tasks[1])
tasks[1] = nil
else
tasks[1] = clock.run(tick_generator(1, 200))
end
-- redraw()
elseif n == 3 then
if tasks[2] then
clock.stop(tasks[2])
tasks[2] = nil
else
tasks[2] = clock.run(tick_generator(2, 400))
end
-- redraw()
end
end
end
function redraw()
screen.clear()
screen.move(2, 8)
screen.text("bpm: " .. bpm)
screen.move(48, 8)
--screen.text(clock.get_time_beats()/36/2)
if tasks[1] then screen.level(15) else screen.level(3) end
screen.rect(16, 16, 16, 16)
if led_states[1] then
screen.fill()
end
screen.stroke()
screen.move(23, 48)
screen.text_center("1/"..dividers[1])
if tasks[2] then screen.level(15) else screen.level(3) end
screen.rect(56, 16, 16, 16)
if led_states[2] then
screen.fill()
end
screen.stroke()
screen.move(63, 48)
screen.text_center("1/"..dividers[2])
if tasks[3] then screen.level(15) else screen.level(3) end
screen.rect(96, 16, 16, 16)
if led_states[3] then
screen.fill()
end
screen.stroke()
screen.move(104, 48)
screen.text_center("1/"..dividers[3])
screen.update()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment