f3c39275cb5b39ceb4d5e5fa79ef4ea2cb675460
[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, plus re-adding certain deleted plugins.]]
7 }
8
9 function factory() return function()
10
11         local invalidate = {}
12         local path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua")
13
14         function get_processor_by_name(track, name)
15                 local i = 0
16                 local proc = track:nth_processor(i)
17                         repeat
18                                 if ( proc:display_name() == name ) then
19                                         return proc
20                                 else
21                                         i = i + 1
22                                 end
23                                 proc = track:nth_processor(i)
24                         until proc:isnil()
25                 end
26
27         function new_plugin(name)
28                 for x = 0, 6 do
29                         plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
30                         if not(plugin:isnil()) then return plugin end
31                 end
32         end
33
34         function group_by_id(id)
35                 local id  = tonumber(id)
36                 for g in Session:route_groups():iter() do
37                         local group_id = tonumber(g:to_stateful():id():to_s())
38                         if group_id == id then return g end
39                 end
40         end
41
42         function route_groupid_interrogate(t)
43                 local group = false
44                 for g in Session:route_groups():iter() do
45                         for r in g:route_list():iter() do
46                                 if r:name() == t:name() then group = g:to_stateful():id():to_s() end
47                         end
48                 end return group
49         end
50
51         function route_group_interrogate(t)
52                 for g in Session:route_groups():iter() do
53                         for r in g:route_list():iter() do
54                                 if r:name() == t:name() then return g end
55                         end
56                 end
57         end
58
59         function empty_last_store()  --empty current file from last run
60                 local file = io.open(path, "w")
61                 file:write("")
62                 file:close()
63         end
64
65         function mark_selected_tracks()
66                 empty_last_store()
67
68                 local sel = Editor:get_selection ()
69                 local groups_to_write = {}
70                 local i = 0
71
72                 for r in sel.tracks:routelist():iter() do
73                         local group = route_group_interrogate(r)
74                         if group then groups_to_write[#groups_to_write + 1] = group end
75                 end
76
77                 for k, g in pairs(groups_to_write) do
78                         local g_route_str, group_str = "", ""
79                         group_str = "instance = {group_id = " .. g:to_stateful():id():to_s() .. ", name = " .. "\"" .. g:name() .. "\"" .. ", routes = {"
80                         for t in g:route_list():iter() do
81                                 g_route_str = g_route_str .."[".. i .."] = " .. t:to_stateful():id():to_s() .. ","
82                                 i = i + 1
83                         end
84                         group_str = group_str .. g_route_str .. "}}"
85                         if not(group_str == "") then --sometimes there are no groups in the session
86                                 file = io.open(path, "a")
87                                 file:write(group_str, "\r\n")
88                                 file:close()
89                         end
90                 end
91
92                 for r in sel.tracks:routelist():iter() do
93                         if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
94
95                         local order = ARDOUR.ProcessorList()
96                         local x = 0
97                         repeat
98                                 local proc = r:nth_processor(x)
99                                 if not proc:isnil() then
100                                         order:push_back(proc)
101                                 end
102                                 x = x + 1
103                         until proc:isnil()
104
105                         local route_str, proc_order_str, cache_str = "", "", ""
106                         local rid = r:to_stateful():id():to_s()
107                         local pan = r:pan_azimuth_control()
108                         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
109
110                         local on = 0
111                         for p in order:iter() do
112                                 local pid = p:to_stateful():id():to_s()
113                                 if not(string.find(p:display_name(), "latcomp")) then
114                                         proc_order_str = proc_order_str .. "[" .. on .. "] = " .. pid ..","
115                                         cache_str = cache_str .. "[" .. pid .. "] = " .. "\"" .. p:display_name() .. "\"" ..","
116                                 end
117                                 on = on + 1
118                         end
119
120                         route_str = "instance = {route_id = " .. rid .. ", route_name = " .. r:name() .. ", gain_control = " .. r:gain_control():get_value() .. ", trim_control = " .. r:trim_control():get_value() .. ", pan_control = " .. tostring(pan) .. ", muted = " .. tostring(r:muted()) .. ", soloed = " .. tostring(r:soloed()) .. ", order = {" .. proc_order_str .."}, cache = {" .. cache_str .. "}, group = " .. tostring(route_groupid_interrogate(r))  .. "}"
121                         file = io.open(path, "a")
122                         file:write(route_str, "\r\n")
123                         file:close()
124
125                         local i = 0
126                         while true do
127                                 local params = {}
128                                 local proc_str, params_str = "", ""
129                                 local proc = r:nth_plugin (i)
130                                 if proc:isnil () then break end
131                                 local active = proc:active()
132                                 local id = proc:to_stateful():id():to_s()
133                                 local plug = proc:to_insert ():plugin (0)
134                                 local n = 0 -- count control-ports
135                                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
136                                         if plug:parameter_is_control (j) then
137                                                 local label = plug:parameter_label (j)
138                                                 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
139                                                         local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
140                                                         local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
141                                                         --print(r:name(), "->", proc:display_name(), label, val)
142                                                         params[n] = val
143                                                 end
144                                                 n = n + 1
145                                         end
146                                 end
147                                 i = i + 1
148                                 for k, v in pairs(params) do
149                                         params_str = params_str .. "[".. k .."] = " .. v .. ","
150                                 end
151                                 proc_str = "instance = {plugin_id = " .. id .. ", parameters = {" .. params_str .. "}, active = " .. tostring(active) .. "}"
152                                 file = io.open(path, "a")
153                                 file:write(proc_str, "\r\n")
154                                 file:close()
155                         end
156                         ::nextroute::
157                 end
158         end
159
160         function mark_all_tracks()
161                 empty_last_store()
162
163                 local i = 0
164                 for g in Session:route_groups():iter() do --@ToDo: Color, and other bools
165                         local g_route_str, group_str = "", ""
166                         group_str = "instance = {group_id = " .. g:to_stateful():id():to_s() .. ", name = " .. "\"" .. g:name() .. "\"" .. ", routes = {"
167                         for t in g:route_list():iter() do
168                                 g_route_str = g_route_str .."[".. i .."] = " .. t:to_stateful():id():to_s() .. ","
169                                 i = i + 1
170                         end
171                         group_str = group_str .. g_route_str .. "}}"
172                         if not(group_str == "") then --sometimes there are no groups in the session
173                                 file = io.open(path, "a")
174                                 file:write(group_str, "\r\n")
175                                 file:close()
176                         end
177                 end
178
179                 for r in Session:get_routes():iter() do
180                         if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
181
182                         local order = ARDOUR.ProcessorList()
183                         local x = 0
184                         repeat
185                                 local proc = r:nth_processor(x)
186                                 if not proc:isnil() then
187                                         order:push_back(proc)
188                                 end
189                                 x = x + 1
190                         until proc:isnil()
191
192                         local route_str, proc_order_str, cache_str = "", "", ""
193                         local rid = r:to_stateful():id():to_s()
194                         local pan = r:pan_azimuth_control()
195                         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
196
197                         local on = 0
198                         for p in order:iter() do
199                                 local pid = p:to_stateful():id():to_s()
200                                 if not(string.find(p:display_name(), "latcomp")) then
201                                         proc_order_str = proc_order_str .. "[" .. on .. "] = " .. pid ..","
202                                         cache_str = cache_str .. "[" .. pid .. "] = " .. "\"" .. p:display_name() .. "\"" ..","
203                                 end
204                                 on = on + 1
205                         end
206
207                         route_str = "instance = {route_id = " .. rid .. ", route_name = '" .. r:name() .. "', gain_control = " .. r:gain_control():get_value() .. ", trim_control = " .. r:trim_control():get_value() .. ", pan_control = " .. tostring(pan) .. ", muted = " .. tostring(r:muted()) .. ", soloed = " .. tostring(r:soloed()) .. ", order = {" .. proc_order_str .."}, cache = {" .. cache_str .. "}, group = " .. tostring(route_groupid_interrogate(r))  .. "}"
208                         file = io.open(path, "a")
209                         file:write(route_str, "\r\n")
210                         file:close()
211
212                         local i = 0
213                         while true do
214                                 local params = {}
215                                 local proc_str, params_str = "", ""
216                                 local proc = r:nth_plugin (i)
217                                 if proc:isnil () then break end
218                                 local active = proc:active()
219                                 local id = proc:to_stateful():id():to_s()
220                                 local plug = proc:to_insert ():plugin (0)
221                                 local n = 0 -- count control-ports
222                                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
223                                         if plug:parameter_is_control (j) then
224                                                 local label = plug:parameter_label (j)
225                                                 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
226                                                         local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
227                                                         local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
228                                                         --print(r:name(), "->", proc:display_name(), label, val)
229                                                         params[n] = val
230                                                 end
231                                                 n = n + 1
232                                         end
233                                 end
234                                 i = i + 1
235                                 for k, v in pairs(params) do
236                                         params_str = params_str .. "[".. k .."] = " .. v .. ","
237                                 end
238                                 proc_str = "instance = {plugin_id = " .. id .. ", parameters = {" .. params_str .. "}, active = " .. tostring(active) .. "}"
239                                 file = io.open(path, "a")
240                                 file:write(proc_str, "\r\n")
241                                 file:close()
242                         end
243                         ::nextroute::
244                 end
245         end
246
247         function recall()
248                 local file = io.open(path, "r")
249                 assert(file, "File not found!")
250                 for l in file:lines() do
251                         --print(l)
252
253                         local plugin, route, group = false, false, false
254                         local f = load(l)
255                         f ()
256
257                         if instance["route_id"]  then route = true end
258                         if instance["plugin_id"] then plugin = true end
259                         if instance["group_id"]  then group = true end
260
261                         if group then
262                                 local g_id   = instance["group_id"]
263                                 local routes = instance["routes"]
264                                 local name   = instance["name"]
265                                 local group  = group_by_id(g_id)
266                                 if not(group) then group = Session:new_route_group(name) end
267                                 for k, v in pairs(routes) do
268                                         local rt = Session:route_by_id(PBD.ID(v))
269                                         if not(rt:isnil()) then group:add(rt) end
270                                 end
271                         end
272
273                         if route then
274
275                                 local old_order = ARDOUR.ProcessorList()
276                                 local r_id = PBD.ID(instance["route_id"])
277                                 local muted, soloed = instance["muted"], instance["soloed"]
278                                 local order = instance["order"]
279                                 local cache = instance["cache"]
280                                 local group = instance["group"]
281                                 local name  = instance["route_name"]
282                                 local gc, tc, pc = instance["gain_control"], instance["trim_control"], instance["pan_control"]
283
284                                 local rt = Session:route_by_id(r_id)
285                                 if rt:isnil() then rt = Session:route_by_name(name) end
286                                 if rt:isnil() then goto nextline end
287
288                                 local cur_group_id = route_groupid_interrogate(rt)
289                                 if not(group) and (cur_group_id) then
290                                         local g = group_by_id(cur_group_id)
291                                         if g then g:remove(rt) end
292                                 end
293
294                                 well_known = {'PRE', 'Trim', 'EQ', 'Comp', 'Fader', 'POST'}
295
296                                 for k, v in pairs(order) do
297                                         local proc = Session:processor_by_id(PBD.ID(v))
298                                         if proc:isnil() then
299                                                 for id, name in pairs(cache) do
300                                                         if v == id then
301                                                                 proc = new_plugin(name)
302                                                                 for _, control in pairs(well_known) do
303                                                                         if name == control then
304                                                                                 proc = get_processor_by_name(rt, control)
305                                                                                 invalidate[v] = proc:to_stateful():id():to_s()
306                                                                                 goto nextproc
307                                                                         end
308                                                                 end
309                                                                 if not(proc) then goto nextproc end
310                                                                 if not(proc:isnil()) then
311                                                                         rt:add_processor_by_index(proc, 0, nil, true)
312                                                                         invalidate[v] = proc:to_stateful():id():to_s()
313                                                                 end
314                                                         end
315                                                 end
316                                         end
317                                         ::nextproc::
318                                         if proc and not(proc:isnil()) then old_order:push_back(proc) end
319                                 end
320
321                                 if muted  then rt:mute_control():set_value(1, 1) else rt:mute_control():set_value(0, 1) end
322                                 if soloed then rt:solo_control():set_value(1, 1) else rt:solo_control():set_value(0, 1) end
323                                 rt:gain_control():set_value(gc, 1)
324                                 rt:trim_control():set_value(tc, 1)
325                                 if pc ~= false then rt:pan_azimuth_control():set_value(pc, 1) end
326                                 rt:reorder_processors(old_order, nil)
327                         end
328
329                         if plugin then
330                                 local enable = {}
331                                 local params = instance["parameters"]
332                                 local p_id   = instance["plugin_id"]
333                                 local act    = instance["active"]
334
335                                 for k, v in pairs(invalidate) do --invalidate any deleted plugin's id
336                                         if p_id == k then
337                                                 p_id = v
338                                         end
339                                 end
340
341                                 local proc = Session:processor_by_id(PBD.ID(p_id))
342                                 if proc:isnil() then goto nextline end
343                                 local plug = proc:to_insert():plugin(0)
344
345                                 for k, v in pairs(params) do
346                                         local label = plug:parameter_label(k)
347                                         if string.find(label, "Assign") or string.find(label, "Enable") then --@ToDo: Check Plugin type == LADSPA or VST?
348                                                 enable[k] = v --queue any assignments/enables for after the initial parameter recalling to duck the 'in-on-change' feature
349                                         end
350                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
351                                 end
352
353                                 for k, v in pairs(enable) do
354                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
355                                 end
356                                 if act then proc:activate() else proc:deactivate() end
357                         end
358                         ::nextline::
359                 end
360         end
361
362         local dialog_options = {
363                 { type = "label", colspan = 5, title = "" },
364                 { type = "radio", col = 1, colspan = 7, key = "select", title = "", values ={ ["Store"] = "store", ["Recall"] = "recall" }, default = "Store"},
365                 { type = "label", colspan = 5, title = "" },
366         }
367
368         local store_options = {
369                 { type = "label", colspan = 5, title = "" },
370                 { type = "checkbox", col=1, colspan = 1, key = "selected", default = false, title = "Selected tracks only"},
371                 { type = "entry", col=2, colspan = 10, key = "filename", default = "params", title = "Store name" },
372                 { type = "label", colspan = 5, title = "" },
373         }
374
375         local recall_options = {
376                 { type = "label", colspan = 5, title = "" },
377                 { type = "file", col =1, colspan = 10, key = "file", title = "Select a File",  path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua") },
378                 { type = "label", colspan = 5, title = "" },
379         }
380
381         local rv = LuaDialog.Dialog("Mixer Store:", dialog_options):run()
382
383         if rv then
384                 local choice = rv["select"]
385                 if choice == "store" then
386                         local srv = LuaDialog.Dialog("Mixer Store:", store_options):run()
387                         if srv then
388                                 empty_last_store() --ensures that params.lua will exist for the recall dialog
389                                 path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", srv["filename"] .. ".lua")
390                                 if srv['selected'] then
391                                         mark_selected_tracks()
392                                 else
393                                         mark_all_tracks()
394                                 end
395                         end
396                 end
397
398                 if choice == "recall" then
399                         local rrv = LuaDialog.Dialog("Mixer Store:", recall_options):run()
400                         if rrv then
401                                 if rrv['file'] ~= path then path = rrv['file'] end
402                                 recall()
403                         end
404                 end
405         end
406
407 end end