Video-Frame (not sample)
[ardour.git] / scripts / _vamp_example.lua
1 ardour { ["type"] = "Snippet", name = "Vamp Plugin Example" }
2
3 function factory () return function ()
4
5         -- get a list of all available plugins
6         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
7         -- returns a http://manual.ardour.org/lua-scripting/class_reference/#C:StringVector
8         local plugins = ARDOUR.LuaAPI.Vamp.list_plugins ();
9         for id in plugins:iter () do
10                 print ("--", id)
11         end
12
13         local sel = Editor:get_selection ()
14
15         -- load the Vamp Plugin with Id "libardourvampplugins:dBTP"
16         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:LuaAPI:Vamp
17         local vamp = ARDOUR.LuaAPI.Vamp("libardourvampplugins:dBTP", Session:nominal_sample_rate())
18         print (vamp:plugin():getName())
19
20         -- for each selected region
21         for r in sel.regions:regionlist ():iter () do
22                 print ("Region:", r:name ())
23
24                 -- run the plugin, analyze the first channel of the audio-region
25                 vamp:analyze (r:to_readable (), 0, nil)
26
27                 -- get analysis results
28                 local f = vamp:plugin ():getRemainingFeatures ()
29
30                 -- f is-a Vamp::Plugin::FeatureSet  aka  std::map<int, Vamp::Plugin::FeatureList>
31                 -- http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:FeatureSet
32                 for id, featlist in f:iter () do
33                         print (id, featlist)
34                 end
35
36                 -- get the first FeatureList
37                 local featurelist = f:table()[0]
38                 -- Vamp::Plugin::FeatureList is a typedef for std::vector<Feature>
39                 for feat in featurelist:iter () do
40                         print ("-", feat.label)
41                 end
42
43                 -- get the first feature..
44                 -- http://manual.ardour.org/lua-scripting/class_reference/#Vamp:Plugin:Feature
45                 local feature = featurelist:at(0)
46                 -- ..and the values of the feature, which is-a std::vector<float>
47                 local values = feature.values
48                 -- iterate over the std::vector<float>
49                 for val in values:iter () do
50                         print ("*", val)
51                 end
52
53                 -- access the first element of Vamp::Plugin::Feature's "values" vector
54                 -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatVector
55                 local value = values:at(0)
56                 -- in case of libardourvampplugins:dBTP that's the true-peak (signal value)
57                 local dbtp = 20 * math.log (value) / math.log(10) -- convert it to dB
58                 print (string.format ("Region '%s': %.2f dBTP", r:name (), dbtp))
59
60                 -- reset the plugin for the next iteration
61                 vamp:reset ()
62         end
63 end end