Update Lua VAMP scripts to show a progress dialog
[ardour.git] / scripts / _vamp_note_example.lua
1 ardour { ["type"] = "Snippet", name = "Vamp Audio Transcription Example" }
2
3 function factory () return function ()
4
5         -- get Editor selection
6         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Editor
7         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
8         local sel = Editor:get_selection ()
9         local sr = Session:nominal_sample_rate ()
10
11         -- Instantiate a Vamp Plugin
12         -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
13         local vamp = ARDOUR.LuaAPI.Vamp ("libardourvampplugins:qm-transcription", sr)
14
15         -- prepare progress dialog
16         local progress_total = 0;
17         local progress_part = 0
18         local pdialog = LuaDialog.LuaProgressWindow ("Audio to MIDI", true)
19         function cb (_, pos)
20                 return pdialog:progress ((pos + progress_part) / progress_total, "Analyzing")
21         end
22
23         -- calculate max progress
24         for r in sel.regions:regionlist ():iter () do
25                 progress_total = progress_total + r:to_readable ():readable_length ()
26         end
27
28         -- for each selected region
29         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
30         for r in sel.regions:regionlist ():iter () do
31                 -- "r" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Region
32
33                 vamp:analyze (r:to_readable (), 0, cb)
34
35                 if pdialog:canceled () then
36                         goto out
37                 end
38
39                 progress_part = progress_part + r:to_readable ():readable_length ()
40                 pdialog:progress (progress_part / progress_total, "Post Processing")
41
42                 print ("-- Post Processing: ", r:name ())
43
44                 -- post-processing takes longer than actually parsing the data :(
45                 local f = vamp:plugin ():getRemainingFeatures ()
46
47                 local fl = f:table ()[0]
48                 print (" Time (sample) |  Len  | Midi-Note");
49                 if fl then for f in fl:iter () do
50                         assert (f.hasTimestamp and f.hasDuration);
51                         local ft = Vamp.RealTime.realTime2Frame (f.timestamp, sr)
52                         local fd = Vamp.RealTime.realTime2Frame (f.duration, sr)
53                         local fn = f.values:at (0) -- midi note number
54                         print (string.format (" %14d %7d %d", ft, fd, fn))
55                 end end
56
57                 -- reset the plugin (prepare for next iteration)
58                 vamp:reset ()
59         end
60
61         ::out::
62         -- hide modal progress dialog and destroy it
63         pdialog:done ();
64         pdialog = nil
65         vamp = nil;
66         collectgarbage ()
67
68 end end