Tweaks to Mixer and Monitor keybindings:
[ardour.git] / scripts / _vamp_tempomap_example.lua
1 ardour { ["type"] = "Snippet", name = "Vamp TempoMap Test" }
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 the QM BarBeat Tracker
11         -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
12         -- http://vamp-plugins.org/plugin-doc/qm-vamp-plugins.html#qm-barbeattracker
13         local vamp = ARDOUR.LuaAPI.Vamp("libardourvampplugins:qm-barbeattracker", Session:nominal_sample_rate())
14
15         -- prepare table to hold results
16         local beats = {}
17         local bars = {}
18
19         -- for each selected region
20         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
21         for r in sel.regions:regionlist ():iter () do
22                 -- "r" is-a http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Region
23
24                 -- prepare lua table to hold results for the given region (by name)
25                 beats[r:name ()] = {}
26                 bars[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
32                         -- get the first output. here: Beats, estimated beat locations & beat-number
33                         local fl = feats:table()[0]
34                         -- "fl" is-a http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:FeatureList
35                         -- which may be empty or not nil
36                         if fl then
37                                 -- iterate over returned features
38                                 for f in fl:iter () do
39                                         -- "f" is-a  http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:Feature
40                                         if f.hasTimestamp then
41                                                 local fn = Vamp.RealTime.realTime2Frame (f.timestamp, 48000)
42                                                 table.insert (beats[r:name ()], {pos = fn, beat = f.label})
43                                         end
44                                 end
45                         end
46
47                         -- get the 2nd output. here: estimated bar locations
48                         local fl = feats:table()[1]
49                         if fl then
50                                 for f in fl:iter () do
51                                         if f.hasTimestamp then
52                                                 local fn = Vamp.RealTime.realTime2Frame (f.timestamp, 48000)
53                                                 table.insert (bars[r:name ()],  fn)
54                                         end
55                                 end
56                         end
57                 end
58
59                 vamp:plugin ():setParameter ("Beats Per Bar", 4); -- TODO ask
60
61                 -- run the plugin, analyze the first channel of the audio-region
62                 vamp:analyze (r:to_readable (), 0, callback)
63                 -- get remaining features (end of analyis)
64                 callback (vamp:plugin ():getRemainingFeatures ())
65                 -- reset the plugin (prepare for next iteration)
66                 vamp:reset ()
67         end
68
69         -- print results (for now)
70         -- TODO: calculate and set tempo-map
71         for n,o in pairs(bars) do
72                 print ("Bar analysis for region:", n)
73                 for _,t in ipairs(o) do
74                         print ("-", t)
75                 end
76         end
77         for n,o in pairs(beats) do
78                 print ("Beat analysis for region:", n)
79                 for _,t in ipairs(o) do
80                         print ("-", t['pos'], t['beat'])
81                 end
82         end
83
84 end end