diff options
author | Robin Gareus <robin@gareus.org> | 2016-05-29 20:36:16 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2016-05-29 20:36:32 +0200 |
commit | 39e818a03afdf76a123bb802f020008276646708 (patch) | |
tree | 79e73fd6399cd415cc11c1540b5a53e10c864d46 /scripts | |
parent | 60200bf92300388d2b0d002559a3db1b544d9705 (diff) |
prototype lua midi generators & filters and port event-rewrite
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/midi_rewite.lua | 36 | ||||
-rw-r--r-- | scripts/midifilter.lua | 43 | ||||
-rw-r--r-- | scripts/midigenerator.lua | 52 | ||||
-rw-r--r-- | scripts/synth1.lua | 6 |
4 files changed, 134 insertions, 3 deletions
diff --git a/scripts/midi_rewite.lua b/scripts/midi_rewite.lua new file mode 100644 index 0000000000..35d2e1ddfc --- /dev/null +++ b/scripts/midi_rewite.lua @@ -0,0 +1,36 @@ +ardour { + ["type"] = "session", + name = "Rewrite Midi", + license = "MIT", + author = "Robin Gareus", + email = "robin@gareus.org", + site = "http://gareus.org", + description = [[An example session script preprocesses midi buffers.]] +} + +function factory () + -- this function is called in every process cycle, before processing + return function (n_samples) + _, t = Session:engine ():get_ports (ARDOUR.DataType.midi (), ARDOUR.PortList ()) + 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 + + midiport = p:to_midiport () + assert (not midiport:isnil ()) + mb = midiport:get_midi_buffer (n_samples); + + events = mb:table() -- copy event list into lua table + mb:silence (n_samples, 0); -- clear existing buffer + + for _,e in pairs (events) do + -- e is an http://ardourman/lua-scripting/class_reference/#Evoral:MidiEvent + e:set_channel (2) + mb:push_event (e) + end + + ::next:: + end + end +end diff --git a/scripts/midifilter.lua b/scripts/midifilter.lua new file mode 100644 index 0000000000..ceb115a727 --- /dev/null +++ b/scripts/midifilter.lua @@ -0,0 +1,43 @@ +ardour { + ["type"] = "dsp", + name = "Midi Filter", + category = "Utility", + license = "MIT", + author = "Robin Gareus", + email = "robin@gareus.org", + site = "http://gareus.org", + description = [[An Example Midi Filter for prototyping.]] +} + +function dsp_ioconfig () + return { { audio_in = 0, audio_out = 0}, } +end + +function dsp_has_midi_input () return true end +function dsp_has_midi_output () return true 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 + + -- 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 + + if (#d == 3 and bit32.band (d[1], 240) == 144) then -- note on + tx_midi (t, d) + end + if (#d == 3 and bit32.band (d[1], 240) == 128) then -- note off + tx_midi (t, d) + end + end +end diff --git a/scripts/midigenerator.lua b/scripts/midigenerator.lua new file mode 100644 index 0000000000..e9aa55d280 --- /dev/null +++ b/scripts/midigenerator.lua @@ -0,0 +1,52 @@ +ardour { + ["type"] = "dsp", + name = "Midi Generator", + category = "Utility", + license = "MIT", + author = "Robin Gareus", + email = "robin@gareus.org", + site = "http://gareus.org", + description = [[An Example Midi Generator for prototyping.]] +} + +function dsp_ioconfig () + return { { audio_in = 0, audio_out = 0}, } +end + +function dsp_has_midi_output () return true end + +local tme = 0 -- sample-counter +local seq = 1 -- sequence-step +local spb = 0 -- samples per beat + +local midi_sequence = { + { 0x90, 64, 127 }, + { 0x80, 64, 0 }, +} + +function dsp_init (rate) + local bpm = 120 + spb = rate * 60 / bpm + if spb < 2 then spb = 2 end +end + +function dsp_run (_, _, n_samples) + assert (type(midiout) == "table") + assert (spb > 1) + local m = 1 + + for time = 1,n_samples do -- not very efficient + -- TODO, timestamp the sequence in beats, calc/skip to next event + tme = tme + 1 + + if tme >= spb then + midiout[m] = {} + midiout[m]["time"] = time + midiout[m]["data"] = midi_sequence[seq] + + tme = 0 + m = m + 1 + if seq == #midi_sequence then seq = 1 else seq = seq + 1 end + end + end +end diff --git a/scripts/synth1.lua b/scripts/synth1.lua index 99a5e4d6c8..9d04e29ec1 100644 --- a/scripts/synth1.lua +++ b/scripts/synth1.lua @@ -21,7 +21,7 @@ function dsp_ioconfig () } end -function dsp_midi_input () +function dsp_has_midi_input () return true end @@ -70,8 +70,8 @@ function dsp_run (ins, outs, n_samples) local tme = 1 -- parse midi messages - assert (type(mididata) == "table") -- global table of midi events (for now) - for _,b in pairs (mididata) do + assert (type(midiin) == "table") -- global table of midi events (for now) + for _,b in pairs (midiin) do local t = b["time"] -- t = [ 1 .. n_samples ] -- synth sound until event |