1 /* 2 * Copyright 2020 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 package org.hyphonate.megaaudio.common; 17 18 import android.media.AudioDeviceInfo; 19 import android.media.AudioTrack; 20 21 /** 22 * Base class for Stream Builders. 23 * 24 * Contains common stream attributes that will be used when building a contcrete stream object. 25 */ 26 public abstract class BuilderBase { 27 @SuppressWarnings("unused") 28 private static final String TAG = BuilderBase.class.getSimpleName(); 29 @SuppressWarnings("unused") 30 private static final boolean LOG = false; 31 32 // API Types - enumerated in high nibble 33 public static final int TYPE_MASK = 0xF000; 34 public static final int TYPE_UNDEFINED = 0xF000; 35 public static final int TYPE_NONE = 0x0000; 36 public static final int TYPE_JAVA = 0x1000; 37 public static final int TYPE_OBOE = 0x2000; 38 39 // API subtypes - enumerated in low nibble 40 public static final int SUB_TYPE_MASK = 0x0000F; 41 public static final int SUB_TYPE_OBOE_DEFAULT = 0x0000; 42 public static final int SUB_TYPE_OBOE_AAUDIO = 0x0001; 43 public static final int SUB_TYPE_OBOE_OPENSL_ES = 0x0002; 44 45 /** 46 * The type id of the stream to create. Composed of the above constants. 47 */ 48 protected int mType = TYPE_UNDEFINED; 49 50 /** 51 * The number of frames per exchange of audio data to/from the AudioSink/AudioSource 52 * associated with this stream. 53 * Note: this is not the same as the size of record/playback buffers used by the 54 * underlying Native/Java player/recorder objects. That size is determined by the 55 * underlying Native/Java player/recorder objects. 56 */ 57 protected int mNumExchangeFrames = 128; 58 59 /** 60 * The sample rate for the created stream. 61 */ 62 protected int mSampleRate = 48000; 63 64 /** 65 * The number of channels for the created stream. 66 */ 67 protected int mChannelCount = 2; 68 69 // Performance Mode Constants 70 public static final int PERFORMANCE_MODE_NONE = 10; // AAUDIO_PERFORMANCE_MODE_NONE 71 public static final int 72 PERFORMANCE_MODE_POWERSAVING = 11; // AAUDIO_PERFORMANCE_MODE_POWER_SAVING, 73 public static final int 74 PERFORMANCE_MODE_LOWLATENCY = 12; // AAUDIO_PERFORMANCE_MODE_LOW_LATENCY 75 /** 76 * The performance mode for the created stream. It can be any one of the constants above. 77 */ 78 protected int mPerformanceMode = PERFORMANCE_MODE_LOWLATENCY; 79 80 // Sharing Mode Constants 81 public static final int SHARING_MODE_EXCLUSIVE = 0; // AAUDIO_SHARING_MODE_EXCLUSIVE 82 public static final int SHARING_MODE_SHARED = 1; // AAUDIO_SHARING_MODE_SHARED 83 /** 84 * The sharing mode for the created stream. It can be any one of the constants above. 85 */ 86 protected int mSharingMode = SHARING_MODE_EXCLUSIVE; 87 88 /** 89 * If non-null, the device to route the stream to/from upon creation. 90 */ 91 protected AudioDeviceInfo mRouteDevice; 92 93 /** 94 * Sets the number of frames exchanged between Players/Recorder and 95 * the corresponding AudioSource/AudioSink objects. 96 * @param numFrames the number of frames to exchange. 97 * @return this BuilderBase (for cascaded calls) 98 */ setNumExchangeFrames(int numFrames)99 public BuilderBase setNumExchangeFrames(int numFrames) { 100 mNumExchangeFrames = numFrames; 101 return this; 102 } 103 104 /** 105 * @return The number of frames of audio exchanged between Players/Recorder and 106 * the corresponding AudioSource/AudioSink objects, 107 * specified in the setNumExchangeFrames() method. 108 */ getNumExchangeFrames()109 public int getNumExchangeFrames() { 110 return mNumExchangeFrames; 111 } 112 113 /** 114 * Specifies the sample rate for the created stream 115 * @param sampleRate The sample rate for the created stream. 116 * @return this BuilderBase (for cascaded calls) 117 */ setSampleRate(int sampleRate)118 public BuilderBase setSampleRate(int sampleRate) { 119 mSampleRate = sampleRate; 120 return this; 121 } 122 123 /** 124 * @return the sample rate for the created stream, specified in the 125 * setSampleRate() method. 126 */ getSampleRate()127 public int getSampleRate() { 128 return mSampleRate; 129 } 130 131 /** 132 * Specifies a channel count for a stream 133 * @param channelCount The number of channels for the created stream. 134 * @return this BuilderBase (for cascaded calls) 135 */ setChannelCount(int channelCount)136 public BuilderBase setChannelCount(int channelCount) { 137 mChannelCount = channelCount; 138 return this; 139 } 140 141 /** 142 * @return the number of channels for the created stream, specified in the 143 * setChannelCount() method. 144 */ getChannelCount()145 public int getChannelCount() { 146 return mChannelCount; 147 } 148 149 /** 150 * Sets the sharing mode for the created stream. 151 * @param mode See "Sharing Mode Constants" Above 152 * @return this BuilderBase (for cascaded calls) 153 */ setSharingMode(int mode)154 public BuilderBase setSharingMode(int mode) { 155 mSharingMode = mode; 156 return this; 157 } 158 159 /** 160 * @return The sharing mode for the created stream, set in the setSharingMode() method. 161 */ getSharingMode()162 public int getSharingMode() { 163 return mSharingMode; 164 } 165 166 /** 167 * Sets the performance mode for the created stream. 168 * @param mode See "Performance Mode Constants" above 169 * @return this BuilderBase (for cascaded calls) 170 */ setPerformanceMode(int mode)171 public BuilderBase setPerformanceMode(int mode) { 172 mPerformanceMode = mode; 173 return this; 174 } 175 176 /** 177 * @return The performance mode for the created stream, set in the setPerformanceMode() method. 178 */ getPerformanceMode()179 public int getPerformanceMode() { 180 return mPerformanceMode; 181 } 182 183 // This is needed because the performance mode constants for the Java API 184 // are different than those for the AAudio/Oboe API 185 // These are the JAVA AudioTrack constants 186 // public static final int PERFORMANCE_MODE_NONE = 0; 187 // public static final int PERFORMANCE_MODE_LOW_LATENCY = 1; 188 // public static final int PERFORMANCE_MODE_POWER_SAVING = 2; 189 /** 190 * Maps from MegaAudio (and AAudio/Oboe) performance mode constants the equivalent 191 * Java AudioTrack constants. 192 * @return The Java AudioTrack constant corresponding the current performance mode 193 * for the created stream. 194 */ getJavaPerformanceMode()195 public int getJavaPerformanceMode() { 196 switch (mPerformanceMode) { 197 case PERFORMANCE_MODE_NONE: 198 return AudioTrack.PERFORMANCE_MODE_NONE; 199 200 case PERFORMANCE_MODE_POWERSAVING: 201 return AudioTrack.PERFORMANCE_MODE_POWER_SAVING; 202 203 case PERFORMANCE_MODE_LOWLATENCY: 204 default: 205 return AudioTrack.PERFORMANCE_MODE_LOW_LATENCY; 206 } 207 } 208 209 /** 210 * Specifies the device to route the stream to/from upon creation. If null, the default 211 * route is selected. 212 * @param routeDevice The device to route to, or null for the default device. 213 * @return This Builderbase (for cascaded calls) 214 */ setRouteDevice(AudioDeviceInfo routeDevice)215 public BuilderBase setRouteDevice(AudioDeviceInfo routeDevice) { 216 mRouteDevice = routeDevice; 217 return this; 218 } 219 220 /** 221 * @return The AudioDevice to route the stream to/from. If null, specifies the default device. 222 */ getRouteDevice()223 public AudioDeviceInfo getRouteDevice() { 224 return mRouteDevice; 225 } 226 227 // Indicates no specific routing (default routing). 228 public static final int ROUTED_DEVICE_ID_DEFAULT = -1; 229 230 /** 231 * For convenience in converting the AudioDeviceInfo to an integer device ID. 232 * @return The integer device ID of the device to route to/from. -1 if no device is specified. 233 */ getRouteDeviceId()234 public int getRouteDeviceId() { 235 return mRouteDevice == null ? ROUTED_DEVICE_ID_DEFAULT : mRouteDevice.getId(); 236 } 237 } 238