Android: Ask system for optimal audio buffer size and sample rate

This can reduce audio latency according to
https://developer.android.com/ndk/guides/audio/opensl/opensl-prog-notes#perform.

Previously we were using the hardcoded values of 48000 Hz and 256 frames
per buffer. The sample rate we use with this change is 48000 Hz on all
devices I'm aware of, but the buffer size does vary across devices.

Terminology note: The old code used the term "sample" to refer to what
Android refers to as a "frame". "Frame" is a clearer term to use for
this, so I've changed OpenSLESStream's terminology. One frame consists
of one sample per channel.
This commit is contained in:
JosJuice
2024-06-06 15:38:41 +02:00
parent 34e8fb068f
commit f99d3dbd5c
5 changed files with 92 additions and 13 deletions

View File

@ -4,6 +4,9 @@
#pragma once
#ifdef HAVE_OPENSL_ES
#include <array>
#include <vector>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#endif // HAVE_OPENSL_ES
@ -35,11 +38,11 @@ private:
SLAndroidSimpleBufferQueueItf m_bq_player_buffer_queue;
SLVolumeItf m_bq_player_volume;
static constexpr int BUFFER_SIZE = 512;
static constexpr int BUFFER_SIZE_IN_SAMPLES = BUFFER_SIZE / 2;
SLuint32 m_frames_per_buffer;
SLuint32 m_bytes_per_buffer;
// Double buffering.
short m_buffer[2][BUFFER_SIZE];
std::array<std::vector<short>, 2> m_buffer;
int m_current_buffer = 0;
#endif // HAVE_OPENSL_ES
};