framework for silent-roll-while-slave-syncing
[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
10         -- prepare undo operation
11         Session:begin_reversible_command ("Fancy Fade Out")
12         local add_undo = false -- keep track if something has changed
13
14         -- iterate over selected tracks
15         for r in rl:iter () do
16                 local ac = r:amp ():gain_control () -- ARDOUR:AutomationControl
17                 local al = ac:alist () -- ARDOUR:AutomationList (state, high-level)
18
19                 -- set automation state to "Touch"
20                 ac:set_automation_state (ARDOUR.AutoState.Touch)
21
22                 -- query the value at the playhead position
23                 local g = al:eval (playhead)
24
25                 -- get state for undo
26                 local before = al:get_state ()
27
28                 -- delete all events after the playhead...
29                 al:truncate_end (playhead)
30
31                 -- ...and generate some new ones.
32                 for i=0,50 do
33                         -- use a sqrt fade-out (the shape is recognizable, and otherwise
34                         -- not be possible to achieve with existing ardour fade shapes)
35                         al:add (playhead + i * samplerate / 50,
36                                 g * (1 - math.sqrt (i / 50)),
37                                 false, true)
38                 end
39
40                 -- remove dense events
41                 al:thin (20)
42
43                 -- save undo
44                 local after = al:get_state ()
45                 Session:add_command (al:memento_command (before, after))
46                 add_undo = true
47
48                 ::out::
49         end
50
51         -- all done, commit the combined Undo Operation
52         if add_undo then
53                 -- the 'nil' Commend here mean to use the collected diffs added above
54                 Session:commit_reversible_command (nil)
55         else
56                 Session:abort_reversible_command ()
57         end
58 end end