exercise new lua bindings
[ardour.git] / scripts / tomsloop.lua
1 ardour { ["type"] = "Snippet", name = "Tom's Loop",
2         license     = "MIT",
3         author      = "Robin Gareus",
4         email       = "robin@gareus.org",
5         site        = "http://gareus.org",
6         description = [[Bounce the loop-range of all non muted audio tracks, paste N times at playhead]]
7 }
8
9 -- unused ;  cfg parameter for ["type"] = "EditorAction"
10 function action_params ()
11         return { ["times"]   = { title = "Number of copies to add", default = "1"}, }
12 end
13
14 function factory () return function ()
15         -- get options
16         local p = params or {}
17         local npaste   = p["times"] or 1
18         assert (npaste > 0)
19
20         local proc     = ARDOUR.LuaAPI.nil_proc () -- bounce w/o processing
21         local itt      = ARDOUR.InterThreadInfo () -- bounce progress info (unused)
22
23         local loop     = Session:locations ():auto_loop_location ()
24         local playhead = Session:transport_frame ()
25
26         -- make sure we have a loop, and the playhead (edit point) is after it
27         -- TODO: only print an error and return
28         -- TODO: use the edit-point (not playhead) ? maybe.
29         assert (loop)
30         assert (loop:start () < loop:_end ())
31         assert (loop:_end () < playhead)
32
33         for route in Session:get_tracks ():iter () do
34                 -- skip muted tracks
35                 if route:muted () then
36                         goto continue
37                 end
38                 -- test if bouncing is possible
39                 local track = route:to_track ()
40                 if not track:bounceable (proc, false) then
41                         goto continue;
42                 end
43                 -- only audio tracks
44                 local playlist = track:playlist ()
45                 if playlist:data_type ():to_string() ~= "audio" then
46                         goto continue
47                 end
48
49                 -- check if there are any regions in the loop-range of this track
50                 local range = Evoral.Range (loop:start (), loop:_end ())
51                 if playlist:regions_with_start_within (range):empty () then
52                         goto continue
53                 end
54                 if playlist:regions_with_end_within (range):empty () then
55                         goto continue
56                 end
57
58                 -- all set
59                 --print (track:name ())
60
61                 -- do the actual work
62                 local region = track:bounce_range (loop:start (), loop:_end (), itt, proc, false)
63                 playlist:add_region(region, playhead, npaste, false)
64
65                 ::continue::
66         end
67 end end