Fix ExportFormatSpecification copy-c'tor
[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, 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:rectangle (wh - wh * .6, wh - .7 * wh, wh * 1.2, .5 * wh)
79         stroke_outline (.7)
80
81         ctx:rectangle (wh - wh * .6, wh + .1 * wh, wh * 1.2, .5 * wh)
82         stroke_outline (.9)
83
84         -- arrow
85         ctx:set_source_rgba (0, 1, 0, 1)
86         ctx:set_line_width (ar * .7)
87
88         ctx:move_to (wh, wh - .5 * wh)
89         ctx:rel_line_to (0, wh)
90         ctx:stroke ()
91
92         ctx:move_to (wh, wh + .5 * wh)
93         ctx:rel_line_to (-ar, -ar)
94         ctx:rel_line_to (2 * ar, 0)
95         ctx:close_path ()
96         ctx:stroke ()
97
98
99
100 end end