Trying to set buffer sizes on Android

I have been struggling to try to change the buffer size on any android device.

  1. I initialize the device,
  2. I get the current AudioDeviceSetup
  3. I set the buffer size to one of the available device buffer sizes
  4. I set the setup on the audioDeviceManager

On a pixel where the default buffer size is 94, this has no effect. Not a huge deal… 94 default is fine for my purposes anyway.
On a nexus where the default buffer size is a mind-boggling 1920, the app crashes on the call to ‘setAudioDeviceSetup’ (. (*Base::owner.engine)->CreateAudioPlayer(…) line 410 of juce_android_OpenSL.cpp)

I’m afraid, as far as a I know at least, that on most Android devices you are stuck with such mind-boggling buffer sizes…

Which Nexus device are you referring to? Off the top of my head Nexus 9 has a buffer size of 128, Nexus 6P has 192 and Nexus 5X has 192.

Incidentally I think you mean 96 frames on Pixel, not 94.

Either way, 1920 is a very large buffer and unless it’s a very old Nexus device it sounds too high to be correct.

@Don_Turner my assumption from my tests a while ago is that on most devices we are not able to change the buffer size, is that correct?

Yes, on the Pixel 2 I have, I did mean 96, not 94 :slight_smile:

I have a nexus 7 running 6.0.1 which is giving me a default frame size of 1920.
(I am actually considering not supporting it, because of this:
Support USB Audio devices on Android)

I don’t know of any device where you can change the buffer size of the underlying audio device (we generally refer to this as a “burst” - a discrete chunk of audio frames which are read/written by the audio device in a single operation).

You can, however, change the software buffer size - rule of thumb is to use 2 bursts. On API 26+ this can be done dynamically on an audio stream, on <26 this can only be set before the audio stream is opened.

@jemur Nexus 7 is 6 years old! That’s a lifetime in phone terms! :wink: Yes, I wouldn’t support it if latency is critical to the user experience.

BTW in case it’s useful ROLI published the buffer sizes of many Android phones in their MAQ index:

I’m not sure on the exact calculation used. For example, Pixel has a burst size of 192 frames which equates to 4ms @ 48kHz. The MAQ states 16ms latency which would indicate that their calculation (for Pixel at least) is 4 * burst size.

If that formula is consistent throughout the list then it could be a useful list of burst sizes for popular Android devices.

@jules Can you share any more info about the latency calculation formula?

I am using the below JUCE estimates for latency, and at least on the Pixel 2 they seem ok.
Definitely get that the Nexus is old… I literally have two devices to test on… the Pixel 2 and the Nexus 7… it was the best of times, it was the worst of times…

    inputLatency  = (javaDevice.minBufferSizeIn  * 2) / 3;
    outputLatency = (javaDevice.minBufferSizeOut * 2) / 3;

    const int64 longestLatency = jmax (inputLatency, outputLatency);
    const int64 totalLatency = inputLatency + outputLatency;
    inputLatency  = (int) ((longestLatency * inputLatency)  / totalLatency) & ~15;
    outputLatency = (int) ((longestLatency * outputLatency) / totalLatency) & ~15;