AU: mark preset dirty when parameter changes
[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                 end
47
48                 -- Configure Vamp plugin
49                 --
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");
59
60                 -- run the plugin, analyze the first channel of the audio-region
61                 --
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)
66
67                 -- get remaining features (end of analyis)
68                 callback (vamp:plugin ():getRemainingFeatures ())
69
70                 -- reset the plugin (prepare for next iteration)
71                 vamp:reset ()
72         end
73
74         -- print results
75         for n,o in pairs(onsets) do
76                 print ("Onset analysis for region:", n)
77                 for _,t in ipairs(o) do
78                         print ("-", t)
79                 end
80         end
81
82 end end