Elaborate MIDI rewrite example script

This commit is contained in:
Robin Gareus 2020-10-14 01:56:36 +02:00
parent 1698928f3d
commit 3cae10ab40
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
1 changed files with 27 additions and 8 deletions

View File

@ -13,18 +13,37 @@ function factory ()
for p in t[2]:iter () do
if not p:receives_input () then goto next end
if not p:name () == "MIDI/midi_in 1" then goto next end
-- search-filter port
if not p:name () == "MIDITrackName/midi_in 1" then
goto next -- skip
end
midiport = p:to_midiport ()
assert (not midiport:isnil ())
mb = midiport:get_midi_buffer (n_samples);
-- ensure that the port is-a https://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:MidiPort
assert (not p:to_midiport ():isnil ())
events = mb:table() -- copy event list into lua table
mb:silence (n_samples, 0); -- clear existing buffer
-- https://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:MidiBuffer
local mb = p:to_midiport ():get_midi_buffer (n_samples)
-- When an I/O port is connected (source -> sink), the
-- buffer is shared. The MidiBuffer is in fact the buffer
-- from the backend. Changing events in this buffer affects
-- all destinations that use this buffer as source.
local events = mb:table() -- *copy* event list to a Lua table
mb:silence (n_samples, 0) -- clear existing buffer
-- now iterate over events that were in the buffer
for _,e in pairs (events) do
-- e is-a http://manual.ardour.org/lua-scripting/class_reference/#Evoral:MidiEvent
e:set_channel (2)
-- e is-a http://manual.ardour.org/lua-scripting/class_reference/#Evoral:Event
if e:size () == 3 then
-- found a 3 byte event
local buffer = e:buffer():array()
local ev_type = buffer[1] >> 4 -- get MIDI event type (upper 4 bits)
if ev_type == 8 or ev_type == 9 then -- note on or note off event
buffer[1] = (buffer[1] & 0xf0) + 2 -- change the MIDI channel to "2"
buffer[3] = buffer[3] >> 1 -- reduce the velocity by half
end
end
-- finally place the event back into the Port's MIDI buffers
mb:push_event (e)
end