1 ardour { ["type"] = "Snippet", name = "Vamp Audio Transcription Example" }
3 function factory () return function ()
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 ()
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)
15 -- prepare progress dialog
16 local progress_total = 0;
17 local progress_part = 0
18 local pdialog = LuaDialog.LuaProgressWindow ("Audio to MIDI", true)
20 return pdialog:progress ((pos + progress_part) / progress_total, "Analyzing")
23 -- calculate max progress
24 for r in sel.regions:regionlist ():iter () do
25 progress_total = progress_total + r:to_readable ():readable_length ()
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
33 vamp:analyze (r:to_readable (), 0, cb)
35 if pdialog:canceled () then
39 progress_part = progress_part + r:to_readable ():readable_length ()
40 pdialog:progress (progress_part / progress_total, "Post Processing")
42 print ("-- Post Processing: ", r:name ())
44 -- post-processing takes longer than actually parsing the data :(
45 local f = vamp:plugin ():getRemainingFeatures ()
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))
57 -- reset the plugin (prepare for next iteration)
62 -- hide modal progress dialog and destroy it