1ardour { ["type"] = "EditorAction", name = "MIDI CC to Plugin Automation",
2	license     = "MIT",
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]]
5}
6
7function 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
11	local targets = {}
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
15		local i = 0
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
34						have_entries = true
35					end
36					n = n + 1
37				end
38			end
39			i = i + 1
40		end
41		::nextroute::
42	end
43
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 ()
47		return
48	end
49
50	-- create a dialog, ask user which MIDI-CC to map and to what parameter
51	local dialog_options = {
52		{ type = "heading", title = "MIDI CC Source", align = "left" },
53		{ type = "number", key = "channel", title = "Channel",  min = 1, max = 16, step = 1, digits = 0 },
54		{ type = "number", key = "ccparam", title = "CC Parameter",  min = 0, max = 127, step = 1, digits = 0 },
55		{ type = "heading", title = "Target Track and Plugin", align = "left"},
56		{ type = "dropdown", key = "param", title = "Target Parameter", values = targets }
57	}
58	local rv = LuaDialog.Dialog ("Select Taget", dialog_options):run ()
59
60	targets = nil -- drop references (the table holds shared-pointer references to all plugins)
61	collectgarbage () -- and release the references immediately
62
63	if not rv then return end -- user cancelled
64
65	-- parse user response
66
67	assert (type (rv["param"]) == "function")
68	local midi_channel = rv["channel"] - 1 -- MIDI channel 0..15
69	local cc_param = rv["ccparam"]
70	local pp = rv["param"]() -- evaluate function, retrieve table {["p"] = proc, ["n"] = nn}
71	local al, _, pd = ARDOUR.LuaAPI.plugin_automation (pp["p"], pp["n"])
72	rv = nil -- drop references
73	assert (not al:isnil ())
74	assert (midi_channel >= 0 and midi_channel < 16)
75	assert (cc_param >= 0 and cc_param < 128)
76
77	-- all systems go
78	local add_undo = false
79	Session:begin_reversible_command ("CC to Automation")
80	local before = al:get_state () -- save previous state (for undo)
81	al:clear_list () -- clear target automation-list
82
83	-- for all selected MIDI regions
84	local sel = Editor:get_selection ()
85	for r in sel.regions:regionlist ():iter () do
86		local mr = r:to_midiregion ()
87		if mr:isnil () then goto next end
88
89		-- get list of MIDI-CC events for the given channel and parameter
90		local ec = mr:control (Evoral.Parameter (ARDOUR.AutomationType.MidiCCAutomation, midi_channel, cc_param), false)
91		if ec:isnil () then goto next end
92		if ec:list ():events ():size () == 0 then goto next end
93
94		-- MIDI events are timestamped in "bar-beat" units, we need to convert those
95		-- using the tempo-map, relative to the region-start
96		local bfc = ARDOUR.DoubleBeatsSamplesConverter (Session:tempo_map (), r:start ())
97
98		-- iterate over CC-events
99		for av in ec:list ():events ():iter () do
100			-- re-scale event to target range
101			local val = pd.lower + (pd.upper - pd.lower) * av.value / 127
102			-- and add it to the target-parameter automation-list
103			al:add (r:position () - r:start () + bfc:to (av.when), val, false, true)
104			add_undo = true
105		end
106		::next::
107	end
108
109	-- save undo
110	if add_undo then
111		local after = al:get_state ()
112		Session:add_command (al:memento_command (before, after))
113		Session:commit_reversible_command (nil)
114	else
115		Session:abort_reversible_command ()
116		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 ()
117	end
118end end
119
120function icon (params) return function (ctx, width, height, fg)
121	local txt = Cairo.PangoLayout (ctx, "ArdourMono ".. math.ceil (height / 3) .. "px")
122	txt:set_text ("CC\nPA")
123	local tw, th = txt:get_pixel_size ()
124	ctx:set_source_rgba (ARDOUR.LuaAPI.color_to_rgba (fg))
125	ctx:move_to (.5 * (width - tw), .5 * (height - th))
126	txt:show_in_cairo_context (ctx)
127end end
128