Update Lua VAMP scripts to show a progress dialog

This commit is contained in:
Robin Gareus 2019-09-02 05:19:27 +02:00
parent 6edb649b53
commit a1f9beb355
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
4 changed files with 56 additions and 19 deletions

View file

@ -2,19 +2,6 @@ ardour { ["type"] = "Snippet", name = "Vamp Audio Transcription Example" }
function factory () return function ()
-- simple progress information print()ing
--[[
local progress_total;
local progress_last;
function cb (_f, pos)
local progress = 100 * pos / progress_total;
if progress - progress_last > 5 then
progress_last = progress;
print ("Progress: ", progress)
end
end
--]]
-- get Editor selection
-- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Editor
-- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
@ -25,16 +12,33 @@ function factory () return function ()
-- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
local vamp = ARDOUR.LuaAPI.Vamp ("libardourvampplugins:qm-transcription", sr)
-- prepare progress dialog
local progress_total = 0;
local progress_part = 0
local pdialog = LuaDialog.LuaProgressWindow ("Audio to MIDI", true)
function cb (_, pos)
return pdialog:progress ((pos + progress_part) / progress_total, "Analyzing")
end
-- calculate max progress
for r in sel.regions:regionlist ():iter () do
progress_total = progress_total + r:to_readable ():readable_length ()
end
-- for each selected region
-- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
for r in sel.regions:regionlist ():iter () do
-- "r" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Region
--[[
progress_total = r:to_readable ():readable_length ()
progress_last = 0
--]]
vamp:analyze (r:to_readable (), 0, nil --[[cb--]])
vamp:analyze (r:to_readable (), 0, cb)
if pdialog:canceled () then
goto out
end
progress_part = progress_part + r:to_readable ():readable_length ()
pdialog:progress (progress_part / progress_total, "Post Processing")
print ("-- Post Processing: ", r:name ())
-- post-processing takes longer than actually parsing the data :(
@ -54,4 +58,11 @@ function factory () return function ()
vamp:reset ()
end
::out::
-- hide modal progress dialog and destroy it
pdialog:done ();
pdialog = nil
vamp = nil;
collectgarbage ()
end end

View file

@ -43,6 +43,7 @@ function factory () return function ()
end
end
end
return false -- continue, !cancel
end
-- Configure Vamp plugin

View file

@ -54,6 +54,7 @@ function factory () return function ()
end
end
end
return false -- continue, !cancel
end
vamp:plugin ():setParameter ("Beats Per Bar", 4); -- TODO ask

View file

@ -22,6 +22,8 @@ function factory () return function ()
local audio_regions = {}
local start_time = Session:current_end_sample ()
local end_time = Session:current_start_sample ()
local max_pos = 0
local cur_pos = 0
for r in sel.regions:regionlist ():iter () do
if r:to_midiregion():isnil() then
local st = r:position()
@ -34,6 +36,7 @@ function factory () return function ()
end_time = et
end
table.insert(audio_regions, r)
max_pos = max_pos + r:to_readable ():readable_length ()
else
midi_region = r:to_midiregion()
end
@ -42,11 +45,24 @@ function factory () return function ()
midi_region:set_initial_position(start_time)
midi_region:set_length(end_time - start_time, 0)
local pdialog = LuaDialog.LuaProgressWindow ("Audio to MIDI", true)
function progress (_, pos)
return pdialog:progress ((cur_pos + pos) / max_pos, "Analyzing")
end
for i,ar in pairs(audio_regions) do
local a_off = ar:position ()
local b_off = midi_region:quarter_note () - midi_region:start_beats ()
vamp:analyze (ar:to_readable (), 0, nil)
vamp:analyze (ar:to_readable (), 0, progress)
if pdialog:canceled () then
goto out
end
cur_pos = cur_pos + ar:to_readable ():readable_length ()
pdialog:progress (cur_pos / max_pos, "Generating MIDI")
local fl = vamp:plugin ():getRemainingFeatures ():at (0)
if fl and fl:size() > 0 then
local mm = midi_region:midi_source(0):model()
@ -66,7 +82,15 @@ function factory () return function ()
end
mm:apply_command (Session, midi_command)
end
-- reset the plugin (prepare for next iteration)
vamp:reset ()
end
::out::
pdialog:done ();
pdialog = nil
vamp = nil;
collectgarbage ()
end end
function icon (params) return function (ctx, width, height, fg)