From f99d3dbd5ccd80bcd071cf49e689f6b6724df499 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Thu, 6 Jun 2024 15:38:41 +0200 Subject: 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. --- Source/Android/jni/AndroidCommon/IDCache.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'Source/Android/jni/AndroidCommon/IDCache.cpp') diff --git a/Source/Android/jni/AndroidCommon/IDCache.cpp b/Source/Android/jni/AndroidCommon/IDCache.cpp index 08ec2c9804..d8dfa2b7c4 100644 --- a/Source/Android/jni/AndroidCommon/IDCache.cpp +++ b/Source/Android/jni/AndroidCommon/IDCache.cpp @@ -122,6 +122,10 @@ static jmethodID s_permission_handler_request_record_audio_permission; static jmethodID s_runnable_run; +static jclass s_audio_utils_class; +static jmethodID s_audio_utils_get_sample_rate; +static jmethodID s_audio_utils_get_frames_per_buffer; + namespace IDCache { JNIEnv* GetEnvForThread() @@ -562,6 +566,21 @@ jmethodID GetRunnableRun() return s_runnable_run; } +jclass GetAudioUtilsClass() +{ + return s_audio_utils_class; +} + +jmethodID GetAudioUtilsGetSampleRate() +{ + return s_audio_utils_get_sample_rate; +} + +jmethodID GetAudioUtilsGetFramesPerBuffer() +{ + return s_audio_utils_get_frames_per_buffer; +} + } // namespace IDCache extern "C" { @@ -798,6 +817,13 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) s_runnable_run = env->GetMethodID(runnable_class, "run", "()V"); env->DeleteLocalRef(runnable_class); + const jclass audio_utils_class = env->FindClass("org/dolphinemu/dolphinemu/utils/AudioUtils"); + s_audio_utils_class = reinterpret_cast(env->NewGlobalRef(audio_utils_class)); + s_audio_utils_get_sample_rate = env->GetStaticMethodID(audio_utils_class, "getSampleRate", "()I"); + s_audio_utils_get_frames_per_buffer = + env->GetStaticMethodID(audio_utils_class, "getFramesPerBuffer", "()I"); + env->DeleteLocalRef(audio_utils_class); + return JNI_VERSION; } @@ -834,5 +860,6 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) env->DeleteGlobalRef(s_core_device_control_class); env->DeleteGlobalRef(s_input_detector_class); env->DeleteGlobalRef(s_permission_handler_class); + env->DeleteGlobalRef(s_audio_utils_class); } } -- cgit v1.2.3