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