Skip to content

Commit

Permalink
[orx-midi] program change support (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron authored and edwinRNDR committed Jul 3, 2020
1 parent 2ef4a6a commit 5adf04a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions orx-midi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions orx-midi/src/main/kotlin/MidiEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)"
}
}
14 changes: 14 additions & 0 deletions orx-midi/src/main/kotlin/MidiTransceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 5adf04a

Please sign in to comment.