pcm/Normalizer: use std::span

This commit is contained in:
Max Kellermann 2023-03-13 12:45:01 +01:00
parent 738254b2fc
commit 8ea9b89321
4 changed files with 11 additions and 11 deletions

View File

@ -54,7 +54,7 @@ NormalizeFilter::FilterPCM(std::span<const std::byte> 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() };
}

View File

@ -8,7 +8,7 @@
#include "Traits.hxx"
void
PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
PcmNormalizer::ProcessS16(const std::span<int16_t> audio) noexcept
{
constexpr SampleFormat format = SampleFormat::S16;
using Traits = SampleTraits<format>;
@ -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<format>(audio[i] * curGain >> 10);

View File

@ -6,6 +6,7 @@
#include <cstddef>
#include <cstdint>
#include <span>
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<int16_t> audio) noexcept;
};
//! TODO: Compressor_Process_int32, Compressor_Process_float, others as needed

View File

@ -11,6 +11,7 @@
#include "pcm/AudioParser.hxx"
#include "pcm/AudioFormat.hxx"
#include "util/PrintException.hxx"
#include "util/SpanCast.hxx"
#include <stdexcept>
@ -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] <IN >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<int16_t>(std::span{buffer}.first(nbytes)));
[[maybe_unused]] ssize_t ignored = write(1, buffer, nbytes);
}