1 ardour { ["type"] = "Snippet", name = "Vamp Onset Detection 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 ()
10 -- Instantiate a Vamp Plugin
11 -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
13 -- here: the "Queen Mary Note Onset Detector" Vamp plugin (which comes with Ardour)
14 -- http://vamp-plugins.org/plugin-doc/qm-vamp-plugins.html#qm-onsetdetector
15 local vamp = ARDOUR.LuaAPI.Vamp("libardourvampplugins:qm-onsetdetector", Session:nominal_sample_rate())
17 -- prepare table to hold results
20 -- for each selected region
21 -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
22 for r in sel.regions:regionlist ():iter () do
23 -- "r" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Region
25 -- prepare lua table to hold results for the given region (by name)
26 onsets[r:name ()] = {}
28 -- callback to handle Vamp-Plugin analysis results
29 function callback (feats)
30 -- "feats" is-a http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:FeatureSet
31 -- get the first output. here: Detected note onset times
32 local fl = feats:table()[0]
33 -- "fl" is-a http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:FeatureList
34 -- which may be empty or not nil
36 -- iterate over returned features
37 for f in fl:iter () do
38 -- "f" is-a http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:Feature
39 if f.hasTimestamp then
40 local fn = Vamp.RealTime.realTime2Frame (f.timestamp, 48000)
41 --print ("-", f.timestamp:toString(), fn)
42 table.insert (onsets[r:name ()], fn)
46 return false -- continue, !cancel
49 -- Configure Vamp plugin
51 -- One could query the Parameter and Program List:
52 -- http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin
53 -- but since the Plugin is known, we can directly refer to the plugin doc:
54 -- http://vamp-plugins.org/plugin-doc/qm-vamp-plugins.html#qm-onsetdetector
55 vamp:plugin ():setParameter ("dftype", 3);
56 vamp:plugin ():setParameter ("sensitivity", 50);
57 vamp:plugin ():setParameter ("whiten", 0);
58 -- in this case the above (3, 50, 0) is equivalent to
59 --vamp:plugin ():selectProgram ("General purpose");
61 -- run the plugin, analyze the first channel of the audio-region
63 -- This uses a "high-level" convenience wrapper provided by Ardour
64 -- which reads raw audio-data from the region and and calls
65 -- f = vamp:plugin ():process (); callback (f)
66 vamp:analyze (r:to_readable (), 0, callback)
68 -- get remaining features (end of analyis)
69 callback (vamp:plugin ():getRemainingFeatures ())
71 -- reset the plugin (prepare for next iteration)
76 for n,o in pairs(onsets) do
77 print ("Onset analysis for region:", n)
78 for _,t in ipairs(o) do