use ordered list of routes and fix some dialog box names
[ardour.git] / scripts / mixer_settings_store.lua
1 ardour {
2         ["type"]    = "EditorAction",
3         name        = "Store Mixer Settings",
4         author      = "Mixbus Team",
5         description = [[
6
7         Stores the current Mixer state as a file
8         that can be read and recalled arbitrarily
9         by it's companion script, Recall Mixer Settings.
10
11         Supports: processor settings, grouping,
12         mute, solo, gain, trim, pan and processor ordering,
13         plus re-adding certain deleted plugins.
14
15         ]]
16 }
17
18 function factory () return function ()
19
20         local user_cfg = ARDOUR.user_config_directory(-1)
21         local local_path = ARDOUR.LuaAPI.build_filename(Session:path(), 'mixer_settings')
22         local global_path = ARDOUR.LuaAPI.build_filename(user_cfg, 'mixer_settings')
23
24         function exists(file)
25                 local ok, err, code = os.rename(file, file)
26                 if not ok then
27                         if code == 13 then -- Permission denied, but it exists
28                                 return true
29                         end
30                 end return ok, err
31         end
32
33         function whoami()
34                 if not pcall(function() local first_check = Session:get_mixbus(0) end) then
35                         return "Ardour"
36                 else
37                         local second_check = Session:get_mixbus(11)
38                         if second_check:isnil() then
39                                 return "Mixbus"
40                         else
41                                 return "32C"
42                         end
43                 end
44         end
45
46         function isdir(path)
47                 return exists(path.."/")
48         end
49
50         function setup_paths()
51                 local global_ok, local_ok = false, false
52
53                 if not(isdir(global_path)) then
54                         global_ok, _, _ = os.execute('mkdir '.. global_path)
55                         if global_ok == 0 then
56                                 global_ok = true
57                         end
58                 else
59                         global_ok = true
60                 end
61                 if not(isdir(local_path)) then
62                         local_ok, _, _ = os.execute('mkdir '.. local_path)
63                         if local_ok == 0 then
64                                 local_ok = true
65                         end
66                 else
67                         local_ok = true
68                 end
69                 return global_ok, local_ok
70         end
71
72         function get_processor_by_name(track, name)
73                 local i = 0
74                 local proc = track:nth_processor(i)
75                         repeat
76                                 if(proc:display_name() == name) then
77                                         return proc
78                                 else
79                                         i = i + 1
80                                 end
81                                 proc = track:nth_processor(i)
82                         until proc:isnil()
83                 end
84
85         function new_plugin(name)
86                 for x = 0, 6 do
87                         local plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
88                         if not(plugin:isnil()) then return plugin end
89                 end
90         end
91
92         function group_by_id(id)
93                 local id  = tonumber(id)
94                 for g in Session:route_groups():iter() do
95                         local group_id = tonumber(g:to_stateful():id():to_s())
96                         if group_id == id then return g end
97                 end
98         end
99
100         function group_by_name(name)
101                 for g in Session:route_groups():iter() do
102                         if g:name() == name then return g end
103                 end
104         end
105
106         function route_groupid_interrogate(t)
107                 local group = false
108                 for g in Session:route_groups():iter() do
109                         for r in g:route_list():iter() do
110                                 if r:name() == t:name() then group = g:to_stateful():id():to_s() end
111                         end
112                 end return group
113         end
114
115         function route_group_interrogate(t)
116                 for g in Session:route_groups():iter() do
117                         for r in g:route_list():iter() do
118                                 if r:name() == t:name() then return g end
119                         end
120                 end
121         end
122
123         function empty_last_store(path)  --empty current file from last run
124                 local file = io.open(path, "w")
125                 file:write("")
126                 file:close()
127         end
128
129         function mark_tracks(selected, path)
130
131                 empty_last_store(path)
132
133                 local route_string = [[instance = {
134                          route_id = %d,
135                          route_name = '%s',
136                          gain_control = %f,
137                          trim_control = %f,
138                          pan_control = %s,
139                          muted = %s,
140                          soloed = %s,
141                          order = {%s},
142                          cache = {%s},
143                          group = %s,
144                          group_name = '%s',
145                          pi_order = %d
146                 }]]
147
148                 local group_string = [[instance = {
149                          group_id = %s,
150                          name = '%s',
151                          routes = {%s},
152                 }]]
153
154                 local processor_string = [[instance = {
155                          plugin_id = %d,
156                          display_name = '%s',
157                          owned_by_route_name = '%s',
158                          owned_by_route_id = %d,
159                          parameters = {%s},
160                          active = %s,
161                 }]]
162
163                 local group_route_string = " [%d] = %s,"
164                 local proc_order_string  = " [%d] = %d,"
165                 local proc_cache_string  = " [%d] = '%s',"
166                 local params_string      = " [%d] = %f,"
167
168                 --ensure easy-to-read formatting doesn't make it through
169                 local route_string     = string.gsub(route_string, "[\n\t]", "")
170                 local group_string     = string.gsub(group_string, "[\n\t]", "")
171                 local processor_string = string.gsub(processor_string, "[\n\t]", "")
172
173                 local sel = Editor:get_selection ()
174                 local groups_to_write = {}
175                 local i = 0
176
177                 local tracks = Session:get_stripables()
178
179                 if selected then tracks = sel.tracks:routelist() end
180
181                 for r in tracks:iter() do
182                         local group = route_group_interrogate(r)
183                         if group then
184                                 local already_there = false
185                                 for _, v in pairs(groups_to_write) do
186                                         if group == v then
187                                                 already_there = true
188                                         end
189                                 end
190                                 if not(already_there) then
191                                         groups_to_write[#groups_to_write + 1] = group
192                                 end
193                         end
194                 end
195
196                 for _, g in pairs(groups_to_write) do
197                         local tmp_str = ""
198                         for t in g:route_list():iter() do
199                                 tmp_str = tmp_str .. string.format(group_route_string, i, t:to_stateful():id():to_s())
200                                 i = i + 1
201                         end
202                         local group_str = string.format(
203                                 group_string,
204                                 g:to_stateful():id():to_s(),
205                                 g:name(),
206                                 tmp_str
207                         )
208
209                         file = io.open(path, "a")
210                         file:write(group_str, "\r\n")
211                         file:close()
212                 end
213
214                 for r in tracks:iter() do
215                         if r:is_monitor () or r:is_auditioner () or not(r:to_vca():isnil()) then goto nextroute end -- skip special routes
216                         r = r:to_route()
217                         if r:isnil() then goto nextroute end
218                         local order = ARDOUR.ProcessorList()
219                         local x = 0
220                         repeat
221                                 local proc = r:nth_processor(x)
222                                 if not proc:isnil() then
223                                         order:push_back(proc)
224                                 end
225                                 x = x + 1
226                         until proc:isnil()
227
228                         local route_group = route_group_interrogate(r)
229                         if route_group then route_group = route_group:name() else route_group = "" end
230                         local rid = r:to_stateful():id():to_s()
231                         local pan = r:pan_azimuth_control()
232                         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
233
234                         local order_nmbr = 0
235                         local tmp_order_str, tmp_cache_str = "", ""
236                         for p in order:iter() do
237                                 local pid = p:to_stateful():id():to_s()
238                                 if not(string.find(p:display_name(), "latcomp")) then
239                                         tmp_order_str = tmp_order_str .. string.format(proc_order_string, order_nmbr, pid)
240                                         tmp_cache_str = tmp_cache_str .. string.format(proc_cache_string, pid, p:display_name())
241                                 end
242                                 order_nmbr = order_nmbr + 1
243                         end
244
245                         local route_str = string.format(
246                                         route_string,
247                                         rid,
248                                         r:name(),
249                                         r:gain_control():get_value(),
250                                         r:trim_control():get_value(),
251                                         tostring(pan),
252                                         r:muted(),
253                                         r:soloed(),
254                                         tmp_order_str,
255                                         tmp_cache_str,
256                                         route_groupid_interrogate(r),
257                                         route_group,
258                                         r:presentation_info_ptr():order()
259                                 )
260
261                         file = io.open(path, "a")
262                         file:write(route_str, "\n")
263                         file:close()
264
265                         local i = 0
266                         while true do
267                                 local params = {}
268                                 local proc = r:nth_plugin (i)
269                                 if proc:isnil () then break end
270                                 local active = proc:active()
271                                 local id = proc:to_stateful():id():to_s()
272                                 local plug = proc:to_insert ():plugin (0)
273                                 local n = 0 -- count control-ports
274                                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
275                                         if plug:parameter_is_control (j) then
276                                                 local label = plug:parameter_label (j)
277                                                 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
278                                                         local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
279                                                         local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
280                                                         --print(r:name(), "->", proc:display_name(), label, val)
281                                                         params[n] = val
282                                                 end
283                                                 n = n + 1
284                                         end
285                                 end
286                                 i = i + 1
287
288                                 local tmp_params_str = ""
289                                 for k, v in pairs(params) do
290                                         tmp_params_str = tmp_params_str .. string.format(params_string, k, v)
291                                 end
292
293                                 local proc_str = string.format(
294                                                 processor_string,
295                                                 id,
296                                                 proc:display_name(),
297                                                 r:name(),
298                                                 r:to_stateful():id():to_s(),
299                                                 tmp_params_str,
300                                                 active
301                                         )
302                                 file = io.open(path, "a")
303                                 file:write(proc_str, "\n")
304                                 file:close()
305                         end
306                         ::nextroute::
307                 end
308         end
309
310         local store_options = {
311                 { type = "label",    col=0, colspan=1, align="right", title = "Name:" },
312                 { type = "entry",    col=1, colspan=1, align="left" , key = "filename", default = Session:name(), title=""},
313                 { type = "label",    col=0, colspan=1, align="right", title = "Location:" },
314                 {
315                         type = "radio",  col=1, colspan=3, align="left", key = "store-dir", title = "", values =
316                         {
317                                 ['Global (accessible from any session)'] = 1, ['Local (this session only)'] = 2
318                         },
319                         default = 'Locally (this session only)'
320                 },
321                 { type = "hseparator", title="", col=0, colspan = 3},
322                 { type = "label",    col=0, colspan=1, align="right", title = "Selected Tracks Only:" },
323                 { type = "checkbox", col=1, colspan=1, align="left",  key = "selected", default = false, title = ""},
324                 --{ type = "label", col=0, colspan=2, align="left", title = ''},
325                 --{ type = "label", col=0, colspan=2, align="left", title = "Global Path: " .. global_path},
326                 --{ type = "label", col=0, colspan=2, align="left", title = "Local Path: "  .. local_path},
327         }
328
329         local global_ok, local_ok = setup_paths()
330
331         if global_ok and local_ok then
332                 local rv = LuaDialog.Dialog("Store Mixer Settings:", store_options):run()
333
334                 if not(rv) then return end
335
336                 local filename = rv['filename']
337                 if rv['store-dir'] == 1 then
338                         local store_path = ARDOUR.LuaAPI.build_filename(global_path, string.format("%s-%s.lua", filename, whoami()))
339                         local selected = rv['selected']
340                         mark_tracks(selected, store_path)
341                 end
342
343                 if rv['store-dir'] == 2 then
344                         local store_path = ARDOUR.LuaAPI.build_filename(local_path, string.format("%s-%s.lua", filename, whoami()))
345                         print(store_path)
346                         local selected = rv['selected']
347                         mark_tracks(selected, store_path)
348                 end
349         end
350
351 end end