Add toString and MidiTransceiver.open to simplify API use

This commit is contained in:
Edwin Jakobs
2019-11-19 12:30:34 +01:00
parent 3d1a472753
commit 1f37c8af5a
2 changed files with 17 additions and 11 deletions

View File

@@ -6,7 +6,6 @@ enum class MidiEventType {
CONTROL_CHANGED CONTROL_CHANGED
} }
class MidiEvent(val eventType: MidiEventType) { class MidiEvent(val eventType: MidiEventType) {
var origin = Origin.DEVICE var origin = Origin.DEVICE
var control: Int = 0 var control: Int = 0
@@ -44,4 +43,8 @@ class MidiEvent(val eventType: MidiEventType) {
return midiEvent return midiEvent
} }
} }
override fun toString(): String {
return "MidiEvent(eventType=$eventType, origin=$origin, control=$control, note=$note, channel=$channel, value=$value, velocity=$velocity)"
}
} }

View File

@@ -7,9 +7,13 @@ data class MidiDeviceName(val name: String, val vendor: String)
class MidiDeviceCapabilities { class MidiDeviceCapabilities {
var receive: Boolean = false var receive: Boolean = false
var transmit: Boolean = false var transmit: Boolean = false
override fun toString(): String {
return "MidiDeviceCapabilities(receive=$receive, transmit=$transmit)"
}
} }
class MidiDeviceDescription(val name: String, val vendor: String, val receive: Boolean, val transmit: Boolean) { data class MidiDeviceDescription(val name: String, val vendor: String, val receive: Boolean, val transmit: Boolean) {
companion object { companion object {
fun list(): List<MidiDeviceDescription> { fun list(): List<MidiDeviceDescription> {
val caps = mutableMapOf<MidiDeviceName, MidiDeviceCapabilities>() val caps = mutableMapOf<MidiDeviceName, MidiDeviceCapabilities>()
@@ -34,6 +38,14 @@ class MidiDeviceDescription(val name: String, val vendor: String, val receive: B
} }
} }
} }
fun open() : MidiTransceiver {
require(receive && transmit) {
"device should be a receiver and transmitter"
}
return MidiTransceiver.fromDeviceVendor(name, vendor)
}
} }
class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: MidiDevice) { class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: MidiDevice) {
@@ -52,11 +64,9 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
if (device.maxTransmitters != 0 && device.maxReceivers == 0) { if (device.maxTransmitters != 0 && device.maxReceivers == 0) {
transmitterDevice = device transmitterDevice = device
} }
if (device.maxReceivers != 0 && device.maxTransmitters == 0) { if (device.maxReceivers != 0 && device.maxTransmitters == 0) {
receiverDevice = device receiverDevice = device
} }
} }
} }
} catch (e: MidiUnavailableException) { } catch (e: MidiUnavailableException) {
@@ -71,8 +81,6 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
} else { } else {
throw IllegalArgumentException("midi device not found ${name}:${vendor} ${receiverDevice} ${transmitterDevice}") throw IllegalArgumentException("midi device not found ${name}:${vendor} ${receiverDevice} ${transmitterDevice}")
} }
} }
} }
@@ -95,9 +103,7 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
ShortMessage.CONTROL_CHANGE -> controlChanged.trigger(MidiEvent.controlChange(channel,cmd[1].toInt() and 0xff, cmd[2].toInt() and 0xff)) ShortMessage.CONTROL_CHANGE -> controlChanged.trigger(MidiEvent.controlChange(channel,cmd[1].toInt() and 0xff, cmd[2].toInt() and 0xff))
} }
} }
override fun close() { override fun close() {
} }
} }
} }
@@ -116,7 +122,6 @@ class MidiTransceiver(val receiverDevice: MidiDevice, val transmitterDevicer: Mi
} catch (e: InvalidMidiDataException) { } catch (e: InvalidMidiDataException) {
// //
} }
} }
fun noteOn(channel: Int, key: Int, velocity: Int) { fun noteOn(channel: Int, key: Int, velocity: Int) {
@@ -141,11 +146,9 @@ fun main() {
MidiDeviceDescription.list().forEach { MidiDeviceDescription.list().forEach {
println("> ${it.name}, ${it.vendor} r:${it.receive} t:${it.transmit}") println("> ${it.name}, ${it.vendor} r:${it.receive} t:${it.transmit}")
} }
val dev = MidiTransceiver.fromDeviceVendor("BCR2000 [hw:2,0,0]", "ALSA (http://www.alsa-project.org)") val dev = MidiTransceiver.fromDeviceVendor("BCR2000 [hw:2,0,0]", "ALSA (http://www.alsa-project.org)")
dev.controlChanged.listen { dev.controlChanged.listen {
println("${it.channel} ${it.control} ${it.value}") println("${it.channel} ${it.control} ${it.value}")
} }
// dev.destroy() // dev.destroy()
} }