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