summaryrefslogtreecommitdiff
path: root/scripts/midi_remap.lua
diff options
context:
space:
mode:
authorAlby M <1473644+Linux-cpp-lisp@users.noreply.github.com>2019-02-18 00:19:51 -0600
committerRobin Gareus <robin@gareus.org>2019-02-19 15:38:09 +0100
commitef431413011841e869f3f31906fc146fc8c72258 (patch)
tree836d02317a60a6425d3e0fcc4b7372982ee2db6e /scripts/midi_remap.lua
parent125a38bdbf973b469f59d35ddf1087beda46b784 (diff)
Added MIDI Remap Lua DSP plugin
Diffstat (limited to 'scripts/midi_remap.lua')
-rw-r--r--scripts/midi_remap.lua103
1 files changed, 103 insertions, 0 deletions
diff --git a/scripts/midi_remap.lua b/scripts/midi_remap.lua
new file mode 100644
index 0000000000..ca1f0a9bbe
--- /dev/null
+++ b/scripts/midi_remap.lua
@@ -0,0 +1,103 @@
+ardour {
+ ["type"] = "dsp",
+ name = "MIDI Remap Notes",
+ category = "Utility",
+ license = "MIT",
+ author = "Alby Musaelian",
+ description = [[Map arbitrary MIDI notes to others. Affects Note On/Off and polyphonic key pressure.]]
+}
+
+-- The number of remapping pairs to allow. Increasing this (at least in theory)
+-- decreases performace, so it's set fairly low as a default. The user can
+-- increase this if they have a need to.
+N_REMAPINGS = 10
+
+function dsp_ioconfig ()
+ return { { midi_in = 1, midi_out = 1, audio_in = 0, audio_out = 0}, }
+end
+
+function dsp_params ()
+ local note_names = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}
+
+ local map_scalepoints = {}
+ for note=0,127 do
+ local octave = -5 + math.floor(note / 12)
+ local name = note_names[(note % 12) + 1]
+ map_scalepoints[string.format("%03d (%s%d)", note, name, octave)] = note
+ end
+
+ local map_params = {}
+
+ i = 1
+ for mapnum = 1,N_REMAPINGS do
+ -- From and to
+ for _,name in pairs({"| #" .. mapnum .. " Map note", "|__ to"}) do
+ map_params[i] = {
+ ["type"] = "input",
+ name = name,
+ min = 0,
+ max = 127,
+ default = 0,
+ integer = true,
+ enum = true,
+ scalepoints = map_scalepoints
+ }
+ i = i + 1
+ end
+ end
+
+ return map_params
+end
+
+function dsp_init(samplerate)
+ -- Init translation table, reserve memory
+ translation_table = {}
+ for i = 0,127 do
+ translation_table[i] = i
+ end
+end
+
+
+function dsp_run (_, _, n_samples)
+ assert (type(midiin) == "table")
+ assert (type(midiout) == "table")
+ local cnt = 1;
+
+ function tx_midi (time, data)
+ midiout[cnt] = {}
+ midiout[cnt]["time"] = time;
+ midiout[cnt]["data"] = data;
+ cnt = cnt + 1;
+ end
+
+ -- We build the translation table every buffer because, as far as I can tell,
+ -- there's no way to only rebuild it when the parameters have changed.
+ -- As a result, it has to be updated every buffer for the parameters to have
+ -- any effect.
+
+ -- Restore translation table
+ for i = 0,127 do
+ translation_table[i] = i
+ end
+ -- Update it
+ local ctrl = CtrlPorts:array()
+ for i=1,N_REMAPINGS*2,2 do
+ translation_table[ctrl[i]] = ctrl[i + 1]
+ end
+
+ -- for each incoming midi event
+ for _,b in pairs (midiin) do
+ local t = b["time"] -- t = [ 1 .. n_samples ]
+ local d = b["data"] -- get midi-event
+ local event_type
+ if #d == 0 then event_type = -1 else event_type = d[1] >> 4 end
+
+ if (#d == 3) and (event_type == 9 or event_type == 8 or event_type == 10) then -- note on, note off, poly. afterpressure
+ -- Do the mapping - 2 is note byte for these types
+ d[2] = translation_table[d[2]]
+ tx_midi (t, d)
+ else
+ tx_midi (t, d)
+ end
+ end
+end