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