[orx-midi] program change support (#128)

This commit is contained in:
Byron
2020-06-20 00:52:07 +03:00
committed by GitHub
parent b9401e03d3
commit 9cc2209fc7
3 changed files with 30 additions and 2 deletions

View File

@@ -18,6 +18,11 @@ val dev = MidiTransceiver.fromDeviceVendor("BCR2000 [hw:2,0,0]", "ALSA (http://w
dev.controlChanged.listen {
println("${it.channel} ${it.control} ${it.value}")
}
// or program changes
dev.programChange.listen {
println("${it.channel} ${it.program}")
}
```
## Further reading

View File

@@ -3,12 +3,14 @@ package org.openrndr.extra.midi
enum class MidiEventType {
NOTE_ON,
NOTE_OFF,
CONTROL_CHANGED
CONTROL_CHANGED,
PROGRAM_CHANGE
}
class MidiEvent(val eventType: MidiEventType) {
var origin = Origin.DEVICE
var control: Int = 0
var program: Int = 0
var note: Int = 0
var channel: Int = 0
var value: Int = 0
@@ -42,9 +44,16 @@ class MidiEvent(val eventType: MidiEventType) {
midiEvent.value = value
return midiEvent
}
fun programChange(channel:Int, program: Int): MidiEvent {
val midiEvent = MidiEvent(MidiEventType.PROGRAM_CHANGE)
midiEvent.channel = channel
midiEvent.program = program
return midiEvent
}
}
override fun toString(): String {
return "MidiEvent(eventType=$eventType, origin=$origin, control=$control, note=$note, channel=$channel, value=$value, velocity=$velocity)"
return "MidiEvent(eventType=$eventType, origin=$origin, program=$program, control=$control, note=$note, channel=$channel, value=$value, velocity=$velocity)"
}
}

View File

@@ -101,6 +101,7 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
ShortMessage.NOTE_ON -> noteOn.trigger(MidiEvent.noteOn(channel, cmd[1].toInt() and 0xff, cmd[2].toInt() and 0xff))
ShortMessage.NOTE_OFF -> noteOff.trigger(MidiEvent.noteOff(channel, cmd[1].toInt() and 0xff))
ShortMessage.CONTROL_CHANGE -> controlChanged.trigger(MidiEvent.controlChange(channel,cmd[1].toInt() and 0xff, cmd[2].toInt() and 0xff))
ShortMessage.PROGRAM_CHANGE -> programChanged.trigger(MidiEvent.programChange(channel,cmd[1].toInt() and 0xff))
}
}
override fun close() {
@@ -116,6 +117,7 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
}
val controlChanged = Event<MidiEvent>("midi-transceiver::controller-changed").signature(MidiEvent::class.java)
val programChanged = Event<MidiEvent>("midi-transceiver::program-changed").signature(MidiEvent::class.java)
val noteOn = Event<MidiEvent>("midi-transceiver::note-on").signature(MidiEvent::class.java)
val noteOff = Event<MidiEvent>("midi-transceiver::note-off").signature(MidiEvent::class.java)
@@ -131,6 +133,18 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
}
}
fun programChange(channel: Int, program: Int) {
try {
val msg = ShortMessage(ShortMessage.PROGRAM_CHANGE, channel, program)
if (receiverDevice != null) {
val tc = receiverDevice!!.microsecondPosition
receiver.send(msg, tc)
}
} catch (e: InvalidMidiDataException) {
//
}
}
fun noteOn(channel: Int, key: Int, velocity: Int) {
try {
val msg = ShortMessage(ShortMessage.NOTE_ON, channel, key, velocity)