pcm/Normalizer: make variables more local

This commit is contained in:
Max Kellermann 2023-03-13 12:25:27 +01:00
parent 941636b87b
commit e990d6eecc

View File

@ -7,18 +7,10 @@
void void
PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
{ {
std::size_t i; const int slot = (pos + 1) % bufsz;
int curGain = gain[pos];
int newGain;
int peakVal = 1;
int peakPos = 0;
int slot = (pos + 1) % bufsz;
int *clipped_ = clipped + slot;
std::size_t ramp = count;
int delta;
for (i = 0; i < count; i++) int peakVal = 1, peakPos = 0;
{ for (std::size_t i = 0; i < count; i++) {
int val = audio[i]; int val = audio[i];
if (val < 0) if (val < 0)
val = -val; val = -val;
@ -30,9 +22,7 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
} }
peaks[slot] = peakVal; peaks[slot] = peakVal;
for (std::size_t i = 0; i < bufsz; i++) {
for (i = 0; i < bufsz; i++)
{
if (peaks[i] > peakVal) if (peaks[i] > peakVal)
{ {
peakVal = peaks[i]; peakVal = peaks[i];
@ -41,9 +31,10 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
} }
//! Determine target gain //! Determine target gain
newGain = (1 << 10)*target/peakVal; int newGain = (1 << 10)*target/peakVal;
//! Adjust the gain with inertia from the previous gain value //! Adjust the gain with inertia from the previous gain value
int curGain = gain[pos];
newGain = (curGain*((1 << smooth) - 1) + newGain) >> smooth; newGain = (curGain*((1 << smooth) - 1) + newGain) >> smooth;
//! Make sure it's no more than the maximum gain value //! Make sure it's no more than the maximum gain value
@ -55,6 +46,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;
if ((peakVal*newGain >> 10) > 32767) if ((peakVal*newGain >> 10) > 32767)
{ {
newGain = (32767 << 10)/peakVal; newGain = (32767 << 10)/peakVal;
@ -69,22 +61,22 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
ramp = 1; ramp = 1;
if (!curGain) if (!curGain)
curGain = 1 << 10; curGain = 1 << 10;
delta = (newGain - curGain) / (int)ramp; const int delta = (newGain - curGain) / (int)ramp;
*clipped_ = 0; auto &clipped_ = clipped[slot];
for (i = 0; i < count; i++) clipped_ = 0;
{ for (std::size_t i = 0; i < count; i++) {
int sample; int sample;
//! Amplify the sample //! Amplify the sample
sample = audio[i] * curGain >> 10; sample = audio[i] * curGain >> 10;
if (sample < -32768) if (sample < -32768)
{ {
*clipped_ += -32768 - sample; clipped_ += -32768 - sample;
sample = -32768; sample = -32768;
} else if (sample > 32767) } else if (sample > 32767)
{ {
*clipped_ += sample - 32767; clipped_ += sample - 32767;
sample = 32767; sample = 32767;
} }
audio[i] = sample; audio[i] = sample;
@ -98,4 +90,3 @@ PcmNormalizer::ProcessS16(int16_t *audio, std::size_t count) noexcept
pos = slot; pos = slot;
} }