get rid of ugly concatenation in favor of string.format(), and add small readout...
[ardour.git] / scripts / store_recall_mixer.lua
1 ardour {
2         ["type"] = "EditorAction",
3         name = "Mixer Store",
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.]]
8 }
9
10 function factory() return function()
11
12         local invalidate = {}
13         local path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua")
14
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')
17                 local dialog = {
18                         { type = "label", colspan = 5, title = mismatch_str },
19                         { type = "checkbox", col=1, colspan = 1, key = "use", default = true, title = checkbox_str },
20                 }
21                 local mismatch_return = LuaDialog.Dialog("", dialog):run()
22                 if mismatch_return then
23                         return mismatch_return['use']
24                 else
25                         return false
26                 end
27         end
28
29         function get_processor_by_name(track, name)
30                 local i = 0
31                 local proc = track:nth_processor(i)
32                         repeat
33                                 if ( proc:display_name() == name ) then
34                                         return proc
35                                 else
36                                         i = i + 1
37                                 end
38                                 proc = track:nth_processor(i)
39                         until proc:isnil()
40                 end
41
42         function new_plugin(name)
43                 for x = 0, 6 do
44                         plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
45                         if not(plugin:isnil()) then return plugin end
46                 end
47         end
48
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
54                 end
55         end
56
57         function route_groupid_interrogate(t)
58                 local group = false
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
62                         end
63                 end return group
64         end
65
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
70                         end
71                 end
72         end
73
74         function empty_last_store()  --empty current file from last run
75                 local file = io.open(path, "w")
76                 file:write("")
77                 file:close()
78         end
79
80         function mark_tracks(selected)
81
82                 empty_last_store()
83
84                 local route_string = [[instance = {
85                          route_id = %d,
86                          route_name = '%s',
87                          gain_control = %f,
88                          trim_control = %f,
89                          pan_control = %s,
90                          muted = %s,
91                          soloed = %s,
92                          order = {%s},
93                          cache = {%s},
94                          group = %s
95                 }]]
96
97                 local group_string = [[instance = {
98                          group_id = %s,
99                          name = '%s',
100                          routes = {%s},
101                 }]]
102
103                 local processor_string = [[instance = {
104                          plugin_id = %d,
105                          display_name = '%s',
106                          owned_by_route_name = '%s',
107                          owned_by_route_id = %d,
108                          parameters = {%s},
109                          active = %s,
110                 }]]
111
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,"
116
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]", "")
120
121                 local sel = Editor:get_selection ()
122                 local groups_to_write = {}
123                 local i = 0
124
125                 local tracks = Session:get_routes()
126
127                 if selected then tracks = sel.tracks:routelist() end
128
129                 for r in tracks:iter() do
130                         local group = route_group_interrogate(r)
131                         if group then
132                                 local already_there = false
133                                 for _, v in pairs(groups_to_write) do
134                                         if group == v then
135                                                 already_there = true
136                                         end
137                                 end
138                                 if not(already_there) then
139                                         groups_to_write[#groups_to_write + 1] = group
140                                 end
141                         end
142                 end
143
144                 for _, g in pairs(groups_to_write) do
145                         local tmp_str = ""
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())
148                                 i = i + 1
149                         end
150                         local group_str = string.format(
151                                 group_string,
152                                 g:to_stateful():id():to_s(),
153                                 g:name(),
154                                 tmp_str
155                         )
156
157                         file = io.open(path, "a")
158                         file:write(group_str, "\r\n")
159                         file:close()
160                 end
161
162                 for r in tracks:iter() do
163                         if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
164
165                         local order = ARDOUR.ProcessorList()
166                         local x = 0
167                         repeat
168                                 local proc = r:nth_processor(x)
169                                 if not proc:isnil() then
170                                         order:push_back(proc)
171                                 end
172                                 x = x + 1
173                         until proc:isnil()
174
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.
178
179                         local order_nmbr = 0
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())
186                                 end
187                                 order_nmbr = order_nmbr + 1
188                         end
189
190                         local route_str = string.format(
191                                         route_string,
192                                         rid,
193                                         r:name(),
194                                         r:gain_control():get_value(),
195                                         r:trim_control():get_value(),
196                                         tostring(pan),
197                                         r:muted(),
198                                         r:soloed(),
199                                         tmp_order_str,
200                                         tmp_cache_str,
201                                         route_groupid_interrogate(r)
202                                 )
203
204                         file = io.open(path, "a")
205                         file:write(route_str, "\n")
206                         file:close()
207
208                         local i = 0
209                         while true do
210                                 local params = {}
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)
224                                                         params[n] = val
225                                                 end
226                                                 n = n + 1
227                                         end
228                                 end
229                                 i = i + 1
230
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)
234                                 end
235
236                                 local proc_str = string.format(
237                                                 processor_string,
238                                                 id,
239                                                 proc:display_name(),
240                                                 r:name(),
241                                                 r:to_stateful():id():to_s(),
242                                                 tmp_params_str,
243                                                 active
244                                         )
245                                 file = io.open(path, "a")
246                                 file:write(proc_str, "\n")
247                                 file:close()
248                         end
249                         ::nextroute::
250                 end
251         end
252
253         function recall(debug, dry_run)
254                 local file = io.open(path, "r")
255                 assert(file, "File not found!")
256
257                 local i = 0
258                 for l in file:lines() do
259                         --print(i, l)
260
261                         local exec_line = dry_run["dothis-"..i]
262                         local skip_line = false
263                         if not(exec_line == nil) and not(exec_line) then
264                                 skip_line = true
265                         end
266
267                         local plugin, route, group = false, false, false
268                         local f = load(l)
269
270                         if debug then
271                                 print(i, string.sub(l, 0, 29), f)
272                         end
273
274                         if f then f() end
275
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
279
280                         if group then
281                                 if skip_line then goto nextline end
282
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)
287                                 if not(group) then
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)
291
292                                         --if continue then
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
298                                         end
299                                         --end
300                                 end
301                         end
302
303                         if route then
304                                 if skip_line then goto nextline end
305
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"]
314
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
318
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
323                                 end
324
325                                 well_known = {'PRE', 'Trim', 'EQ', 'Comp', 'Fader', 'POST'}
326
327                                 for k, v in pairs(order) do
328                                         local proc = Session:processor_by_id(PBD.ID(v))
329                                         if proc:isnil() then
330                                                 for id, name in pairs(cache) do
331                                                         if v == id then
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()
337                                                                                 goto nextproc
338                                                                         end
339                                                                 end
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()
344                                                                 end
345                                                         end
346                                                 end
347                                         end
348                                         ::nextproc::
349                                         if proc and not(proc:isnil()) then old_order:push_back(proc) end
350                                 end
351
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)
358                         end
359
360                         if plugin then
361                                 if skip_line then goto nextline end
362
363                                 local enable = {}
364                                 local params = instance["parameters"]
365                                 local p_id   = instance["plugin_id"]
366                                 local act    = instance["active"]
367
368                                 for k, v in pairs(invalidate) do --invalidate any deleted plugin's id
369                                         if p_id == k then
370                                                 p_id = v
371                                         end
372                                 end
373
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)
377
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
382                                         end
383                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
384                                 end
385
386                                 for k, v in pairs(enable) do
387                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
388                                 end
389                                 if act then proc:activate() else proc:deactivate() end
390                         end
391
392                         ::nextline::
393                         i = i + 1
394
395                 end
396         end
397
398         function dry_run(debug)
399                 --returns a dialog-able table of
400                 --everything we do (logically)
401                 --in the recall function
402
403                 local i = 0
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!")
407
408                 for l in file:lines() do
409                         local do_plugin, do_route, do_group = false, false, false
410                         local f = load(l)
411
412                         if debug then
413                                 print(i, string.sub(l, 0, 29), f)
414                         end
415
416                         if f then f() end
417
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
421
422                         if do_group then
423                                 local group_id   = instance["group_id"]
424                                 local group_name = instance["name"]
425                                 local dlg_title  = ""
426
427                                 local group_ptr  = group_by_id(group_id)
428
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())
432                                 else
433                                         dlg_title = string.format("Group ID Match: %s.", group_ptr:name())
434                                 end
435                                 table.insert(dry_table, {
436                                         type = "label", key =  "group-"..i , col = 0, colspan = 1, title = dlg_title
437                                 })
438                                 table.insert(dry_table, {
439                                         type = "checkbox", col=1, colspan = 1, key = "dothis-"..i, default = true, title = "line:"..i
440                                 })
441                         end
442
443                         if do_route then
444                                 local route_id   = instance["route_id"]
445                                 local route_name = instance["route_name"]
446                                 local dlg_title = ""
447
448                                 local route_ptr = Session:route_by_id(PBD.ID(route_id))
449
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())
454                                         else
455                                                 dlg_title = string.format("Route: %s-> (cannot find matching ID or name) ????", route_name)
456                                         end
457                                 else
458                                         dlg_title = string.format("Route Found by ID: %s", route_ptr:name())
459                                 end
460                                 table.insert(dry_table, {
461                                         type = "label", key =  "route-"..i , col = 0, colspan = 1, title = dlg_title
462                                 })
463                                 table.insert(dry_table, {
464                                         type = "checkbox", col=1, colspan = 1, key = "dothis-"..i, default = true, title = "line:"..i
465                                 })
466                         end
467                         i = i + 1
468                 end
469                 return dry_table
470         end
471
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 = "" },
476         }
477
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 = "" },
483         }
484
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 = "" },
489         }
490
491         local rv = LuaDialog.Dialog("Mixer Store:", dialog_options):run()
492
493         if rv then
494                 local choice = rv["select"]
495                 if choice == "store" then
496                         local srv = LuaDialog.Dialog("Mixer Store:", store_options):run()
497                         if srv then
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'])
501                         end
502                 end
503
504                 if choice == "recall" then
505                         local rrv = LuaDialog.Dialog("Mixer Store:", recall_options):run()
506                         if rrv then
507                                 if rrv['file'] ~= path then path = rrv['file'] end
508                                 --recall(true)
509                                 local dry_return = LuaDialog.Dialog("Dry Run Info:", dry_run(true)):run()
510                                 recall(true, dry_return)
511                         end
512                 end
513         end
514 collectgarbage()
515 end end