1 ardour { ["type"] = "EditorAction", name = "MIDI CC to Plugin Automation",
3 author = "Ardour Team",
4 description = [[Parse a given MIDI control changes (CC) from all selected MIDI regions and convert them into plugin parameter automation]]
7 function factory () return function ()
8 -- find possible target parameters, collect them in a nested table
9 -- [track-name] -> [plugin-name] -> [parameters]
10 -- to allow selection in a dropdown menu
12 local have_entries = false
13 for r in Session:get_routes ():iter () do -- for every track/bus
14 if r:is_monitor () or r:is_auditioner () then goto nextroute end -- skip special routes
16 while true do -- iterate over all plugins on the route
17 local proc = r:nth_plugin (i)
18 if proc:isnil () then break end
19 local plug = proc:to_insert ():plugin (0) -- we know it's a plugin-insert (we asked for nth_plugin)
20 local n = 0 -- count control-ports
21 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
22 if plug:parameter_is_control (j) then
23 local label = plug:parameter_label (j)
24 if plug:parameter_is_input (j) and label ~= "hidden" and label:sub (1,1) ~= "#" then
25 local nn = n --local scope for return value function
26 -- create table parents only if needed (if there's at least one parameter)
27 if not targets [r:name ()] then targets [r:name ()] = {} end
28 -- TODO handle ambiguity if there are 2 plugins with the same name on the same track
29 if not targets [r:name ()][proc:display_name ()] then targets [r:name ()][proc:display_name ()] = {} end
30 -- we need 2 return values: the plugin-instance and the parameter-id, so we use a table (associative array)
31 -- however, we cannot directly use a table: the dropdown menu would expand it as another sub-menu.
32 -- so we produce a function that will return the table.
33 targets [r:name ()][proc:display_name ()][label] = function () return {["p"] = proc, ["n"] = nn} end
44 -- bail out if there are no parameters
45 if not have_entries then
46 LuaDialog.Message ("CC to Plugin Automation", "No Plugins found", LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run ()
52 -- create a dialog, ask user which MIDI-CC to map and to what parameter
53 local dialog_options = {
54 { type = "heading", title = "MIDI CC Source", align = "left" },
55 { type = "number", key = "channel", title = "Channel", min = 1, max = 16, step = 1, digits = 0 },
56 { type = "number", key = "ccparam", title = "CC Parameter", min = 0, max = 127, step = 1, digits = 0 },
57 { type = "heading", title = "Target Track and Plugin", align = "left"},
58 { type = "dropdown", key = "param", title = "Target Parameter", values = targets }
60 local rv = LuaDialog.Dialog ("Select Taget", dialog_options):run ()
62 targets = nil -- drop references (the table holds shared-pointer references to all plugins)
63 collectgarbage () -- and release the references immediately
65 if not rv then return end -- user cancelled
67 -- parse user response
69 assert (type (rv["param"]) == "function")
70 local midi_channel = rv["channel"] - 1 -- MIDI channel 0..15
71 local cc_param = rv["ccparam"]
72 local pp = rv["param"]() -- evaluate function, retrieve table {["p"] = proc, ["n"] = nn}
73 local al, _, pd = ARDOUR.LuaAPI.plugin_automation (pp["p"], pp["n"])
74 rv = nil -- drop references
75 assert (not al:isnil ())
76 assert (midi_channel >= 0 and midi_channel < 16)
77 assert (cc_param >= 0 and cc_param < 128)
80 local add_undo = false
81 Session:begin_reversible_command ("CC to Automation")
82 local before = al:get_state () -- save previous state (for undo)
83 al:clear_list () -- clear target automation-list
85 -- for all selected MIDI regions
86 local sel = Editor:get_selection ()
87 for r in sel.regions:regionlist ():iter () do
88 local mr = r:to_midiregion ()
89 if mr:isnil () then goto next end
91 -- get list of MIDI-CC events for the given channel and parameter
92 local ec = mr:control (Evoral.Parameter (ARDOUR.AutomationType.MidiCCAutomation, midi_channel, cc_param), false)
93 if ec:isnil () then goto next end
94 if ec:list ():events ():size () == 0 then goto next end
96 -- MIDI events are timestamped in "bar-beat" units, we need to convert those
97 -- using the tempo-map, relative to the region-start
98 local bfc = ARDOUR.DoubleBeatsFramesConverter (Session:tempo_map (), r:start ())
100 -- iterate over CC-events
101 for av in ec:list ():events ():iter () do
102 -- re-scale event to target range
103 local val = pd.lower + (pd.upper - pd.lower) * av.value / 127
104 -- and add it to the target-parameter automation-list
105 al:add (r:position () - r:start () + bfc:to (av.when), val, false, true)
113 local after = al:get_state ()
114 Session:add_command (al:memento_command (before, after))
115 Session:commit_reversible_command (nil)
117 Session:abort_reversible_command ()
118 LuaDialog.Message ("CC to Plugin Automation", "No data was converted. Was a MIDI-region with CC-automation selected? ", LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run ()
123 function icon (params) return function (ctx, width, height, fg)
124 local txt = Cairo.PangoLayout (ctx, "ArdourMono ".. math.ceil (width * .45) .. "px")
125 txt:set_text ("CC\nPA")
126 local tw, th = txt:get_pixel_size ()
127 ctx:set_source_rgba (ARDOUR.LuaAPI.color_to_rgba (fg))
128 ctx:move_to (.5 * (width - tw), .5 * (height - th))
129 txt:show_in_cairo_context (ctx)