Commit 596892e6 authored by Stefan Westerfeld's avatar Stefan Westerfeld

Limiter: introduce max buffer.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 89da8e5b
......@@ -17,6 +17,7 @@ class Limiter
{
float mx = 1;
uint look_ahead = 0;
vector<float> max_buffer;
vector<float> buffer;
public:
Limiter (int sample_rate)
......@@ -28,7 +29,24 @@ public:
process (const vector<float>& samples)
{
for (size_t i = 0; i < samples.size(); i++)
buffer.push_back (samples[i]);
{
buffer.push_back (samples[i]);
max_buffer.push_back (1);
}
for (size_t i = 0; i < buffer.size(); i++)
{
if (fabs (buffer[i]) > 1)
{
for (uint j = 0; j < look_ahead; j++)
{
if (int (i) - int (j) > 0)
{
double alpha = double (j) / look_ahead;
max_buffer[i - j] = max<float> (max_buffer[i - j], fabs (buffer[i]) * (1 - alpha) + 1 * alpha);
}
}
}
}
vector<float> out;
if (buffer.size() > look_ahead)
......@@ -36,24 +54,16 @@ public:
size_t todo = buffer.size() - look_ahead;
for (size_t i = 0; i < todo; i++)
{
float xmx = 1;
for (uint j = 0; j < look_ahead; j++)
xmx = max (fabs (buffer[i + j]), xmx);
mx = mx * 0.99 + xmx * 0.01;
mx = mx * 0.99 + max_buffer[i] * 0.01;
if (mx < max_buffer[i])
mx = max_buffer[i];
out.push_back (buffer[i] / mx);
printf ("%f %f\n", buffer[i], out.back());
}
#if 0
for (size_t i = 0; i < samples.size(); i++)
{
if (fabs (samples[i]) > mx)
mx = fabs (samples[i]);
out.push_back (samples[i] / mx);
if (mx > 1)
mx = max (mx - 0.01f, 1.f);
}
#endif
buffer.erase (buffer.begin(), buffer.begin() + todo);
max_buffer.erase (max_buffer.begin(), max_buffer.begin() + todo);
}
return out;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment