Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | /** |
Phil Burk | a45be8b | 2017-04-05 14:45:48 -0700 | [diff] [blame] | 18 | * @addtogroup Audio |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file AAudio.h |
| 24 | */ |
| 25 | |
| 26 | /** |
| 27 | * This is the 'C' API for AAudio. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 28 | */ |
| 29 | #ifndef AAUDIO_AAUDIO_H |
| 30 | #define AAUDIO_AAUDIO_H |
| 31 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 32 | #include <time.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 38 | /** |
| 39 | * This is used to represent a value that has not been specified. |
| 40 | * For example, an application could use AAUDIO_UNSPECIFIED to indicate |
| 41 | * that is did not not care what the specific value of a parameter was |
| 42 | * and would accept whatever it was given. |
| 43 | */ |
| 44 | #define AAUDIO_UNSPECIFIED 0 |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 45 | |
| 46 | enum { |
| 47 | AAUDIO_DIRECTION_OUTPUT, |
| 48 | AAUDIO_DIRECTION_INPUT |
| 49 | }; |
| 50 | typedef int32_t aaudio_direction_t; |
| 51 | |
| 52 | enum { |
| 53 | AAUDIO_FORMAT_INVALID = -1, |
| 54 | AAUDIO_FORMAT_UNSPECIFIED = 0, |
| 55 | AAUDIO_FORMAT_PCM_I16, |
Phil Burk | 7473345 | 2017-04-18 19:50:28 -0700 | [diff] [blame] | 56 | AAUDIO_FORMAT_PCM_FLOAT |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 57 | }; |
| 58 | typedef int32_t aaudio_format_t; |
| 59 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 60 | enum { |
| 61 | AAUDIO_OK, |
| 62 | AAUDIO_ERROR_BASE = -900, // TODO review |
| 63 | AAUDIO_ERROR_DISCONNECTED, |
| 64 | AAUDIO_ERROR_ILLEGAL_ARGUMENT, |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 65 | // reserved |
| 66 | AAUDIO_ERROR_INTERNAL = AAUDIO_ERROR_ILLEGAL_ARGUMENT + 2, |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 67 | AAUDIO_ERROR_INVALID_STATE, |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 68 | // reserved |
| 69 | // reserved |
| 70 | AAUDIO_ERROR_INVALID_HANDLE = AAUDIO_ERROR_INVALID_STATE + 3, |
| 71 | // reserved |
| 72 | AAUDIO_ERROR_UNIMPLEMENTED = AAUDIO_ERROR_INVALID_HANDLE + 2, |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 73 | AAUDIO_ERROR_UNAVAILABLE, |
| 74 | AAUDIO_ERROR_NO_FREE_HANDLES, |
| 75 | AAUDIO_ERROR_NO_MEMORY, |
| 76 | AAUDIO_ERROR_NULL, |
| 77 | AAUDIO_ERROR_TIMEOUT, |
| 78 | AAUDIO_ERROR_WOULD_BLOCK, |
| 79 | AAUDIO_ERROR_INVALID_FORMAT, |
| 80 | AAUDIO_ERROR_OUT_OF_RANGE, |
| 81 | AAUDIO_ERROR_NO_SERVICE, |
| 82 | AAUDIO_ERROR_INVALID_RATE |
| 83 | }; |
| 84 | typedef int32_t aaudio_result_t; |
| 85 | |
| 86 | enum |
| 87 | { |
| 88 | AAUDIO_STREAM_STATE_UNINITIALIZED = 0, |
| 89 | AAUDIO_STREAM_STATE_UNKNOWN, |
| 90 | AAUDIO_STREAM_STATE_OPEN, |
| 91 | AAUDIO_STREAM_STATE_STARTING, |
| 92 | AAUDIO_STREAM_STATE_STARTED, |
| 93 | AAUDIO_STREAM_STATE_PAUSING, |
| 94 | AAUDIO_STREAM_STATE_PAUSED, |
| 95 | AAUDIO_STREAM_STATE_FLUSHING, |
| 96 | AAUDIO_STREAM_STATE_FLUSHED, |
| 97 | AAUDIO_STREAM_STATE_STOPPING, |
| 98 | AAUDIO_STREAM_STATE_STOPPED, |
| 99 | AAUDIO_STREAM_STATE_CLOSING, |
| 100 | AAUDIO_STREAM_STATE_CLOSED, |
| 101 | AAUDIO_STREAM_STATE_DISCONNECTED |
| 102 | }; |
| 103 | typedef int32_t aaudio_stream_state_t; |
| 104 | |
| 105 | |
| 106 | enum { |
| 107 | /** |
| 108 | * This will be the only stream using a particular source or sink. |
| 109 | * This mode will provide the lowest possible latency. |
| 110 | * You should close EXCLUSIVE streams immediately when you are not using them. |
| 111 | */ |
| 112 | AAUDIO_SHARING_MODE_EXCLUSIVE, |
| 113 | /** |
| 114 | * Multiple applications will be mixed by the AAudio Server. |
| 115 | * This will have higher latency than the EXCLUSIVE mode. |
| 116 | */ |
| 117 | AAUDIO_SHARING_MODE_SHARED |
| 118 | }; |
| 119 | typedef int32_t aaudio_sharing_mode_t; |
| 120 | |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 121 | |
| 122 | enum { |
| 123 | /** |
| 124 | * No particular performance needs. Default. |
| 125 | */ |
| 126 | AAUDIO_PERFORMANCE_MODE_NONE = 10, |
| 127 | |
| 128 | /** |
| 129 | * Extending battery life is most important. |
| 130 | */ |
| 131 | AAUDIO_PERFORMANCE_MODE_POWER_SAVING, |
| 132 | |
| 133 | /** |
| 134 | * Reducing latency is most important. |
| 135 | */ |
| 136 | AAUDIO_PERFORMANCE_MODE_LOW_LATENCY |
| 137 | }; |
| 138 | typedef int32_t aaudio_performance_mode_t; |
| 139 | |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 140 | typedef struct AAudioStreamStruct AAudioStream; |
| 141 | typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 142 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 143 | #ifndef AAUDIO_API |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 144 | #define AAUDIO_API /* export this symbol */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 145 | #endif |
| 146 | |
| 147 | // ============================================================ |
| 148 | // Audio System |
| 149 | // ============================================================ |
| 150 | |
| 151 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 152 | * The text is the ASCII symbol corresponding to the returnCode, |
| 153 | * or an English message saying the returnCode is unrecognized. |
| 154 | * This is intended for developers to use when debugging. |
| 155 | * It is not for display to users. |
| 156 | * |
| 157 | * @return pointer to a text representation of an AAudio result code. |
| 158 | */ |
| 159 | AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode); |
| 160 | |
| 161 | /** |
| 162 | * The text is the ASCII symbol corresponding to the stream state, |
| 163 | * or an English message saying the state is unrecognized. |
| 164 | * This is intended for developers to use when debugging. |
| 165 | * It is not for display to users. |
| 166 | * |
| 167 | * @return pointer to a text representation of an AAudio state. |
| 168 | */ |
| 169 | AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state); |
| 170 | |
| 171 | // ============================================================ |
| 172 | // StreamBuilder |
| 173 | // ============================================================ |
| 174 | |
| 175 | /** |
| 176 | * Create a StreamBuilder that can be used to open a Stream. |
| 177 | * |
| 178 | * The deviceId is initially unspecified, meaning that the current default device will be used. |
| 179 | * |
| 180 | * The default direction is AAUDIO_DIRECTION_OUTPUT. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 181 | * The default sharing mode is AAUDIO_SHARING_MODE_SHARED. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 182 | * The data format, samplesPerFrames and sampleRate are unspecified and will be |
| 183 | * chosen by the device when it is opened. |
| 184 | * |
| 185 | * AAudioStreamBuilder_delete() must be called when you are done using the builder. |
| 186 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 187 | AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Request an audio device identified device using an ID. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 191 | * On Android, for example, the ID could be obtained from the Java AudioManager. |
| 192 | * |
Glenn Kasten | 5f510d2 | 2017-05-30 15:52:15 -0700 | [diff] [blame] | 193 | * The default, if you do not call this function, is AAUDIO_UNSPECIFIED, |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 194 | * in which case the primary device will be used. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 195 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 196 | * @param builder reference provided by AAudio_createStreamBuilder() |
Glenn Kasten | 5f510d2 | 2017-05-30 15:52:15 -0700 | [diff] [blame] | 197 | * @param deviceId device identifier or AAUDIO_UNSPECIFIED |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 198 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 199 | AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 200 | int32_t deviceId); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 201 | |
| 202 | /** |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 203 | * Request a sample rate in Hertz. |
| 204 | * |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 205 | * The default, if you do not call this function, is AAUDIO_UNSPECIFIED. |
Phil Burk | 8f62489 | 2017-05-11 11:44:20 -0700 | [diff] [blame] | 206 | * An optimal value will then be chosen when the stream is opened. |
| 207 | * After opening a stream with an unspecified value, the application must |
| 208 | * query for the actual value, which may vary by device. |
| 209 | * |
| 210 | * If an exact value is specified then an opened stream will use that value. |
| 211 | * If a stream cannot be opened with the specified value then the open will fail. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 212 | * |
| 213 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 214 | * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 215 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 216 | AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 217 | int32_t sampleRate); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 218 | |
| 219 | /** |
Phil Burk | 20523ed | 2017-04-24 17:04:01 -0700 | [diff] [blame] | 220 | * Request a number of channels for the stream. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 221 | * |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 222 | * The default, if you do not call this function, is AAUDIO_UNSPECIFIED. |
Phil Burk | 8f62489 | 2017-05-11 11:44:20 -0700 | [diff] [blame] | 223 | * An optimal value will then be chosen when the stream is opened. |
| 224 | * After opening a stream with an unspecified value, the application must |
| 225 | * query for the actual value, which may vary by device. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 226 | * |
Phil Burk | 8f62489 | 2017-05-11 11:44:20 -0700 | [diff] [blame] | 227 | * If an exact value is specified then an opened stream will use that value. |
| 228 | * If a stream cannot be opened with the specified value then the open will fail. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 229 | * |
| 230 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 20523ed | 2017-04-24 17:04:01 -0700 | [diff] [blame] | 231 | * @param channelCount Number of channels desired. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 232 | */ |
Phil Burk | 20523ed | 2017-04-24 17:04:01 -0700 | [diff] [blame] | 233 | AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder, |
| 234 | int32_t channelCount); |
| 235 | |
| 236 | /** |
Phil Burk | e74240d | 2017-08-03 15:25:43 -0700 | [diff] [blame^] | 237 | * Identical to AAudioStreamBuilder_setChannelCount(). |
| 238 | * |
| 239 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 240 | * @param samplesPerFrame Number of samples in a frame. |
| 241 | */ |
| 242 | AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder, |
| 243 | int32_t samplesPerFrame); |
| 244 | |
| 245 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 246 | * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 247 | * |
| 248 | * The default, if you do not call this function, is AAUDIO_UNSPECIFIED. |
Phil Burk | 8f62489 | 2017-05-11 11:44:20 -0700 | [diff] [blame] | 249 | * An optimal value will then be chosen when the stream is opened. |
| 250 | * After opening a stream with an unspecified value, the application must |
| 251 | * query for the actual value, which may vary by device. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 252 | * |
Phil Burk | 8f62489 | 2017-05-11 11:44:20 -0700 | [diff] [blame] | 253 | * If an exact value is specified then an opened stream will use that value. |
| 254 | * If a stream cannot be opened with the specified value then the open will fail. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 255 | * |
| 256 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 8f62489 | 2017-05-11 11:44:20 -0700 | [diff] [blame] | 257 | * @param format common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 258 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 259 | AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder, |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 260 | aaudio_format_t format); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 261 | |
| 262 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 263 | * Request a mode for sharing the device. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 264 | * |
| 265 | * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED. |
| 266 | * |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 267 | * The requested sharing mode may not be available. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 268 | * The application can query for the actual mode after the stream is opened. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 269 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 270 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 271 | * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 272 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 273 | AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 274 | aaudio_sharing_mode_t sharingMode); |
| 275 | |
| 276 | /** |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 277 | * Request the direction for a stream. |
| 278 | * |
| 279 | * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 280 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 281 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 282 | * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 283 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 284 | AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder, |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 285 | aaudio_direction_t direction); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 286 | |
| 287 | /** |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 288 | * Set the requested buffer capacity in frames. |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 289 | * The final AAudioStream capacity may differ, but will probably be at least this big. |
| 290 | * |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 291 | * The default, if you do not call this function, is AAUDIO_UNSPECIFIED. |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 292 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 293 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 294 | * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 295 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 296 | AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder, |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 297 | int32_t numFrames); |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * Set the requested performance mode. |
| 301 | * |
| 302 | * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE. |
| 303 | * |
| 304 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 305 | * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY |
| 306 | */ |
| 307 | AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder, |
| 308 | aaudio_performance_mode_t mode); |
| 309 | |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Return one of these values from the data callback function. |
| 312 | */ |
| 313 | enum { |
| 314 | |
| 315 | /** |
| 316 | * Continue calling the callback. |
| 317 | */ |
| 318 | AAUDIO_CALLBACK_RESULT_CONTINUE = 0, |
| 319 | |
| 320 | /** |
| 321 | * Stop calling the callback. |
| 322 | * |
| 323 | * The application will still need to call AAudioStream_requestPause() |
| 324 | * or AAudioStream_requestStop(). |
| 325 | */ |
| 326 | AAUDIO_CALLBACK_RESULT_STOP, |
| 327 | |
| 328 | }; |
| 329 | typedef int32_t aaudio_data_callback_result_t; |
| 330 | |
| 331 | /** |
| 332 | * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback(). |
| 333 | * |
| 334 | * For an output stream, this function should render and write numFrames of data |
| 335 | * in the streams current data format to the audioData buffer. |
| 336 | * |
| 337 | * For an input stream, this function should read and process numFrames of data |
| 338 | * from the audioData buffer. |
| 339 | * |
| 340 | * Note that this callback function should be considered a "real-time" function. |
| 341 | * It must not do anything that could cause an unbounded delay because that can cause the |
| 342 | * audio to glitch or pop. |
| 343 | * |
| 344 | * These are things the function should NOT do: |
| 345 | * <ul> |
| 346 | * <li>allocate memory using, for example, malloc() or new</li> |
| 347 | * <li>any file operations such as opening, closing, reading or writing</li> |
| 348 | * <li>any network operations such as streaming</li> |
| 349 | * <li>use any mutexes or other synchronization primitives</li> |
| 350 | * <li>sleep</li> |
| 351 | * </ul> |
| 352 | * |
| 353 | * If you need to move data, eg. MIDI commands, in or out of the callback function then |
| 354 | * we recommend the use of non-blocking techniques such as an atomic FIFO. |
| 355 | * |
| 356 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 357 | * @param userData the same address that was passed to AAudioStreamBuilder_setCallback() |
| 358 | * @param audioData a pointer to the audio data |
| 359 | * @param numFrames the number of frames to be processed |
| 360 | * @return AAUDIO_CALLBACK_RESULT_* |
| 361 | */ |
| 362 | typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)( |
| 363 | AAudioStream *stream, |
| 364 | void *userData, |
| 365 | void *audioData, |
| 366 | int32_t numFrames); |
| 367 | |
| 368 | /** |
| 369 | * Request that AAudio call this functions when the stream is running. |
| 370 | * |
| 371 | * Note that when using this callback, the audio data will be passed in or out |
| 372 | * of the function as an argument. |
| 373 | * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream |
| 374 | * that has an active data callback. |
| 375 | * |
| 376 | * The callback function will start being called after AAudioStream_requestStart() is called. |
| 377 | * It will stop being called after AAudioStream_requestPause() or |
| 378 | * AAudioStream_requestStop() is called. |
| 379 | * |
| 380 | * This callback function will be called on a real-time thread owned by AAudio. See |
Glenn Kasten | 0d80436 | 2017-04-13 09:20:14 -0700 | [diff] [blame] | 381 | * {@link AAudioStream_dataCallback} for more information. |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 382 | * |
| 383 | * Note that the AAudio callbacks will never be called simultaneously from multiple threads. |
| 384 | * |
| 385 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 386 | * @param callback pointer to a function that will process audio data. |
| 387 | * @param userData pointer to an application data structure that will be passed |
| 388 | * to the callback functions. |
| 389 | */ |
| 390 | AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder, |
| 391 | AAudioStream_dataCallback callback, |
| 392 | void *userData); |
| 393 | |
| 394 | /** |
| 395 | * Set the requested data callback buffer size in frames. |
| 396 | * See {@link AAudioStream_dataCallback}. |
| 397 | * |
| 398 | * The default, if you do not call this function, is AAUDIO_UNSPECIFIED. |
| 399 | * |
| 400 | * For the lowest possible latency, do not call this function. AAudio will then |
| 401 | * call the dataProc callback function with whatever size is optimal. |
| 402 | * That size may vary from one callback to another. |
| 403 | * |
| 404 | * Only use this function if the application requires a specific number of frames for processing. |
| 405 | * The application might, for example, be using an FFT that requires |
| 406 | * a specific power-of-two sized buffer. |
| 407 | * |
| 408 | * AAudio may need to add additional buffering in order to adapt between the internal |
| 409 | * buffer size and the requested buffer size. |
| 410 | * |
| 411 | * If you do call this function then the requested size should be less than |
| 412 | * half the buffer capacity, to allow double buffering. |
| 413 | * |
| 414 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 415 | * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED |
| 416 | */ |
| 417 | AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder, |
| 418 | int32_t numFrames); |
| 419 | |
| 420 | /** |
| 421 | * Prototype for the callback function that is passed to |
| 422 | * AAudioStreamBuilder_setErrorCallback(). |
| 423 | * |
| 424 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 425 | * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback() |
| 426 | * @param error an AAUDIO_ERROR_* value. |
| 427 | */ |
| 428 | typedef void (*AAudioStream_errorCallback)( |
| 429 | AAudioStream *stream, |
| 430 | void *userData, |
| 431 | aaudio_result_t error); |
| 432 | |
| 433 | /** |
| 434 | * Request that AAudio call this functions if any error occurs on a callback thread. |
| 435 | * |
| 436 | * It will be called, for example, if a headset or a USB device is unplugged causing the stream's |
| 437 | * device to be unavailable. |
| 438 | * In response, this function could signal or launch another thread to reopen a |
| 439 | * stream on another device. Do not reopen the stream in this callback. |
| 440 | * |
| 441 | * This will not be called because of actions by the application, such as stopping |
| 442 | * or closing a stream. |
| 443 | * |
| 444 | * Another possible cause of error would be a timeout or an unanticipated internal error. |
| 445 | * |
| 446 | * Note that the AAudio callbacks will never be called simultaneously from multiple threads. |
| 447 | * |
| 448 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 449 | * @param callback pointer to a function that will be called if an error occurs. |
| 450 | * @param userData pointer to an application data structure that will be passed |
| 451 | * to the callback functions. |
| 452 | */ |
| 453 | AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder, |
| 454 | AAudioStream_errorCallback callback, |
| 455 | void *userData); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * Open a stream based on the options in the StreamBuilder. |
| 459 | * |
| 460 | * AAudioStream_close must be called when finished with the stream to recover |
| 461 | * the memory and to free the associated resources. |
| 462 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 463 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 464 | * @param stream pointer to a variable to receive the new stream reference |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 465 | * @return AAUDIO_OK or a negative error. |
| 466 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 467 | AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder, |
| 468 | AAudioStream** stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * Delete the resources associated with the StreamBuilder. |
| 472 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 473 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 474 | * @return AAUDIO_OK or a negative error. |
| 475 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 476 | AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 477 | |
| 478 | // ============================================================ |
| 479 | // Stream Control |
| 480 | // ============================================================ |
| 481 | |
| 482 | /** |
| 483 | * Free the resources associated with a stream created by AAudioStreamBuilder_openStream() |
| 484 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 485 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 486 | * @return AAUDIO_OK or a negative error. |
| 487 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 488 | AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * Asynchronously request to start playing the stream. For output streams, one should |
| 492 | * write to the stream to fill the buffer before starting. |
| 493 | * Otherwise it will underflow. |
| 494 | * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED. |
| 495 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 496 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 497 | * @return AAUDIO_OK or a negative error. |
| 498 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 499 | AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * Asynchronous request for the stream to pause. |
| 503 | * Pausing a stream will freeze the data flow but not flush any buffers. |
| 504 | * Use AAudioStream_Start() to resume playback after a pause. |
| 505 | * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED. |
| 506 | * |
Phil Burk | 068c10f | 2017-05-08 16:36:41 -0700 | [diff] [blame] | 507 | * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams. |
| 508 | * For input streams use AAudioStream_requestStop(). |
| 509 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 510 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 511 | * @return AAUDIO_OK or a negative error. |
| 512 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 513 | AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * Asynchronous request for the stream to flush. |
| 517 | * Flushing will discard any pending data. |
| 518 | * This call only works if the stream is pausing or paused. TODO review |
| 519 | * Frame counters are not reset by a flush. They may be advanced. |
| 520 | * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED. |
| 521 | * |
Phil Burk | 068c10f | 2017-05-08 16:36:41 -0700 | [diff] [blame] | 522 | * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams. |
| 523 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 524 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 525 | * @return AAUDIO_OK or a negative error. |
| 526 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 527 | AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * Asynchronous request for the stream to stop. |
| 531 | * The stream will stop after all of the data currently buffered has been played. |
| 532 | * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED. |
| 533 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 534 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 535 | * @return AAUDIO_OK or a negative error. |
| 536 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 537 | AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 538 | |
| 539 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 540 | * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 541 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 542 | * This function will immediately return the state without updating the state. |
| 543 | * If you want to update the client state based on the server state then |
| 544 | * call AAudioStream_waitForStateChange() with currentState |
| 545 | * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout. |
| 546 | * |
| 547 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 548 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 549 | AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * Wait until the current state no longer matches the input state. |
| 553 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 554 | * This will update the current client state. |
| 555 | * |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 556 | * <pre><code> |
| 557 | * aaudio_stream_state_t currentState; |
| 558 | * aaudio_result_t result = AAudioStream_getState(stream, ¤tState); |
| 559 | * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) { |
| 560 | * result = AAudioStream_waitForStateChange( |
| 561 | * stream, currentState, ¤tState, MY_TIMEOUT_NANOS); |
| 562 | * } |
| 563 | * </code></pre> |
| 564 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 565 | * @param stream A reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 566 | * @param inputState The state we want to avoid. |
| 567 | * @param nextState Pointer to a variable that will be set to the new state. |
| 568 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
| 569 | * @return AAUDIO_OK or a negative error. |
| 570 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 571 | AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 572 | aaudio_stream_state_t inputState, |
| 573 | aaudio_stream_state_t *nextState, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 574 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 575 | |
| 576 | // ============================================================ |
| 577 | // Stream I/O |
| 578 | // ============================================================ |
| 579 | |
| 580 | /** |
| 581 | * Read data from the stream. |
| 582 | * |
| 583 | * The call will wait until the read is complete or until it runs out of time. |
| 584 | * If timeoutNanos is zero then this call will not wait. |
| 585 | * |
| 586 | * Note that timeoutNanoseconds is a relative duration in wall clock time. |
| 587 | * Time will not stop if the thread is asleep. |
| 588 | * So it will be implemented using CLOCK_BOOTTIME. |
| 589 | * |
| 590 | * This call is "strong non-blocking" unless it has to wait for data. |
| 591 | * |
| 592 | * @param stream A stream created using AAudioStreamBuilder_openStream(). |
| 593 | * @param buffer The address of the first sample. |
| 594 | * @param numFrames Number of frames to read. Only complete frames will be written. |
| 595 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 596 | * @return The number of frames actually read or a negative error. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 597 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 598 | AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 599 | void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 600 | int32_t numFrames, |
| 601 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * Write data to the stream. |
| 605 | * |
| 606 | * The call will wait until the write is complete or until it runs out of time. |
| 607 | * If timeoutNanos is zero then this call will not wait. |
| 608 | * |
| 609 | * Note that timeoutNanoseconds is a relative duration in wall clock time. |
| 610 | * Time will not stop if the thread is asleep. |
| 611 | * So it will be implemented using CLOCK_BOOTTIME. |
| 612 | * |
| 613 | * This call is "strong non-blocking" unless it has to wait for room in the buffer. |
| 614 | * |
| 615 | * @param stream A stream created using AAudioStreamBuilder_openStream(). |
| 616 | * @param buffer The address of the first sample. |
| 617 | * @param numFrames Number of frames to write. Only complete frames will be written. |
| 618 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
| 619 | * @return The number of frames actually written or a negative error. |
| 620 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 621 | AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 622 | const void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 623 | int32_t numFrames, |
| 624 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 625 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 626 | // ============================================================ |
| 627 | // Stream - queries |
| 628 | // ============================================================ |
| 629 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 630 | /** |
| 631 | * This can be used to adjust the latency of the buffer by changing |
| 632 | * the threshold where blocking will occur. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 633 | * By combining this with AAudioStream_getXRunCount(), the latency can be tuned |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 634 | * at run-time for each device. |
| 635 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 636 | * This cannot be set higher than AAudioStream_getBufferCapacityInFrames(). |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 637 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 638 | * Note that you will probably not get the exact size you request. |
| 639 | * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is. |
| 640 | * |
| 641 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 642 | * @param numFrames requested number of frames that can be filled without blocking |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 643 | * @return actual buffer size in frames or a negative error |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 644 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 645 | AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream, |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 646 | int32_t numFrames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 647 | |
| 648 | /** |
| 649 | * Query the maximum number of frames that can be filled without blocking. |
| 650 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 651 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 652 | * @return buffer size in frames. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 653 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 654 | AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 655 | |
| 656 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 657 | * Query the number of frames that the application should read or write at |
| 658 | * one time for optimal performance. It is OK if an application writes |
| 659 | * a different number of frames. But the buffer size may need to be larger |
| 660 | * in order to avoid underruns or overruns. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 661 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 662 | * Note that this may or may not match the actual device burst size. |
| 663 | * For some endpoints, the burst size can vary dynamically. |
| 664 | * But these tend to be devices with high latency. |
| 665 | * |
| 666 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 667 | * @return burst size |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 668 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 669 | AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 670 | |
| 671 | /** |
| 672 | * Query maximum buffer capacity in frames. |
| 673 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 674 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 675 | * @return buffer capacity in frames |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 676 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 677 | AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 678 | |
| 679 | /** |
Phil Burk | e057ca9 | 2017-03-28 11:31:34 -0700 | [diff] [blame] | 680 | * Query the size of the buffer that will be passed to the dataProc callback |
| 681 | * in the numFrames parameter. |
| 682 | * |
| 683 | * This call can be used if the application needs to know the value of numFrames before |
| 684 | * the stream is started. This is not normally necessary. |
| 685 | * |
| 686 | * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames() |
| 687 | * then this will be the same size. |
| 688 | * |
| 689 | * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will |
| 690 | * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED. |
| 691 | * |
| 692 | * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream |
| 693 | * may vary from one dataProc callback to the next. |
| 694 | * |
| 695 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 696 | * @return callback buffer size in frames or AAUDIO_UNSPECIFIED |
| 697 | */ |
| 698 | AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream); |
| 699 | |
| 700 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 701 | * An XRun is an Underrun or an Overrun. |
| 702 | * During playing, an underrun will occur if the stream is not written in time |
| 703 | * and the system runs out of valid data. |
| 704 | * During recording, an overrun will occur if the stream is not read in time |
| 705 | * and there is no place to put the incoming data so it is discarded. |
| 706 | * |
| 707 | * An underrun or overrun can cause an audible "pop" or "glitch". |
| 708 | * |
Phil Burk | 068c10f | 2017-05-08 16:36:41 -0700 | [diff] [blame] | 709 | * Note that some INPUT devices may not support this function. |
| 710 | * In that case a 0 will always be returned. |
| 711 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 712 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 713 | * @return the underrun or overrun count |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 714 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 715 | AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 716 | |
| 717 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 718 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 719 | * @return actual sample rate |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 720 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 721 | AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 722 | |
| 723 | /** |
Phil Burk | 20523ed | 2017-04-24 17:04:01 -0700 | [diff] [blame] | 724 | * A stream has one or more channels of data. |
| 725 | * A frame will contain one sample for each channel. |
| 726 | * |
| 727 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 728 | * @return actual number of channels |
| 729 | */ |
| 730 | AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream); |
| 731 | |
| 732 | /** |
Phil Burk | e74240d | 2017-08-03 15:25:43 -0700 | [diff] [blame^] | 733 | * Identical to AAudioStream_getChannelCount(). |
| 734 | * |
| 735 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 736 | * @return actual number of samples frame |
| 737 | */ |
| 738 | AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream); |
| 739 | |
| 740 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 741 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 742 | * @return actual device ID |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 743 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 744 | AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 745 | |
| 746 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 747 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 748 | * @return actual data format |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 749 | */ |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 750 | AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 751 | |
| 752 | /** |
| 753 | * Provide actual sharing mode. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 754 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 755 | * @return actual sharing mode |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 756 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 757 | AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 758 | |
| 759 | /** |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 760 | * Get the performance mode used by the stream. |
| 761 | * |
| 762 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 763 | */ |
| 764 | AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream); |
| 765 | |
| 766 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 767 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 768 | * @return direction |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 769 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 770 | AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 771 | |
| 772 | /** |
| 773 | * Passes back the number of frames that have been written since the stream was created. |
| 774 | * For an output stream, this will be advanced by the application calling write(). |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 775 | * For an input stream, this will be advanced by the endpoint. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 776 | * |
| 777 | * The frame position is monotonically increasing. |
| 778 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 779 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 780 | * @return frames written |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 781 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 782 | AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 783 | |
| 784 | /** |
| 785 | * Passes back the number of frames that have been read since the stream was created. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 786 | * For an output stream, this will be advanced by the endpoint. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 787 | * For an input stream, this will be advanced by the application calling read(). |
| 788 | * |
| 789 | * The frame position is monotonically increasing. |
| 790 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 791 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 792 | * @return frames read |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 793 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 794 | AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 795 | |
| 796 | /** |
| 797 | * Passes back the time at which a particular frame was presented. |
| 798 | * This can be used to synchronize audio with video or MIDI. |
| 799 | * It can also be used to align a recorded stream with a playback stream. |
| 800 | * |
| 801 | * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED. |
| 802 | * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started. |
| 803 | * Note that because requestStart() is asynchronous, timestamps will not be valid until |
| 804 | * a short time after calling requestStart(). |
| 805 | * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error. |
| 806 | * Just try calling again later. |
| 807 | * |
| 808 | * If an error occurs, then the position and time will not be modified. |
| 809 | * |
| 810 | * The position and time passed back are monotonically increasing. |
| 811 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 812 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | ec15950 | 2017-07-25 17:33:47 -0700 | [diff] [blame] | 813 | * @param clockid CLOCK_MONOTONIC or CLOCK_BOOTTIME |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 814 | * @param framePosition pointer to a variable to receive the position |
| 815 | * @param timeNanoseconds pointer to a variable to receive the time |
| 816 | * @return AAUDIO_OK or a negative error |
| 817 | */ |
Phil Burk | e2155ef | 2017-02-24 13:50:29 -0800 | [diff] [blame] | 818 | AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 819 | clockid_t clockid, |
| 820 | int64_t *framePosition, |
| 821 | int64_t *timeNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 822 | |
| 823 | #ifdef __cplusplus |
| 824 | } |
| 825 | #endif |
| 826 | |
| 827 | #endif //AAUDIO_AAUDIO_H |
Phil Burk | a45be8b | 2017-04-05 14:45:48 -0700 | [diff] [blame] | 828 | |
| 829 | /** @} */ |