1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.verifier.audio.midilib; 18 19 import android.media.midi.MidiDevice; 20 import android.media.midi.MidiDeviceInfo; 21 import android.media.midi.MidiInputPort; 22 import android.media.midi.MidiManager; 23 import android.media.midi.MidiOutputPort; 24 import android.media.midi.MidiReceiver; 25 26 import android.util.Log; 27 28 import java.io.IOException; 29 30 /** 31 * A class to hold the MidiIODevice and ports objects associated with a MIDI I/O peripheral. 32 */ 33 public class MidiIODevice { 34 private static final String TAG = "MidiIODevice"; 35 private static final boolean DEBUG = false; 36 37 private final int mDeviceType; 38 39 public MidiDeviceInfo mSendDevInfo; 40 public MidiDeviceInfo mReceiveDevInfo; 41 42 public MidiInputPort mSendPort; 43 public MidiOutputPort mReceivePort; 44 MidiIODevice(int deviceType)45 public MidiIODevice(int deviceType) { 46 mDeviceType = deviceType; 47 } 48 scanDevices(MidiDeviceInfo[] devInfos)49 public void scanDevices(MidiDeviceInfo[] devInfos) { 50 if (DEBUG) { 51 Log.i(TAG, "---- scanDevices() typeID: " + mDeviceType); 52 } 53 mSendDevInfo = null; 54 mReceiveDevInfo = null; 55 mSendPort = null; 56 mReceivePort = null; 57 58 for(MidiDeviceInfo devInfo : devInfos) { 59 // Inputs? 60 int numInPorts = devInfo.getInputPortCount(); 61 if (numInPorts <= 0) { 62 continue; // none? 63 } 64 if (devInfo.getType() == mDeviceType && mSendDevInfo == null) { 65 mSendDevInfo = devInfo; 66 } 67 68 // Outputs? 69 int numOutPorts = devInfo.getOutputPortCount(); 70 if (numOutPorts <= 0) { 71 continue; // none? 72 } 73 if (devInfo.getType() == mDeviceType && mReceiveDevInfo == null) { 74 mReceiveDevInfo = devInfo; 75 } 76 77 if (mSendDevInfo != null && mReceiveDevInfo != null) { 78 break; // we have an in and out device, so we can stop scanning 79 } 80 } 81 82 if (DEBUG) { 83 if (mSendDevInfo != null) { 84 Log.i(TAG, "---- mSendDevInfo: " + mSendDevInfo); 85 } 86 if (mReceiveDevInfo != null) { 87 Log.i(TAG, "---- mReceiveDevInfo: " + mReceiveDevInfo); 88 } 89 } 90 } 91 openPorts(MidiDevice device, MidiReceiver receiver)92 public void openPorts(MidiDevice device, MidiReceiver receiver) { 93 if (DEBUG) { 94 Log.i(TAG, "---- openPorts()"); 95 } 96 MidiDeviceInfo deviceInfo = device.getInfo(); 97 int numOutputs = deviceInfo.getOutputPortCount(); 98 if (numOutputs > 0) { 99 mReceivePort = device.openOutputPort(0); 100 mReceivePort.connect(receiver); 101 } 102 103 int numInputs = deviceInfo.getInputPortCount(); 104 if (numInputs != 0) { 105 mSendPort = device.openInputPort(0); 106 } 107 108 if (DEBUG) { 109 Log.i(TAG, "---- mSendPort:" + mSendPort); 110 } 111 } 112 closePorts()113 public void closePorts() { 114 if (DEBUG) { 115 Log.i(TAG, "---- closePorts()"); 116 } 117 try { 118 if (mSendPort != null) { 119 mSendPort.close(); 120 mSendPort = null; 121 } 122 if (mReceivePort != null) { 123 mReceivePort.close(); 124 mReceivePort = null; 125 } 126 } catch (IOException ex) { 127 Log.e(TAG, "IOException Closing MIDI ports: " + ex); 128 } 129 } 130 getInputName()131 public String getInputName() { 132 if (mReceiveDevInfo != null) { 133 return mDeviceType == MidiDeviceInfo.TYPE_VIRTUAL 134 ? "Virtual MIDI Device" 135 : mReceiveDevInfo.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME); 136 } else { 137 return ""; 138 } 139 } 140 getOutputName()141 public String getOutputName() { 142 if (mSendDevInfo != null) { 143 return mDeviceType == MidiDeviceInfo.TYPE_VIRTUAL 144 ? "Virtual MIDI Device" 145 : mSendDevInfo.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME); 146 } else { 147 return ""; 148 } 149 } 150 } /* class MidiIODevice */ 151