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