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_frame_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)
48 -- Configure Vamp plugin
50 -- One could query the Parameter and Program List:
51 -- http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin
52 -- but since the Plugin is known, we can directly refer to the plugin doc:
53 -- http://vamp-plugins.org/plugin-doc/qm-vamp-plugins.html#qm-onsetdetector
54 vamp:plugin ():setParameter ("dftype", 3);
55 vamp:plugin ():setParameter ("sensitivity", 50);
56 vamp:plugin ():setParameter ("whiten", 0);
57 -- in this case the above (3, 50, 0) is equivalent to
58 --vamp:plugin ():selectProgram ("General purpose");
60 -- run the plugin, analyze the first channel of the audio-region
62 -- This uses a "high-level" convenience wrapper provided by Ardour
63 -- which reads raw audio-data from the region and and calls
64 -- f = vamp:plugin ():process (); callback (f)
65 vamp:analyze (r:to_readable (), 0, callback)
67 -- get remaining features (end of analyis)
68 callback (vamp:plugin ():getRemainingFeatures ())
70 -- reset the plugin (prepare for next iteration)
75 for n,o in pairs(onsets) do
76 print ("Onset analysis for region:", n)
77 for _,t in ipairs(o) do