| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2016 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 | #ifndef UTILITY_AAUDIO_UTILITIES_H | 
|  | 18 | #define UTILITY_AAUDIO_UTILITIES_H | 
|  | 19 |  | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame^] | 20 | #include <algorithm> | 
|  | 21 | #include <functional> | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | #include <stdint.h> | 
|  | 23 | #include <sys/types.h> | 
|  | 24 |  | 
|  | 25 | #include <utils/Errors.h> | 
|  | 26 | #include <hardware/audio.h> | 
|  | 27 |  | 
| Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 28 | #include "aaudio/AAudio.h" | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | /** | 
|  | 31 | * Convert an AAudio result into the closest matching Android status. | 
|  | 32 | */ | 
|  | 33 | android::status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result); | 
|  | 34 |  | 
|  | 35 | /** | 
|  | 36 | * Convert an Android status into the closest matching AAudio result. | 
|  | 37 | */ | 
|  | 38 | aaudio_result_t AAudioConvert_androidToAAudioResult(android::status_t status); | 
|  | 39 |  | 
| Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 40 | /** | 
|  | 41 | * Convert an array of floats to an array of int16_t. | 
|  | 42 | * | 
|  | 43 | * @param source | 
|  | 44 | * @param destination | 
|  | 45 | * @param numSamples number of values in the array | 
|  | 46 | * @param amplitude level between 0.0 and 1.0 | 
|  | 47 | */ | 
|  | 48 | void AAudioConvert_floatToPcm16(const float *source, | 
|  | 49 | int16_t *destination, | 
|  | 50 | int32_t numSamples, | 
|  | 51 | float amplitude); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 52 |  | 
| Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 53 | /** | 
|  | 54 | * Convert floats to int16_t and scale by a linear ramp. | 
|  | 55 | * | 
|  | 56 | * The ramp stops just short of reaching amplitude2 so that the next | 
|  | 57 | * ramp can start at amplitude2 without causing a discontinuity. | 
|  | 58 | * | 
|  | 59 | * @param source | 
|  | 60 | * @param destination | 
|  | 61 | * @param numFrames | 
|  | 62 | * @param samplesPerFrame AKA number of channels | 
|  | 63 | * @param amplitude1 level at start of ramp, between 0.0 and 1.0 | 
|  | 64 | * @param amplitude2 level past end of ramp, between 0.0 and 1.0 | 
|  | 65 | */ | 
|  | 66 | void AAudioConvert_floatToPcm16(const float *source, | 
|  | 67 | int16_t *destination, | 
|  | 68 | int32_t numFrames, | 
|  | 69 | int32_t samplesPerFrame, | 
|  | 70 | float amplitude1, | 
|  | 71 | float amplitude2); | 
|  | 72 |  | 
|  | 73 | /** | 
|  | 74 | * Convert int16_t array to float array ranging from -1.0 to +1.0. | 
|  | 75 | * @param source | 
|  | 76 | * @param destination | 
|  | 77 | * @param numSamples | 
|  | 78 | */ | 
|  | 79 | //void AAudioConvert_pcm16ToFloat(const int16_t *source, int32_t numSamples, | 
|  | 80 | //                                float *destination); | 
|  | 81 |  | 
|  | 82 | /** | 
|  | 83 | * | 
|  | 84 | * Convert int16_t array to float array ranging from +/- amplitude. | 
|  | 85 | * @param source | 
|  | 86 | * @param destination | 
|  | 87 | * @param numSamples | 
|  | 88 | * @param amplitude | 
|  | 89 | */ | 
|  | 90 | void AAudioConvert_pcm16ToFloat(const int16_t *source, | 
|  | 91 | float *destination, | 
|  | 92 | int32_t numSamples, | 
|  | 93 | float amplitude); | 
|  | 94 |  | 
|  | 95 | /** | 
|  | 96 | * Convert floats to int16_t and scale by a linear ramp. | 
|  | 97 | * | 
|  | 98 | * The ramp stops just short of reaching amplitude2 so that the next | 
|  | 99 | * ramp can start at amplitude2 without causing a discontinuity. | 
|  | 100 | * | 
|  | 101 | * @param source | 
|  | 102 | * @param destination | 
|  | 103 | * @param numFrames | 
|  | 104 | * @param samplesPerFrame AKA number of channels | 
|  | 105 | * @param amplitude1 level at start of ramp, between 0.0 and 1.0 | 
|  | 106 | * @param amplitude2 level at end of ramp, between 0.0 and 1.0 | 
|  | 107 | */ | 
|  | 108 | void AAudioConvert_pcm16ToFloat(const int16_t *source, | 
|  | 109 | float *destination, | 
|  | 110 | int32_t numFrames, | 
|  | 111 | int32_t samplesPerFrame, | 
|  | 112 | float amplitude1, | 
|  | 113 | float amplitude2); | 
|  | 114 |  | 
|  | 115 | /** | 
|  | 116 | * Scale floats by a linear ramp. | 
|  | 117 | * | 
|  | 118 | * The ramp stops just short of reaching amplitude2 so that the next | 
|  | 119 | * ramp can start at amplitude2 without causing a discontinuity. | 
|  | 120 | * | 
|  | 121 | * @param source | 
|  | 122 | * @param destination | 
|  | 123 | * @param numFrames | 
|  | 124 | * @param samplesPerFrame | 
|  | 125 | * @param amplitude1 | 
|  | 126 | * @param amplitude2 | 
|  | 127 | */ | 
|  | 128 | void AAudio_linearRamp(const float *source, | 
|  | 129 | float *destination, | 
|  | 130 | int32_t numFrames, | 
|  | 131 | int32_t samplesPerFrame, | 
|  | 132 | float amplitude1, | 
|  | 133 | float amplitude2); | 
|  | 134 |  | 
|  | 135 | /** | 
|  | 136 | * Scale int16_t's by a linear ramp. | 
|  | 137 | * | 
|  | 138 | * The ramp stops just short of reaching amplitude2 so that the next | 
|  | 139 | * ramp can start at amplitude2 without causing a discontinuity. | 
|  | 140 | * | 
|  | 141 | * @param source | 
|  | 142 | * @param destination | 
|  | 143 | * @param numFrames | 
|  | 144 | * @param samplesPerFrame | 
|  | 145 | * @param amplitude1 | 
|  | 146 | * @param amplitude2 | 
|  | 147 | */ | 
|  | 148 | void AAudio_linearRamp(const int16_t *source, | 
|  | 149 | int16_t *destination, | 
|  | 150 | int32_t numFrames, | 
|  | 151 | int32_t samplesPerFrame, | 
|  | 152 | float amplitude1, | 
|  | 153 | float amplitude2); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 154 |  | 
|  | 155 | /** | 
|  | 156 | * Calculate the number of bytes and prevent numeric overflow. | 
|  | 157 | * @param numFrames frame count | 
|  | 158 | * @param bytesPerFrame size of a frame in bytes | 
|  | 159 | * @param sizeInBytes total size in bytes | 
|  | 160 | * @return AAUDIO_OK or negative error, eg. AAUDIO_ERROR_OUT_OF_RANGE | 
|  | 161 | */ | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 162 | int32_t AAudioConvert_framesToBytes(int32_t numFrames, | 
|  | 163 | int32_t bytesPerFrame, | 
|  | 164 | int32_t *sizeInBytes); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 165 |  | 
| Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 166 | audio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudio_format); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 167 |  | 
| Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 168 | aaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t format); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 169 |  | 
|  | 170 | /** | 
|  | 171 | * @return the size of a sample of the given format in bytes or AAUDIO_ERROR_ILLEGAL_ARGUMENT | 
|  | 172 | */ | 
| Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 173 | int32_t AAudioConvert_formatToSizeInBytes(aaudio_format_t format); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 174 |  | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 175 |  | 
|  | 176 | // Note that this code may be replaced by Settings or by some other system configuration tool. | 
|  | 177 |  | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 178 | #define AAUDIO_PROP_MMAP_POLICY           "aaudio.mmap_policy" | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | /** | 
|  | 181 | * Read system property. | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 182 | * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 183 | */ | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 184 | int32_t AAudioProperty_getMMapPolicy(); | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 185 |  | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 186 | #define AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY "aaudio.mmap_exclusive_policy" | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 187 |  | 
|  | 188 | /** | 
|  | 189 | * Read system property. | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 190 | * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 191 | */ | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 192 | int32_t AAudioProperty_getMMapExclusivePolicy(); | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 193 |  | 
|  | 194 | #define AAUDIO_PROP_MIXER_BURSTS           "aaudio.mixer_bursts" | 
|  | 195 |  | 
|  | 196 | /** | 
|  | 197 | * Read system property. | 
|  | 198 | * @return number of bursts per mixer cycle | 
|  | 199 | */ | 
|  | 200 | int32_t AAudioProperty_getMixerBursts(); | 
|  | 201 |  | 
|  | 202 | #define AAUDIO_PROP_HW_BURST_MIN_USEC      "aaudio.hw_burst_min_usec" | 
|  | 203 |  | 
|  | 204 | /** | 
|  | 205 | * Read system property. | 
|  | 206 | * This is handy in case the DMA is bursting too quickly for the CPU to keep up. | 
|  | 207 | * For example, there may be a DMA burst every 100 usec but you only | 
|  | 208 | * want to feed the MMAP buffer every 2000 usec. | 
|  | 209 | * | 
|  | 210 | * This will affect the framesPerBurst for an MMAP stream. | 
|  | 211 | * | 
|  | 212 | * @return minimum number of microseconds for a MMAP HW burst | 
|  | 213 | */ | 
|  | 214 | int32_t AAudioProperty_getHardwareBurstMinMicros(); | 
|  | 215 |  | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame^] | 216 | /** | 
|  | 217 | * Try a function f until it returns true. | 
|  | 218 | * | 
|  | 219 | * The function is always called at least once. | 
|  | 220 | * | 
|  | 221 | * @param f the function to evaluate, which returns a bool. | 
|  | 222 | * @param times the number of times to evaluate f. | 
|  | 223 | * @param sleepMs the sleep time per check of f, if greater than 0. | 
|  | 224 | * @return true if f() eventually returns true. | 
|  | 225 | */ | 
|  | 226 | static inline bool AAudio_tryUntilTrue( | 
|  | 227 | std::function<bool()> f, int times, int sleepMs) { | 
|  | 228 | static const useconds_t US_PER_MS = 1000; | 
|  | 229 |  | 
|  | 230 | sleepMs = std::max(sleepMs, 0); | 
|  | 231 | for (;;) { | 
|  | 232 | if (f()) return true; | 
|  | 233 | if (times <= 1) return false; | 
|  | 234 | --times; | 
|  | 235 | usleep(sleepMs * US_PER_MS); | 
|  | 236 | } | 
|  | 237 | } | 
|  | 238 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 239 | #endif //UTILITY_AAUDIO_UTILITIES_H |