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 target parameters
10 local have_entries = false
11 for r in Session:get_routes():iter() do -- for every track/bus
13 while 1 do -- iterate over all plugins on the route
14 local proc = r:nth_plugin (i)
15 if proc:isnil () then break end
16 local plug = proc:to_insert ():plugin (0)
18 for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
19 if plug:parameter_is_control(j) then
20 if plug:parameter_is_input(j) then
22 if not targets [r:name ()] then targets [r:name ()] = {} end
23 if not targets [r:name ()][proc:display_name ()] then targets [r:name ()][proc:display_name ()] = {} end
24 targets [r:name ()][proc:display_name ()][plug:parameter_label(j)] = function () return {["p"] = proc, ["n"] = nn} end
34 -- bail out if there are no parameters
35 if not have_entries then
36 LuaDialog.Message ("CC to Plugin Automation", "No Plugins found", LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run()
41 -- create a dialog, ask user which MIDI-CC to map and to what parameter
42 local dialog_options = {
43 { type = "heading", title = "MIDI CC Source", align = "left" },
44 { type = "number", key = "channel", title = "Channel", min = 1, max = 16, step = 1, digits = 0 },
45 { type = "number", key = "ccparam", title = "CC Parameter", min = 0, max = 127, step = 1, digits = 0 },
46 { type = "heading", title = "Target Track and Plugin", align = "left"},
47 { type = "dropdown", key = "param", title = "Target Parameter", values = targets }
49 local od = LuaDialog.Dialog ("Select Taget", dialog_options)
53 od = nil collectgarbage ()
57 -- parse user response
59 local midi_channel = rv["channel"] - 1
60 local cc_param = rv["ccparam"]
61 local pp = rv["param"]()
62 local al, _, pd = ARDOUR.LuaAPI.plugin_automation (pp["p"], pp["n"])
63 od = nil collectgarbage ()
65 assert (midi_channel >= 0 and midi_channel < 16)
66 assert (cc_param >= 0 and cc_param < 128)
69 local add_undo = false
70 Session:begin_reversible_command ("CC to Automation")
71 local before = al:get_state()
74 -- for all selected MIDI regions
75 local sel = Editor:get_selection ()
76 for r in sel.regions:regionlist ():iter () do
77 local mr = r:to_midiregion ()
78 if mr:isnil () then goto next end
80 local bfc = ARDOUR.DoubleBeatsFramesConverter (Session:tempo_map (), r:position ())
81 local ec = mr:control (Evoral.Parameter (ARDOUR.AutomationType.MidiCCAutomation, midi_channel, cc_param), false)
82 if ec:isnil () then goto next end
83 if ec:list ():events ():size() == 0 then goto next end
85 for av in ec:list ():events ():iter () do
86 local val = pd.lower + (pd.upper - pd.lower) * av.value / 127
87 al:add (bfc:to (av.when), val, false, true)
95 local after = al:get_state()
96 Session:add_command (al:memento_command(before, after))
97 Session:commit_reversible_command (nil)
99 Session:abort_reversible_command ()
100 LuaDialog.Message ("CC to Plugin Automation", "No data was converted. Was a MIDI-region selected?", LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run()
106 function icon (params) return function (ctx, width, height, fg)
107 local txt = Cairo.PangoLayout (ctx, "ArdourMono ".. math.ceil(width * .45) .. "px")
108 txt:set_text ("CC\nPA")
109 local tw, th = txt:get_pixel_size ()
110 ctx:set_source_rgba (ARDOUR.LuaAPI.color_to_rgba (fg))
111 ctx:move_to (.5 * (width - tw), .5 * (height - th))
112 txt:show_in_cairo_context (ctx)