godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.07k stars 69 forks source link

Replace the current audio driver with Oboe on Android #2358

Open mattkwang opened 3 years ago

mattkwang commented 3 years ago

Describe the project you are working on

Godot Audio on Android

Describe the problem or limitation you are having in your project

Godot's audio setup on Android currently uses OpenSL ES, an older audio API supported on Android API 9 and above. This API has high device coverage, but is bulkier and less suitable for low-latency applications compared to the native AAudio API introduced in Android API 26.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Oboe is Android's recommended modern audio API, acting as a C++ wrapper library that chooses between OpenSL ES and AAudio depending on device support and best performance. Oboe is compatible with API 16 onwards (covers 99% of Android devices). We expect this to modernize Godot's audio setup on Android, with lower audio latency and minimal loss of device coverage.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

We will create a class AudioDriverOboe housed under godot/platform/android, which will take the place of AudioDriveOpenSL in OS_Android. Much of its functionality will mirror AudioDriverOpenSL, informed by Oboe's migration guide.

Steps:

  1. Add Oboe to godot/thirdparty and link with SCons build system.
  2. Implement AudioDriverOboe with the set of methods inherited from AudioDriver, and modify os_android.h to use it. More implementation details are available if requested.
  3. Develop test apps for latency measurement and potentially device coverage.

AudioDriverOboe Member variables:

Mutex *mutex
static AudioDriverOboe *s_ad (singleton)
AudioStream player
AudioStream recorder
bool active
bool pause

AudioDriverOboe Implemented API:

const char *get_name()
Error init()
void start()
int get_mix_rate()
SpeakerMode get_speaker_mode()
void lock()
void unlock()
void finish()
void set_pause(bool p_pause)
Error capture_start()
Error capture_stop()
Constructor AudioDriverOboe()

For latency testing, the simplest version is the Larsen test with two AudioStreamPlayer nodes, one that records audio input and one that plays the input continuously. By measuring the time between sounds, we capture the sum of input and output latency, with some device/engine overhead. This should serve as an adequate measure for approximate latency improvement.

For testing device coverage, we should have a test app that covers audio features on Godot. This may be using the mic_record demo or some variation upon it which will use both input and output. If run on some device farm or set of devices, we may use app crashes or bad sound behavior to verify minimal loss in coverage.

If this enhancement will not be used often, can it be worked around with a few lines of script?

N/A. This is expected to be used for all Godot games exported for Android.

Is there a reason why this should be core and not an add-on in the asset library?

This proposal changes the audio (a core element of the engine) intending to impact all games for the Android platform.

Android Games DevTech Team @ Google

BastiaanOlij commented 3 years ago

Sounds like a good enhancement to me. Oboe is under an Apache 2.0 license? So shouldn't be anything in our way to include it.

dvdfu commented 2 weeks ago

Just seconding to say I'd love to see this coming to Godot 4.x! I'm interested in developing music-playing apps with Godot, but haven't been able to achieve Android audio latency shorter than 240ms, which is a dealbreaker for these kinds of applications.

Calinou commented 2 weeks ago

@dvdfu Can you test https://github.com/godotengine/godot/pull/86776 on your project to see if it improves latency?

dvdfu commented 9 hours ago

@dvdfu Can you test godotengine/godot#86776 on your project to see if it improves latency?

Sorry, I hadn't read this reply until now, and it seems like the artifacts are no longer available for download. Happy to test again if they are!