Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/midi transceiver fails on short/system messages #347

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ktor = "2.3.11"
jgit = "6.9.0.202403050737-r"
javaosc = "0.9"
jsoup = "1.17.2"
mockk = "1.13.11"

[libraries]
kotlin-logging = { group = "io.github.oshai", name = "kotlin-logging", version.ref = "kotlinLogging" }
Expand All @@ -49,6 +50,7 @@ kotest-assertions = { group = "io.kotest", name = "kotest-assertions-core", vers
kotest-runner = { group = "io.kotest", name = "kotest-runner-junit5", version.ref = "kotest" }
kotest-framework-engine = { group = "io.kotest", name = "kotest-framework-engine", version.ref = "kotest" }

mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk"}

openrndr-application = { group = "org.openrndr", name = "openrndr-application", version.ref = "openrndr" }
openrndr-extensions = { group = "org.openrndr", name = "openrndr-extensions", version.ref = "openrndr" }
Expand Down
5 changes: 4 additions & 1 deletion orx-jvm/orx-midi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ dependencies {
implementation(libs.kotlin.coroutines)
implementation(project(":orx-property-watchers"))
implementation(project(":orx-parameters"))
}

testImplementation(libs.mockk)
testImplementation(libs.kotest.assertions)
}
30 changes: 15 additions & 15 deletions orx-jvm/orx-midi/src/main/kotlin/MidiBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun Program.bindMidiControl(
val low = anno?.low ?: 0.0
val high = anno?.high ?: 1.0
transceiver.controlChanged.listen {
if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channel && it.control == control) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channel && it.control == control) {
val value = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
property.set(value)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ fun Program.bindMidiControl(
control: Int
) {
transceiver.controlChanged.listen {
if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channel && it.control == control) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channel && it.control == control) {
property.set(it.value >= 64)
}
}
Expand Down Expand Up @@ -126,12 +126,12 @@ fun Program.bindMidiControl(
var y = v.y
var changed = false

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelX && it.control == controlX) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelX && it.control == controlX) {
changed = true
x = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelY && it.control == controlY) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelY && it.control == controlY) {
changed = true
y = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}
Expand Down Expand Up @@ -187,17 +187,17 @@ fun Program.bindMidiControl(
var z = v.z
var changed = false

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelX && it.control == controlX) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelX && it.control == controlX) {
changed = true
x = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelY && it.control == controlY) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelY && it.control == controlY) {
changed = true
y = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelZ && it.control == controlZ) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelZ && it.control == controlZ) {
changed = true
z = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}
Expand Down Expand Up @@ -257,22 +257,22 @@ fun Program.bindMidiControl(
var a = v.alpha
var changed = false

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelR && it.control == controlR) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelR && it.control == controlR) {
changed = true
r = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelG && it.control == controlG) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelG && it.control == controlG) {
changed = true
g = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelB && it.control == controlB) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelB && it.control == controlB) {
changed = true
b = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelA && it.control == controlA) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelA && it.control == controlA) {
changed = true
a = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}
Expand Down Expand Up @@ -335,22 +335,22 @@ fun Program.bindMidiControl(
var w = v.w
var changed = false

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelX && it.control == controlX) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelX && it.control == controlX) {
changed = true
x = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelY && it.control == controlY) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelY && it.control == controlY) {
changed = true
y = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelZ && it.control == controlZ) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelZ && it.control == controlZ) {
changed = true
z = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}

if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channelW && it.control == controlW) {
if (it.eventType == MidiEventType.CONTROL_CHANGE && it.channel == channelW && it.control == controlW) {
changed = true
w = it.value.toDouble().map(0.0, 127.0, low, high, clamp = true)
}
Expand Down
52 changes: 43 additions & 9 deletions orx-jvm/orx-midi/src/main/kotlin/MidiEvent.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
package org.openrndr.extra.midi

enum class MidiEventType {
NOTE_ON,
NOTE_OFF,
CONTROL_CHANGED,
PROGRAM_CHANGE,
CHANNEL_PRESSURE,
PITCH_BEND
import javax.sound.midi.MidiMessage
import javax.sound.midi.ShortMessage

enum class MidiEventType(val status: Int) {

MIDI_TIME_CODE(ShortMessage.MIDI_TIME_CODE),
SONG_POSITION_POINTER(ShortMessage.SONG_POSITION_POINTER),
SONG_SELECT(ShortMessage.SONG_SELECT),
TUNE_REQUEST(ShortMessage.TUNE_REQUEST),
END_OF_EXCLUSIVE(ShortMessage.END_OF_EXCLUSIVE),
TIMING_CLOCK(ShortMessage.TIMING_CLOCK),
START(ShortMessage.START),
CONTINUE(ShortMessage.CONTINUE),
STOP(ShortMessage.STOP),
ACTIVE_SENSING(ShortMessage.ACTIVE_SENSING),
SYSTEM_RESET(ShortMessage.SYSTEM_RESET),
NOTE_ON(ShortMessage.NOTE_ON),
NOTE_OFF(ShortMessage.NOTE_OFF),
CONTROL_CHANGE(ShortMessage.CONTROL_CHANGE),
PROGRAM_CHANGE(ShortMessage.PROGRAM_CHANGE),
CHANNEL_PRESSURE(ShortMessage.CHANNEL_PRESSURE),
PITCH_BEND(ShortMessage.PITCH_BEND);

companion object {

private val statusMap: Map<Int, MidiEventType> =
entries.associateBy { it.status }

fun fromStatus(
status: Int
): MidiEventType = requireNotNull(
statusMap[if (status >= 0xf0) status else status and 0xf0]
) {
"Invalid MIDI status: $status"
}

}

}

val MidiMessage.eventType: MidiEventType get() = MidiEventType.fromStatus(status)

class MidiEvent(val eventType: MidiEventType) {
var origin = Origin.DEVICE
var control: Int = 0
Expand All @@ -34,15 +67,16 @@ class MidiEvent(val eventType: MidiEventType) {
return midiEvent
}

fun noteOff(channel: Int, note: Int): MidiEvent {
fun noteOff(channel: Int, note: Int, velocity: Int): MidiEvent {
val midiEvent = MidiEvent(MidiEventType.NOTE_OFF)
midiEvent.note = note
midiEvent.channel = channel
midiEvent.velocity = velocity
return midiEvent
}

fun controlChange(channel: Int, control: Int, value: Int): MidiEvent {
val midiEvent = MidiEvent(MidiEventType.CONTROL_CHANGED)
val midiEvent = MidiEvent(MidiEventType.CONTROL_CHANGE)
midiEvent.channel = channel
midiEvent.control = control
midiEvent.value = value
Expand Down
Loading