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