blob: f2347f52030a95aa2efb4a3a51291307cf74bc3f [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
Phil Burkd4ccc622017-12-20 15:32:44 -0800170
171/**
172 * Note that this function does not validate the passed in value.
173 * That is done somewhere else.
174 * @return internal value
175 */
176
177audio_usage_t AAudioConvert_usageToInternal(aaudio_usage_t usage);
178
179/**
180 * Note that this function does not validate the passed in value.
181 * That is done somewhere else.
182 * @return internal value
183 */
184audio_content_type_t AAudioConvert_contentTypeToInternal(aaudio_content_type_t contentType);
185
186/**
187 * Note that this function does not validate the passed in value.
188 * That is done somewhere else.
189 * @return internal audio source
190 */
191audio_source_t AAudioConvert_inputPresetToAudioSource(aaudio_input_preset_t preset);
192
Phil Burk5ed503c2017-02-01 09:38:15 -0800193/**
194 * @return the size of a sample of the given format in bytes or AAUDIO_ERROR_ILLEGAL_ARGUMENT
195 */
Phil Burk9dca9822017-05-26 14:27:43 -0700196int32_t AAudioConvert_formatToSizeInBytes(aaudio_format_t format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800197
Phil Burkc8f69a02017-05-11 15:53:06 -0700198
199// Note that this code may be replaced by Settings or by some other system configuration tool.
200
Phil Burkd04aeea2017-05-23 13:56:41 -0700201#define AAUDIO_PROP_MMAP_POLICY "aaudio.mmap_policy"
Phil Burkc8f69a02017-05-11 15:53:06 -0700202
203/**
204 * Read system property.
Phil Burkd04aeea2017-05-23 13:56:41 -0700205 * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS
Phil Burkc8f69a02017-05-11 15:53:06 -0700206 */
Phil Burkd04aeea2017-05-23 13:56:41 -0700207int32_t AAudioProperty_getMMapPolicy();
Phil Burkc8f69a02017-05-11 15:53:06 -0700208
Phil Burkd04aeea2017-05-23 13:56:41 -0700209#define AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY "aaudio.mmap_exclusive_policy"
Phil Burkc8f69a02017-05-11 15:53:06 -0700210
211/**
212 * Read system property.
Phil Burkd04aeea2017-05-23 13:56:41 -0700213 * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS
Phil Burkc8f69a02017-05-11 15:53:06 -0700214 */
Phil Burkd04aeea2017-05-23 13:56:41 -0700215int32_t AAudioProperty_getMMapExclusivePolicy();
Phil Burkc8f69a02017-05-11 15:53:06 -0700216
217#define AAUDIO_PROP_MIXER_BURSTS "aaudio.mixer_bursts"
218
219/**
220 * Read system property.
Phil Burkfd34a932017-07-19 07:03:52 -0700221 * @return number of bursts per AAudio service mixer cycle
Phil Burkc8f69a02017-05-11 15:53:06 -0700222 */
223int32_t AAudioProperty_getMixerBursts();
224
225#define AAUDIO_PROP_HW_BURST_MIN_USEC "aaudio.hw_burst_min_usec"
226
227/**
Phil Burkfd34a932017-07-19 07:03:52 -0700228 * Read a system property that specifies the number of extra microseconds that a thread
229 * should sleep when waiting for another thread to service a FIFO. This is used
230 * to avoid the waking thread from being overly optimistic about the other threads
231 * wakeup timing. This value should be set high enough to cover typical scheduling jitter
232 * for a real-time thread.
233 *
234 * @return number of microseconds to delay the wakeup.
235 */
236int32_t AAudioProperty_getWakeupDelayMicros();
237
238#define AAUDIO_PROP_WAKEUP_DELAY_USEC "aaudio.wakeup_delay_usec"
239
240/**
241 * Read a system property that specifies the minimum sleep time when polling the FIFO.
242 *
243 * @return minimum number of microseconds to sleep.
244 */
245int32_t AAudioProperty_getMinimumSleepMicros();
246
247#define AAUDIO_PROP_MINIMUM_SLEEP_USEC "aaudio.minimum_sleep_usec"
248
249/**
Phil Burkc8f69a02017-05-11 15:53:06 -0700250 * Read system property.
251 * This is handy in case the DMA is bursting too quickly for the CPU to keep up.
252 * For example, there may be a DMA burst every 100 usec but you only
253 * want to feed the MMAP buffer every 2000 usec.
254 *
255 * This will affect the framesPerBurst for an MMAP stream.
256 *
257 * @return minimum number of microseconds for a MMAP HW burst
258 */
259int32_t AAudioProperty_getHardwareBurstMinMicros();
260
Andy Hung47c5e532017-06-26 18:28:00 -0700261/**
262 * Try a function f until it returns true.
263 *
264 * The function is always called at least once.
265 *
266 * @param f the function to evaluate, which returns a bool.
267 * @param times the number of times to evaluate f.
268 * @param sleepMs the sleep time per check of f, if greater than 0.
269 * @return true if f() eventually returns true.
270 */
271static inline bool AAudio_tryUntilTrue(
272 std::function<bool()> f, int times, int sleepMs) {
273 static const useconds_t US_PER_MS = 1000;
274
275 sleepMs = std::max(sleepMs, 0);
276 for (;;) {
277 if (f()) return true;
278 if (times <= 1) return false;
279 --times;
280 usleep(sleepMs * US_PER_MS);
281 }
282}
283
Phil Burk97350f92017-07-21 15:59:44 -0700284
285/**
286 * Simple double buffer for a structure that can be written occasionally and read occasionally.
287 * This allows a SINGLE writer with multiple readers.
288 *
289 * It is OK if the FIFO overflows and we lose old values.
290 * It is also OK if we read an old value.
291 * Thread may return a non-atomic result if the other thread is rapidly writing
292 * new values on another core.
293 */
294template <class T>
295class SimpleDoubleBuffer {
296public:
297 SimpleDoubleBuffer()
Phil Burkbcc36742017-08-31 17:24:51 -0700298 : mValues() {}
Phil Burk97350f92017-07-21 15:59:44 -0700299
300 __attribute__((no_sanitize("integer")))
301 void write(T value) {
302 int index = mCounter.load() & 1;
303 mValues[index] = value;
304 mCounter++; // Increment AFTER updating storage, OK if it wraps.
305 }
306
Phil Burkbcc36742017-08-31 17:24:51 -0700307 /**
308 * This should only be called by the same thread that calls write() or when
309 * no other thread is calling write.
310 */
311 void clear() {
312 mCounter.store(0);
313 }
314
Phil Burk97350f92017-07-21 15:59:44 -0700315 T read() const {
316 T result;
317 int before;
318 int after;
319 int timeout = 3;
320 do {
321 // Check to see if a write occurred while were reading.
322 before = mCounter.load();
323 int index = (before & 1) ^ 1;
324 result = mValues[index];
325 after = mCounter.load();
Phil Burkbcc36742017-08-31 17:24:51 -0700326 } while ((after != before) && (after > 0) && (--timeout > 0));
Phil Burk97350f92017-07-21 15:59:44 -0700327 return result;
328 }
329
330 /**
331 * @return true if at least one value has been written
332 */
333 bool isValid() const {
334 return mCounter.load() > 0;
335 }
336
337private:
338 T mValues[2];
Phil Burkbcc36742017-08-31 17:24:51 -0700339 std::atomic<int> mCounter{0};
Phil Burk97350f92017-07-21 15:59:44 -0700340};
341
342class Timestamp {
343public:
344 Timestamp()
345 : mPosition(0)
346 , mNanoseconds(0) {}
347 Timestamp(int64_t position, int64_t nanoseconds)
348 : mPosition(position)
349 , mNanoseconds(nanoseconds) {}
350
351 int64_t getPosition() const { return mPosition; }
352
353 int64_t getNanoseconds() const { return mNanoseconds; }
354
355private:
356 // These cannot be const because we need to implement the copy assignment operator.
357 int64_t mPosition;
358 int64_t mNanoseconds;
359};
360
Phil Burkbcc36742017-08-31 17:24:51 -0700361
362/**
363 * Pass a request to another thread.
364 * This is used when one thread, A, wants another thread, B, to do something.
365 * A naive approach would be for A to set a flag and for B to clear it when done.
366 * But that creates a race condition. This technique avoids the race condition.
367 *
368 * Assumes only one requester and one acknowledger.
369 */
370class AtomicRequestor {
371public:
Phil Burk2d5ba532017-09-06 14:36:11 -0700372
373 __attribute__((no_sanitize("integer")))
Phil Burkbcc36742017-08-31 17:24:51 -0700374 void request() {
Phil Burkbcc36742017-08-31 17:24:51 -0700375 mRequested++;
376 }
377
Phil Burk2d5ba532017-09-06 14:36:11 -0700378 __attribute__((no_sanitize("integer")))
Phil Burkbcc36742017-08-31 17:24:51 -0700379 bool isRequested() {
Phil Burk2d5ba532017-09-06 14:36:11 -0700380 return (mRequested.load() - mAcknowledged.load()) > 0;
Phil Burkbcc36742017-08-31 17:24:51 -0700381 }
382
Phil Burk2d5ba532017-09-06 14:36:11 -0700383 __attribute__((no_sanitize("integer")))
Phil Burkbcc36742017-08-31 17:24:51 -0700384 void acknowledge() {
385 mAcknowledged++;
386 }
387
388private:
389 std::atomic<int> mRequested{0};
390 std::atomic<int> mAcknowledged{0};
391};
Phil Burk5ed503c2017-02-01 09:38:15 -0800392#endif //UTILITY_AAUDIO_UTILITIES_H