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