mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-06-28 01:49:33 -06:00
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:
@ -0,0 +1,28 @@
|
||||
package org.dolphinemu.dolphinemu.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.media.AudioManager
|
||||
import androidx.annotation.Keep
|
||||
import org.dolphinemu.dolphinemu.DolphinApplication
|
||||
|
||||
object AudioUtils {
|
||||
@JvmStatic @Keep
|
||||
fun getSampleRate(): Int =
|
||||
getAudioServiceProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE, 48000)
|
||||
|
||||
@JvmStatic @Keep
|
||||
fun getFramesPerBuffer(): Int =
|
||||
getAudioServiceProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER, 256)
|
||||
|
||||
private fun getAudioServiceProperty(property: String, fallback: Int): Int {
|
||||
return try {
|
||||
val context = DolphinApplication.getAppContext()
|
||||
val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||
Integer.parseUnsignedInt(am.getProperty(property))
|
||||
} catch (e: NullPointerException) {
|
||||
fallback
|
||||
} catch (e: NumberFormatException) {
|
||||
fallback
|
||||
}
|
||||
}
|
||||
}
|
@ -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<jclass>(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);
|
||||
}
|
||||
}
|
||||
|
@ -121,4 +121,8 @@ jmethodID GetPermissionHandlerRequestRecordAudioPermission();
|
||||
|
||||
jmethodID GetRunnableRun();
|
||||
|
||||
jclass GetAudioUtilsClass();
|
||||
jmethodID GetAudioUtilsGetSampleRate();
|
||||
jmethodID GetAudioUtilsGetFramesPerBuffer();
|
||||
|
||||
} // namespace IDCache
|
||||
|
Reference in New Issue
Block a user