add vamp-plugin example lua script
[ardour.git] / scripts / _vamp_example.lua
1 ardour { ["type"] = "Snippet", name = "Vamp Plugin Example" }
2
3 function factory () return function ()
4         local sel = Editor:get_selection ()
5
6         local vamp = ARDOUR.LuaAPI.Vamp("libardourvampplugins:dBTP", Session:nominal_frame_rate())
7         print (vamp:plugin():getName())
8
9         -- for each selected region
10         for r in sel.regions:regionlist ():iter () do
11                 print ("Region:", r:name ())
12
13                 -- run the plugin, analyze the first channel of the audio-region
14                 vamp:analyze (r:to_readable (), 0, nil)
15
16                 -- get analysis results
17                 local f = vamp:plugin ():getRemainingFeatures ()
18
19                 -- f is-a Vamp::Plugin::FeatureSet  aka  std::map<int, Vamp::Plugin::FeatureList>
20                 for id, featlist in f:iter () do
21                         print (id, featlist)
22                 end
23
24                 -- get the first FeatureList
25                 local featurelist = f:table()[0]
26                 -- Vamp::Plugin::FeatureList is a typedef for std::vector<Feature>
27                 for feat in featurelist:iter () do
28                         print ("-", feat.label)
29                 end
30
31                 -- get the first feature..
32                 local feature = featurelist:at(0)
33                 -- ..and the values of the feature, which is-a std::vector<float>
34                 local values = feature.values
35                 -- iterate over the std::vector<float>
36                 for val in values:iter () do
37                         print ("*", val)
38                 end
39
40                 -- access the first element of Vamp::Plugin::Feature's "values" vector
41                 local value = values:at(0)
42                 -- in case of libardourvampplugins:dBTP that's the true-peak
43                 local dbtp = 20 * math.log (value) / math.log(10)
44                 print (string.format ("Region '%s': %.2f dBTP", r:name (), dbtp))
45
46                 -- reset the plugin
47                 vamp:reset ()
48         end
49 end end