MPD with custom Sox recipes

2020-09-04: The custom sox recipes patch has been accepted by MPD on the master branch!
2020-09-14: The custom sox recipes are part of the upcoming moOde 7.0.0 release. MoOde even provide a nice GUI for it.

MPD can use the famous soxr library for resampling. Soxr is very flexible with the configuration of the resampler. Only MPD only provide the default recipes with the quality setting.

You can find a lot on internet about the usage of soxr or sox (the commandline version of soxr) , including a kind of measurements.

The patch mpd-0.21.x_soxr_customrecipe.patch (or for mpd-0.22~git) will add settings to MPD section of the resampler. In only unlock the already present settings in soxr, the functionality of sox is untouched.

SettingDescriptiondefault
precisionNumber of used bits [16|20|24|28|32]20 (matches HQ)
phase_response0 =MINIMUM_PHASE, 50=LINEAR_PHASE, intermediate is between those.50
passband_endOriginal bandwidth of source used (%, < 100) 95.0%
stopband_begin(% > 100)100.0%
attenuationLowers the source to prevent clipping. Expressed in db. 0 db
flagsBit fields:
01 ROLLOFF_SMALL 0.01 dB
01 ROLLOFF_MEDIUM 0.35 dB
02 ROLLOFF_NONE For Chebyshev bandwidth.
08 HI_PREC_CLOCK Increase `irrational’ ratio accuracy.
16 DOUBLE_PRECISION Use D.P. calcs even if precision <= 20
32 SOXR_VR Variable-rate resampling.
0

To enable the custom recipe the quality should be set to "custom". If another quality is selected and the custom settings are still present you will get warnings in the MPD log about unused settings.

Example MPD configuration:

resampler {
  plugin "soxr"
  quality "custom"
  threads "2"
  precision "24"
  phase_response "45"
  passband_end "95.0"
  stopband_begin "100.0"
  attenuation "1.0"
  flags "0"
}

More MPD soxr selective resample modes

2020-09-14: The MPD soxr selective resample modes are part of the upcoming moOde 7.0.0 release. MoOde even provide a nice GUI for it.

Earlier I wrote about moOde with MPD Selective Resample about sox and resample. With selective resample takes in account if source sample rate is multiple of 44.1kHz or 48kHz. With selective resample the target sample rate is a integer multiply of source sample rate.

Sox with integer ratio only cost 50% of the CPU resource. Sound wise maybe less artifacts are introduced, but that is up to you and your ears to decide.

The selective resample could only be turned on or off. With a new patch some more flexibility is available.

The selective resample mode is set by combining several flags:

SELECTIVE_RESAMPLE_MODE = ADHERE_BASE_CLOCK and [UPSAMPLE_ALL | UPSAMPLE_ONLY_48 | UPSAMPLE_ONLY_44]

If non is set (0) then default scheme of sox is followed, resample (up and down) everything to the provided target rate.

If the resampled target rate should be a multiple of the source base rate can be set with the flag ADHERE_BASE_CLOCK.

When to resample a source:

  • UPSAMPLE_ALL- Only upsampling.
  • UPSAMPLE_ONLY_48 – only upsampling sources below 88.2kHz.
  • UPSAMPLE_ONLY_44 – only upsampling when source is 44.1kHz or below.

Where the flags have the following value:

  • ADHERE_BASE_CLOCK = 0x08
  • UPSAMPLE_ALL = 0x03
  • UPSAMPLE_ONLY_48 = 0x02
  • UPSAMPLE_ONLY_44= 0x01

In table the behavior is shown for several source sample rates and target sample rate of 192kHz and 176.4kHz.

Selective Resample Modes

When resampling is used the output rate is shown bold.
Note that using a target rate as expressed in a multiple of 48kHz behaves more natural then express in a multiple of 44.1kHz.

Use

To support this modes you need to patch your MPD executable. The patch is available for the latest 0.21.xx [24|25| and upcoming 26] releases and the 0.22~git development branch.

You can find the 0.21.xx patch here and the 0.22~git patch here.

Applying the to your MPD source tree:

cd MPD/src
patch -b -p2 < /path_to_the_patch/mpd_0.21.xx_selective_resample_mode.patch

After applying the patch, you can build your MPD executable.

If you have an MPD version with support for it, you need to add the following to the MPD configuration:

selective_resample_mode "4"

Note: If you switch back to a version with mode support remember to remove this line, else MPD will not start.