Skip silent sources on session-archive -- fixes #7699
[ardour.git] / scripts / faders_to_trims.lua
1 ardour {
2         ["type"]    = "EditorAction",
3         name        = "Faders to Trims",
4         license     = "MIT",
5         author      = "PSmith",
6         description = [[Add 'Trim' plugins to all tracks.  Move the fader value into the trim.]]
7 }
8
9 function action_params ()
10         return
11         {
12         }
13 end
14
15
16 function factory (params)
17         return function ()
18                 -- loop over all tracks
19                 for t in Session:get_tracks():iter() do
20                         
21                         fader_value = t:gain_control():get_value()
22                         if fader_value == 1 then
23                                 goto skip
24                         end
25                         if t:gain_control():automation_state() ~= ARDOUR.AutoState.Off then
26                                 goto skip
27                         end
28
29                         -- TODO: skip MIDI tracks without or with a post-fader synth
30                         -- (fader is MIDI-velocity)
31
32                         v = math.log(fader_value, 10)
33                         trim_gain = 20*v
34                         fader_pos = 0
35                         local proc;
36                         local i = 0;
37                         
38                         repeat
39                                 -- find the fader proc
40                                 proc = t:nth_processor (i)
41                                 if (not proc:isnil() and proc:display_name () == "Fader") then
42                                         fader_pos = i
43                                 end
44                                 i = i + 1
45                         until proc:isnil()
46                         
47                         -- apply the trim
48                         trim = t:nth_processor (fader_pos+1)
49                         if (not trim:isnil() and trim:display_name () == "a-Amplifier") then
50                                 --existing trim found; sum the trim and the fader gain, and set the trim to that value
51                                 cur_gain = ARDOUR.LuaAPI.get_processor_param (trim, 0)
52                                 ARDOUR.LuaAPI.set_processor_param (trim, 0, trim_gain+cur_gain)
53                         else
54                                 --create a new Trim processor, and set its value to match the fader
55                                 local a = ARDOUR.LuaAPI.new_luaproc(Session, "a-Amplifier");
56                                 if (not a:isnil()) then
57                                         t:add_processor_by_index(a, fader_pos-1, nil, true)
58                                         ARDOUR.LuaAPI.set_processor_param (a, 0, trim_gain)
59                                         a = nil -- explicitly drop shared-ptr reference
60                                 end
61                         end
62
63                         --zero the fader gain
64                         t:gain_control():set_value(1, PBD.GroupControlDisposition.NoGroup)
65
66                         ::skip::
67
68                 end --foreach track
69
70         end  --function
71
72 end --factory