Skip to content

TechNote_WhyGlitches

Phil Burk edited this page Jul 28, 2021 · 2 revisions

Why is my App Glitching?

... And what can I do about it?

Glitches in Audio are generally caused by underflowing an output buffer or overflowing an input buffer. Output glitches generally happen when a source of audio cannot provide data fast enough for the receiver. There are several possible causes for this including:

  • Preemption by higher priority tasks
  • Improperly sized buffers
  • CPU running too slow for real-time
  • Bugs in the audio framework or DSP

You may find that OboeTester is handy for reproducing and investigating these issues.

This document will help you diagnose some of these issues. Here are some common issues and their symptoms.

Your task may Require More Computation than the CPU can Provide

For example, it may take two seconds to generate one seconds worth of audio. So you can never run in real-time.

Symptoms include:

  • Constant glitching, it never sounds right.
  • Making the buffers very big does not help.
  • It sounds OK a fast flagship Android device but not on a low end device.

Solutions include:

  • Reducing the number of voices if you are running a synthesizer.
  • Optimize your rendering code. Use -Ofast.
  • Remove any heavy operations from your callback, like allocating memory or doing I/O.

Your Callback may be Preempted by other tasks.

Symptoms include:

  • Glitches seem random or may be associated with system activity.
  • Fewer glitches when in Airplane mode.
  • Increasing buffer size reduces glitches.
  • It sounds OK a fast flagship Android device but not on a low end device.
  • In a systrace, you may be able to see other programs running when you glitch.
  • getXRunCount() returns increasing numbers when there are glitches.

Solutions include:

  • Try setting CPU affinity.
  • Increase your buffer sizes, setBufferSizeInFrames().

The HAL may not be Reporting the correct DSP position.

This can cause AAudio to think the DSP is reading at a different position, which can lead to collisions between the DSP reads and the CPU writes in the shared buffer.

Symptoms Include:

  • Disabling MMAP in OboeTester eliminates the glitches.
  • getXRunCount() returns zero even when there are glitches.
  • Increasing buffer size reduces glitches.

Solutions include:

  • Increase your buffer sizes, setBufferSizeInFrames().
  • Manufacturers should fix the reporting of the DSP position. Generate a DSP timing profile by running the dsp_timing.py script.

The CPU may not be Responding Quickly enough to a Changing Workload

Symptoms include:

  • It glitches when your workload suddenly increases, such as when you suddenly play a lot more notes. Then the glitches stop as you hold the notes.
  • SynthMark shows a high clock ramp time.
  • Tapping you finger on a blank part of the screen reduces the glitching.

Solutions include:

  • Try the StabilizedCallback class.
  • Manufacturers can experiment with Boost Mode or UtilClamp.
Clone this wiki locally