]> git.sesse.net Git - c64tapwav/blob - level.cpp
Move detect_pulses into its own function.
[c64tapwav] / level.cpp
1 // A small program to try to even out the audio levels within a file
2 // (essentially a compressor with infinite lookahead).
3
4 #include <stdio.h>
5 #include <math.h>
6 #include <vector>
7 #include <algorithm>
8
9 #define BUFSIZE 4096
10 #define WAVE_FREQ 44100.0
11
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
16
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
22
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
26
27 // 6dB/oct per round.
28 #define FILTER_DEPTH 4
29
30 struct stereo_sample {
31         short left, right;
32 };
33
34 inline short clip(int x)
35 {
36         if (x < -32768) {
37                 return x;
38         } else if (x > 32767) {
39                 return 32767;
40         } else {
41                 return short(x);
42         }
43 }
44
45 static float a1, a2, b0, b1, b2;
46 static float d0, d1;
47
48 static void filter_init(float cutoff_radians)
49 {
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));
53
54         // coefficients for lowpass filter
55         float a0 = 1 + alpha;
56         b0 = (1 - cs) * 0.5f;
57         b1 = 1 - cs;
58         b2 = b0;
59         a1 = -2 * cs;
60         a2 = 1 - alpha;
61
62         b0 /= a0;
63         b1 /= a0;
64         b2 /= a0;
65         a1 /= a0;
66         a2 /= a0;
67
68         // reset filter delays
69         d0 = d1 = 0.0f;
70 }
71
72 static float filter_update(float in)
73 {
74         float out = b0*in + d0;
75         d0 = b1 * in - a1 * out + d1;
76         d1 = b2 * in - a2 * out;
77         return out;
78 }
79
80 int main(int argc, char **argv)
81 {
82         std::vector<short> pcm;
83
84         while (!feof(stdin)) {
85                 short buf[BUFSIZE];
86                 ssize_t ret = fread(buf, sizeof(short), BUFSIZE, stdin);
87                 if (ret >= 0) {
88                         pcm.insert(pcm.end(), buf, buf + ret);
89                 }
90         }
91         
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());
96
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]));
100         }
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]);
104         }
105
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]);
110                 }
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]);
114                 }
115         }
116
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);
121         }
122 }