From 5fa76102642a6faf043922e0e60f0b1cae1d84da Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 5 Jul 2019 19:03:00 +0200 Subject: [PATCH] pcm/Volume: Open() returns output sample format Prepare for a new mode which may convert to a different sample format when applying volume, to reduce dithering. --- src/filter/plugins/ReplayGainFilterPlugin.cxx | 2 +- src/filter/plugins/VolumeFilterPlugin.cxx | 2 +- src/pcm/Volume.cxx | 4 ++-- src/pcm/Volume.hxx | 5 +++-- test/software_volume.cxx | 6 +++++- test/test_pcm_volume.cxx | 2 +- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/filter/plugins/ReplayGainFilterPlugin.cxx b/src/filter/plugins/ReplayGainFilterPlugin.cxx index 8260ced70..43906fb63 100644 --- a/src/filter/plugins/ReplayGainFilterPlugin.cxx +++ b/src/filter/plugins/ReplayGainFilterPlugin.cxx @@ -77,7 +77,7 @@ public: mixer(_mixer), base(_base) { info.Clear(); - pv.Open(out_audio_format.format); + out_audio_format.format = pv.Open(out_audio_format.format); } void SetInfo(const ReplayGainInfo *_info) { diff --git a/src/filter/plugins/VolumeFilterPlugin.cxx b/src/filter/plugins/VolumeFilterPlugin.cxx index d10f5d1b4..8ebf6c97b 100644 --- a/src/filter/plugins/VolumeFilterPlugin.cxx +++ b/src/filter/plugins/VolumeFilterPlugin.cxx @@ -30,7 +30,7 @@ class VolumeFilter final : public Filter { public: explicit VolumeFilter(const AudioFormat &audio_format) :Filter(audio_format) { - pv.Open(out_audio_format.format); + out_audio_format.format = pv.Open(out_audio_format.format); } unsigned GetVolume() const noexcept { diff --git a/src/pcm/Volume.cxx b/src/pcm/Volume.cxx index 5fc8e3edc..563b95db4 100644 --- a/src/pcm/Volume.cxx +++ b/src/pcm/Volume.cxx @@ -96,7 +96,7 @@ pcm_volume_change_float(float *dest, const float *src, size_t n, dest[i] = src[i] * volume; } -void +SampleFormat PcmVolume::Open(SampleFormat _format) { assert(format == SampleFormat::UNDEFINED); @@ -118,7 +118,7 @@ PcmVolume::Open(SampleFormat _format) break; } - format = _format; + return format = _format; } ConstBuffer diff --git a/src/pcm/Volume.hxx b/src/pcm/Volume.hxx index 3dbf7b738..52f170ead 100644 --- a/src/pcm/Volume.hxx +++ b/src/pcm/Volume.hxx @@ -94,9 +94,10 @@ public: * * Throws on error. * - * @param format the sample format + * @param format the input sample format + * @return the output sample format */ - void Open(SampleFormat format); + SampleFormat Open(SampleFormat format); /** * Closes the object. After that, you may call Open() again. diff --git a/test/software_volume.cxx b/test/software_volume.cxx index 4c21b4418..17b9b7167 100644 --- a/test/software_volume.cxx +++ b/test/software_volume.cxx @@ -50,7 +50,11 @@ try { audio_format = ParseAudioFormat(argv[1], false); PcmVolume pv; - pv.Open(audio_format.format); + const auto out_sample_format = pv.Open(audio_format.format); + + if (out_sample_format != audio_format.format) + fprintf(stderr, "Converting to %s\n", + sample_format_to_string(out_sample_format)); while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { auto dest = pv.Apply({buffer, size_t(nbytes)}); diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx index edad9952a..536fee9e3 100644 --- a/test/test_pcm_volume.cxx +++ b/test/test_pcm_volume.cxx @@ -36,7 +36,7 @@ TestVolume(G g=G()) typedef typename Traits::value_type value_type; PcmVolume pv; - pv.Open(F); + EXPECT_EQ(pv.Open(F), F); constexpr size_t N = 509; static value_type zero[N];