incorporate program name into save and recalling
[ardour.git] / scripts / mixer_settings_recall.lua
1 ardour {
2         ["type"]    = "EditorAction",
3         name        = "Recall Mixer Settings",
4         author      = "Mixbus Team",
5         description = [[
6
7         Recalls mixer settings outined by files
8         created by Store Mixer Settings.
9
10         Allows for some room to change Source
11         and Destination.
12
13         ]]
14 }
15
16 function factory () return function ()
17
18         local user_cfg = ARDOUR.user_config_directory(-1)
19         local local_path = ARDOUR.LuaAPI.build_filename(Session:path(), 'mixer_settings')
20         local global_path = ARDOUR.LuaAPI.build_filename(user_cfg, 'mixer_settings')
21
22         local invalidate = {}
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 get_processor_by_name(track, name)
51                 local i = 0
52                 local proc = track:nth_processor(i)
53                         repeat
54                                 if(proc:display_name() == name) then
55                                         return proc
56                                 else
57                                         i = i + 1
58                                 end
59                                 proc = track:nth_processor(i)
60                         until proc:isnil()
61                 end
62
63         function new_plugin(name)
64                 for x = 0, 6 do
65                         local plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
66                         if not(plugin:isnil()) then return plugin end
67                 end
68         end
69
70         function group_by_id(id)
71                 local id  = tonumber(id)
72                 for g in Session:route_groups():iter() do
73                         local group_id = tonumber(g:to_stateful():id():to_s())
74                         if group_id == id then return g end
75                 end
76         end
77
78         function group_by_name(name)
79                 for g in Session:route_groups():iter() do
80                         if g:name() == name then return g end
81                 end
82         end
83
84         function route_groupid_interrogate(t)
85                 local group = false
86                 for g in Session:route_groups():iter() do
87                         for r in g:route_list():iter() do
88                                 if r:name() == t:name() then group = g:to_stateful():id():to_s() end
89                         end
90                 end return group
91         end
92
93         function route_group_interrogate(t)
94                 for g in Session:route_groups():iter() do
95                         for r in g:route_list():iter() do
96                                 if r:name() == t:name() then return g end
97                         end
98                 end
99         end
100
101         function recall(debug, path, dry_run)
102                 local file = io.open(path, "r")
103                 assert(file, "File not found!")
104                 local bypass_routes = {}
105
106                 local i = 0
107                 for l in file:lines() do
108                         --print(i, l)
109
110                         local exec_line = dry_run["dothis-"..i]
111                         local skip_line = false
112                         if not(exec_line == nil) and not(exec_line) then
113                                 skip_line = true
114                         end
115
116                         local plugin, route, group = false, false, false
117                         local f = load(l)
118
119                         if debug then
120                                 print(i, string.sub(l, 0, 29), f)
121                         end
122
123                         if f then f() end
124
125                         if instance["route_id"]  then route = true end
126                         if instance["plugin_id"] then plugin = true end
127                         if instance["group_id"]  then group = true end
128
129                         if group then
130                                 if skip_line then goto nextline end
131
132                                 local g_id   = instance["group_id"]
133                                 local routes = instance["routes"]
134                                 local name   = instance["name"]
135                                 local group  = group_by_id(g_id)
136                                 if not(group) then
137                                         local group = Session:new_route_group(name)
138                                         for _, v in pairs(routes) do
139                                                 local rt = Session:route_by_id(PBD.ID(v))
140                                                 if rt:isnil() then rt = Session:route_by_name(name) end
141                                                 if not(rt:isnil()) then group:add(rt) end
142                                         end
143                                 end
144                         end
145
146                         if route then
147                                 local substitution = tonumber(dry_run["destination-"..i])
148                                 if skip_line or (substitution == 0) then
149                                         bypass_routes[#bypass_routes + 1] = instance["route_id"]
150                                         goto nextline
151                                 end
152
153                                 local old_order = ARDOUR.ProcessorList()
154                                 local route_id = instance["route_id"]
155                                 local r_id = PBD.ID(instance["route_id"])
156                                 local muted, soloed = instance["muted"], instance["soloed"]
157                                 local order = instance["order"]
158                                 local cache = instance["cache"]
159                                 local group = instance["group"]
160                                 local group_name = instance["group_name"]
161                                 local name  = instance["route_name"]
162                                 local gc, tc, pc = instance["gain_control"], instance["trim_control"], instance["pan_control"]
163
164                                 if not(substitution == instance["route_id"]) then
165                                         print('SUBSTITUTION FOR: ', name, substitution, Session:route_by_id(PBD.ID(substitution)):name())
166                                         --bypass_routes[#bypass_routes + 1] = route_id
167                                         was_subbed = true
168                                         r_id = PBD.ID(substitution)
169                                 end
170
171                                 local rt = Session:route_by_id(r_id)
172                                 if rt:isnil() then rt = Session:route_by_name(name) end
173                                 if rt:isnil() then goto nextline end
174
175                                 local cur_group_id = route_groupid_interrogate(rt)
176                                 if not(group) and (cur_group_id) then
177                                         local g = group_by_id(cur_group_id)
178                                         if g then g:remove(rt) end
179                                 end
180
181                                 local rt_group = group_by_name(group_name)
182                                 if rt_group then rt_group:add(rt) end
183
184                                 well_known = {'PRE', 'Trim', 'EQ', 'Comp', 'Fader', 'POST'}
185
186                                 for k, v in pairs(order) do
187                                         local proc = Session:processor_by_id(PBD.ID(1))
188                                         if not(was_subbed) then
189                                                 proc = Session:processor_by_id(PBD.ID(v))
190                                         end
191                                         if proc:isnil() then
192                                                 for id, name in pairs(cache) do
193                                                         if v == id then
194                                                                 proc = new_plugin(name)
195                                                                 for _, control in pairs(well_known) do
196                                                                         if name == control then
197                                                                                 proc = get_processor_by_name(rt, control)
198                                                                                 invalidate[v] = proc:to_stateful():id():to_s()
199                                                                                 goto nextproc
200                                                                         end
201                                                                 end
202                                                                 if not(proc) then goto nextproc end
203                                                                 if not(proc:isnil()) then
204                                                                         rt:add_processor_by_index(proc, 0, nil, true)
205                                                                         invalidate[v] = proc:to_stateful():id():to_s()
206                                                                 end
207                                                         end
208                                                 end
209                                         end
210                                         ::nextproc::
211                                         if proc and not(proc:isnil()) then old_order:push_back(proc) end
212                                 end
213                                 rt:reorder_processors(old_order, nil)
214                                 if muted  then rt:mute_control():set_value(1, 1) else rt:mute_control():set_value(0, 1) end
215                                 if soloed then rt:solo_control():set_value(1, 1) else rt:solo_control():set_value(0, 1) end
216                                 rt:gain_control():set_value(gc, 1)
217                                 rt:trim_control():set_value(tc, 1)
218                                 if pc ~= false then rt:pan_azimuth_control():set_value(pc, 1) end
219                         end
220
221                         if plugin then
222                                 if skip_line then goto nextline end
223
224                                 --if the plugin is owned by a route
225                                 --we decided not to use, skip it
226                                 for _, v in pairs(bypass_routes) do
227                                         if instance["owned_by_route_id"] == v then
228                                                 goto nextline
229                                         end
230                                 end
231
232                                 local enable = {}
233                                 local params = instance["parameters"]
234                                 local p_id   = instance["plugin_id"]
235                                 local act    = instance["active"]
236
237                                 for k, v in pairs(invalidate) do --invalidate any deleted plugin's id
238                                         if p_id == k then
239                                                 p_id = v
240                                         end
241                                 end
242
243                                 local proc = Session:processor_by_id(PBD.ID(p_id))
244                                 if proc:isnil() then goto nextline end
245                                 local plug = proc:to_insert():plugin(0)
246
247                                 for k, v in pairs(params) do
248                                         local label = plug:parameter_label(k)
249                                         if string.find(label, "Assign") or string.find(label, "Enable") then --@ToDo: Check Plugin type == LADSPA or VST?
250                                                 enable[k] = v --queue any assignments/enables for after the initial parameter recalling to duck the 'in-on-change' feature
251                                         end
252                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
253                                 end
254
255                                 for k, v in pairs(enable) do
256                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
257                                 end
258                                 if act then proc:activate() else proc:deactivate() end
259                         end
260
261                         ::nextline::
262                         i = i + 1
263
264                 end
265         end
266
267         function dry_run(debug, path)
268                 --returns a dialog-able table of
269                 --everything we do (logically)
270                 --in the recall function
271                 local route_values = {['----'] = "0"}
272                 for r in Session:get_routes():iter() do
273                         route_values[r:name()] =  r:to_stateful():id():to_s()
274                 end
275
276                 local i = 0
277                 local dry_table = {
278                         {type = "label", align="left", key="col-0-title", col=0, colspan=1, title = 'Source'},
279                         {type = "label", align="left", key="col-2-title", col=1, colspan=1, title = 'Destination:'},
280                         --{type = "label", align="left", key="col-0-title", col=1, colspan=1, title = 'Actions:'},
281                         --{type = "label", align="left", key="col-2-title", col=3, colspan=1, title = 'Do this?'},
282                 }
283                 local file = io.open(path, "r")
284                 assert(file, "File not found!")
285
286                 for l in file:lines() do
287                         local do_plugin, do_route, do_group = false, false, false
288                         local f = load(l)
289
290                         if debug then
291                                 print(i, string.sub(l, 0, 29), f)
292                         end
293
294                         if f then f() end
295
296                         if instance["route_id"]  then do_route = true end
297                         if instance["plugin_id"] then do_plugin = true end
298                         if instance["group_id"]  then do_group = true end
299
300                         if do_group then
301                                 local group_id   = instance["group_id"]
302                                 local group_name = instance["name"]
303                                 local dlg_title, action_title  = "", ""
304
305                                 local group_ptr  = group_by_id(group_id)
306
307                                 if not(group_ptr) then
308                                         new_group = Session:new_route_group(group_name)
309                                         dlg_title = string.format("(Group) %s.", group_name, new_group:name())
310                                         --action_title = "will create and use settings"
311                                 else
312                                         dlg_title = string.format("(Group) %s.", group_ptr:name())
313                                         --action_title = "will use group settings"
314                                 end
315                                 table.insert(dry_table, {
316                                         type = "label", align = "left", key =  "group-"..i , col = 0, colspan = 1, title = dlg_title
317                                 })
318                         end
319
320                         if do_route then
321                                 local route_id   = instance["route_id"]
322                                 local route_name = instance["route_name"]
323                                 local dlg_title = ""
324
325                                 local route_ptr = Session:route_by_id(PBD.ID(route_id))
326
327                                 if route_ptr:isnil() then
328                                         route_ptr = Session:route_by_name(route_name)
329                                         if not(route_ptr:isnil()) then
330                                                 dlg_title = string.format("(Strip) %s", route_ptr:name())
331                                                 --action_title = "will use route settings"
332                                         else
333                                                 dlg_title = string.format("Strip) %s", route_name)
334                                                 --action_title = "will be ignored"
335                                         end
336                                 else
337                                         dlg_title = string.format("(Strip) %s", route_ptr:name())
338                                         --action_title = "will use route settings"
339                                 end
340                                 if route_ptr:isnil() then name = route_name else name = route_ptr:name() end
341                                 table.insert(dry_table, {
342                                         type = "label", align = "left", key = "route-"..i , col = 0, colspan = 1, title = dlg_title
343                                 })
344                                 table.insert(dry_table, {
345                                         type = "dropdown", align = "left", key = "destination-"..i, col = 1, colspan = 1, title = "", values = route_values, default = name or "----"
346                                 })
347                         end
348                         i = i + 1
349                 end
350                 return dry_table
351         end
352
353         local global_vs_local_dlg = {
354                 { type = "label", col=0, colspan=20, align="left", title = "" },
355                 {
356                         type = "radio", col=0, colspan=20, align="left", key = "recall-dir", title = "", values =
357                         {
358                                 ['Pick from Global Settings'] = 1, ['Pick from Local Settings'] = 2
359                         },
360                         default = 'Pick from Local Settings'
361                 },
362                 { type = "label", col=0, colspan=20, align="left", title = ""},
363         }
364
365         local recall_options = {
366                 { type = "label", col=0, colspan=10, align="left", title = "" },
367                 { type = "file",  col=0, colspan=15, align="left", key = "file", title = "Select a Settings File:",  path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua") },
368                 { type = "label", col=0, colspan=10, align="left", title = "" },
369         }
370
371         local gvld = LuaDialog.Dialog("Recall Mixer Settings:", global_vs_local_dlg):run()
372
373         if not(gvld) then
374                 return
375         else
376                 if gvld['recall-dir'] == 1 then
377                         local global_ok = isdir(global_path)
378                         local global_default_path = ARDOUR.LuaAPI.build_filename(global_path, string.format("FactoryDefault-%s.lua", whoami()))
379                         print(global_default_path)
380                         if global_ok then
381                                 recall_options[2]['path'] = global_default_path
382                                 local rv = LuaDialog.Dialog("Recall Mixer Settings:", recall_options):run()
383                                 if not(rv) then return end
384                                 local dry_return = LuaDialog.Dialog("Mixer Store:", dry_run(false, rv['file'])):run()
385                                 if dry_return then
386                                         recall(false, rv['file'], dry_return)
387                                 else
388                                         return
389                                 end
390                         else
391                                 LuaDialog.Message ("Recall Mixer Settings:",
392                                         global_path .. [[does not exist!
393                                         Please run Store Mixer Settings first.]],
394                                         LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run()
395                         end
396                 end
397
398                 if gvld['recall-dir'] == 2 then
399                         local local_ok = isdir(local_path)
400                         local local_default_path = ARDOUR.LuaAPI.build_filename(local_path, 'stub')
401                         print(local_default_path)
402                         if local_ok then
403                                 recall_options[2]['path'] = local_path
404                                 local rv = LuaDialog.Dialog("Recall Mixer Settings:", recall_options):run()
405                                 if not(rv) then return end
406                                 local dry_return = LuaDialog.Dialog("Mixer Store:", dry_run(false, rv['file'])):run()
407                                 if dry_return then
408                                         recall(false, rv['file'], dry_return)
409                                 else
410                                         return
411                                 end
412                         else
413                                 LuaDialog.Message ("Recall Mixer Settings:",
414                                         local_path .. [[does not exist!
415                                         Please run Store Mixer Settings first.]],
416                                         LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run()
417                         end
418                 end
419         end
420
421 end end