1 // A small program to try to even out the audio levels within a file
2 // (essentially a compressor with infinite lookahead).
10 #define WAVE_FREQ 44100.0
12 // The frequency to filter on, in Hertz. Larger values makes the
13 // compressor react faster, but if it is too large, you'll
14 // ruin the waveforms themselves.
15 #define LPFILTER_FREQ 50.0
17 // The minimum estimated sound level at any given point.
18 // If you decrease this, you'll be able to amplify really silent signals
19 // by more, but you'll also increase the level of silent (ie. noise-only) segments,
20 // possibly caused misdetected pulses in these segments.
21 #define MIN_LEVEL 0.05
23 // A final scalar to get the audio within approximately the right range.
24 // Increase to _lower_ overall volume.
25 #define DAMPENING_FACTOR 5.0
28 #define FILTER_DEPTH 4
30 struct stereo_sample {
34 inline short clip(int x)
38 } else if (x > 32767) {
45 static float a1, a2, b0, b1, b2;
48 static void filter_init(float cutoff_radians)
50 float resonance = 1.0f / sqrt(2.0f);
51 float sn = sin(cutoff_radians), cs = cos(cutoff_radians);
52 float alpha = float(sn / (2 * resonance));
54 // coefficients for lowpass filter
68 // reset filter delays
72 static float filter_update(float in)
74 float out = b0*in + d0;
75 d0 = b1 * in - a1 * out + d1;
76 d1 = b2 * in - a2 * out;
80 int main(int argc, char **argv)
82 std::vector<short> pcm;
84 while (!feof(stdin)) {
86 ssize_t ret = fread(buf, sizeof(short), BUFSIZE, stdin);
88 pcm.insert(pcm.end(), buf, buf + ret);
92 // filter forwards, then backwards (perfect phase filtering)
93 std::vector<float> filtered_samples, refiltered_samples;
94 filtered_samples.resize(pcm.size());
95 refiltered_samples.resize(pcm.size());
97 filter_init(M_PI * LPFILTER_FREQ / WAVE_FREQ);
98 for (unsigned i = 0; i < pcm.size(); ++i) {
99 filtered_samples[i] = filter_update(fabs(pcm[i]));
101 filter_init(M_PI * LPFILTER_FREQ / WAVE_FREQ);
102 for (unsigned i = pcm.size(); i --> 0; ) {
103 refiltered_samples[i] = filter_update(filtered_samples[i]);
106 for (int i = 1; i < FILTER_DEPTH; ++i) {
107 filter_init(M_PI * LPFILTER_FREQ / WAVE_FREQ);
108 for (unsigned i = 0; i < pcm.size(); ++i) {
109 filtered_samples[i] = filter_update(refiltered_samples[i]);
111 filter_init(M_PI * LPFILTER_FREQ / WAVE_FREQ);
112 for (unsigned i = pcm.size(); i --> 0; ) {
113 refiltered_samples[i] = filter_update(filtered_samples[i]);
117 for (unsigned i = 0; i < pcm.size(); ++i) {
118 float f = DAMPENING_FACTOR * std::max(refiltered_samples[i] * (1.0 / 32768.0), MIN_LEVEL);
119 short s = clip(lrintf(pcm[i] / f));
120 fwrite(&s, sizeof(s), 1, stdout);