when using toggle-editor-and-mixer, if current tab is neither, go to mixer first.
[ardour.git] / scripts / bounce_replace.lua
1 ardour { ["type"] = "EditorAction", name = "Bounce+Replace Regions",
2         license     = "MIT",
3         author      = "Robin Gareus",
4         email       = "robin@gareus.org",
5         site        = "http://gareus.org",
6         description = [[Bounce selected regions with processing and replace region]]
7 }
8
9 function factory (params) return function ()
10         -- there is currently no direct way to find the track
11         -- corresponding to a [selected] region
12         function find_track_for_region (region_id)
13                 for route in Session:get_tracks():iter() do
14                         local track = route:to_track();
15                         local pl = track:playlist ()
16                         if not pl:region_by_id (region_id):isnil () then
17                                 return track
18                         end
19                 end
20                 assert (0); -- can't happen, region must be in a playlist
21         end
22
23         -- get selection
24         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
25         local sel = Editor:get_selection ()
26
27         -- prepare undo operation
28         Session:begin_reversible_command ("Bounce+Replace Regions")
29         local add_undo = false -- keep track if something has changed
30
31         -- Iterate over Regions part of the selection
32         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
33         for r in sel.regions:regionlist ():iter () do
34                 -- each of the items 'r' is a
35                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Region
36
37                 local track = find_track_for_region (r:to_stateful():id())
38                 local playlist = track:playlist ()
39
40                 -- clear existing changes, prepare "diff" of state for undo
41                 playlist:to_stateful ():clear_changes ()
42
43                 -- bounce the region with processing
44                 local region = track:bounce_range (r:position (), r:position() + r:length (), ARDOUR.InterThreadInfo (), track:main_outs (), false);
45
46                 -- remove old region..
47                 playlist:remove_region (r);
48                 -- ..and add the newly bounced one
49                 playlist:add_region (region, r:position (), 1, false)
50
51                 -- create a diff of the performed work, add it to the session's undo stack
52                 -- and check if it is not empty
53                 if not Session:add_stateful_diff_command (playlist:to_statefuldestructible ()):empty () then
54                         add_undo = true
55                 end
56         end
57
58         -- all done, commit the combined Undo Operation
59         if add_undo then
60                 -- the 'nil' Command here mean to use the collected diffs added above
61                 Session:commit_reversible_command (nil)
62         else
63                 Session:abort_reversible_command ()
64         end
65
66 end end