incorporate program name into save and recalling
[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                                 local default_file = ARDOUR.LuaAPI.build_filename(global_path, 'FactoryDefault-'..whoami()..'.lua')
57                                 local file = io.open(default_file, "w")
58                                 file:write("")
59                                 file:close()
60                                 global_ok = true
61                         end
62                 else
63                         global_ok = true
64                 end
65                 if not(isdir(local_path)) then
66                         local_ok, _, _ = os.execute('mkdir '.. local_path)
67                         if local_ok == 0 then
68                                 local stub_file = ARDOUR.LuaAPI.build_filename(local_path, 'stub')
69                                 local file = io.open(stub_file, "w")
70                                 file:write("")
71                                 file:close()
72                                 local_ok = true
73                         end
74                 else
75                         local_ok = true
76                 end
77                 return global_ok, local_ok
78         end
79
80         function get_processor_by_name(track, name)
81                 local i = 0
82                 local proc = track:nth_processor(i)
83                         repeat
84                                 if(proc:display_name() == name) then
85                                         return proc
86                                 else
87                                         i = i + 1
88                                 end
89                                 proc = track:nth_processor(i)
90                         until proc:isnil()
91                 end
92
93         function new_plugin(name)
94                 for x = 0, 6 do
95                         local plugin = ARDOUR.LuaAPI.new_plugin(Session, name, x, "")
96                         if not(plugin:isnil()) then return plugin end
97                 end
98         end
99
100         function group_by_id(id)
101                 local id  = tonumber(id)
102                 for g in Session:route_groups():iter() do
103                         local group_id = tonumber(g:to_stateful():id():to_s())
104                         if group_id == id then return g end
105                 end
106         end
107
108         function group_by_name(name)
109                 for g in Session:route_groups():iter() do
110                         if g:name() == name then return g end
111                 end
112         end
113
114         function route_groupid_interrogate(t)
115                 local group = false
116                 for g in Session:route_groups():iter() do
117                         for r in g:route_list():iter() do
118                                 if r:name() == t:name() then group = g:to_stateful():id():to_s() end
119                         end
120                 end return group
121         end
122
123         function route_group_interrogate(t)
124                 for g in Session:route_groups():iter() do
125                         for r in g:route_list():iter() do
126                                 if r:name() == t:name() then return g end
127                         end
128                 end
129         end
130
131         function empty_last_store(path)  --empty current file from last run
132                 local file = io.open(path, "w")
133                 file:write("")
134                 file:close()
135         end
136
137         function mark_tracks(selected, path)
138
139                 empty_last_store(path)
140
141                 local route_string = [[instance = {
142                          route_id = %d,
143                          route_name = '%s',
144                          gain_control = %f,
145                          trim_control = %f,
146                          pan_control = %s,
147                          muted = %s,
148                          soloed = %s,
149                          order = {%s},
150                          cache = {%s},
151                          group = %s,
152                          group_name = '%s'
153                 }]]
154
155                 local group_string = [[instance = {
156                          group_id = %s,
157                          name = '%s',
158                          routes = {%s},
159                 }]]
160
161                 local processor_string = [[instance = {
162                          plugin_id = %d,
163                          display_name = '%s',
164                          owned_by_route_name = '%s',
165                          owned_by_route_id = %d,
166                          parameters = {%s},
167                          active = %s,
168                 }]]
169
170                 local group_route_string = " [%d] = %s,"
171                 local proc_order_string  = " [%d] = %d,"
172                 local proc_cache_string  = " [%d] = '%s',"
173                 local params_string      = " [%d] = %f,"
174
175                 --ensure easy-to-read formatting doesn't make it through
176                 local route_string     = string.gsub(route_string, "[\n\t]", "")
177                 local group_string     = string.gsub(group_string, "[\n\t]", "")
178                 local processor_string = string.gsub(processor_string, "[\n\t]", "")
179
180                 local sel = Editor:get_selection ()
181                 local groups_to_write = {}
182                 local i = 0
183
184                 local tracks = Session:get_routes()
185
186                 if selected then tracks = sel.tracks:routelist() end
187
188                 for r in tracks:iter() do
189                         local group = route_group_interrogate(r)
190                         if group then
191                                 local already_there = false
192                                 for _, v in pairs(groups_to_write) do
193                                         if group == v then
194                                                 already_there = true
195                                         end
196                                 end
197                                 if not(already_there) then
198                                         groups_to_write[#groups_to_write + 1] = group
199                                 end
200                         end
201                 end
202
203                 for _, g in pairs(groups_to_write) do
204                         local tmp_str = ""
205                         for t in g:route_list():iter() do
206                                 tmp_str = tmp_str .. string.format(group_route_string, i, t:to_stateful():id():to_s())
207                                 i = i + 1
208                         end
209                         local group_str = string.format(
210                                 group_string,
211                                 g:to_stateful():id():to_s(),
212                                 g:name(),
213                                 tmp_str
214                         )
215
216                         file = io.open(path, "a")
217                         file:write(group_str, "\r\n")
218                         file:close()
219                 end
220
221                 for r in tracks:iter() do
222                         if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
223
224                         local order = ARDOUR.ProcessorList()
225                         local x = 0
226                         repeat
227                                 local proc = r:nth_processor(x)
228                                 if not proc:isnil() then
229                                         order:push_back(proc)
230                                 end
231                                 x = x + 1
232                         until proc:isnil()
233
234                         local route_group = route_group_interrogate(r)
235                         if route_group then route_group = route_group:name() else route_group = "" end
236                         local rid = r:to_stateful():id():to_s()
237                         local pan = r:pan_azimuth_control()
238                         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
239
240                         local order_nmbr = 0
241                         local tmp_order_str, tmp_cache_str = "", ""
242                         for p in order:iter() do
243                                 local pid = p:to_stateful():id():to_s()
244                                 if not(string.find(p:display_name(), "latcomp")) then
245                                         tmp_order_str = tmp_order_str .. string.format(proc_order_string, order_nmbr, pid)
246                                         tmp_cache_str = tmp_cache_str .. string.format(proc_cache_string, pid, p:display_name())
247                                 end
248                                 order_nmbr = order_nmbr + 1
249                         end
250
251                         local route_str = string.format(
252                                         route_string,
253                                         rid,
254                                         r:name(),
255                                         r:gain_control():get_value(),
256                                         r:trim_control():get_value(),
257                                         tostring(pan),
258                                         r:muted(),
259                                         r:soloed(),
260                                         tmp_order_str,
261                                         tmp_cache_str,
262                                         route_groupid_interrogate(r),
263                                         route_group
264                                 )
265
266                         file = io.open(path, "a")
267                         file:write(route_str, "\n")
268                         file:close()
269
270                         local i = 0
271                         while true do
272                                 local params = {}
273                                 local proc = r:nth_plugin (i)
274                                 if proc:isnil () then break end
275                                 local active = proc:active()
276                                 local id = proc:to_stateful():id():to_s()
277                                 local plug = proc:to_insert ():plugin (0)
278                                 local n = 0 -- count control-ports
279                                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
280                                         if plug:parameter_is_control (j) then
281                                                 local label = plug:parameter_label (j)
282                                                 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
283                                                         local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
284                                                         local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
285                                                         --print(r:name(), "->", proc:display_name(), label, val)
286                                                         params[n] = val
287                                                 end
288                                                 n = n + 1
289                                         end
290                                 end
291                                 i = i + 1
292
293                                 local tmp_params_str = ""
294                                 for k, v in pairs(params) do
295                                         tmp_params_str = tmp_params_str .. string.format(params_string, k, v)
296                                 end
297
298                                 local proc_str = string.format(
299                                                 processor_string,
300                                                 id,
301                                                 proc:display_name(),
302                                                 r:name(),
303                                                 r:to_stateful():id():to_s(),
304                                                 tmp_params_str,
305                                                 active
306                                         )
307                                 file = io.open(path, "a")
308                                 file:write(proc_str, "\n")
309                                 file:close()
310                         end
311                         ::nextroute::
312                 end
313         end
314
315         local store_options = {
316                 { type = "label",    col=0, colspan=1, align="left", title = "Settings name:" },
317                 { type = "entry",    col=1, colspan=1, align="right" , key = "filename", default = Session:name(), title=""},
318                 { type = "label",    col=0, colspan=1, align="left", title = "Selected Tracks Only:" },
319                 { type = "checkbox", col=1, colspan=1, align="right",  key = "selected", default = false, title = ""},
320                 { type = "hseparator", title="", col=0, colspan = 3},
321                 { type = "label",    col=0, colspan=1, align="left", title = "Store Settings:" },
322                 {
323                         type = "radio",  col=1, colspan=3, align="right", key = "store-dir", title = "", values =
324                         {
325                                 ['Global (accessible from any session)'] = 1, ['Locally (this session only)'] = 2
326                         },
327                         default = 'Locally (this session only)'
328                 },
329                 --{ type = "label", col=0, colspan=2, align="left", title = ''},
330                 --{ type = "label", col=0, colspan=2, align="left", title = "Global Path: " .. global_path},
331                 --{ type = "label", col=0, colspan=2, align="left", title = "Local Path: "  .. local_path},
332         }
333
334         local global_ok, local_ok = setup_paths()
335
336         if global_ok and local_ok then
337                 local rv = LuaDialog.Dialog("Store Mixer Settings:", store_options):run()
338
339                 if not(rv) then return end
340
341                 local filename = rv['filename']
342                 if rv['store-dir'] == 1 then
343                         local store_path = ARDOUR.LuaAPI.build_filename(global_path, string.format("%s-%s.lua", filename, whoami()))
344                         local selected = rv['selected']
345                         mark_tracks(selected, store_path)
346                 end
347
348                 if rv['store-dir'] == 2 then
349                         local store_path = ARDOUR.LuaAPI.build_filename(local_path, string.format("%s-%s.lua", filename, whoami()))
350                         print(store_path)
351                         local selected = rv['selected']
352                         mark_tracks(selected, store_path)
353                 end
354         end
355
356 end end