15ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk/*
25ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Copyright 2016 The Android Open Source Project
35ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *
45ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Licensed under the Apache License, Version 2.0 (the "License");
55ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * you may not use this file except in compliance with the License.
65ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * You may obtain a copy of the License at
75ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *
85ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *      http://www.apache.org/licenses/LICENSE-2.0
95ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *
105ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Unless required by applicable law or agreed to in writing, software
115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * distributed under the License is distributed on an "AS IS" BASIS,
125ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * See the License for the specific language governing permissions and
145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * limitations under the License.
155ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk */
165ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#ifndef UTILITY_AAUDIO_UTILITIES_H
185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define UTILITY_AAUDIO_UTILITIES_H
195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <stdint.h>
215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <sys/types.h>
225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <utils/Errors.h>
245ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <hardware/audio.h>
255ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
26a4eb0d86a29be2763be5fac51727858d5095794bPhil Burk#include "aaudio/AAudio.h"
275ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk/**
295ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Convert an AAudio result into the closest matching Android status.
305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk */
315ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkandroid::status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result);
325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk/**
345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Convert an Android status into the closest matching AAudio result.
355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk */
365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkaaudio_result_t AAudioConvert_androidToAAudioResult(android::status_t status);
375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
38e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
39e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Convert an array of floats to an array of int16_t.
40e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
41e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
42e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
43e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numSamples number of values in the array
44e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude level between 0.0 and 1.0
45e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
46e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burkvoid AAudioConvert_floatToPcm16(const float *source,
47e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int16_t *destination,
48e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int32_t numSamples,
49e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float amplitude);
50e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk
51e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
52e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Convert floats to int16_t and scale by a linear ramp.
53e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
54e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * The ramp stops just short of reaching amplitude2 so that the next
55e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * ramp can start at amplitude2 without causing a discontinuity.
56e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
57e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
58e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
59e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numFrames
60e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param samplesPerFrame AKA number of channels
61e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude1 level at start of ramp, between 0.0 and 1.0
62e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude2 level past end of ramp, between 0.0 and 1.0
63e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
64e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burkvoid AAudioConvert_floatToPcm16(const float *source,
65e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int16_t *destination,
66e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int32_t numFrames,
67e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int32_t samplesPerFrame,
68e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float amplitude1,
69e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float amplitude2);
70e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk
71e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
72e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Convert int16_t array to float array ranging from -1.0 to +1.0.
73e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
74e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
75e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numSamples
76e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
77e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk//void AAudioConvert_pcm16ToFloat(const int16_t *source, int32_t numSamples,
78e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk//                                float *destination);
79e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk
80e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
81e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
82e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Convert int16_t array to float array ranging from +/- amplitude.
83e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
84e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
85e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numSamples
86e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude
87e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
88e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burkvoid AAudioConvert_pcm16ToFloat(const int16_t *source,
89e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float *destination,
90e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int32_t numSamples,
91e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float amplitude);
925ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
93e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
94e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Convert floats to int16_t and scale by a linear ramp.
95e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
96e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * The ramp stops just short of reaching amplitude2 so that the next
97e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * ramp can start at amplitude2 without causing a discontinuity.
98e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
99e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
100e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
101e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numFrames
102e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param samplesPerFrame AKA number of channels
103e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude1 level at start of ramp, between 0.0 and 1.0
104e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude2 level at end of ramp, between 0.0 and 1.0
105e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
106e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burkvoid AAudioConvert_pcm16ToFloat(const int16_t *source,
107e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float *destination,
108e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int32_t numFrames,
109e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                int32_t samplesPerFrame,
110e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float amplitude1,
111e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                                float amplitude2);
112e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk
113e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
114e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Scale floats by a linear ramp.
115e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
116e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * The ramp stops just short of reaching amplitude2 so that the next
117e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * ramp can start at amplitude2 without causing a discontinuity.
118e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
119e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
120e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
121e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numFrames
122e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param samplesPerFrame
123e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude1
124e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude2
125e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
126e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burkvoid AAudio_linearRamp(const float *source,
127e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       float *destination,
128e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       int32_t numFrames,
129e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       int32_t samplesPerFrame,
130e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       float amplitude1,
131e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       float amplitude2);
132e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk
133e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk/**
134e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * Scale int16_t's by a linear ramp.
135e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
136e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * The ramp stops just short of reaching amplitude2 so that the next
137e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * ramp can start at amplitude2 without causing a discontinuity.
138e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk *
139e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param source
140e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param destination
141e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param numFrames
142e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param samplesPerFrame
143e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude1
144e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk * @param amplitude2
145e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk */
146e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burkvoid AAudio_linearRamp(const int16_t *source,
147e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       int16_t *destination,
148e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       int32_t numFrames,
149e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       int32_t samplesPerFrame,
150e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       float amplitude1,
151e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk                       float amplitude2);
1525ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
1535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk/**
1545ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Calculate the number of bytes and prevent numeric overflow.
1555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * @param numFrames frame count
1565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * @param bytesPerFrame size of a frame in bytes
1575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * @param sizeInBytes total size in bytes
1585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * @return AAUDIO_OK or negative error, eg. AAUDIO_ERROR_OUT_OF_RANGE
1595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk */
1603316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burkint32_t AAudioConvert_framesToBytes(int32_t numFrames,
1613316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                            int32_t bytesPerFrame,
1623316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                            int32_t *sizeInBytes);
1635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
1649dca9824da74d50be02bc81f539cc77b7bde678aPhil Burkaudio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudio_format);
1655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
1669dca9824da74d50be02bc81f539cc77b7bde678aPhil Burkaaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t format);
1675ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
1685ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk/**
1695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * @return the size of a sample of the given format in bytes or AAUDIO_ERROR_ILLEGAL_ARGUMENT
1705ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk */
1719dca9824da74d50be02bc81f539cc77b7bde678aPhil Burkint32_t AAudioConvert_formatToSizeInBytes(aaudio_format_t format);
1725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
173c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
174c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk// Note that this code may be replaced by Settings or by some other system configuration tool.
175c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
176d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#define AAUDIO_PROP_MMAP_POLICY           "aaudio.mmap_policy"
177c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
178c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk/**
179c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * Read system property.
180d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS
181c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk */
182d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burkint32_t AAudioProperty_getMMapPolicy();
183c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
184d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#define AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY "aaudio.mmap_exclusive_policy"
185c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
186c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk/**
187c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * Read system property.
188d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS
189c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk */
190d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burkint32_t AAudioProperty_getMMapExclusivePolicy();
191c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
192c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk#define AAUDIO_PROP_MIXER_BURSTS           "aaudio.mixer_bursts"
193c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
194c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk/**
195c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * Read system property.
196c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * @return number of bursts per mixer cycle
197c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk */
198c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burkint32_t AAudioProperty_getMixerBursts();
199c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
200c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk#define AAUDIO_PROP_HW_BURST_MIN_USEC      "aaudio.hw_burst_min_usec"
201c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
202c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk/**
203c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * Read system property.
204c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * This is handy in case the DMA is bursting too quickly for the CPU to keep up.
205c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * For example, there may be a DMA burst every 100 usec but you only
206c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * want to feed the MMAP buffer every 2000 usec.
207c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk *
208c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * This will affect the framesPerBurst for an MMAP stream.
209c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk *
210c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk * @return minimum number of microseconds for a MMAP HW burst
211c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk */
212c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burkint32_t AAudioProperty_getHardwareBurstMinMicros();
213c8f69a08a409fd163873d725c63f8d60259ae21dPhil Burk
2145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif //UTILITY_AAUDIO_UTILITIES_H
215