Commit b7e7edad authored by Stefan Westerfeld's avatar Stefan Westerfeld

Move Limiter implementation to separate files.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 85c73d2f
......@@ -3,7 +3,8 @@ bin_PROGRAMS = audiowmark
COMMON_SRC = utils.hh utils.cc convcode.hh convcode.cc random.hh random.cc wavdata.cc wavdata.hh \
audiostream.cc audiostream.hh sfinputstream.cc sfinputstream.hh stdoutwavoutputstream.cc stdoutwavoutputstream.hh \
sfoutputstream.cc sfoutputstream.hh rawinputstream.cc rawinputstream.hh rawoutputstream.cc rawoutputstream.hh \
rawconverter.cc rawconverter.hh mp3inputstream.cc mp3inputstream.hh wmcommon.cc wmcommon.hh fft.cc fft.hh
rawconverter.cc rawconverter.hh mp3inputstream.cc mp3inputstream.hh wmcommon.cc wmcommon.hh fft.cc fft.hh \
limiter.cc limiter.hh
COMMON_LIBS = $(SNDFILE_LIBS) $(FFTW_LIBS) $(LIBGCRYPT_LIBS) $(LIBMPG123_LIBS)
audiowmark_SOURCES = audiowmark.cc wmget.cc wmadd.cc $(COMMON_SRC)
......
#include "limiter.hh"
#include <assert.h>
#include <math.h>
#include <stdio.h>
using std::vector;
using std::max;
Limiter::Limiter (int sample_rate)
{
look_ahead = sample_rate * 0.005;
assert (look_ahead >= 1);
decay_coeff = exp (log (0.5) / (sample_rate * 0.05));
}
vector<float>
Limiter::process (const vector<float>& samples)
{
for (size_t i = 0; i < samples.size(); 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)
{
size_t todo = buffer.size() - look_ahead;
for (size_t i = 0; i < todo; i++)
{
maximum = maximum * decay_coeff + max_buffer[i] * (1 - decay_coeff);
if (maximum < max_buffer[i])
maximum = max_buffer[i];
out.push_back (buffer[i] / maximum);
//printf ("%f %f\n", buffer[i], out.back());
}
buffer.erase (buffer.begin(), buffer.begin() + todo);
max_buffer.erase (max_buffer.begin(), max_buffer.begin() + todo);
}
return out;
}
#ifndef AUDIOWMARK_LIMITER_HH
#define AUDIOWMARK_LIMITER_HH
#include <vector>
#include <sys/types.h>
class Limiter
{
float maximum = 1;
double decay_coeff = 1;
uint look_ahead = 0;
std::vector<float> max_buffer;
std::vector<float> buffer;
public:
Limiter (int sample_rate);
std::vector<float> process (const std::vector<float>& samples);
};
#endif /* AUDIOWMARK_LIMITER_HH */
......@@ -5,87 +5,29 @@
#include <math.h>
#include "sfinputstream.hh"
#include "stdoutwavoutputstream.hh"
#include "sfoutputstream.hh"
#include "utils.hh"
#include "limiter.hh"
using std::string;
using std::vector;
using std::max;
using std::min;
class Limiter
{
float mx = 1;
double decay_coeff;
uint look_ahead = 0;
vector<float> max_buffer;
vector<float> buffer;
public:
Limiter (int sample_rate)
{
look_ahead = sample_rate * 0.005;
assert (look_ahead >= 1);
decay_coeff = exp (log (0.5) / (sample_rate * 0.05));
}
vector<float>
process (const vector<float>& samples)
{
for (size_t i = 0; i < samples.size(); 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)
{
size_t todo = buffer.size() - look_ahead;
for (size_t i = 0; i < todo; i++)
{
mx = mx * decay_coeff + max_buffer[i] * (1 - decay_coeff);
if (mx < max_buffer[i])
mx = max_buffer[i];
out.push_back (buffer[i] / mx);
printf ("%f %f\n", buffer[i], out.back());
}
buffer.erase (buffer.begin(), buffer.begin() + todo);
max_buffer.erase (max_buffer.begin(), max_buffer.begin() + todo);
}
return out;
}
};
int
main (int argc, char **argv)
{
SFInputStream in;
StdoutWavOutputStream out;
SFOutputStream out;
std::string filename = (argc >= 2) ? argv[1] : "-";
Error err = in.open (filename.c_str());
Error err = in.open (argv[1]);
if (err)
{
fprintf (stderr, "teststream: open input failed: %s\n", err.message());
return 1;
}
err = out.open (in.n_channels(), in.sample_rate(), 16, in.n_frames());
err = out.open (argv[2], in.n_channels(), in.sample_rate(), 16, in.n_frames());
if (err)
{
fprintf (stderr, "teststream: open output failed: %s\n", err.message());
......@@ -99,7 +41,7 @@ main (int argc, char **argv)
for (auto& s: samples)
s *= 1.1;
samples = limiter.process (samples);
//out.write_frames (samples);
out.write_frames (samples);
}
while (samples.size());
}
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