• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.MidiOutputPort;
23 import android.media.midi.MidiReceiver;
24 import android.os.Bundle;
25 import android.util.Log;
26 
27 import java.io.IOException;
28 
29 /**
30  * A class to hold the MidiIODevice and ports objects associated with a MIDI I/O peripheral.
31  */
32 public class MidiIODevice {
33     private static final String TAG = "MidiIODevice";
34     private static final boolean DEBUG = false;
35 
36     private final int mDeviceType;
37 
38     public MidiDeviceInfo mSendDevInfo;
39     public MidiDeviceInfo mReceiveDevInfo;
40 
41     public MidiInputPort   mSendPort;
42     public MidiOutputPort  mReceivePort;
43 
MidiIODevice(int deviceType)44     public MidiIODevice(int deviceType) {
45         mDeviceType = deviceType;
46     }
47 
scanDevices(MidiDeviceInfo[] devInfos)48     public void scanDevices(MidiDeviceInfo[] devInfos) {
49         if (DEBUG) {
50             Log.i(TAG, "---- scanDevices() typeID: " + mDeviceType);
51         }
52         mSendDevInfo = null;
53         mReceiveDevInfo = null;
54         mSendPort = null;
55         mReceivePort = null;
56 
57         for(MidiDeviceInfo devInfo : devInfos) {
58             Bundle devBundle = devInfo.getProperties();
59             if (DEBUG) {
60                 Log.i(TAG, "  mfg:" + devBundle.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER)
61                         + " prod:" + devBundle.getString(MidiDeviceInfo.PROPERTY_PRODUCT));
62             }
63 
64             // Inputs?
65             int numInPorts = devInfo.getInputPortCount();
66             if (numInPorts <= 0) {
67                 continue; // none?
68             }
69 
70             // For virtual MIDI devices, we need to find
71             // manufacturer=AndroidCTSVerifier and product=VerifierMidiEcho or else
72             // it might be some other MIDI app providing virtual MIDI services
73             if (mDeviceType == MidiDeviceInfo.TYPE_VIRTUAL) {
74                 if (!"AndroidCTSVerifier".equals(
75                         devBundle.getString(MidiDeviceInfo.PROPERTY_MANUFACTURER))
76                         || !"VerifierMidiEcho".equals(
77                                 devBundle.getString(MidiDeviceInfo.PROPERTY_PRODUCT))) {
78                     // Virtual [but not ours]
79                     Log.d(TAG, "Virtual Midi Device:" + devInfo);
80                     continue;
81                 }
82             }
83 
84             if (devInfo.getType() == mDeviceType && mSendDevInfo == null) {
85                 mSendDevInfo = devInfo;
86             }
87 
88             // Outputs?
89             int numOutPorts = devInfo.getOutputPortCount();
90             if (numOutPorts <= 0) {
91                 continue; // none?
92             }
93             if (devInfo.getType() == mDeviceType && mReceiveDevInfo == null) {
94                 mReceiveDevInfo = devInfo;
95             }
96 
97             if (mSendDevInfo != null && mReceiveDevInfo != null) {
98                 break;  // we have an in and out device, so we can stop scanning
99             }
100         }
101 
102         if (DEBUG) {
103             Log.i(TAG, "---- mSendDevInfo: "
104                     + (mSendDevInfo != null ? mSendDevInfo.toString() : "NONE"));
105             Log.i(TAG, "---- mReceiveDevInfo: "
106                     + (mReceiveDevInfo != null ? mReceiveDevInfo.toString() : "NONE"));
107         }
108     }
109 
openPorts(MidiDevice device, MidiReceiver receiver)110     public void openPorts(MidiDevice device, MidiReceiver receiver) {
111         if (DEBUG) {
112             Log.i(TAG, "---- openPorts()");
113         }
114         MidiDeviceInfo deviceInfo = device.getInfo();
115         int numOutputs = deviceInfo.getOutputPortCount();
116         if (numOutputs > 0) {
117             mReceivePort = device.openOutputPort(0);
118             mReceivePort.connect(receiver);
119         }
120 
121         int numInputs = deviceInfo.getInputPortCount();
122         if (numInputs != 0) {
123             mSendPort = device.openInputPort(0);
124         }
125 
126         if (DEBUG) {
127             Log.i(TAG, "---- mSendPort:" + mSendPort);
128         }
129     }
130 
closePorts()131     public void closePorts() {
132         if (DEBUG) {
133             Log.i(TAG, "---- closePorts()");
134         }
135         try {
136             if (mSendPort != null) {
137                 mSendPort.close();
138                 mSendPort = null;
139             }
140             if (mReceivePort != null) {
141                 mReceivePort.close();
142                 mReceivePort = null;
143             }
144         } catch (IOException ex) {
145             Log.e(TAG, "IOException Closing MIDI ports: " + ex);
146         }
147     }
148 
getInputName()149     public String getInputName() {
150         if (mReceiveDevInfo != null) {
151             return mDeviceType == MidiDeviceInfo.TYPE_VIRTUAL
152                     ? "Virtual MIDI Device"
153                     : mReceiveDevInfo.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME);
154         } else {
155             return "";
156         }
157     }
158 
getOutputName()159     public String getOutputName() {
160         if (mSendDevInfo != null) {
161             return mDeviceType == MidiDeviceInfo.TYPE_VIRTUAL
162                     ? "Virtual MIDI Device"
163                     : mSendDevInfo.getProperties().getString(MidiDeviceInfo.PROPERTY_NAME);
164         } else {
165             return "";
166         }
167     }
168 }   /* class MidiIODevice */
169