Add tom's additions to tom's loop and turn it into an Action Script
[ardour.git] / scripts / s_fader_automation.lua
1 ardour { ["type"] = "Snippet", name = "fader automation" }
2
3 function factory () return function ()
4         local playhead = Session:transport_frame ()
5         local samplerate = Session:nominal_frame_rate ()
6
7         -- get selected tracks
8         rl = Editor:get_selection().tracks:routelist()
9         -- prepare undo operation
10         Session:begin_reversible_command ("Fancy Fade Out")
11         local add_undo = false -- keep track if something has changed
12         -- iterate over selected tracks
13         for r in rl:iter() do
14                 local ac = r:amp():gain_control() -- ARDOUR:AutomationControl
15                 local acl = ac:alist() -- ARDOUR:AutomationControlList (state, high-level)
16                 local cl = acl:list()  -- Evoral:ControlList (actual events)
17
18                 ac:set_automation_state(ARDOUR.AutoState.Touch)
19
20                 if cl:isnil() then
21                         goto out
22                 end
23
24                 -- query the value at the playhead position
25                 local g = cl:eval(playhead)
26
27                 -- get state for undo
28                 local before = acl:get_state()
29
30                 -- delete all events after the playhead...
31                 cl:truncate_end (playhead)
32                 -- ...and generate some new ones.
33                 for i=0,50 do
34                         cl:add (playhead + i * samplerate / 50,
35                                  g * (1 - math.sqrt (i / 50)),
36                                  false, true)
37                 end
38                 -- remove dense events
39                 cl:thin(20)
40
41                 -- save undo
42                 local after = acl:get_state()
43                 Session:add_command (acl:memento_command(before, after))
44                 add_undo = true
45
46                 ::out::
47         end
48
49         -- all done, commit the combined Undo Operation
50         if add_undo then
51                 -- the 'nil' Commend here mean to use the collected diffs added above
52                 Session:commit_reversible_command (nil)
53         else
54                 Session:abort_reversible_command ()
55         end
56 end end