AAudio is a new Android C API introduced in the Android O release. It is designed for high-performance audio applications that require low latency. Apps communicate with AAudio by reading and writing data to streams.

The AAudio API is minimal by design,
it doesn't perform these functions:

Audio device enumeration

Automated routing between audio endpoints

File I/O

Decoding of compressed audio

Automatic presentation of all input/streams in a single callback.

Audio streams

AAudio moves audio data between your app and the audio inputs and outputs on your Android device. Your app passes data in and out by reading from and writing to audio streams, represented by the structure AAudioStream. The read/write calls can be blocking or non-blocking.

A stream is defined by the following:

The audiodevice that is the source or sink for the data in the stream.

The sharing mode that determines whether a stream has exclusive access to an audio device that might otherwise be shared among multiple streams.

The format of the audio data in the stream.

Audio device

Each stream is attached to a single audio device.

An audio device is a hardware interface or virtual endpoint that acts as a source or sink for a continuous stream of digital audio data. Don't confuse an audio device
(a built-in mic or bluetooth headset) with the Android device (the phone or watch) that is running your app.

You can use the AudioManager method getDevices() to discover the audio devices that are available on your Android device. The method returns information about the type of each device.

Each audio device has a unique ID on the Android device. You can use the ID to bind an audio stream to a specific audio device. However, in most cases you can let AAudio choose the default primary device rather than specifying one yourself.

The audio device attached to a stream determines whether the stream is for input or output. A stream can only move data in one direction. When you define a stream you also set its direction. When you open a stream Android checks to ensure that the audio device and stream direction agree.

Sharing mode

A stream has a sharing mode:

AAUDIO_SHARING_MODE_EXCLUSIVE means the stream has exclusive access to its audio device; the device cannot be used by any other audio stream. If the audio device is already in use, it might not be possible for the stream to have exclusive access. Exclusive streams are likely to have lower latency, but they are also more likely to get disconnected. You should close exclusive streams as soon as you no longer need them, so that other apps can access the device. Exclusive streams provide the lowest possible latency.

AAUDIO_SHARING_MODE_SHARED allows AAudio to mix audio. AAudio mixes all the shared streams assigned to the same device.

You can set the sharing mode explicitly when you create a stream. By default,
the sharing mode is SHARED.

Audio format

The data passed through a stream has the usual digital audio attributes. These are as follows:

AAudio might perform sample conversion on its own. For example, if an app is writing FLOAT data but the HAL uses PCM_I16, AAudio might convert the samples automatically. Conversion can happen in either direction. If your app processes audio input, it is wise to verify the input format and be prepared to convert data if necessary, as in this example:

Note that these methods do not report errors, such as an undefined constant or value out of range.

If you do not specify the deviceId, the default is the primary output device.
If you do not specify the stream direction, the default is an output stream.
For all other parameters, you can explicitly set a value, or let the system
assign the optimal value by not specifying the parameter at all or setting
it to AAUDIO_UNSPECIFIED.

To be safe, check the state of the audio stream after you create it, as explained in step 4, below.

When the AAudioStreamBuilder is configured, use it to create a stream:

After creating the stream, verify its configuration. If you specified
sample format, sample rate, or samples per frame they will not change.
If you specified sharing mode or buffer capacity, they might change
depending on the capabilities of the stream's audio device and the
Android device on which it's running. As a matter of good defensive
programming, you should check the stream's configuration before using it.
There are functions to retrieve the stream setting that corresponds to each
builder setting:

The function does not detect a state change on its own, and does not wait for a
specific state. It waits until the current state
is different than inputState, which you specify.

For example, after requesting to pause, a stream should immediately enter
the transient state Pausing, and arrive sometime later at the Paused state - though there's no guarantee it will.
Since you can't wait for the Paused state, use waitForStateChange() to wait for any state
other than Pausing. Here's how that's done:

If the stream's state is not Pausing (the inputState, which we assumed was the
current state at call time), the function returns immediately. Otherwise, it
blocks until the state is no longer Pausing or the timeout expires. When the
function returns, the parameter nextState shows the current state of the
stream.

You can use this same technique after calling request start, stop, or flush,
using the corresponding transient state as the inputState. Do not call
waitForStateChange() after calling AAudioStream_close() since the stream
will be deleted as soon as it closes. And do not call AAudioStream_close()
while waitForStateChange() is running in another thread.

Reading and writing to an audio stream

For a blocking read or write that transfers the specified number of frames, set timeoutNanos greater than zero. For a non-blocking call, set timeoutNanos to zero. In this case the result is the actual number of frames transferred.

When you read input, you should verify the correct number of
frames was read. If not, the buffer might contain unknown data that could cause an
audio glitch. You can pad the buffer with zeros to create a
silent dropout:

You can prime the stream's buffer before starting the stream by writing data or silence into it. This must be done in a non-blocking call with timeoutNanos set to zero.

The data in the buffer must match the data format returned by AAudioStream_getDataFormat().

Closing an audio stream

When you are finished using a stream, close it:

AAudioStream_close(stream);

After you close a stream you cannot use it with any AAudio stream-based function.

Disconnected audio stream

An audio stream can become disconnected at any time if one of these events happens:

The associated audio device is no longer connected (for example when headphones are unplugged).

An error occurs internally.

An audio device is no longer the primary audio device.

When a stream is disconnected, it has the state "Disconnected" and any attempts to execute write() or other functions return AAUDIO_ERROR_DISCONNECTED. When a stream is disconnected, all you can do is close it.

The callback should check the state of the stream as shown in the following
example. You should not close or reopen the stream from the callback, use
another thread instead. Note that if you open a new
stream it might have different characteristics than the
original stream (for example framesPerBurst):

An app doesn't have to use the entire capacity of a buffer. AAudio fills a buffer up to a size which you can set. The size of a buffer can be no larger than its capacity, and it is often smaller. By controlling the buffer size you determine the number of bursts needed to fill it, and thus control latency. Use the methods AAudioStreamBuilder_setBufferSizeInFrames()
and
AAudioStreamBuilder_getBufferSizeInFrames()
to work with the buffer size.

When an application plays audio out, it writes to a buffer and blocks until the write is complete. AAudio reads from the buffer in discrete bursts. Each burst contains a multiple number of audio frames and is usually smaller than the size of the buffer being read.
The system controls burst size and rate, these properties are typically dictated by the audio device's circuitry.
Though you can't change the size of a burst or the burst rate, you can set the size of the internal buffer according to the number of bursts it contains.
Generally, you get the lowest latency if your AAudioStream's buffer size is a multiple of the reported burst size.

One way to optimize the buffer size is to start with a large buffer and gradually lower it until underruns begin, then nudge it back up. Alternatively, you can start with a small buffer size and if that produces underruns, increase the buffer size until the output flows cleanly again.

This process can take place very quickly, possibly before the user plays the first sound. You may want to perform the initial buffer sizing first, using silence, so that the user won't hear any audio glitches. System performance may change over time (for example, the user might turn off airplane mode). Since buffer tuning adds very little overhead, your app
can do it continuously while the app reads or writes data to a stream.

There is no advantage to using this technique to optimize the buffer size for an input stream.
Input streams run as fast as possible, trying to keep the amount
of buffered data to a minimum, and then filling up when the app is preempted.

Using a high priority callback

If your app reads or writes audio data from an ordinary thread, it may be preempted or experience timing jitter. This can cause audio glitches.
Using larger buffers might guard against such glitches, but a large buffer also introduces longer audio latency.
For applications that require low latency, an audio stream can use an asynchronous callback function to transfer data to and from your app.
AAudio executes the callback in a higher-priority thread that has better performance.

In the simplest case, the stream periodically executes the callback function to
acquire the data for its next burst.

The callback function should not perform a read or write on the stream that
invoked it. If the callback belongs to an input stream, your code should process
the data that is supplied in the audioData buffer (specified as the third
argument). If the callback belongs to an output stream, your code should place
data into the buffer.

For example, you could use a callback to continuously generate a sine wave output
like this:

It is possible to process more than one stream using AAudio. You can use one stream as the master, and
pass pointers to other streams in the user data. Register a callback for the master stream. Then use non-blocking I/O on the other streams. Here is an example of a round-trip callback that passes an input stream to an output stream.
The master calling stream is the output stream. The input stream is included in the user data.

The callback does a non-blocking read from the input stream placing the data into the buffer of the output stream:

Note that in this example it is assumed the input and output streams have the same number of channels, format and sample rate. The format of the streams can be mismatched - as long as the code handles the translations properly.

Setting performance mode

Every AAudioStream has a performance mode which has a large effect on your app's behavior. There are three modes:

AAUDIO_PERFORMANCE_MODE_NONE is the default mode. It uses a basic stream that balances latency and power savings.

If low latency is more important than power savings in your application, use AAUDIO_PERFORMANCE_MODE_LOW_LATENCY.
This is useful for apps that are very interactive, such as games or keyboard synthesizers.

If saving power is more important than low latency in your application, use AAUDIO_PERFORMANCE_MODE_POWER_SAVING.
This is typical for apps that play back previously generated music, such as streaming audio or MIDI file players.

In the current version of AAudio, in order to achieve the lowest possible latency you must use the AAUDIO_PERFORMANCE_MODE_LOW_LATENCY performance mode along with a high-priority callback. Follow this example:

Thread safety

The AAudio API is not completely thread safe.
You cannot call some of the AAudio functions concurrently from more than one thread at a time.
This is because AAudio avoids using mutexes, which can cause thread preemption and glitches.

To be safe, don't call AAudioStream_waitForStateChange() or read or write to the same stream from two different threads. Similarly, don't close a stream in one thread while reading or writing to it in another thread.

Calls that return stream settings, like AAudioStream_getSampleRate() and AAudioStream_getChannelCount(), are thread safe.

These calls are also thread safe:

AAudio_convert*ToText()

AAudio_createStreamBuilder()

AAudioStream_get*() except for AAudioStream_getTimestamp()

Note: When a stream uses a callback function, it's safe to read/write from the callback thread while also closing the stream
from the thread in which it is running.