Skip to content

Instantly share code, notes, and snippets.

@dndrks
Last active May 14, 2022 01:46
Show Gist options
  • Save dndrks/029be488d16f557acd24d6f22ef8a25d to your computer and use it in GitHub Desktop.
Save dndrks/029be488d16f557acd24d6f22ef8a25d to your computer and use it in GitHub Desktop.
cheat codes user script: pad sequencer example
-- cheat codes user script: pad sequencer example
-- required container, do not change this:
local user_script = {}
my_pads = {} -- use unique variables to reduce conflicts with the rest of cheat codes
-- this function is called whenever a loaded collection has a 'user_script.lua' file in the collection:
function user_script.init()
my_pads[1] = { -- sequence bank a
steps = {
[1] = 1,
[7] = 8,
[17] = 12,
},
start_point = 1,
end_point = 32,
current_step = 1,
sync_val = 1/4,
active = true
}
my_pads[2] = { -- sequence bank b
steps = {
[1] = 3,
[3] = 9,
},
start_point = 1,
end_point = 4,
current_step = 1,
sync_val = 1,
active = true
}
my_pads[3] = { -- sequence bank c
steps = {
[9] = 1,
[33] = 12,
[34] = 12,
[35] = 12,
[36] = 12,
[37] = 16,
},
start_point = 1,
end_point = 48,
current_step = 1,
sync_val = 1/4,
active = true
}
print("hello from the user script")
end
-- this function is called whenever the transport starts:
function user_script.transport_start()
print("user script started")
-- you can add your own code to execute when the transport starts:
for i = 1,#my_pads do
if my_pads[i].active then
user_script.start_clock(i)
end
end
end
-- this function is called whenever the transport stops:
function user_script.transport_stop()
print("user script stopped")
-- you can add your own code to execute when the transport stops:
for i = 1,#my_pads do
my_pads[i].current_step = my_pads[i].start_point
user_script.stop_clock(i)
end
end
-- you can add any other functions you want, just prepend them with 'user_script.'
-- this one starts the sequence clocks
function user_script.start_clock(i)
my_pads[i].clock = clock.run(
function()
while true do
local step = my_pads[i].current_step
if my_pads[i].steps[step] ~= nil then
local bank = i -- 1 = a, 2 = b, 3 = c
local pad = my_pads[i].steps[step]
-- "from user_script" just lets cheat codes know the 'pad down' isn't from the grid itself
-- 'silent' exempts these sequenced pads from the pattern recorder, remove if you want to add them!
grid_actions.pad_down(bank,pad,"from user_script",silent)
clock.run(
function()
clock.sleep(0.01)
-- "from user_script" just lets cheat codes know the 'pad up' isn't from the grid itself
grid_actions.pad_up(bank,pad,"from user_script") -- bank pad up (in case you want to run these into the arpeggiator)
end
)
end
clock.sync(my_pads[i].sync_val)
user_script.advance_step(i)
end
end
)
end
-- this one advances the sequence step
function user_script.advance_step(i)
my_pads[i].current_step = util.wrap(
my_pads[i].current_step+1,
my_pads[i].start_point,
my_pads[i].end_point
)
end
-- this one clears the sequence clocks, called when the transport stops
function user_script.stop_clock(i)
if my_pads[i].clock ~= nil then
clock.cancel(my_pads[i].clock)
end
end
-- required return, do not change this:
return user_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment