summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2017-04-24 04:15:16 +0200
committerRobin Gareus <robin@gareus.org>2017-04-24 04:21:25 +0200
commit061f005ac3101797e1d3b0bd5452cf6132423717 (patch)
tree057cc2ee3599afade209f2d57543067ac027c5ca /scripts
parentb7b1ccc8b611fed8942c9c66b727a075b1cd0e47 (diff)
Add example script to convert MIDI-CC to Plugin Automation
Diffstat (limited to 'scripts')
-rw-r--r--scripts/_midi_cc_to_automation.lua47
1 files changed, 47 insertions, 0 deletions
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