-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MergedMidiAccess (bring it back from managed-midi).
- Loading branch information
1 parent
4d8afab
commit 49b41db
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
ktmidi/src/commonMain/kotlin/dev/atsushieno/ktmidi/MergedMidiAccess.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.atsushieno.ktmidi | ||
|
||
|
||
class MergedMidiAccess(override val name: String, private val list: List<MidiAccess>) : MidiAccess() { | ||
private class MidiPortDetailsWrapper( | ||
val midiAccess: MidiAccess, | ||
val source: MidiPortDetails | ||
) : MidiPortDetails { | ||
override val id = "${midiAccess.name}_${source.id}" | ||
override val manufacturer = source.manufacturer | ||
override val name = source.name | ||
override val version = source.version | ||
override val midiTransportProtocol = source.midiTransportProtocol | ||
} | ||
|
||
private class MidiInputWrapper( | ||
override val details: MidiPortDetailsWrapper, | ||
private val impl: MidiInput | ||
) : MidiInput { | ||
override fun setMessageReceivedListener(listener: OnMidiReceivedEventListener) = | ||
impl.setMessageReceivedListener(listener) | ||
|
||
override val connectionState: MidiPortConnectionState by impl::connectionState | ||
|
||
override fun close() = impl.close() | ||
|
||
} | ||
|
||
private class MidiOutputWrapper( | ||
override val details: MidiPortDetailsWrapper, | ||
private val impl: MidiOutput | ||
): MidiOutput { | ||
override fun send(mevent: ByteArray, offset: Int, length: Int, timestampInNanoseconds: Long) = | ||
impl.send(mevent, offset, length, timestampInNanoseconds) | ||
|
||
override val connectionState: MidiPortConnectionState by impl::connectionState | ||
|
||
override fun close() = impl.close() | ||
} | ||
|
||
override val inputs: Iterable<MidiPortDetails> | ||
get() = list.flatMap { access -> access.inputs.map { MidiPortDetailsWrapper(access, it) } } | ||
override val outputs: Iterable<MidiPortDetails> | ||
get() = list.flatMap { access -> access.outputs.map { MidiPortDetailsWrapper(access, it) } } | ||
|
||
override suspend fun openInput(portId: String): MidiInput { | ||
val details = inputs.first { it.id == portId } as MidiPortDetailsWrapper | ||
return MidiInputWrapper(details, details.midiAccess.openInput(details.source.id)) | ||
} | ||
|
||
override suspend fun openOutput(portId: String): MidiOutput { | ||
val details = outputs.first { it.id == portId } as MidiPortDetailsWrapper | ||
return MidiOutputWrapper(details, details.midiAccess.openOutput(details.source.id)) | ||
} | ||
} |