Severe clipping of audio when using ALSA - libspeexdsp1 broken


I have mopidy set up on two Raspberry Pis.

On one of them there is very severe clipping of the audio when mopidy volume is at or near max.
It very much sounds like a digital audio gain overflowing/clipping somewhere in the playback chain.
I can’t hear any clipping when changing to pulsesink, even with volume at max.

Both PIs are using a “TinyTOSLINK” add-on card for output.

Differences between the two systems:

Good system (new):

  • mopidy version 1.1.0
  • mopidy-spotify 2.0.1

Bad system (old):

  • Mopidy version 1.1.0
  • mopidy-spotify 2.0.1
  • Pulseaudio installed

Where should I start debugging this?
Is there a way to dump a graph showing the audio chain, ideally including gains?

EDIT: I upgraded mopidy on the bad system to same version as on the good system


I’ve not noticed this. First make sure you are running the same versions of
everything. Including, most importantly, your pi firmware. On the 'good’
system what audio sink is mopidy actually using? Can you try the 'bad’
system with a different sound device?



After comparing differencies, it turns out the problem is caused by the resampler
On the “good” board, without pulseaudio, only the linear resampler is available.
On the “bad” board, several other resamplers are available (installed as dependencies of pulseaudio).
According to the documentation, ALSA will choose “speexrate_medium”, but I have not verified this.
After adding this line to /etc/asound.conf, there is no more clipping on the “bad” board:

defaults.pcm.rate_converter "linear"

Of course, linear resampling is not acceptable because it introduces too much distortion.

Any suggestions on how to go further?
I’d like to confirm the resampler library itself is causing the audio clipping, is there a way to generate PCM dumps along the ALSA audio routes?
Or should I just try with a few different settings for “defaults.pcm.rate_converter” and see what happens?



My problem is definitely caused by the speex resampler.
Today I did some quick testing with various settings for defaults.pcm.rate_converter

The following settings all work well:


With these settings, there is severe distortion when volume control is at max:


As an additional test, I modified pulse audio configuration to also do upsampling:

default-sample-rate = 48000
alternate-sample-rate = 48000

Result: The same clipping / distortion.

Pulseaudio also defaults to the speex, and as a final confirmation that something is wrong with the speex resampler:

resample-method = src-sinc-medium-quality

Again, no more distortion!!

The Debian speex packages are not based on the latest speex release, although some patches have been backported.
Additional tests:

  • Rebuild of - Clipping/Distorsion
  • Rebuild of, without Debian patches in the resampler - Clipping/Distorsion
  • Rebuild of, but resampler.c updated to match latest speex release (SpeexDSP 1.2 rc3) - Clean audio!!

Bug #799176 filed on Debian.