I said, LESS ugly, not more :)
[ardour.git] / scripts / _store_recall_mixer.lua
1 ardour {
2     ["type"] = "EditorAction",
3     name = "Mixer Store",
4     author = "Mixbus Lua Taskforce",
5     description = [[]]
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 curent 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 i = 0
29         while true do
30             local params = {}
31             local proc_str, params_str = "", ""
32             local proc = r:nth_plugin (i)
33             if proc:isnil () then break end
34             local active = proc:active()
35             local id = proc:to_stateful():id():to_s()
36             local plug = proc:to_insert ():plugin (0)
37                 local n = 0 -- count control-ports
38                 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
39                     if plug:parameter_is_control (j) then
40                         local label = plug:parameter_label (j)
41                         if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
42                             local _, _, pd = ARDOUR.LuaAPI.plugin_automation(proc, n)
43                             local val = ARDOUR.LuaAPI.get_processor_param(proc, j, true)
44                             if not(val == pd.normal) then
45                                 params[n] = val
46                             end
47                         end
48                         n = n + 1
49                     end
50                 end
51                 i = i + 1
52             for k, v in pairs(params) do
53                 params_str = params_str .. "[".. k .."] = " .. v .. ","
54             end
55             proc_str = "instance = {plugin_id = " .. id .. ", parameters = {" .. params_str .. "}, active = " .. tostring(active) .. "}"
56             file = io.open(path, "a")
57             file:write(proc_str, "\r\n")
58             file:close()
59         end
60
61         local route_str, proc_order_str = "", ""
62         local rid = r:to_stateful():id():to_s()
63         local pan = r:pan_azimuth_control()
64         if pan:isnil() then pan = false else pan = pan:get_value() end --sometimes a route doesn't have pan, like the master.
65
66         local on = 0
67         for p in order:iter() do
68             local pid = p:to_stateful():id():to_s()
69             proc_order_str = proc_order_str .. "[" .. on .. "] = " .. pid ..","
70             on = on + 1
71         end
72
73         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 .."}" .. "}"
74         file = io.open(path, "a")
75         file:write(route_str, "\r\n")
76         file:close()
77         ::nextroute::
78
79         end
80     end
81
82     function recall()
83         local file = io.open(path, "r")
84         assert(file, "File not found!")
85         for l in file:lines() do
86
87         local plugin, route = false, false
88         local f = load(l)
89         f ()
90
91         if instance["route_id"]  ~= nil then route = true end
92         if instance["plugin_id"] ~= nil then plugin = true end
93
94         if route then
95             local old_order = ARDOUR.ProcessorList()
96             for k, v in pairs(instance["order"]) do
97                 local proc = Session:processor_by_id(PBD.ID(v))
98                 if proc:isnil() then goto nextline end
99                 old_order:push_back(proc)
100             end
101             local rid = PBD.ID(instance["route_id"])
102             local rt = Session:route_by_id(rid)
103             if rt:isnil() then goto nextline end
104             local gc, tc, pc, act = instance["gain_control"], instance["trim_control"], instance["pan_control"], instance["active"]
105             rt:gain_control():set_value(gc, 1)
106             rt:trim_control():set_value(tc, 1)
107             if pc ~= false then rt:pan_azimuth_control():set_value(pc, 1) end
108             rt:reorder_processors(old_order, nil)
109          end
110
111         if plugin then
112             local id = PBD.ID(instance["plugin_id"])
113             local proc = Session:processor_by_id(id)
114             if proc:isnil() then goto nextline end
115             for k, v in pairs(instance["parameters"]) do
116                 ARDOUR.LuaAPI.set_processor_param(proc, k, v)
117             end
118         end
119         ::nextline::
120         instance = nil
121         end
122     end
123
124     local dialog_options = {
125         { type = "label", colspan= 10, title = "" },
126         {type = "radio",  colspan= 10, key = "select", title = "", values ={ ["1. Mark"] = "mark", ["2. Recall"] = "recall" }, default = "1. Mark"},
127         { type = "label", colspan= 10, title = "" },
128     }
129
130     local rv = LuaDialog.Dialog("Mixer Store:", dialog_options):run()
131     assert(rv, 'Dialog box was cancelled or is ' .. type(rv))
132     local c = rv["select"]
133     if c == "mark" then mark() end
134     if c == "recall" then recall() end
135
136 end end