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