Skip to content

Instantly share code, notes, and snippets.

@catfact
Last active December 28, 2020 22:31
Show Gist options
  • Save catfact/b291e43cfbd9ad7f2ce23334c52cddbb to your computer and use it in GitHub Desktop.
Save catfact/b291e43cfbd9ad7f2ce23334c52cddbb to your computer and use it in GitHub Desktop.
norns clocks comparison
--- clock timing test
---
--- uses a clock with two coroutines
engine.name = 'Boomtick'
-- two 16-note patterns
-- should be independent but synchronized
pattern_1 = {4, 4, 4, 3, 1}
len_1 = #pattern_1
track_1 = function()
while (true) do
for i=1,len_1 do
engine.boom()
clock.sync(pattern_1[i])
end
end
end
pattern_2 = {2, 3, 3}
len_2 = #pattern_2
track_2 = function()
while (true) do
for i=1,len_2 do
engine.tick()
clock.sync(pattern_2[i])
end
end
end
init = function()
params:set("reverb", 1)
-- set the clock to run at 16th-notes, 120 bpm
clock.set_source("internal")
clock.internal.set_tempo(120 * 4)
clock.run(track_1)
clock.run(track_2)
clock.internal.start()
end
--- clock timing test
--
--- uses a soft timer driven by internal clock
engine.name = 'Boomtick'
-- two 16-note patterns
-- should be independent but synchronized
pattern_1 = {4, 4, 4, 3, 1}
pattern_2 = {2, 3, 3}
state_1 = {
stage= 1,
count= 0,
nstages = #pattern_1
}
state_2 = {
stage= 1,
count= 0,
nstages = #pattern_2
}
function update(state, pattern, action)
if state.count == 0 then action() end
state.count = state.count + 1
if state.count == pattern[state.stage] then
state.count = 0
state.stage = state.stage + 1
if state.stage > state.nstages then
state.stage = 1
end
end
return state
end
main = function()
state_1 = update(state_1, pattern_1, function() engine.boom() end)
state_2 = update(state_2, pattern_2, function() engine.tick() end)
clock.sync(1)
end
init = function()
params:set("reverb", 1)
clock.set_source("internal")
clock.internal.set_tempo(120 * 4)
clock.run(main)
end
--- clock timing test
---
--- uses a soft timer driven from a metro
engine.name = 'Boomtick'
-- two 16-note patterns
-- should be independent but synchronized
pattern_1 = {4, 4, 4, 3, 1}
pattern_2 = {2, 3, 3}
state_1 = {
stage= 1,
count= 0,
nstages = #pattern_1
}
state_2 = {
stage= 1,
count= 0,
nstages = #pattern_2
}
function update(state, pattern, action)
if state.count == 0 then action() end
state.count = state.count + 1
if state.count == pattern[state.stage] then
state.count = 0
state.stage = state.stage + 1
if state.stage > state.nstages then
state.stage = 1
end
end
return state
end
main = function()
state_1 = update(state_1, pattern_1, function() engine.boom() end)
state_2 = update(state_2, pattern_2, function() engine.tick() end)
end
init = function()
params:set("reverb", 1)
local m = metro.init(main, 60 / (120 * 4), -1)
m:start()
end
Engine_Boomtick : CroneEngine {
*new { arg context, doneCallback;
^super.new(context, doneCallback);
}
alloc {
var s = Crone.server;
SynthDef.new(\boom, { arg out=0, hz=55, amp=0.2, atk=0, rel=0.1, pan= -0.5;
Out.ar(out, Pan2.ar(EnvGen.ar(Env.perc(atk, rel), doneAction:2) * SinOsc.ar(hz) * amp, pan));
}).send(s);
SynthDef.new(\tick, { arg out=0, amp=0.1, atk=0, rel=0.025, pan=0.5;
Out.ar(out, Pan2.ar(EnvGen.ar(Env.perc(atk, rel), doneAction:2) * WhiteNoise.ar * amp, pan));
}).send(s);
s.sync;
this.addCommand("boom", "", { Synth.new(\boom, target:Crone.server); });
this.addCommand("tick", "", { Synth.new(\tick, target:Crone.server); });
}
free { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment