From afcf0795c44260f54e74ec854297de439e7562c3 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 22 Dec 2013 17:39:26 +0100 Subject: pcm/Volume: improved dithering Instead of just adding a rectangular random value before shifting back to the normal scale, use the existing PcmDither library. --- src/pcm/Volume.cxx | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'src/pcm/Volume.cxx') diff --git a/src/pcm/Volume.cxx b/src/pcm/Volume.cxx index 0ccfe03b5..6347657c6 100644 --- a/src/pcm/Volume.cxx +++ b/src/pcm/Volume.cxx @@ -25,62 +25,69 @@ #include "util/ConstBuffer.hxx" #include "util/Error.hxx" +#include "PcmDither.cxx" // including the .cxx file to get inlined templates + #include #include template> static inline typename Traits::value_type -pcm_volume_sample(typename Traits::value_type _sample, +pcm_volume_sample(PcmDither &dither, + typename Traits::value_type _sample, int volume) { typename Traits::long_type sample(_sample); - sample = (sample * volume + pcm_volume_dither() + - PCM_VOLUME_1S / 2) - >> PCM_VOLUME_BITS; - - return PcmClamp(sample); + return dither.DitherShift(sample * volume); } template> static void -pcm_volume_change(typename Traits::pointer_type dest, +pcm_volume_change(PcmDither &dither, + typename Traits::pointer_type dest, typename Traits::const_pointer_type src, typename Traits::const_pointer_type end, int volume) { while (src < end) { const auto sample = *src++; - *dest++ = pcm_volume_sample(sample, volume); + *dest++ = pcm_volume_sample(dither, sample, volume); } } static void -pcm_volume_change_8(int8_t *dest, const int8_t *src, const int8_t *end, +pcm_volume_change_8(PcmDither &dither, + int8_t *dest, const int8_t *src, const int8_t *end, int volume) { - pcm_volume_change(dest, src, end, volume); + pcm_volume_change(dither, dest, src, end, volume); } static void -pcm_volume_change_16(int16_t *dest, const int16_t *src, const int16_t *end, +pcm_volume_change_16(PcmDither &dither, + int16_t *dest, const int16_t *src, const int16_t *end, int volume) { - pcm_volume_change(dest, src, end, volume); + pcm_volume_change(dither, dest, src, end, volume); } static void -pcm_volume_change_24(int32_t *dest, const int32_t *src, const int32_t *end, +pcm_volume_change_24(PcmDither &dither, + int32_t *dest, const int32_t *src, const int32_t *end, int volume) { - pcm_volume_change(dest, src, end, volume); + pcm_volume_change(dither, dest, src, end, + volume); } static void -pcm_volume_change_32(int32_t *dest, const int32_t *src, const int32_t *end, +pcm_volume_change_32(PcmDither &dither, + int32_t *dest, const int32_t *src, const int32_t *end, int volume) { - pcm_volume_change(dest, src, end, volume); + pcm_volume_change(dither, dest, src, end, volume); } static void @@ -143,28 +150,28 @@ PcmVolume::Apply(ConstBuffer src) gcc_unreachable(); case SampleFormat::S8: - pcm_volume_change_8((int8_t *)data, + pcm_volume_change_8(dither, (int8_t *)data, (const int8_t *)src.data, (const int8_t *)end, volume); break; case SampleFormat::S16: - pcm_volume_change_16((int16_t *)data, + pcm_volume_change_16(dither, (int16_t *)data, (const int16_t *)src.data, (const int16_t *)end, volume); break; case SampleFormat::S24_P32: - pcm_volume_change_24((int32_t *)data, + pcm_volume_change_24(dither, (int32_t *)data, (const int32_t *)src.data, (const int32_t *)end, volume); break; case SampleFormat::S32: - pcm_volume_change_32((int32_t *)data, + pcm_volume_change_32(dither, (int32_t *)data, (const int32_t *)src.data, (const int32_t *)end, volume); -- cgit v1.2.3