MusIT

Oboe: Fight Android Audio Latency

Definition: in gaming, the audio latency is the delay between a user action and the sound being heard.

This latency can be split into two parts:

  • UI Latency is the time between a user’s tap or click on the screen and the processing of the event on the software side.
    To reduce UI latency, you can use either the new Unity input system or interface like OnPointerDown from UnityEngine.EventSystems. Look for TestTapMidi.cs source in TestOboe scene (Maestro Pro).
  • Hardware Latency origin is mainly related to the time need by the audio component (FMOD by default) to process and play the sound build by the software (like Maestro MPTK). The latency depends on the buffer size (DspBufferSize) and the synth sample rate.

This page explain how to reduce the hardware latency by changing parameters in the audio settings in Unity/FMOD (first option) or using audio component Oboe/AAudio/OpenSL (second option).

Android platform limitation: It’s quite difficult to guarantee a stable and accurate MIDI playing. Android threading is not accurate. It is largely sufficient for games, but should be avoided for more musical applications. Check with the demo before or start with a quick MVP.

First Option: Modify Unity Audio Settings

This the recommended method.

According to your need, you could have only to change some properties on the Unity setting to resolve your latency issue.

From Unity menu Edit → Project Settings → Audio

With Unity 2019
  • Set DSP Buffer size to Best latency.
  • Try with higher Sample Rate or let to zero.
  • Use audio effects with caution, as they use up a lot of CPU.

You can also change these parameters from the MPTK prefab inspectors.

Tips:

  • Audio latency is not a specific issue with Android but also on Windows, IOs, Mac, … Obviously, these setting are useful also for them.
  • Depending on your system, a too low buffer size can generate weird sound with some material.
  • Also, forget Bluetooth headset because of a high latency (for now!).

Second Option: Replace FMOD by Oboe

FMOD is the standard audio engine for Unity. It’s a powerful engine with a lot of features, well tested on a lot of hardware, but not compliant with very low buffer size. However, use the FMOD audio engine if latency is not critical to your creation.

Oboe is an low latency audio library that uses OpenSL and AAudio to handle the audio data on Android. Its requires Maestro Pro and some (simple) configuration.

Some important notes

  • Many different devices use Android, it’s not possible to check all smartphone and tablet hardware. Unfortunately, these limitations have been identified on some devices:
    • Like any audio software, and especially fluidsynth, audio is processed in blocks of 64 values. It’s important that the audio buffer size is greater than or equal to 64 and a multiple of 64, otherwise you’ll get bad sound. Try different values to find the right buffer size to improve latency and keep a good playback!
    • When buffer is not a multiple of 64, the SoundFont effects are disabled.
    • The Oboe demo is able to display these information.
    • November, 17 – BREAKING NEWS – Now MPTK/Oboe plays well with all my test devices … I need to confirm with other devices. Ice on the cake, the playing is now accurate.
  • Unity effects and 3D sound are disabled (these features are based on FMOD).
  • Oboe is working only on Android.

How to use Oboe with your MPTK application ?

  • Recommended Unity version: 2022.3 and above.
  • Recommended Maestro version: 2.12 and above.
  • Create a new Unity project.
  • Oboe is working only for Android, select build platform to Android in “Build Settings”.
  • Download and import in your project the Oboe package from here (version tested:1.6.2).
  • Download and import in your project Maestro Pro from here.
  • Go to Unity menu Edit → Project Settings → Player
    • add symbol UNITY_OBOE (just remove this symbol to restore the legacy FMOD mode)
    • add symbol DEBUG_HISTO_DSPSIZE to get buffer statistic (for dev step only)
    • allow unsafe code
With Unity 2022.3
With Unity 2022.3
Configuration in Player Settings
Configuration in Player Settings

Starting with Maestro MPTK v2.12, the definition of assemblies has been added. This reduces the build time. Assemblies are perfect for isolating each component, the downside is that you have to manually declare which assemblies are used by the MPTK assembly.

  • Open the Midi Player Run assembly definition, allow unsafe code (search MidiPlayer.Run in MidiPlayer/Scripts), add reference to Oboe and apply.
Add reference to Oboe assembly
Add reference to Oboe assembly
  • Same action for Midi Player Demo assembly definition, allow unsafe code, add reference to Oboe and apply.
Add reference to Oboe assembly
  • Build and deploy your APK and that’s all folks!!! MPTK has automatically switched to the Oboe audio engine.
  • To restore the legacy FMOD mode. just remove UNITY_OBOE symbol from Project Settings → Player

Demo

As usual with Maestro MPTK, all sources are available with the package on the Unity store.

  • The Oboe Test is a simple demonstration of MIDI and direct note playing. To play notes, tap or wipe on the magenta zone. You can adjust the synth settings in real-time to improve latency and sound quality:
  • Download APK with Oboe This demo is also useful to check the compatibility with the device. Look at the Frame length historic. If some frame are not a multiple of 64, the sound will be degraded.
  • Download APK with FMOD
Oboe Demo
Oboe Demo
  • MIDI Euclidean Rhythms
    • This demo is useful for experimenting with the MPTK synthesizer voices, runtime effects, and running multiple players simultaneously. There are two players running simultaneously: one for the drums, which is a MidiStreamPlayer prefab, and one for the midi, which is a MidiFilePlayer prefab.
    • Download the APK
MIDI Euclidean Rhythms Demo
Video from a Windows desktop

Have fun!

Get MPTK from the Unity store

If you like Midi Player Tool Kit, please leave a review on the Asset Store. It’s very appreciated!!!

Maestro MPTK on ChatGPT!

From different MPTK documentation sources DarkSky42 has created a custom LLM based on ChatGPT. You are now able to ask all the questions you want and get a good level of response, request code example, verify your source code …

Contact

If you have questions, please don’t hesitate to contact us via the dedicated Unity forum or our Discord  channel.

Reach the Discord archive by topic.

We are always happy to discuss your projects!

Add MIDI Music With 3 Clicks for Free

Sound Spatialisation, MPTK is ready for Virtual Reality [Pro]

Sound Spatialisation, MPTK is ready for Virtual Reality [free]

Midi Synth : Real Time Voice Effect Change

Euclidean Rhythm demo

The Deezer playlist that helped me create Maestro