pcm/Normalizer: use std::span
This commit is contained in:
parent
738254b2fc
commit
8ea9b89321
|
@ -54,7 +54,7 @@ NormalizeFilter::FilterPCM(std::span<const std::byte> src)
|
||||||
auto *dest = (int16_t *)buffer.Get(src.size());
|
auto *dest = (int16_t *)buffer.Get(src.size());
|
||||||
memcpy(dest, src.data(), 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() };
|
return { (const std::byte *)dest, src.size() };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include "Traits.hxx"
|
#include "Traits.hxx"
|
||||||
|
|
||||||
void
|
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;
|
constexpr SampleFormat format = SampleFormat::S16;
|
||||||
using Traits = SampleTraits<format>;
|
using Traits = SampleTraits<format>;
|
||||||
|
@ -16,7 +16,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
|
||||||
const int slot = (pos + 1) % bufsz;
|
const int slot = (pos + 1) % bufsz;
|
||||||
|
|
||||||
int peakVal = 1, peakPos = 0;
|
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];
|
int val = audio[i];
|
||||||
if (val < 0)
|
if (val < 0)
|
||||||
val = -val;
|
val = -val;
|
||||||
|
@ -52,7 +52,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
|
||||||
newGain = 1 << 10;
|
newGain = 1 << 10;
|
||||||
|
|
||||||
//! Make sure the adjusted gain won't cause clipping
|
//! 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)
|
if ((peakVal*newGain >> 10) > Traits::MAX)
|
||||||
{
|
{
|
||||||
newGain = (Traits::MAX << 10)/peakVal;
|
newGain = (Traits::MAX << 10)/peakVal;
|
||||||
|
@ -69,7 +69,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
|
||||||
curGain = 1 << 10;
|
curGain = 1 << 10;
|
||||||
const int delta = (newGain - curGain) / (int)ramp;
|
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
|
//! Amplify the sample
|
||||||
audio[i] = PcmClamp<format>(audio[i] * curGain >> 10);
|
audio[i] = PcmClamp<format>(audio[i] * curGain >> 10);
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
class PcmNormalizer {
|
class PcmNormalizer {
|
||||||
///! Target level (on a scale of 0-32767)
|
///! Target level (on a scale of 0-32767)
|
||||||
|
@ -40,7 +41,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Process 16-bit signed data
|
//! 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
|
//! TODO: Compressor_Process_int32, Compressor_Process_float, others as needed
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include "pcm/AudioParser.hxx"
|
#include "pcm/AudioParser.hxx"
|
||||||
#include "pcm/AudioFormat.hxx"
|
#include "pcm/AudioFormat.hxx"
|
||||||
#include "util/PrintException.hxx"
|
#include "util/PrintException.hxx"
|
||||||
|
#include "util/SpanCast.hxx"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
@ -22,9 +23,6 @@
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
try {
|
try {
|
||||||
static char buffer[4096];
|
|
||||||
ssize_t nbytes;
|
|
||||||
|
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
fprintf(stderr, "Usage: run_normalize [FORMAT] <IN >OUT\n");
|
fprintf(stderr, "Usage: run_normalize [FORMAT] <IN >OUT\n");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -36,9 +34,10 @@ try {
|
||||||
|
|
||||||
PcmNormalizer normalizer;
|
PcmNormalizer normalizer;
|
||||||
|
|
||||||
|
static std::byte buffer[4096];
|
||||||
|
ssize_t nbytes;
|
||||||
while ((nbytes = read(0, buffer, sizeof(buffer))) > 0) {
|
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);
|
[[maybe_unused]] ssize_t ignored = write(1, buffer, nbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue