fix drawing of zero-length notes
[ardour.git] / scripts / _vamp_onset_example.lua
1 ardour { ["type"] = "Snippet", name = "Vamp Onset Detection 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
10         -- Instantiate a Vamp Plugin
11         -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
12         --
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())
16
17         -- prepare table to hold results
18         local onsets = {}
19
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
24
25                 -- prepare lua table to hold results for the given region (by name)
26                 onsets[r:name ()] = {}
27
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
35                         if fl then
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)
43                                         end
44                                 end
45                         end
46                         return false -- continue, !cancel
47                 end
48
49                 -- Configure Vamp plugin
50                 --
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");
60
61                 -- run the plugin, analyze the first channel of the audio-region
62                 --
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)
67
68                 -- get remaining features (end of analyis)
69                 callback (vamp:plugin ():getRemainingFeatures ())
70
71                 -- reset the plugin (prepare for next iteration)
72                 vamp:reset ()
73         end
74
75         -- print results
76         for n,o in pairs(onsets) do
77                 print ("Onset analysis for region:", n)
78                 for _,t in ipairs(o) do
79                         print ("-", t)
80                 end
81         end
82
83 end end