Add Pianokeyboard + Velocity Control to PC Dialog
[ardour.git] / scripts / _stereo_to_mono.lua
1 ardour { ["type"] = "EditorAction", name = "Stereo to Mono",
2         license     = "MIT",
3         author      = "Ardour Team",
4         description = [[Convert a Stereo Track into two Mono Tracks]]
5 }
6
7
8 function factory (params) return function ()
9         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
10         -- the Ardour Selection can include multiple items
11         -- (regions, tracks, ranges, markers, automation, midi-notes etc)
12         local sel = Editor:get_selection ()
13
14         -- for each track..
15         for t in sel.tracks:routelist ():iter () do
16                 local track = t:to_track ()
17                 if track:isnil() then goto next end
18
19                 -- only audio tracks
20                 local playlist = track:playlist ()
21                 if playlist:data_type ():to_string () ~= "audio" then goto next end
22
23                 -- skip tracks without any regions
24                 if playlist:region_list ():size() == 0 then goto next end
25
26                 -- we can't access diskstream n_channels()
27                 local channels = track:n_inputs(): n_audio()
28
29                 -- stereo only
30                 if channels ~= 2 then goto next end
31
32                 -- create 2 new tracks (using the name of the original track)(
33                 local newtracks = Session:new_audio_track (2, 2, nil, 2, t:name(),  ARDOUR.PresentationInfo.max_order, ARDOUR.TrackMode.Normal)
34                 assert (newtracks:size() == 2)
35
36                 for r in playlist:region_list ():iter () do
37                         local region = r:to_audioregion ()
38                         local rl = ARDOUR.RegionVector ()
39                         local _, rv = region:separate_by_channel (rl)
40                         assert (rv[1]:size () == 2)
41                         -- 1:1 mapping of regions to new tacks
42                         local plc = 1
43                         for nr in rv[1]:iter () do
44                                 local pl = newtracks:table()[plc]:playlist()
45                                 pl:add_region (nr, r:position(), 1, false, 0, 0, false)
46                                 plc = plc + 1
47                         end
48                 end
49
50                 -- TODO remove the old track
51
52                 -- drop references for good.
53                 collectgarbage ()
54                 ::next::
55         end
56
57 end end