[orx-midi] Add MidiConsole extension, translate note on w/ velocity=0 events to note off

This commit is contained in:
Edwin Jakobs
2023-04-25 18:08:20 +02:00
parent f3235cab94
commit be9c7dc51a
3 changed files with 85 additions and 12 deletions

View File

@@ -1,16 +1,15 @@
import org.openrndr.application
import org.openrndr.extra.midi.MidiConsole
import org.openrndr.extra.midi.MidiDeviceDescription
import org.openrndr.extra.midi.MidiTransceiver
import org.openrndr.extra.midi.bindMidiControl
import org.openrndr.extra.parameters.DoubleParameter
fun main() {
application {
program {
//MidiDeviceDescription.list().forEach { println(it.toString()) }
val midi = MidiTransceiver.fromDeviceVendor(this,"MIDI2x2 [hw:3,0,0]", "ALSA (http://www.alsa-project.org)")
midi.controlChanged.listen {
println(it)
MidiDeviceDescription.list().forEach { println(it.toString()) }
val midi = MidiTransceiver.fromDeviceVendor(this,"Launchpad [hw:4,0,0]", "ALSA (http://www.alsa-project.org)")
extend(MidiConsole()) {
register(midi)
}
}
}

View File

@@ -0,0 +1,61 @@
package org.openrndr.extra.midi
import org.openrndr.Extension
import org.openrndr.Program
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.Drawer
import org.openrndr.draw.loadFont
import org.openrndr.math.Vector2
import org.openrndr.shape.Rectangle
import java.io.File
class MidiConsole: Extension {
override var enabled = true
var box = Rectangle(0.0, 0.0, 130.0, 200.0)
val messages = mutableListOf<String>()
var historySize = 2
val demoFont = File("demo-data/fonts/IBMPlexMono-Regular.ttf").exists()
fun register(transceiver: MidiTransceiver) {
transceiver.controlChanged.listen {
synchronized(messages) {
messages.add("CC ${it.control}: ${it.value}")
if (messages.size > historySize) {
messages.removeAt(0)
}
}
}
transceiver.noteOn.listen {
synchronized(messages) {
messages.add("NOTE ON ${it.note}: ${it.velocity}")
if (messages.size > historySize) {
messages.removeAt(0)
}
}
}
transceiver.noteOff.listen {
synchronized(messages) {
messages.add("NOTE OFF ${it.note}")
if (messages.size > historySize) {
messages.removeAt(0)
}
}
}
}
override fun afterDraw(drawer: Drawer, program: Program) {
drawer.defaults()
box = Rectangle(drawer.width - box.width, 0.0, box.width, drawer.height*1.0)
val positions = List(messages.size) { index ->
Vector2(box.x, box.y + index * 16.0 + 16.0)
}
if (demoFont) {
drawer.fontMap = loadFont("demo-data/fonts/IBMPlexMono-Regular.ttf", 16.0)
}
drawer.fill = ColorRGBa.WHITE
drawer.texts(messages, positions)
}
}

View File

@@ -124,14 +124,27 @@ class MidiTransceiver(program: Program, val receiverDevice: MidiDevice?, val tra
override fun send(message: MidiMessage, timeStamp: Long) {
val cmd = message.message
val channel = (cmd[0].toInt() and 0xff) and 0x0f
val velocity = cmd[2].toInt() and 0xff
when ((cmd[0].toInt() and 0xff) and 0xf0) {
ShortMessage.NOTE_ON -> noteOn.trigger(
MidiEvent.noteOn(
channel,
cmd[1].toInt() and 0xff,
cmd[2].toInt() and 0xff
ShortMessage.NOTE_ON -> if (velocity > 0) {
noteOn.trigger(
MidiEvent.noteOn(
channel,
cmd[1].toInt() and 0xff,
velocity
)
)
)
} else {
noteOff.trigger(
MidiEvent.noteOff(
channel,
cmd[1].toInt() and 0xff
)
)
}
ShortMessage.NOTE_OFF -> noteOff.trigger(
MidiEvent.noteOff(