Skip to content

Low latency support for compliant devices

nettoyeurny edited this page Dec 28, 2012 · 4 revisions

The latest version of Pd for Android (as of 12/28/2012) supports low-latency audio for compliant Android devices. When updating your copy, make sure to pull the latest version of both pd-for-android and the libpd submodule from GitHub.

At the time of writing, Galaxy Nexus, Nexus 4, and Nexus 10 provide a low-latency track for audio output. In order to hit the low-latency track, an app must use OpenSL, and it must operate at the correct sample rate and buffer size. Those parameters are device dependent (Galaxy Nexus and Nexus 10 operate at 44100Hz, while Nexus 4 operates at 48000Hz; the buffer size is different for each device).

As is its wont, Pd for Android papers over all those complexities as much as possible, providing access to the new low-latency features when available while remaining backward compatible with earlier versions of Android. Under the hood, the audio components of Pd for Android will use OpenSL on Android 2.3 and later, while falling back on the old AudioTrack/AudioRecord API in Java on Android 2.2 and earlier.

Configuring audio for low latency

The class org.puredata.android.io.AudioParameters recommends audio parameters. From the point of view of most app developers, the most important methods are init and suggestSampleRate. The AudioParameters class must be explicitly initialized. The PdService class does this on creation, so you won't have to worry about this if you only use AudioParameters after binding to PdService. If you aren't using PdService, or if you want to use AudioParameters before binding to PdService, you need to call AudioParameters.init(this) early in the life of your activity. The init method is safe to call more than once, and so it won't hurt initialize AudioParameters just in case.

If you access AudioParameters before it has been initialized, then it will initialize itself with default parameters. Those parameters are perfectly safe to use, but they won't enable the low-latency track.

So, in order to target the low-latency track, just initialize the AudioParameters class with AudioParameters.init(this) early on (if necessary), then get the required sample rate with AudioParameters.suggestSampleRate() and use it when configuring the audio components. Of course, this means that your Pd patch must work at the recommended sample rate (or you prepare one patch for each possible sample rate and then load the appropriate patch).

You won't have to worry about the buffer size. When using OpenSL, the audio components of Pd for Android will automatically choose the appropriate buffer size, i.e., the initAudio methods of PdAudio and PdService will ignore the buffer size parameter.

Sample code

The CircleOfFifths app that comes with Pd for Android illustrates how to configure libpd for low latency.