2 ["type"] = "EditorAction",
4 author = "Ardour Lua Taskforce",
5 description = [[Stores the current Mixer state as a file that can be read and recalled arbitrarily.
6 Supports: processor settings, grouping, mute, solo, gain, trim, pan and processor ordering,
7 plus re-adding certain deleted plugins.]]
10 function factory() return function()
13 local path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua")
15 function mismatch_dialog(mismatch_str, checkbox_str)
16 --string.format("Track didn't match ID: %d, but did match track in session: %s", 999, 'track')
18 { type = "label", colspan = 5, title = mismatch_str },
19 { type = "checkbox", col=1, colspan = 1, key = "use", default = true, title = checkbox_str },
21 local mismatch_return = LuaDialog.Dialog("", dialog):run()
22 if mismatch_return then
23 return mismatch_return['use']
29 function get_processor_by_name(track, name)
31 local proc = track:nth_processor(i)
33 if ( proc:display_name() == name ) then
38 proc = track:nth_processor(i)
42 function new_plugin(name)
44 plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
45 if not(plugin:isnil()) then return plugin end
49 function group_by_id(id)
50 local id = tonumber(id)
51 for g in Session:route_groups():iter() do
52 local group_id = tonumber(g:to_stateful():id():to_s())
53 if group_id == id then return g end
57 function route_groupid_interrogate(t)
59 for g in Session:route_groups():iter() do
60 for r in g:route_list():iter() do
61 if r:name() == t:name() then group = g:to_stateful():id():to_s() end
66 function route_group_interrogate(t)
67 for g in Session:route_groups():iter() do
68 for r in g:route_list():iter() do
69 if r:name() == t:name() then return g end
74 function empty_last_store() --empty current file from last run
75 local file = io.open(path, "w")
80 function mark_tracks(selected)
84 local route_string = [[instance = {
97 local group_string = [[instance = {
103 local processor_string = [[instance = {
106 owned_by_route_name = '%s',
107 owned_by_route_id = %d,
112 local group_route_string = " [%d] = %s,"
113 local proc_order_string = " [%d] = %d,"
114 local proc_cache_string = " [%d] = '%s',"
115 local params_string = " [%d] = %f,"
117 local route_string = string.gsub(route_string, "[\n\t]", "")
118 local group_string = string.gsub(group_string, "[\n\t]", "")
119 local processor_string = string.gsub(processor_string, "[\n\t]", "")
121 local sel = Editor:get_selection ()
122 local groups_to_write = {}
125 local tracks = Session:get_routes()
127 if selected then tracks = sel.tracks:routelist() end
129 for r in tracks:iter() do
130 local group = route_group_interrogate(r)
132 local already_there = false
133 for _, v in pairs(groups_to_write) do
138 if not(already_there) then
139 groups_to_write[#groups_to_write + 1] = group
144 for _, g in pairs(groups_to_write) do
146 for t in g:route_list():iter() do
147 tmp_str = tmp_str .. string.format(group_route_string, i, t:to_stateful():id():to_s())
150 local group_str = string.format(
152 g:to_stateful():id():to_s(),
157 file = io.open(path, "a")
158 file:write(group_str, "\r\n")
162 for r in tracks:iter() do
163 if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
165 local order = ARDOUR.ProcessorList()
168 local proc = r:nth_processor(x)
169 if not proc:isnil() then
170 order:push_back(proc)
175 local rid = r:to_stateful():id():to_s()
176 local pan = r:pan_azimuth_control()
177 if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
180 local tmp_order_str, tmp_cache_str = "", ""
181 for p in order:iter() do
182 local pid = p:to_stateful():id():to_s()
183 if not(string.find(p:display_name(), "latcomp")) then
184 tmp_order_str = tmp_order_str .. string.format(proc_order_string, order_nmbr, pid)
185 tmp_cache_str = tmp_cache_str .. string.format(proc_cache_string, pid, p:display_name())
187 order_nmbr = order_nmbr + 1
190 local route_str = string.format(
194 r:gain_control():get_value(),
195 r:trim_control():get_value(),
201 route_groupid_interrogate(r)
204 file = io.open(path, "a")
205 file:write(route_str, "\n")
211 local proc = r:nth_plugin (i)
212 if proc:isnil () then break end
213 local active = proc:active()
214 local id = proc:to_stateful():id():to_s()
215 local plug = proc:to_insert ():plugin (0)
216 local n = 0 -- count control-ports
217 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
218 if plug:parameter_is_control (j) then
219 local label = plug:parameter_label (j)
220 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
221 local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
222 local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
223 --print(r:name(), "->", proc:display_name(), label, val)
231 local tmp_params_str = ""
232 for k, v in pairs(params) do
233 tmp_params_str = tmp_params_str .. string.format(params_string, k, v)
236 local proc_str = string.format(
241 r:to_stateful():id():to_s(),
245 file = io.open(path, "a")
246 file:write(proc_str, "\n")
253 function recall(debug, dry_run)
254 local file = io.open(path, "r")
255 assert(file, "File not found!")
258 for l in file:lines() do
261 local exec_line = dry_run["dothis-"..i]
262 local skip_line = false
263 if not(exec_line == nil) and not(exec_line) then
267 local plugin, route, group = false, false, false
271 print(i, string.sub(l, 0, 29), f)
276 if instance["route_id"] then route = true end
277 if instance["plugin_id"] then plugin = true end
278 if instance["group_id"] then group = true end
281 if skip_line then goto nextline end
283 local g_id = instance["group_id"]
284 local routes = instance["routes"]
285 local name = instance["name"]
286 local group = group_by_id(g_id)
288 --local mis_str = string.format("Couldn't find group by ID: %s", g_id)
289 --local chk_str = string.format("Create group and use?")
290 --local continue = mismatch_dialog(mis_str, chk_str)
293 local group = Session:new_route_group(name)
294 for _, v in pairs(routes) do
295 local rt = Session:route_by_id(PBD.ID(v))
296 if rt:isnil() then rt = Session:route_by_name(name) end
297 if not(rt:isnil()) then group:add(rt) end
304 if skip_line then goto nextline end
306 local old_order = ARDOUR.ProcessorList()
307 local r_id = PBD.ID(instance["route_id"])
308 local muted, soloed = instance["muted"], instance["soloed"]
309 local order = instance["order"]
310 local cache = instance["cache"]
311 local group = instance["group"]
312 local name = instance["route_name"]
313 local gc, tc, pc = instance["gain_control"], instance["trim_control"], instance["pan_control"]
315 local rt = Session:route_by_id(r_id)
316 if rt:isnil() then rt = Session:route_by_name(name) end
317 if rt:isnil() then goto nextline end
319 local cur_group_id = route_groupid_interrogate(rt)
320 if not(group) and (cur_group_id) then
321 local g = group_by_id(cur_group_id)
322 if g then g:remove(rt) end
325 well_known = {'PRE', 'Trim', 'EQ', 'Comp', 'Fader', 'POST'}
327 for k, v in pairs(order) do
328 local proc = Session:processor_by_id(PBD.ID(v))
330 for id, name in pairs(cache) do
332 proc = new_plugin(name)
333 for _, control in pairs(well_known) do
334 if name == control then
335 proc = get_processor_by_name(rt, control)
336 invalidate[v] = proc:to_stateful():id():to_s()
340 if not(proc) then goto nextproc end
341 if not(proc:isnil()) then
342 rt:add_processor_by_index(proc, 0, nil, true)
343 invalidate[v] = proc:to_stateful():id():to_s()
349 if proc and not(proc:isnil()) then old_order:push_back(proc) end
352 if muted then rt:mute_control():set_value(1, 1) else rt:mute_control():set_value(0, 1) end
353 if soloed then rt:solo_control():set_value(1, 1) else rt:solo_control():set_value(0, 1) end
354 rt:gain_control():set_value(gc, 1)
355 rt:trim_control():set_value(tc, 1)
356 if pc ~= false then rt:pan_azimuth_control():set_value(pc, 1) end
357 rt:reorder_processors(old_order, nil)
361 if skip_line then goto nextline end
364 local params = instance["parameters"]
365 local p_id = instance["plugin_id"]
366 local act = instance["active"]
368 for k, v in pairs(invalidate) do --invalidate any deleted plugin's id
374 local proc = Session:processor_by_id(PBD.ID(p_id))
375 if proc:isnil() then goto nextline end
376 local plug = proc:to_insert():plugin(0)
378 for k, v in pairs(params) do
379 local label = plug:parameter_label(k)
380 if string.find(label, "Assign") or string.find(label, "Enable") then --@ToDo: Check Plugin type == LADSPA or VST?
381 enable[k] = v --queue any assignments/enables for after the initial parameter recalling to duck the 'in-on-change' feature
383 ARDOUR.LuaAPI.set_processor_param(proc, k, v)
386 for k, v in pairs(enable) do
387 ARDOUR.LuaAPI.set_processor_param(proc, k, v)
389 if act then proc:activate() else proc:deactivate() end
398 function dry_run(debug)
399 --returns a dialog-able table of
400 --everything we do (logically)
401 --in the recall function
404 local dry_table = {{type = "label", key = "col-1-title" , col = 1, colspan = 1, title = 'Do this?'}}
405 local file = io.open(path, "r")
406 assert(file, "File not found!")
408 for l in file:lines() do
409 local do_plugin, do_route, do_group = false, false, false
413 print(i, string.sub(l, 0, 29), f)
418 if instance["route_id"] then do_route = true end
419 if instance["plugin_id"] then do_plugin = true end
420 if instance["group_id"] then do_group = true end
423 local group_id = instance["group_id"]
424 local group_name = instance["name"]
427 local group_ptr = group_by_id(group_id)
429 if not(group_ptr) then
430 new_group = Session:new_route_group(group_name)
431 dlg_title = string.format("Group: %s-> (will be created) %s", group_name, new_group:name())
433 dlg_title = string.format("Group ID Match: %s.", group_ptr:name())
435 table.insert(dry_table, {
436 type = "label", key = "group-"..i , col = 0, colspan = 1, title = dlg_title
438 table.insert(dry_table, {
439 type = "checkbox", col=1, colspan = 1, key = "dothis-"..i, default = true, title = "line:"..i
444 local route_id = instance["route_id"]
445 local route_name = instance["route_name"]
448 local route_ptr = Session:route_by_id(PBD.ID(route_id))
450 if route_ptr:isnil() then
451 route_ptr = Session:route_by_name(route_name)
452 if not(route_ptr:isnil()) then
453 dlg_title = string.format("Route: %s-> (found by name) %s", route_name, route_ptr:name())
455 dlg_title = string.format("Route: %s-> (cannot find matching ID or name) ????", route_name)
458 dlg_title = string.format("Route Found by ID: %s", route_ptr:name())
460 table.insert(dry_table, {
461 type = "label", key = "route-"..i , col = 0, colspan = 1, title = dlg_title
463 table.insert(dry_table, {
464 type = "checkbox", col=1, colspan = 1, key = "dothis-"..i, default = true, title = "line:"..i
472 local dialog_options = {
473 { type = "label", colspan = 5, title = "" },
474 { type = "radio", col = 1, colspan = 7, key = "select", title = "", values ={ ["Store"] = "store", ["Recall"] = "recall" }, default = "Store"},
475 { type = "label", colspan = 5, title = "" },
478 local store_options = {
479 { type = "label", colspan = 5, title = "" },
480 { type = "checkbox", col=1, colspan = 1, key = "selected", default = false, title = "Selected tracks only"},
481 { type = "entry", col=2, colspan = 10, key = "filename", default = "params", title = "Store name" },
482 { type = "label", colspan = 5, title = "" },
485 local recall_options = {
486 { type = "label", colspan = 5, title = "" },
487 { type = "file", col =1, colspan = 10, key = "file", title = "Select a File", path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua") },
488 { type = "label", colspan = 5, title = "" },
491 local rv = LuaDialog.Dialog("Mixer Store:", dialog_options):run()
494 local choice = rv["select"]
495 if choice == "store" then
496 local srv = LuaDialog.Dialog("Mixer Store:", store_options):run()
498 empty_last_store() --ensures that params.lua will exist for the recall dialog
499 path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", srv["filename"] .. ".lua")
500 mark_tracks(srv['selected'])
504 if choice == "recall" then
505 local rrv = LuaDialog.Dialog("Mixer Store:", recall_options):run()
507 if rrv['file'] ~= path then path = rrv['file'] end
509 local dry_return = LuaDialog.Dialog("Dry Run Info:", dry_run(true)):run()
510 recall(true, dry_return)