Skip to content

Commit

Permalink
[orx-midi] Add bindMidiNote
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Apr 25, 2023
1 parent be9c7dc commit a732a05
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions orx-jvm/orx-midi/src/main/kotlin/MidiBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double>, transceiver: MidiTransceiver, channel: Int, control: Int) {
fun Program.bindMidiControl(
property: KMutableProperty0<Double>,
transceiver: MidiTransceiver,
channel: Int,
control: Int
) {
val anno = property.findAnnotations(DoubleParameter::class).firstOrNull()

val low = anno?.low ?: 0.0
Expand All @@ -27,7 +47,7 @@ fun Program.bindMidiControl(property: KMutableProperty0<Double>, transceiver: Mi
}
launch {
var propertyValue = property.get()
while(true) {
while (true) {
val candidateValue = property.get()
if (candidateValue != propertyValue) {
propertyValue = candidateValue
Expand All @@ -40,15 +60,20 @@ fun Program.bindMidiControl(property: KMutableProperty0<Double>, transceiver: Mi
}

@JvmName("bindMidiControlBoolean")
fun Program.bindMidiControl(property: KMutableProperty0<Boolean>, transceiver: MidiTransceiver, channel: Int, control: Int) {
fun Program.bindMidiControl(
property: KMutableProperty0<Boolean>,
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)
}
}
launch {
var propertyValue = property.get()
while(true) {
while (true) {
val candidateValue = property.get()
if (candidateValue != propertyValue) {
propertyValue = candidateValue
Expand All @@ -61,9 +86,11 @@ fun Program.bindMidiControl(property: KMutableProperty0<Boolean>, transceiver: M


@JvmName("bindMidiControlVector2")
fun Program.bindMidiControl(property: KMutableProperty0<Vector2>, transceiver: MidiTransceiver,
channelX: Int, controlX: Int,
channelY: Int = channelX, controlY: Int = controlX + 1) {
fun Program.bindMidiControl(
property: KMutableProperty0<Vector2>, 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
Expand Down Expand Up @@ -91,7 +118,7 @@ fun Program.bindMidiControl(property: KMutableProperty0<Vector2>, transceiver: M
}
launch {
var propertyValue = property.get()
while(true) {
while (true) {
val candidateValue = property.get()
if (candidateValue != propertyValue) {
propertyValue = candidateValue
Expand All @@ -106,10 +133,12 @@ fun Program.bindMidiControl(property: KMutableProperty0<Vector2>, transceiver: M
}

@JvmName("bindMidiControlVector3")
fun Program.bindMidiControl(property: KMutableProperty0<Vector3>, 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<Vector3>, 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
Expand Down Expand Up @@ -143,7 +172,7 @@ fun Program.bindMidiControl(property: KMutableProperty0<Vector3>, transceiver: M
}
launch {
var propertyValue = property.get()
while(true) {
while (true) {
val candidateValue = property.get()
if (candidateValue != propertyValue) {
propertyValue = candidateValue
Expand All @@ -160,12 +189,13 @@ fun Program.bindMidiControl(property: KMutableProperty0<Vector3>, transceiver: M
}

@JvmName("bindMidiControlColorRGBa")
fun Program.bindMidiControl(property: KMutableProperty0<ColorRGBa>, 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<ColorRGBa>, 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 {
Expand Down Expand Up @@ -203,7 +233,7 @@ fun Program.bindMidiControl(property: KMutableProperty0<ColorRGBa>, transceiver:
}
launch {
var propertyValue = property.get()
while(true) {
while (true) {
val candidateValue = property.get()
if (candidateValue != propertyValue) {
propertyValue = candidateValue
Expand Down

0 comments on commit a732a05

Please sign in to comment.