Optimize automation-event process splitting
[ardour.git] / scripts / _route_template_generic_audio.lua
1 ardour {
2         ["type"]    = "EditorAction",
3         name        = "Generic Audio Track",
4         description = [[Add Audio tracks, by default as many as there are physical inputs]]
5 }
6
7 -- If a route_setup function is present in an Editor Action Script
8 -- the script is also listed in the "add track/bus" dialog as meta-template
9 --
10 -- The function is expected to return a Lua table. The table may be empty.
11 function route_setup ()
12         local e = Session:engine()
13         local _, t = e:get_backend_ports ("", ARDOUR.DataType("audio"), ARDOUR.PortFlags.IsOutput | ARDOUR.PortFlags.IsPhysical, C.StringVector())
14         return
15         {
16                 -- keys control which AddRouteDialog controls are made sensitive.
17                 -- The following keys accept a default value to pre-seed the dialog.
18                 ['how_many'] = t[4]:size(),
19                 ['name'] = 'Audio',
20                 ['channels'] = 2,
21                 ['track_mode'] = ARDOUR.TrackMode.Normal,
22                 ['strict_io'] = true,
23                 -- these keys just need to be set (to something other than nil)
24                 -- in order to set the control sensitives
25                 ['insert_at'] = ARDOUR.PresentationInfo.max_order,
26                 ['group'] = false, -- return value will be a RouteGroup*
27                 ['instrument'] = nil, -- return value will be a PluginInfoPtr
28         }
29 end
30
31 -- The Script can be used as EditorAction in which case it *could*
32 -- optionally provide instantiation parmaters..
33 --[[
34 function action_params ()
35         return
36         {
37                 ['how_many'] = { title = "How Many tracks to add", default = "1" },
38                 ["name"]     = { title = "Track Name Prefix", default = "Audio" },
39         }
40 end
41 --]]
42
43
44 function factory (p)
45         -- when used from the AddRouteDialog (or w/action_params)
46         if type (p) == 'table' and p['how_many'] ~= nil then
47                 return function ()
48                         -- When called from the AddRouteDialog, 'p' will be a table with
49                         -- keys as described in route_setup() above.
50                         local name      = p["name"] or 'Audio'
51                         local how_many  = p["how_many"] or 1
52                         local channels  = p["channels"] or 1
53                         local insert_at = p["insert_at"] or ARDOUR.PresentationInfo.max_order;
54                         local group     = p["group"] or nil
55                         local mode      = p["track_mode"] or ARDOUR.TrackMode.Normal
56                         local strict_io = p["strict_io"] or false
57                         local chan_out  = 0
58
59                         if ARDOUR.config():get_output_auto_connect() == ARDOUR.AutoConnectOption.AutoConnectMaster then
60                                 if not Session:master_out():isnil() then
61                                         chan_out = Session:master_out():n_inputs ():n_audio ()
62                                 end
63                         end
64
65                         if chan_out == 0 then
66                                 chan_out = channels;
67                         end
68
69                         local tl = Session:new_audio_track (channels, chan_out, group, how_many, name, insert_at, mode)
70
71                         if strict_io then
72                                 for t in tl:iter() do
73                                         t:set_strict_io (true)
74                                 end
75                         end
76                 end
77         end
78         -- when used as standalone (no action parameters): interactive
79         return function ()
80                 local e = Session:engine()
81                 local _, t = e:get_backend_ports ("", ARDOUR.DataType("audio"), ARDOUR.PortFlags.IsOutput | ARDOUR.PortFlags.IsPhysical, C.StringVector())
82                 local tracks = t[4]:size();
83
84                 local dialog_options = {
85                         { type = "number",   key = "tracks", title = "Create Tracks",  min = 1, max = 128, step = 1, digits = 0, default = tracks },
86                         { type = "entry",    key = "name",   default = 'Audio', title = "Name Prefix" },
87                         { type = "checkbox", key = "stereo", default = false, title = "Stereo" },
88                         { type = "checkbox", key = "recarm", default = false, title = "Record Arm Tracks" },
89                 }
90
91                 local dlg = LuaDialog.Dialog ("Create Audio Tracks", dialog_options)
92                 local rv = dlg:run()
93                 if (not rv or rv['tracks'] == 0) then
94                         return
95                 end
96
97                 local chan_in = stereo and 2 or 1
98                 local chan_out  = 0
99
100                 if ARDOUR.config():get_output_auto_connect() == ARDOUR.AutoConnectOption.AutoConnectMaster then
101                         if not Session:master_out():isnil() then
102                                 chan_out = Session:master_out():n_inputs ():n_audio ()
103                         end
104                 end
105
106                 if chan_out == 0 then
107                         chan_out = chan_in;
108                 end
109
110                 -- create tracks
111                 local tl = Session:new_audio_track (chan_in, chan_out, nil, rv['tracks'], "", ARDOUR.PresentationInfo.max_order, ARDOUR.TrackMode.Normal)
112                 -- and optionally record-arm them
113                 if rv['recarm'] then
114                         for track in tl:iter() do
115                                 track:rec_enable_control ():set_value (1, PBD.GroupControlDisposition.NoGroup)
116                         end
117                 end
118         end
119 end