[orx-midi] Add MidiConsole extension, translate note on w/ velocity=0 events to note off
This commit is contained in:
@@ -1,16 +1,15 @@
|
|||||||
import org.openrndr.application
|
import org.openrndr.application
|
||||||
|
import org.openrndr.extra.midi.MidiConsole
|
||||||
import org.openrndr.extra.midi.MidiDeviceDescription
|
import org.openrndr.extra.midi.MidiDeviceDescription
|
||||||
import org.openrndr.extra.midi.MidiTransceiver
|
import org.openrndr.extra.midi.MidiTransceiver
|
||||||
import org.openrndr.extra.midi.bindMidiControl
|
|
||||||
import org.openrndr.extra.parameters.DoubleParameter
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
application {
|
application {
|
||||||
program {
|
program {
|
||||||
//MidiDeviceDescription.list().forEach { println(it.toString()) }
|
MidiDeviceDescription.list().forEach { println(it.toString()) }
|
||||||
val midi = MidiTransceiver.fromDeviceVendor(this,"MIDI2x2 [hw:3,0,0]", "ALSA (http://www.alsa-project.org)")
|
val midi = MidiTransceiver.fromDeviceVendor(this,"Launchpad [hw:4,0,0]", "ALSA (http://www.alsa-project.org)")
|
||||||
midi.controlChanged.listen {
|
extend(MidiConsole()) {
|
||||||
println(it)
|
register(midi)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
61
orx-jvm/orx-midi/src/main/kotlin/MidiConsole.kt
Normal file
61
orx-jvm/orx-midi/src/main/kotlin/MidiConsole.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -124,14 +124,27 @@ class MidiTransceiver(program: Program, val receiverDevice: MidiDevice?, val tra
|
|||||||
override fun send(message: MidiMessage, timeStamp: Long) {
|
override fun send(message: MidiMessage, timeStamp: Long) {
|
||||||
val cmd = message.message
|
val cmd = message.message
|
||||||
val channel = (cmd[0].toInt() and 0xff) and 0x0f
|
val channel = (cmd[0].toInt() and 0xff) and 0x0f
|
||||||
|
val velocity = cmd[2].toInt() and 0xff
|
||||||
when ((cmd[0].toInt() and 0xff) and 0xf0) {
|
when ((cmd[0].toInt() and 0xff) and 0xf0) {
|
||||||
ShortMessage.NOTE_ON -> noteOn.trigger(
|
|
||||||
MidiEvent.noteOn(
|
|
||||||
channel,
|
|
||||||
cmd[1].toInt() and 0xff,
|
ShortMessage.NOTE_ON -> if (velocity > 0) {
|
||||||
cmd[2].toInt() and 0xff
|
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(
|
ShortMessage.NOTE_OFF -> noteOff.trigger(
|
||||||
MidiEvent.noteOff(
|
MidiEvent.noteOff(
|
||||||
|
|||||||
Reference in New Issue
Block a user