Hi, I’m trying to make lua script that copies midi region several times after it, immediately after being recorded - yet another try to use Ardour for live looping. But I can’t get it to work cause the region is “written” to the playlist of a track only when I hit stop button. Is there a way to access that midi region without stopping playback or tell Ardour to write region to playlist?
This is my code so far:
ardour { ["type"] = "Snippet", name = "dup_midi_region" }
function factory()
return function()
local route = Session:route_by_name("synth")
local track = route:to_track()
local playhead = Session:transport_frame()
local loop = Session:locations():auto_loop_location()
local proc = ARDOUR.LuaAPI.nil_proc() -- bounce w/o processing
local itt = ARDOUR.InterThreadInfo() -- bounce progress info (unused)
if loop then
print("Loop set.")
-- prepare undo operation
Session:begin_reversible_command("dup_midi_region")
local add_undo = false -- keep track if something has changed
local loop_start = loop:start()
local loop_end = loop:_end()
local playlist = track:playlist()
for r in playlist:region_list():iter() do
print("region name: "..tostring(r:name()))
end
local loop_region = playlist:regions_touched(loop_start, loop_end)
local region = track:bounce_range(loop_start, loop_end, itt, proc, false)
print("region captured: "..tostring(region:captured()))
if region then
print("Region found. Copying.")
playlist:add_region(region, playhead, 1, false, 0, 0, false)
end
-- create a diff of the performed work, add it to the session's undo stack
-- and check if it is not empty
if not Session:add_stateful_diff_command(playlist:to_statefuldestructible()):empty() then
add_undo = true
end
-- all done, commit the combined Undo Operation
if add_undo then
-- the 'nil' Command here mean to use the collected diffs added above
Session:commit_reversible_command (nil)
else
Session:abort_reversible_command ()
end
else
print("Loop not set.")
end
end
end
Btw I tried to do it by stopping and starting playback again in lua script using Editor:access_action(), but can’t figure out what action is for playback start (Roll didn’t work and PlaySelection and PlayPreroll aren’t suitable).
Thank you.