From 061f005ac3101797e1d3b0bd5452cf6132423717 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Mon, 24 Apr 2017 04:15:16 +0200 Subject: [PATCH] Add example script to convert MIDI-CC to Plugin Automation --- scripts/_midi_cc_to_automation.lua | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 scripts/_midi_cc_to_automation.lua diff --git a/scripts/_midi_cc_to_automation.lua b/scripts/_midi_cc_to_automation.lua new file mode 100644 index 0000000000..2d97b2c975 --- /dev/null +++ b/scripts/_midi_cc_to_automation.lua @@ -0,0 +1,47 @@ +ardour { ["type"] = "Snippet", name = "MIDI CC to Plugin Automation" } + +function factory () return function () + -- target automation lane: a plugin parameter on a track + local tgt = Session:route_by_name('Audio') -- get track + assert (not tgt:isnil ()) + -- get AutomationList, ControlList and ParameterDescriptor + -- of the first plugin's 2nd parameter + local al, _, pd = ARDOUR.LuaAPI.plugin_automation (tgt:nth_plugin (0), 1) + + -- Source MIDI CC parameter + local midi_channel = 0 -- 0..15 + local cc_param = 0x56 + + local add_undo = false + Session:begin_reversible_command ("CC to Automation") + local before = al:get_state() + al:clear_list () + + -- for all selected MIDI regions + local sel = Editor:get_selection () + for r in sel.regions:regionlist ():iter () do + local mr = r:to_midiregion () + if mr:isnil () then goto next end + + local bfc = ARDOUR.DoubleBeatsFramesConverter (Session:tempo_map (), r:position ()) + local ec = mr:control (Evoral.Parameter (ARDOUR.AutomationType.MidiCCAutomation, midi_channel, cc_param), false) + if ec:isnil () then goto next end + if ec:list ():events ():size() == 0 then goto next end + + for av in ec:list ():events ():iter () do + local val = pd.lower + (pd.upper - pd.lower) * av.value / 127 + al:add (bfc:to (av.when), val, false, true) + add_undo = true + end + ::next:: + end + + -- save undo + if add_undo then + local after = al:get_state() + Session:add_command (al:memento_command(before, after)) + Session:commit_reversible_command (nil) + else + Session:abort_reversible_command () + end +end end