blob: efd663d373ea1656453f7044473009817f5d48e0 [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
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 Hung47c5e532017-06-26 18:28:00 -070020#include <algorithm>
21#include <functional>
Phil Burk5ed503c2017-02-01 09:38:15 -080022#include <stdint.h>
23#include <sys/types.h>
24
25#include <utils/Errors.h>
26#include <hardware/audio.h>
27
Phil Burka4eb0d82017-04-12 15:44:06 -070028#include "aaudio/AAudio.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080029
30/**
31 * Convert an AAudio result into the closest matching Android status.
32 */
33android::status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result);
34
35/**
36 * Convert an Android status into the closest matching AAudio result.
37 */
38aaudio_result_t AAudioConvert_androidToAAudioResult(android::status_t status);
39
Phil Burke572f462017-04-20 13:03:19 -070040/**
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 */
48void AAudioConvert_floatToPcm16(const float *source,
49 int16_t *destination,
50 int32_t numSamples,
51 float amplitude);
Phil Burk5ed503c2017-02-01 09:38:15 -080052
Phil Burke572f462017-04-20 13:03:19 -070053/**
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 */
66void 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 */
90void 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 */
108void 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 */
128void 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 */
148void 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 Burk5ed503c2017-02-01 09:38:15 -0800154
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 Burk3316d5e2017-02-15 11:23:01 -0800162int32_t AAudioConvert_framesToBytes(int32_t numFrames,
163 int32_t bytesPerFrame,
164 int32_t *sizeInBytes);
Phil Burk5ed503c2017-02-01 09:38:15 -0800165
Phil Burk9dca9822017-05-26 14:27:43 -0700166audio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudio_format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800167
Phil Burk9dca9822017-05-26 14:27:43 -0700168aaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800169
170/**
171 * @return the size of a sample of the given format in bytes or AAUDIO_ERROR_ILLEGAL_ARGUMENT
172 */
Phil Burk9dca9822017-05-26 14:27:43 -0700173int32_t AAudioConvert_formatToSizeInBytes(aaudio_format_t format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800174
Phil Burkc8f69a02017-05-11 15:53:06 -0700175
176// Note that this code may be replaced by Settings or by some other system configuration tool.
177
Phil Burkd04aeea2017-05-23 13:56:41 -0700178#define AAUDIO_PROP_MMAP_POLICY "aaudio.mmap_policy"
Phil Burkc8f69a02017-05-11 15:53:06 -0700179
180/**
181 * Read system property.
Phil Burkd04aeea2017-05-23 13:56:41 -0700182 * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS
Phil Burkc8f69a02017-05-11 15:53:06 -0700183 */
Phil Burkd04aeea2017-05-23 13:56:41 -0700184int32_t AAudioProperty_getMMapPolicy();
Phil Burkc8f69a02017-05-11 15:53:06 -0700185
Phil Burkd04aeea2017-05-23 13:56:41 -0700186#define AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY "aaudio.mmap_exclusive_policy"
Phil Burkc8f69a02017-05-11 15:53:06 -0700187
188/**
189 * Read system property.
Phil Burkd04aeea2017-05-23 13:56:41 -0700190 * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS
Phil Burkc8f69a02017-05-11 15:53:06 -0700191 */
Phil Burkd04aeea2017-05-23 13:56:41 -0700192int32_t AAudioProperty_getMMapExclusivePolicy();
Phil Burkc8f69a02017-05-11 15:53:06 -0700193
194#define AAUDIO_PROP_MIXER_BURSTS "aaudio.mixer_bursts"
195
196/**
197 * Read system property.
198 * @return number of bursts per mixer cycle
199 */
200int32_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 */
214int32_t AAudioProperty_getHardwareBurstMinMicros();
215
Andy Hung47c5e532017-06-26 18:28:00 -0700216/**
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 */
226static 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 Burk5ed503c2017-02-01 09:38:15 -0800239#endif //UTILITY_AAUDIO_UTILITIES_H