pcm/Volume: convert S16 to S24 to preserve quality and reduce noise
Applying software volume to S16 samples means several bits of precision are lost; at 25% volume, two bits are lost. Additionally, dithering adds some noise. The problem gets worse when you apply the software volume code twice: for the software mixer volume, and again for the replay gain. This loses some more precision and adds even more dithering noise, which can become audible (see https://github.com/MusicPlayerDaemon/MPD/issues/542). By converting everything to 24 bit, we need to shift only two bits to the right instead of ten, losing nearly no precision, and dithering is not needed. Even if the output device is unable to play S24 directly, we can convert back to S16 with only one stage of dithering. Closes https://github.com/MusicPlayerDaemon/MPD/issues/542
This commit is contained in:
@@ -69,7 +69,7 @@ class ReplayGainFilter final : public Filter {
|
||||
PcmVolume pv;
|
||||
|
||||
public:
|
||||
ReplayGainFilter(const ReplayGainConfig &_config,
|
||||
ReplayGainFilter(const ReplayGainConfig &_config, bool allow_convert,
|
||||
const AudioFormat &audio_format,
|
||||
Mixer *_mixer, unsigned _base)
|
||||
:Filter(audio_format),
|
||||
@@ -77,7 +77,8 @@ public:
|
||||
mixer(_mixer), base(_base) {
|
||||
info.Clear();
|
||||
|
||||
out_audio_format.format = pv.Open(out_audio_format.format);
|
||||
out_audio_format.format = pv.Open(out_audio_format.format,
|
||||
allow_convert);
|
||||
}
|
||||
|
||||
void SetInfo(const ReplayGainInfo *_info) {
|
||||
@@ -120,6 +121,12 @@ class PreparedReplayGainFilter final : public PreparedFilter {
|
||||
*/
|
||||
Mixer *mixer = nullptr;
|
||||
|
||||
/**
|
||||
* Allow the class to convert to a different #SampleFormat to
|
||||
* preserve quality?
|
||||
*/
|
||||
const bool allow_convert;
|
||||
|
||||
/**
|
||||
* The base volume level for scale=1.0, between 1 and 100
|
||||
* (including).
|
||||
@@ -127,8 +134,9 @@ class PreparedReplayGainFilter final : public PreparedFilter {
|
||||
unsigned base;
|
||||
|
||||
public:
|
||||
explicit PreparedReplayGainFilter(const ReplayGainConfig _config)
|
||||
:config(_config) {}
|
||||
explicit PreparedReplayGainFilter(const ReplayGainConfig _config,
|
||||
bool _allow_convert)
|
||||
:config(_config), allow_convert(_allow_convert) {}
|
||||
|
||||
void SetMixer(Mixer *_mixer, unsigned _base) {
|
||||
assert(_mixer == nullptr || (_base > 0 && _base <= 100));
|
||||
@@ -172,15 +180,18 @@ ReplayGainFilter::Update()
|
||||
}
|
||||
|
||||
std::unique_ptr<PreparedFilter>
|
||||
NewReplayGainFilter(const ReplayGainConfig &config) noexcept
|
||||
NewReplayGainFilter(const ReplayGainConfig &config,
|
||||
bool allow_convert) noexcept
|
||||
{
|
||||
return std::make_unique<PreparedReplayGainFilter>(config);
|
||||
return std::make_unique<PreparedReplayGainFilter>(config,
|
||||
allow_convert);
|
||||
}
|
||||
|
||||
std::unique_ptr<Filter>
|
||||
PreparedReplayGainFilter::Open(AudioFormat &af)
|
||||
{
|
||||
return std::make_unique<ReplayGainFilter>(config, af, mixer, base);
|
||||
return std::make_unique<ReplayGainFilter>(config, allow_convert,
|
||||
af, mixer, base);
|
||||
}
|
||||
|
||||
ConstBuffer<void>
|
||||
|
@@ -30,8 +30,13 @@ class Mixer;
|
||||
struct ReplayGainConfig;
|
||||
struct ReplayGainInfo;
|
||||
|
||||
/**
|
||||
* @param allow_convert allow the class to convert to a different
|
||||
* #SampleFormat to preserve quality?
|
||||
*/
|
||||
std::unique_ptr<PreparedFilter>
|
||||
NewReplayGainFilter(const ReplayGainConfig &config) noexcept;
|
||||
NewReplayGainFilter(const ReplayGainConfig &config,
|
||||
bool allow_convert) noexcept;
|
||||
|
||||
/**
|
||||
* Enables or disables the hardware mixer for applying replay gain.
|
||||
|
@@ -30,7 +30,8 @@ class VolumeFilter final : public Filter {
|
||||
public:
|
||||
explicit VolumeFilter(const AudioFormat &audio_format)
|
||||
:Filter(audio_format) {
|
||||
out_audio_format.format = pv.Open(out_audio_format.format);
|
||||
out_audio_format.format = pv.Open(out_audio_format.format,
|
||||
true);
|
||||
}
|
||||
|
||||
unsigned GetVolume() const noexcept {
|
||||
@@ -85,4 +86,3 @@ volume_filter_set(Filter *_filter, unsigned volume) noexcept
|
||||
|
||||
filter->SetVolume(volume);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user