add support to reinstantiate deleted plugins
[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 recalled arbitrarily. Supports: processor settings, gain, trim, pan and processor ordering.]]
6 }
7
8 function factory() return function()
9
10         local path = ARDOUR.LuaAPI.build_filename(Session:path(), "export", "params.lua")
11         function mark()
12                 local file = io.open(path, "w")
13                 file:write("") --empty current file from last run
14                 file:close()
15                 for r in Session:get_routes():iter() do
16                         if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
17
18                         local order = ARDOUR.ProcessorList()
19                         local x = 0
20                         repeat
21                                 local proc = r:nth_processor(x)
22                                 if not proc:isnil() then
23                                         order:push_back(proc)
24                                 end
25                                 x = x + 1
26                         until proc:isnil()
27
28                         local route_str, proc_order_str, cache_str = "", "", ""
29                         local rid = r:to_stateful():id():to_s()
30                         local pan = r:pan_azimuth_control()
31                         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
32
33                         local on = 0
34                         for p in order:iter() do
35                                 local pid = p:to_stateful():id():to_s()
36                                 proc_order_str = proc_order_str .. "[" .. on .. "] = " .. pid ..","
37                                 cache_str = cache_str .. "[" .. pid .. "] = " .. "\"" .. p:display_name() .. "\"" ..","
38                                 on = on + 1
39                         end
40
41                         route_str = "instance = {route_id = " .. rid .. ", gain_control = " .. r:gain_control():get_value() .. ", trim_control = " .. r:trim_control():get_value() .. ", pan_control = " .. tostring(pan) .. ", order = {" .. proc_order_str .."}, cache = {" .. cache_str .. "}" .. "}"
42                         file = io.open(path, "a")
43                         file:write(route_str, "\r\n")
44                         file:close()
45
46                         local i = 0
47                         while true do
48                                 local params = {}
49                                 local proc_str, params_str = "", ""
50                                 local proc = r:nth_plugin (i)
51                                 if proc:isnil () then break end
52                                 local active = proc:active()
53                                 local id = proc:to_stateful():id():to_s()
54                                 local plug = proc:to_insert ():plugin (0)
55                                 local n = 0 -- count control-ports
56                                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
57                                         if plug:parameter_is_control (j) then
58                                                 local label = plug:parameter_label (j)
59                                                 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
60                                                         local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
61                                                         local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
62                                                         if not(val == pd.normal) then
63                                                                 params[n] = val
64                                                         end
65                                                 end
66                                                 n = n + 1
67                                         end
68                                 end
69                                 i = i + 1
70                                 for k, v in pairs(params) do
71                                         params_str = params_str .. "[".. k .."] = " .. v .. ","
72                                 end
73                                 proc_str = "instance = {plugin_id = " .. id .. ", parameters = {" .. params_str .. "}, active = " .. tostring(active) .. "}"
74                                 file = io.open(path, "a")
75                                 file:write(proc_str, "\r\n")
76                                 file:close()
77                         end
78                         ::nextroute::
79                 end
80         end
81         local invalidate = {}
82         function recall()
83                 local file = io.open(path, "r")
84                 assert(file, "File not found!")
85                 for l in file:lines() do
86                         --print(l)
87
88                         local plugin, route = false, false
89                         local f = load(l)
90                         f ()
91
92                         if instance["route_id"]  ~= nil then route = true end
93                         if instance["plugin_id"] ~= nil then plugin = true end
94
95                         function new_plugin(name)
96                                 local plugin = nil
97                                 for x = 0, 6 do
98                                         plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
99                                         if not(plugin:isnil()) then break end
100                                 end return plugin
101                         end
102
103                         if route then
104                                 local old_order = ARDOUR.ProcessorList()
105                                 local rid = PBD.ID(instance["route_id"])
106                                 local rt = Session:route_by_id(rid)
107                                 if rt:isnil() then goto nextline end
108                                 local gc, tc, pc = instance["gain_control"], instance["trim_control"], instance["pan_control"]
109                                 for k, v in pairs(instance["order"]) do
110                                         local proc = Session:processor_by_id(PBD.ID(v))
111                                         if proc:isnil() then
112                                                 for id, name in pairs(instance["cache"]) do
113                                                         if v == id then
114                                                                 proc = new_plugin(name)
115                                                                 invalidate[v] = proc:to_stateful():id():to_s()
116                                                         end
117                                                 end
118                                         end
119                                         if not(proc:isnil()) then old_order:push_back(proc) end
120                                 end
121                                 rt:gain_control():set_value(gc, 1)
122                                 rt:trim_control():set_value(tc, 1)
123                                 if pc ~= false then rt:pan_azimuth_control():set_value(pc, 1) end
124                                 rt:reorder_processors(old_order, nil)
125                         end
126
127                         if plugin then
128                                 for k, v in pairs(invalidate) do --invalidate deleted plugin id's
129                                         if instance["plugin_id"] == k then
130                                                 instance["plugin_id"] = v
131                                         end
132                                 end
133                                 local act = instance["active"]
134                                 local id = PBD.ID(instance["plugin_id"])
135                                 local proc = Session:processor_by_id(id)
136                                 if proc:isnil() then goto nextline end
137                                 for k, v in pairs(instance["parameters"]) do
138                                         ARDOUR.LuaAPI.set_processor_param(proc, k, v)
139                                 end
140                                 if act then proc:activate() else proc:deactivate() end
141                         end
142                         ::nextline::
143                 end
144         end
145
146         local dialog_options = {
147                 { type = "label", colspan= 10, title = "" },
148                 { type = "radio",  colspan= 10, key = "select", title = "", values ={ ["1. Mark"] = "mark", ["2. Recall"] = "recall" }, default = "1. Mark"},
149                 { type = "label", colspan= 10, title = "" },
150         }
151
152         local rv = LuaDialog.Dialog("Mixer Store:", dialog_options):run()
153         assert(rv, 'Dialog box was canceled or is ' .. type(rv))
154         local c = rv["select"]
155         if c == "mark" then mark() end
156         if c == "recall" then recall() end
157
158 end end