Update Lua VAMP scripts to show a progress dialog
authorRobin Gareus <robin@gareus.org>
Mon, 2 Sep 2019 03:19:27 +0000 (05:19 +0200)
committerRobin Gareus <robin@gareus.org>
Mon, 2 Sep 2019 03:19:27 +0000 (05:19 +0200)
scripts/_vamp_note_example.lua
scripts/_vamp_onset_example.lua
scripts/_vamp_tempomap_example.lua
scripts/vamp_audio_to_midi.lua

index dd2cc468706c570f990d582b9384876fd2b14941..5f0c27b94d10186adc6f007800c76a502e243dc1 100644 (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
index 28b4091daa13878c9ebe3febf519faf3cc7a7a6c..adcdc1698228be3729c7de82ee646a76f2dd52b4 100644 (file)
@@ -43,6 +43,7 @@ function factory () return function ()
                                        end
                                end
                        end
+                       return false -- continue, !cancel
                end
 
                -- Configure Vamp plugin
index 44b8771858c4543c735392d0aab1eba2afdacef3..1c216c06f392737960328f583643588d9fa99c00 100644 (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
index 7806dda7c66da56fdedc481645a4911ae230b237..8fdbd36d56a916d79b11a2f40c8ea2394d7d8478 100644 (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)