Skip to content

Instantly share code, notes, and snippets.

@ambv
Created November 19, 2023 20:03
Show Gist options
  • Save ambv/54cfaf2a5824cc6fb37cec4aef1f21c0 to your computer and use it in GitHub Desktop.
Save ambv/54cfaf2a5824cc6fb37cec4aef1f21c0 to your computer and use it in GitHub Desktop.
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
local quantum = params:get('link_quantum')
while true do
print("------")
local hz
pulse_count = math.fmod(math.floor(clock.get_beats()), quantum)
print("beats: " .. tostring(clock.get_beats()))
if pulse_count == 0 then hz = 330
else hz = 220 end
engine.hz(hz)
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