Skip to content

Instantly share code, notes, and snippets.

@catfact
Created November 19, 2023 02:55
Show Gist options
  • Save catfact/aac3fd79c333721ad761bda4a949b2a9 to your computer and use it in GitHub Desktop.
Save catfact/aac3fd79c333721ad761bda4a949b2a9 to your computer and use it in GitHub Desktop.
lonk.lua
engine.name = 'PolyPerc'
local sync_first = true
function key(n,z)
if n == 3 and z == 1 and not transport_running then
print("starting link")
clock.link.start() -- start Link clock
elseif n == 3 and z == 1 and transport_running then
print("stopping link")
clock.link.stop() -- stop Link clock
end
end
function clock.transport.start()
print("clock.transport.start")
sync_first = false
id = clock.run(clock_loop)
transport_running = true
redraw()
end
function clock.transport.stop()
print("clock.transport.stop")
clock.cancel(id)
transport_running = false
pulse_count = 0
redraw()
end
function clock_loop()
--if sync_first then
clock.sync(1)
-- else sync_first = true end
while true do
print("------")
local phase = _norns.clock_link_get_phase()
-- fudge because the reported phase is 0.1ms old:
-- (assuming quantum is 4)
phase = math.fmod((phase + 0.1), 4)
local iphase = math.floor(phase)
print("my link phase: " .. iphase .. " (" ..phase..")")
local hz
if iphase == 0 then hz = 330
else hz = 220 end
engine.hz(hz)
--pulse_count = util.wrap(pulse_count + 1,1,4)
pulse_count = iphase
redraw()
clock.sync(1)
end
end
function redraw()
screen.clear()
screen.move(20,40)
screen.text(transport_running and 'running' or 'not running')
screen.move(pulse_count * 20,60)
screen.text('|')
screen.update()
end
function init()
transport_running = false
pulse_count = 0
capture_preinit()
end
function capture_preinit()
preinit_clock_source = params:get('clock_source')
preinit_sync = params:get('link_start_stop_sync')
preinit_quantum = params:get('link_quantum')
preinit_tempo = params:get('clock_tempo')
print('setting clock source to Link')
params:set('clock_source',3)
print('enabling start/stop sync')
params:set('link_start_stop_sync',2)
print('setting quantum to 1 beat')
params:set('link_quantum', 4)
print('setting tempo to 95 bpm')
params:set('clock_tempo',95)
end
function cleanup()
-- important! stop Link clock with script cleanup:
clock.link.stop()
print('resetting parameters')
params:set('clock_source',preinit_clock_source)
params:set('link_start_stop_sync',preinit_sync)
params:set('link_quantum',preinit_quantum)
params:set('clock_tempo',preinit_tempo)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment