Skip to content

Commit

Permalink
fix handful of issues in the initial MidiUmpDeviceService support code.
Browse files Browse the repository at this point in the history
- we want identifiable device name for UMP, so split resources.
- the service class name was inconsistent in aapinstrumentsample.
- all those MidiDevice properties are now passed as lazy arguments to avoid
  inconsistent status (MidiReceiver is created at MidiDeviceService.onCreate()
  while we cannot use `deviceInfo` property to enumerate how many ports we have)
  • Loading branch information
atsushieno committed Feb 17, 2024
1 parent e8440a7 commit b4bc4c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ abstract class AudioPluginMidiUmpDeviceService : MidiUmpDeviceService() {
// It is designed to be open overridable.
abstract val plugins: List<PluginInformation>

override fun onCreate() {
// weird-looking, but this is the correct order
impl.onCreate()
super.onCreate()
}
override fun onGetInputPortReceivers() = impl.onGetInputPortReceivers().toMutableList()
override fun onDeviceStatusChanged(status: MidiDeviceStatus) {
super.onDeviceStatusChanged(status)
Expand All @@ -34,11 +29,6 @@ abstract class AudioPluginMidiDeviceService : MidiDeviceService() {
// It is designed to be open overridable.
abstract val plugins: List<PluginInformation>

override fun onCreate() {
// weird-looking, but this is the correct order
impl.onCreate()
super.onCreate()
}
override fun onGetInputPortReceivers(): Array<MidiReceiver> = impl.onGetInputPortReceivers()
override fun onDeviceStatusChanged(status: MidiDeviceStatus) {
super.onDeviceStatusChanged(status)
Expand All @@ -47,34 +37,32 @@ abstract class AudioPluginMidiDeviceService : MidiDeviceService() {
}

internal class AudioPluginMidi1Device(owner: AudioPluginMidiDeviceService)
: AudioPluginMidiDevice(owner.applicationContext, owner.deviceInfo, owner.plugins) {
: AudioPluginMidiDevice({ owner.applicationContext }, { owner.deviceInfo }, owner.plugins) {

override val midiProtocol = 1
}

@RequiresApi(35)
internal class AudioPluginMidi2Device(owner: AudioPluginMidiUmpDeviceService)
: AudioPluginMidiDevice(owner.applicationContext, owner.deviceInfo!!, owner.plugins) {
: AudioPluginMidiDevice({ owner.applicationContext }, { owner.deviceInfo!! }, owner.plugins) {

override val midiProtocol = 2
}

abstract class AudioPluginMidiDevice(
val applicationContext: Context,
val deviceInfo: MidiDeviceInfo,
lazyGetApplicationContext: ()->Context,
lazyGetDeviceInfo: ()->MidiDeviceInfo,
candidatePlugins: List<PluginInformation>
) {
val applicationContext by lazy { lazyGetApplicationContext() }
val deviceInfo by lazy { lazyGetDeviceInfo() }

val plugins: List<PluginInformation> = candidatePlugins.filter { p -> isInstrument(p) }
private fun isInstrument(info: PluginInformation) : Boolean {
val c = info.category
return c != null && (c.contains(PluginInformation.PRIMARY_CATEGORY_INSTRUMENT) || c.contains("Synth"))
}

fun onCreate() {
portStatus = Array(deviceInfo.inputPortCount) { false }
}

abstract val midiProtocol: Int

private val receivers: Array<MidiReceiver> by lazy {
Expand All @@ -84,7 +72,7 @@ abstract class AudioPluginMidiDevice(

fun onGetInputPortReceivers(): Array<MidiReceiver> = receivers

private lateinit var portStatus: Array<Boolean>
private val portStatus by lazy { Array(deviceInfo.inputPortCount) { false } }

fun onDeviceStatusChanged(status: MidiDeviceStatus) {
for (i in 0 until deviceInfo.inputPortCount) {
Expand Down
7 changes: 3 additions & 4 deletions samples/aapinstrumentsample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@
android:permission="android.permission.BIND_MIDI_DEVICE_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.media.midi.MidiDeviceService" />
<action android:name="android.media.midi.MidiUmpDeviceService" />
</intent-filter>
<!-- we can reference the *same* device_info.xml as MidiDeviceService so far -->
<meta-data android:name="android.media.midi.MidiDeviceService"
android:resource="@xml/device_info" />
<meta-data android:name="android.media.midi.UmpMidiDeviceService"
android:resource="@xml/ump_device_info" />
</service>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<devices>
<device name="InstrumentAPISample-UMP" manufacturer="androidaudioplugin.org" product="InstrumentSampleMidiDeviceService">
<input-port name="input" />
<output-port name="output" />
</device>
</devices>

0 comments on commit b4bc4c6

Please sign in to comment.