From a732a0559df80061b3861187f068d43c5536a004 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Tue, 25 Apr 2023 18:20:37 +0200 Subject: [PATCH] [orx-midi] Add bindMidiNote --- .../orx-midi/src/main/kotlin/MidiBindings.kt | 70 +++++++++++++------ 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/orx-jvm/orx-midi/src/main/kotlin/MidiBindings.kt b/orx-jvm/orx-midi/src/main/kotlin/MidiBindings.kt index 95edb450..5267cae3 100644 --- a/orx-jvm/orx-midi/src/main/kotlin/MidiBindings.kt +++ b/orx-jvm/orx-midi/src/main/kotlin/MidiBindings.kt @@ -13,8 +13,28 @@ import org.openrndr.math.map import kotlin.reflect.KMutableProperty0 import kotlin.reflect.full.findAnnotations + +fun bindMidiNote(on: () -> Unit, off: () -> Unit, transceiver: MidiTransceiver, channel: Int, note: Int) { + transceiver.noteOn.listen { + if ((channel == -1 || it.channel == channel) && it.note == note) { + on() + } + } + transceiver.noteOff.listen { + if ((channel == -1 || it.channel == channel) && it.note == note) { + on() + } + } +} + + @JvmName("bindMidiControlDouble") -fun Program.bindMidiControl(property: KMutableProperty0, transceiver: MidiTransceiver, channel: Int, control: Int) { +fun Program.bindMidiControl( + property: KMutableProperty0, + transceiver: MidiTransceiver, + channel: Int, + control: Int +) { val anno = property.findAnnotations(DoubleParameter::class).firstOrNull() val low = anno?.low ?: 0.0 @@ -27,7 +47,7 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: Mi } launch { var propertyValue = property.get() - while(true) { + while (true) { val candidateValue = property.get() if (candidateValue != propertyValue) { propertyValue = candidateValue @@ -40,7 +60,12 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: Mi } @JvmName("bindMidiControlBoolean") -fun Program.bindMidiControl(property: KMutableProperty0, transceiver: MidiTransceiver, channel: Int, control: Int) { +fun Program.bindMidiControl( + property: KMutableProperty0, + transceiver: MidiTransceiver, + channel: Int, + control: Int +) { transceiver.controlChanged.listen { if (it.eventType == MidiEventType.CONTROL_CHANGED && it.channel == channel && it.control == control) { property.set(it.value >= 64) @@ -48,7 +73,7 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: M } launch { var propertyValue = property.get() - while(true) { + while (true) { val candidateValue = property.get() if (candidateValue != propertyValue) { propertyValue = candidateValue @@ -61,9 +86,11 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: M @JvmName("bindMidiControlVector2") -fun Program.bindMidiControl(property: KMutableProperty0, transceiver: MidiTransceiver, - channelX: Int, controlX: Int, - channelY: Int = channelX, controlY: Int = controlX + 1) { +fun Program.bindMidiControl( + property: KMutableProperty0, transceiver: MidiTransceiver, + channelX: Int, controlX: Int, + channelY: Int = channelX, controlY: Int = controlX + 1 +) { val anno = property.findAnnotations(Vector2Parameter::class).firstOrNull() val low = anno?.min ?: 0.0 @@ -91,7 +118,7 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: M } launch { var propertyValue = property.get() - while(true) { + while (true) { val candidateValue = property.get() if (candidateValue != propertyValue) { propertyValue = candidateValue @@ -106,10 +133,12 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: M } @JvmName("bindMidiControlVector3") -fun Program.bindMidiControl(property: KMutableProperty0, transceiver: MidiTransceiver, - channelX: Int, controlX: Int, - channelY: Int = channelX, controlY: Int = controlX + 1, - channelZ: Int = channelY, controlZ: Int = controlY + 1) { +fun Program.bindMidiControl( + property: KMutableProperty0, transceiver: MidiTransceiver, + channelX: Int, controlX: Int, + channelY: Int = channelX, controlY: Int = controlX + 1, + channelZ: Int = channelY, controlZ: Int = controlY + 1 +) { val anno = property.findAnnotations(Vector3Parameter::class).firstOrNull() val low = anno?.min ?: 0.0 @@ -143,7 +172,7 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: M } launch { var propertyValue = property.get() - while(true) { + while (true) { val candidateValue = property.get() if (candidateValue != propertyValue) { propertyValue = candidateValue @@ -160,12 +189,13 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: M } @JvmName("bindMidiControlColorRGBa") -fun Program.bindMidiControl(property: KMutableProperty0, transceiver: MidiTransceiver, - channelR: Int, controlR: Int, - channelG: Int = channelR, controlG: Int = controlR + 1, - channelB: Int = channelG, controlB: Int = controlG + 1, - channelA: Int = channelB, controlA: Int = controlB + 1, - ) { +fun Program.bindMidiControl( + property: KMutableProperty0, transceiver: MidiTransceiver, + channelR: Int, controlR: Int, + channelG: Int = channelR, controlG: Int = controlR + 1, + channelB: Int = channelG, controlB: Int = controlG + 1, + channelA: Int = channelB, controlA: Int = controlB + 1, +) { val low = 0.0 val high = 1.0 transceiver.controlChanged.listen { @@ -203,7 +233,7 @@ fun Program.bindMidiControl(property: KMutableProperty0, transceiver: } launch { var propertyValue = property.get() - while(true) { + while (true) { val candidateValue = property.get() if (candidateValue != propertyValue) { propertyValue = candidateValue