fix mistaken "do not roll" conclusion in TransportFSM::compute_should_roll()
[ardour.git] / scripts / bounce_replace.lua
1 ardour { ["type"] = "EditorAction", name = "Bounce+Replace Regions",
2         license     = "MIT",
3         author      = "Ardour Team",
4         description = [[Bounce selected regions with processing and replace region]]
5 }
6
7 function factory (params) return function ()
8         -- there is currently no direct way to find the track
9         -- corresponding to a [selected] region
10         function find_track_for_region (region_id)
11                 for route in Session:get_tracks():iter() do
12                         local track = route:to_track();
13                         local pl = track:playlist ()
14                         if not pl:region_by_id (region_id):isnil () then
15                                 return track
16                         end
17                 end
18                 assert (0); -- can't happen, region must be in a playlist
19         end
20
21         -- get selection
22         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
23         local sel = Editor:get_selection ()
24
25         -- prepare undo operation
26         Session:begin_reversible_command ("Bounce+Replace Regions")
27         local add_undo = false -- keep track if something has changed
28
29         -- Iterate over Regions part of the selection
30         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
31         for r in sel.regions:regionlist ():iter () do
32                 -- each of the items 'r' is a
33                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Region
34
35                 local track = find_track_for_region (r:to_stateful():id())
36                 local playlist = track:playlist ()
37
38                 -- clear existing changes, prepare "diff" of state for undo
39                 playlist:to_stateful ():clear_changes ()
40
41                 -- bounce the region with processing
42                 local region = track:bounce_range (r:position (), r:position() + r:length (), ARDOUR.InterThreadInfo (), track:main_outs (), false);
43
44                 -- remove old region..
45                 playlist:remove_region (r);
46                 -- ..and add the newly bounced one
47                 playlist:add_region (region, r:position (), 1, false, 0, 0, false)
48
49                 -- create a diff of the performed work, add it to the session's undo stack
50                 -- and check if it is not empty
51                 if not Session:add_stateful_diff_command (playlist:to_statefuldestructible ()):empty () then
52                         add_undo = true
53                 end
54         end
55
56         -- all done, commit the combined Undo Operation
57         if add_undo then
58                 -- the 'nil' Command here mean to use the collected diffs added above
59                 Session:commit_reversible_command (nil)
60         else
61                 Session:abort_reversible_command ()
62         end
63
64 end end
65
66 function icon (params) return function (ctx, width, height, fg)
67         local wh = math.min (width, height) * .5
68         local ar = wh * .2
69
70         ctx:set_line_width (1)
71         function stroke_outline (c)
72                 ctx:set_source_rgba (0, 0, 0, 1)
73                 ctx:stroke_preserve ()
74                 ctx:set_source_rgba (c, c, c, 1)
75                 ctx:fill ()
76         end
77
78         ctx:translate (math.floor (width * .5 - wh), math.floor (height * .5 - wh))
79         ctx:rectangle (wh - wh * .6, wh - .7 * wh, wh * 1.2, .5 * wh)
80         stroke_outline (.7)
81
82         ctx:rectangle (wh - wh * .6, wh + .1 * wh, wh * 1.2, .5 * wh)
83         stroke_outline (.9)
84
85         -- arrow
86         ctx:set_source_rgba (0, 1, 0, 1)
87         ctx:set_line_width (ar * .7)
88
89         ctx:move_to (wh, wh - .5 * wh)
90         ctx:rel_line_to (0, wh)
91         ctx:stroke ()
92
93         ctx:move_to (wh, wh + .5 * wh)
94         ctx:rel_line_to (-ar, -ar)
95         ctx:rel_line_to (2 * ar, 0)
96         ctx:close_path ()
97         ctx:stroke ()
98
99
100
101 end end