stop trying to guess plugin type, just ask the plugin during store-time
[ardour.git] / scripts / mixer_settings_store.lua
1 ardour {
2         ["type"]    = "EditorAction",
3         name        = "Store Mixer Settings",
4         author      = "Mixbus Team",
5         description = [[
6
7         Stores the current Mixer state as a file
8         that can be read and recalled arbitrarily
9         by it's companion script, Recall Mixer Settings.
10
11         Supports: processor settings, grouping,
12         mute, solo, gain, trim, pan and processor ordering,
13         plus re-adding certain deleted plugins.
14
15         ]]
16 }
17
18 function factory () return function ()
19
20         local user_cfg = ARDOUR.user_config_directory(-1)
21         local local_path = ARDOUR.LuaAPI.build_filename(Session:path(), 'mixer_settings')
22         local global_path = ARDOUR.LuaAPI.build_filename(user_cfg, 'mixer_settings')
23
24         function exists(file)
25                 local ok, err, code = os.rename(file, file)
26                 if not ok then
27                         if code == 13 then -- Permission denied, but it exists
28                                 return true
29                         end
30                 end return ok, err
31         end
32
33         function whoami()
34                 if not pcall(function() local first_check = Session:get_mixbus(0) end) then
35                         return "Ardour"
36                 else
37                         local second_check = Session:get_mixbus(11)
38                         if second_check:isnil() then
39                                 return "Mixbus"
40                         else
41                                 return "32C"
42                         end
43                 end
44         end
45
46         function isdir(path)
47                 return exists(path.."/")
48         end
49
50         function setup_paths()
51                 local global_ok, local_ok = false, false
52
53                 if not(isdir(global_path)) then
54                         global_ok, _, _ = os.execute('mkdir '.. global_path)
55                         if global_ok == 0 then
56                                 global_ok = true
57                         end
58                 else
59                         global_ok = true
60                 end
61                 if not(isdir(local_path)) then
62                         local_ok, _, _ = os.execute('mkdir '.. local_path)
63                         if local_ok == 0 then
64                                 local_ok = true
65                         end
66                 else
67                         local_ok = true
68                 end
69                 return global_ok, local_ok
70         end
71
72         function get_processor_by_name(track, name)
73                 local i = 0
74                 local proc = track:nth_processor(i)
75                         repeat
76                                 if(proc:display_name() == name) then
77                                         return proc
78                                 else
79                                         i = i + 1
80                                 end
81                                 proc = track:nth_processor(i)
82                         until proc:isnil()
83                 end
84
85         function group_by_id(id)
86                 local id  = tonumber(id)
87                 for g in Session:route_groups():iter() do
88                         local group_id = tonumber(g:to_stateful():id():to_s())
89                         if group_id == id then return g end
90                 end
91         end
92
93         function group_by_name(name)
94                 for g in Session:route_groups():iter() do
95                         if g:name() == name then return g end
96                 end
97         end
98
99         function route_groupid_interrogate(t)
100                 local group = false
101                 for g in Session:route_groups():iter() do
102                         for r in g:route_list():iter() do
103                                 if r:name() == t:name() then group = g:to_stateful():id():to_s() end
104                         end
105                 end return group
106         end
107
108         function route_group_interrogate(t)
109                 for g in Session:route_groups():iter() do
110                         for r in g:route_list():iter() do
111                                 if r:name() == t:name() then return g end
112                         end
113                 end
114         end
115
116         function empty_last_store(path)  --empty current file from last run
117                 local file = io.open(path, "w")
118                 --file:write(string.format("instance = { whoami = '%s' }", whoami())
119                 file:write("")
120                 file:close()
121         end
122
123         function mark_tracks(selected, path)
124
125                 empty_last_store(path)
126
127                 local route_string = [[instance = {
128                          route_id = %d,
129                          route_name = '%s',
130                          gain_control = %s,
131                          trim_control = %s,
132                          pan_control = %s,
133                          muted = %s,
134                          soloed = %s,
135                          order = {%s},
136                          cache = {%s},
137                          group = %s,
138                          group_name = '%s',
139                          pi_order = %d
140                 }]]
141
142                 local group_string = [[instance = {
143                          group_id = %s,
144                          name = '%s',
145                          routes = {%s},
146                 }]]
147
148                 local processor_string = [[instance = {
149                          plugin_id = %d,
150                          type = %d,
151                          display_name = '%s',
152                          owned_by_route_name = '%s',
153                          owned_by_route_id = %d,
154                          parameters = {%s},
155                          active = %s,
156                 }]]
157
158                 local group_route_string = " [%d] = %s,"
159                 local proc_order_string  = " [%d] = %d,"
160                 local proc_cache_string  = " [%d] = {'%s', %d},"
161                 local params_string      = " [%d] = %s,"
162
163                 --ensure easy-to-read formatting doesn't make it through
164                 local route_string     = string.gsub(route_string, "[\n\t]", "")
165                 local group_string     = string.gsub(group_string, "[\n\t]", "")
166                 local processor_string = string.gsub(processor_string, "[\n\t]", "")
167
168                 local sel = Editor:get_selection ()
169                 local groups_to_write = {}
170                 local i = 0
171
172                 local tracks = Session:get_stripables()
173
174                 if selected then tracks = sel.tracks:routelist() end
175
176                 for r in tracks:iter() do
177                         local group = route_group_interrogate(r)
178                         if group then
179                                 local already_there = false
180                                 for _, v in pairs(groups_to_write) do
181                                         if group == v then
182                                                 already_there = true
183                                         end
184                                 end
185                                 if not(already_there) then
186                                         groups_to_write[#groups_to_write + 1] = group
187                                 end
188                         end
189                 end
190
191                 for _, g in pairs(groups_to_write) do
192                         local tmp_str = ""
193                         for t in g:route_list():iter() do
194                                 tmp_str = tmp_str .. string.format(group_route_string, i, t:to_stateful():id():to_s())
195                                 i = i + 1
196                         end
197                         local group_str = string.format(
198                                 group_string,
199                                 g:to_stateful():id():to_s(),
200                                 g:name(),
201                                 tmp_str
202                         )
203
204                         file = io.open(path, "a")
205                         file:write(group_str, "\r\n")
206                         file:close()
207                 end
208
209                 for r in tracks:iter() do
210                         if r:is_monitor () or r:is_auditioner () or not(r:to_vca():isnil()) then goto nextroute end -- skip special routes
211                         r = r:to_route()
212                         if r:isnil() then goto nextroute end
213                         local order = ARDOUR.ProcessorList()
214                         local x = 0
215                         repeat
216                                 local proc = r:nth_processor(x)
217                                 if not proc:isnil() then
218                                         order:push_back(proc)
219                                 end
220                                 x = x + 1
221                         until proc:isnil()
222
223
224                         local route_group = route_group_interrogate(r)
225                         if route_group then route_group = route_group:name() else route_group = "" end
226                         local rid = r:to_stateful():id():to_s()
227                         local pan = r:pan_azimuth_control()
228                         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
229
230                         local order_nmbr = 0
231                         local tmp_order_str, tmp_cache_str = "", ""
232                         for p in order:iter() do
233                                 if not(p:to_insert():isnil()) then
234                                         type = p:to_insert():plugin(0):get_info().type
235                                 else
236                                         type = 99
237                                 end
238                                 local pid = p:to_stateful():id():to_s()
239                                 if not(string.find(p:display_name(), "latcomp")) then
240                                         tmp_order_str = tmp_order_str .. string.format(proc_order_string, order_nmbr, pid)
241                                         tmp_cache_str = tmp_cache_str .. string.format(proc_cache_string, pid, p:display_name(), type)
242                                 end
243                                 order_nmbr = order_nmbr + 1
244                         end
245
246                         local route_str = string.format(
247                                         route_string,
248                                         rid,
249                                         r:name(),
250                                         ARDOUR.LuaAPI.ascii_dtostr(r:gain_control():get_value()),
251                                         ARDOUR.LuaAPI.ascii_dtostr(r:trim_control():get_value()),
252                                         tostring(pan),
253                                         r:muted(),
254                                         r:soloed(),
255                                         tmp_order_str,
256                                         tmp_cache_str,
257                                         route_groupid_interrogate(r),
258                                         route_group,
259                                         r:presentation_info_ptr():order()
260                                 )
261
262                         file = io.open(path, "a")
263                         file:write(route_str, "\n")
264                         file:close()
265
266                         local i = 0
267                         while true do
268                                 local params = {}
269                                 local proc = r:nth_plugin (i)
270                                 if proc:isnil () then break end
271                                 local active = proc:active()
272                                 local id = proc:to_stateful():id():to_s()
273                                 local plug = proc:to_insert ():plugin (0)
274                                 local type = proc:to_insert():plugin(0):get_info().type
275                                 local n = 0 -- count control-ports
276                                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
277                                         if plug:parameter_is_control (j) then
278                                                 local label = plug:parameter_label (j)
279                                                 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
280                                                         local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
281                                                         local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
282                                                         print(r:name(), "->", proc:display_name(), label, val)
283                                                         params[n] = val
284                                                 end
285                                                 n = n + 1
286                                         end
287                                 end
288                                 i = i + 1
289
290                                 local tmp_params_str = ""
291                                 for k, v in pairs(params) do
292                                         tmp_params_str = tmp_params_str .. string.format(params_string, k, ARDOUR.LuaAPI.ascii_dtostr(v))
293                                 end
294
295                                 local proc_str = string.format(
296                                                 processor_string,
297                                                 id,
298                                                 type,
299                                                 proc:display_name(),
300                                                 r:name(),
301                                                 r:to_stateful():id():to_s(),
302                                                 tmp_params_str,
303                                                 active
304                                         )
305                                 file = io.open(path, "a")
306                                 file:write(proc_str, "\n")
307                                 file:close()
308                         end
309                         ::nextroute::
310                 end
311         end
312
313         local store_options = {
314                 { type = "label",    col=0, colspan=1, align="right", title = "Name:" },
315                 { type = "entry",    col=1, colspan=1, align="left" , key = "filename", default = Session:name(), title=""},
316                 { type = "label",    col=0, colspan=1, align="right", title = "Location:" },
317                 {
318                         type = "radio",  col=1, colspan=3, align="left", key = "store-dir", title = "", values =
319                         {
320                                 ['Global (accessible from any session)'] = 1, ['Local (this session only)'] = 2
321                         },
322                         default = 'Locally (this session only)'
323                 },
324                 { type = "hseparator", title="", col=0, colspan = 3},
325                 { type = "label",    col=0, colspan=1, align="right", title = "Selected Tracks Only:" },
326                 { type = "checkbox", col=1, colspan=1, align="left",  key = "selected", default = false, title = ""},
327                 --{ type = "label", col=0, colspan=2, align="left", title = ''},
328                 --{ type = "label", col=0, colspan=2, align="left", title = "Global Path: " .. global_path},
329                 --{ type = "label", col=0, colspan=2, align="left", title = "Local Path: "  .. local_path},
330         }
331
332         local global_ok, local_ok = setup_paths()
333
334         if global_ok and local_ok then
335                 local rv = LuaDialog.Dialog("Store Mixer Settings:", store_options):run()
336
337                 if not(rv) then return end
338
339                 local filename = rv['filename']
340                 if rv['store-dir'] == 1 then
341                         local store_path = ARDOUR.LuaAPI.build_filename(global_path, string.format("%s-%s.lua", filename, whoami()))
342                         local selected = rv['selected']
343                         mark_tracks(selected, store_path)
344                 end
345
346                 if rv['store-dir'] == 2 then
347                         local store_path = ARDOUR.LuaAPI.build_filename(local_path, string.format("%s-%s.lua", filename, whoami()))
348                         print(store_path)
349                         local selected = rv['selected']
350                         mark_tracks(selected, store_path)
351                 end
352         end
353
354 end end