From 8ea9b8932145942ed44289351fe27d08e4ee78b0 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 13 Mar 2023 12:45:01 +0100 Subject: [PATCH] pcm/Normalizer: use std::span --- src/filter/plugins/NormalizeFilterPlugin.cxx | 2 +- src/pcm/Normalizer.cxx | 8 ++++---- src/pcm/Normalizer.hxx | 3 ++- test/run_normalize.cxx | 9 ++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/filter/plugins/NormalizeFilterPlugin.cxx b/src/filter/plugins/NormalizeFilterPlugin.cxx index 74b9d52e9..868787a09 100644 --- a/src/filter/plugins/NormalizeFilterPlugin.cxx +++ b/src/filter/plugins/NormalizeFilterPlugin.cxx @@ -54,7 +54,7 @@ NormalizeFilter::FilterPCM(std::span src) auto *dest = (int16_t *)buffer.Get(src.size()); memcpy(dest, src.data(), src.size()); - normalizer.ProcessS16(dest, src.size() / 2); + normalizer.ProcessS16({dest, src.size() / 2}); return { (const std::byte *)dest, src.size() }; } diff --git a/src/pcm/Normalizer.cxx b/src/pcm/Normalizer.cxx index ab9c67e04..cb15ca27b 100644 --- a/src/pcm/Normalizer.cxx +++ b/src/pcm/Normalizer.cxx @@ -8,7 +8,7 @@ #include "Traits.hxx" void -PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept +PcmNormalizer::ProcessS16(const std::span audio) noexcept { constexpr SampleFormat format = SampleFormat::S16; using Traits = SampleTraits; @@ -16,7 +16,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept const int slot = (pos + 1) % bufsz; int peakVal = 1, peakPos = 0; - for (std::size_t i = 0; i < count; i++) { + for (std::size_t i = 0; i < audio.size(); i++) { int val = audio[i]; if (val < 0) val = -val; @@ -52,7 +52,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept newGain = 1 << 10; //! Make sure the adjusted gain won't cause clipping - std::size_t ramp = count; + std::size_t ramp = audio.size(); if ((peakVal*newGain >> 10) > Traits::MAX) { newGain = (Traits::MAX << 10)/peakVal; @@ -69,7 +69,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept curGain = 1 << 10; const int delta = (newGain - curGain) / (int)ramp; - for (std::size_t i = 0; i < count; i++) { + for (std::size_t i = 0; i < audio.size(); i++) { //! Amplify the sample audio[i] = PcmClamp(audio[i] * curGain >> 10); diff --git a/src/pcm/Normalizer.hxx b/src/pcm/Normalizer.hxx index 507df73be..0c826343d 100644 --- a/src/pcm/Normalizer.hxx +++ b/src/pcm/Normalizer.hxx @@ -6,6 +6,7 @@ #include #include +#include class PcmNormalizer { ///! Target level (on a scale of 0-32767) @@ -40,7 +41,7 @@ public: } //! Process 16-bit signed data - void ProcessS16(int16_t *data, std::size_t count) noexcept; + void ProcessS16(std::span audio) noexcept; }; //! TODO: Compressor_Process_int32, Compressor_Process_float, others as needed diff --git a/test/run_normalize.cxx b/test/run_normalize.cxx index 682c5600f..ac8bfd140 100644 --- a/test/run_normalize.cxx +++ b/test/run_normalize.cxx @@ -11,6 +11,7 @@ #include "pcm/AudioParser.hxx" #include "pcm/AudioFormat.hxx" #include "util/PrintException.hxx" +#include "util/SpanCast.hxx" #include @@ -22,9 +23,6 @@ int main(int argc, char **argv) try { - static char buffer[4096]; - ssize_t nbytes; - if (argc > 2) { fprintf(stderr, "Usage: run_normalize [FORMAT] OUT\n"); return 1; @@ -36,9 +34,10 @@ try { PcmNormalizer normalizer; + static std::byte buffer[4096]; + ssize_t nbytes; while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) { - normalizer.ProcessS16((int16_t *)buffer, nbytes / 2); - + normalizer.ProcessS16(FromBytesStrict(std::span{buffer}.first(nbytes))); [[maybe_unused]] ssize_t ignored = write(1, buffer, nbytes); }