summaryrefslogtreecommitdiffstats
path: root/dbmeasure.c
blob: 16ef8b8211a9bbd7305e56646828b2076f04c3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*-*- Mode: C; c-file-style: "linux"; indent-tabs-mode: nil; c-basic-offset: 8 -*-*/

#include <math.h>
#include <stdlib.h>
#include <asoundlib.h>

static float *generate_signal(unsigned n_samples, double amplitude, double frequency) {
        float *r;
        unsigned i;

        if (!(r = malloc(n_samples * sizeof(float))))
                return NULL;

        for (i = 0; i < n_samples; i++)
                r[i] = amplitude * sin(((double) i*frequency*M_PI*2)/(double) n_samples);

        return r;
}

static float *generate_silence(unsigned n_samples) {
        float *r;

        if (!(r = calloc(n_samples, sizeof(float))))
                return NULL;

        return r;
}

static snd_pcm_t *open_device(const char *name, snd_pcm_stream_t stream, unsigned *rate) {
        snd_pcm_t *d = NULL;
        int r;
        snd_pcm_hw_params_t *hw;
        snd_output_t *output = NULL;
        int dir = 0;
        snd_pcm_uframes_t t;

        snd_pcm_hw_params_alloca(&hw);

        if ((r = snd_pcm_open(&d, name, stream, SND_PCM_NONBLOCK)) < 0) {
                fprintf(stderr, "Cannot open audio device %s: %s\n", name, snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_hw_params_any(d, hw)) < 0) {
                fprintf(stderr, "Cannot initialize hardware parameters: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_hw_params_set_access(d, hw, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
                fprintf(stderr, "Cannot set access type: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_hw_params_set_format(d, hw, SND_PCM_FORMAT_FLOAT_LE)) < 0) {
                fprintf(stderr, "Cannot set sample format: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_hw_params_set_rate_near(d, hw, rate, &dir)) < 0) {
                fprintf(stderr, "Cannot set sample rate: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_hw_params_set_channels(d, hw, 1)) < 0) {
                fprintf(stderr, "Cannot set channel count: %s\n", snd_strerror(r));
                goto finish;
        }

        t = *rate;
        if ((r = snd_pcm_hw_params_set_buffer_size_near(d, hw, &t)) < 0) {
                fprintf(stderr, "Cannot set buffer size: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_hw_params(d, hw)) < 0) {
                fprintf(stderr, "Cannot set parameters: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_output_stdio_attach(&output, stderr, 0)) < 0) {
                fprintf(stderr, "Cannot attach to stderr: %s\n", snd_strerror(r));
                goto finish;
        }

        if ((r = snd_pcm_dump(d, output)) < 0) {
                fprintf(stderr, "Cannot dump status: %s\n", snd_strerror(r));
                goto finish;
        }

        snd_output_close(output);

        return d;

finish:
        if (d)
                snd_pcm_close(d);

        if (output)
                snd_output_close(output);


        return NULL;
}

static double compute_level(const float *buffer, float *sum, unsigned n_samples, unsigned iteration) {
        unsigned i;
        double max = 0.0;

        for (i = 0; i < n_samples; i++) {
                sum[i] += buffer[i];

                if (fabs(sum[i]) > max)
                        max = fabs(sum[i]);
        }

        return max / (double) iteration;
}

static int prompt(const char *t) {
        char r[64];
        fputs(t, stderr);

        if (!fgets(r, sizeof(r), stdin))
                return 0;

        return 1;
}

static double linear_to_dB(double f) {
        return 20.0 * log10(f);
}

int main(int argc, char *argv[]) {
        int ret = 1;
        float *signal = NULL, *silence = NULL, *buffer = NULL, *sum = NULL, *current = NULL;
        snd_pcm_t *input = NULL, *output = NULL;
        unsigned rate = 48000;
        unsigned rindex = 0, windex = 0;
        double frequency = 440.0, amplitude = 0.5, noise_level_dB = -58.0, initial_level_max = 0.97, initial_level_min = 0.8, reference_level;
        unsigned iteration = 0;
        int icount, ocount;
        struct pollfd *pollfd;
        int e;
        unsigned needed_iterations = 0;
        int skip = 1;
        FILE *log = NULL;
        int level_count = 0;

        if (argc != 3) {
                fprintf(stderr, "Need to specify device and output log file.\n");
                goto finish;
        }

        if (!(log = fopen(argv[2], "w"))) {
                fprintf(stderr, "Failed to open log file %s: %s\n", argv[2], strerror(errno));
                goto finish;
        }

        if (!(output = open_device(argv[1], SND_PCM_STREAM_PLAYBACK, &rate)))
                goto finish;

        if (!(input = open_device(argv[1], SND_PCM_STREAM_CAPTURE, &rate)))
                goto finish;

        if (!(silence = generate_silence(rate))) {
                fprintf(stderr, "Failed to generate silence.\n");
                goto finish;
        }

        if (!(signal = generate_signal(rate, amplitude, frequency))) {
                fprintf(stderr, "Failed to generate signal.\n");
                goto finish;
        }

        if (!(buffer = malloc(rate * sizeof(float)))) {
                fprintf(stderr, "Failed to allocate buffer.\n");
                goto finish;
        }

        if (!(sum = calloc(rate, sizeof(float)))) {
                fprintf(stderr, "Failed to allocate sum buffer.\n");
                goto finish;
        }

        if ((icount = snd_pcm_poll_descriptors_count(input)) <= 0 ||
            (ocount = snd_pcm_poll_descriptors_count(output)) <= 0) {
                fprintf(stderr, "Failed to determine number of pollfd descriptors.\n");
                goto finish;
        }

        if (!(pollfd = calloc(icount + ocount, sizeof(struct pollfd)))) {
                fprintf(stderr, "Failed to allocate pollfd array.\n");
                goto finish;
        }

        if ((e = snd_pcm_prepare(input)) < 0 ||
            (e = snd_pcm_prepare(output)) < 0) {
                fprintf(stderr, "snd_pcm_prepare() failed: %s\n", snd_strerror(e));
                goto finish;
        }

        if ((e = snd_pcm_start(input)) < 0) {
                fprintf(stderr, "snd_pcm_start() failed: %s\n", snd_strerror(e));
                goto finish;
        }

        current = silence;

        prompt("Please set your control to the highest volume possible and press return.\n");

        fprintf(stderr, "Measuring noise level.\n");

        for (;;) {
                snd_pcm_sframes_t n;
                unsigned short irevents, orevents;

                if (snd_pcm_poll_descriptors(input, pollfd, icount) != icount ||
                    snd_pcm_poll_descriptors(output, pollfd+icount, ocount) != ocount) {
                        fprintf(stderr, "ALSA changed its mind about number of file descriptors.");
                        goto finish;
                }

                if (poll(pollfd, icount+ocount, -1) < 0) {
                        fprintf(stderr, "poll() failed: %s\n", strerror(errno));
                        goto finish;
                }

                if ((e = snd_pcm_poll_descriptors_revents(input, pollfd, icount, &irevents)) < 0 ||
                    (e = snd_pcm_poll_descriptors_revents(output, pollfd+icount, ocount, &orevents)) < 0) {
                        fprintf(stderr, "Cannot get revents: %s\n", snd_strerror(e));
                        goto finish;
                }

                /* printf("%u out %i in\n", orevents, irevents); */

                if (orevents) {
                        if ((n = snd_pcm_writei(output, current + windex, rate - windex)) < 0) {

                                fprintf(stderr, "Got %s while writing.\n", snd_strerror(n));

                                if (snd_pcm_recover(output, n, 0) >= 0) {
                                        fprintf(stderr, "recovered\n");
                                        continue;
                                }

                                if (snd_pcm_prepare(output) >= 0) {
                                        fprintf(stderr, "prepared\n");
                                        continue;
                                }

                                if (snd_pcm_start(output) >= 0) {
                                        fprintf(stderr, "restarted\n");
                                        continue;
                                }

                                if (n != -EAGAIN) {
                                        fprintf(stderr, "Cannot write samples: %s\n", snd_strerror(n));
                                        goto finish;
                                }

                                n = 0;
                        }

                        windex += n;
                        if (windex == rate)
                                windex = 0;
                }

                if (irevents)  {
                        if ((n = snd_pcm_readi(input, buffer + rindex, rate - rindex)) < 0) {

                                fprintf(stderr, "Got %s while reading.\n", snd_strerror(n));

                                if (snd_pcm_start(input) >= 0) {
                                        fprintf(stderr, "restarted\n");
                                        continue;
                                }

                                if (snd_pcm_recover(input, n, 0) >= 0) {
                                        fprintf(stderr, "recovered\n");
                                        snd_pcm_start(input);
                                        continue;
                                }

                                if (snd_pcm_prepare(input) >= 0) {
                                        fprintf(stderr, "prepared\n");
                                        continue;
                                }

                                if (n != -EAGAIN) {
                                        fprintf(stderr, "Cannot read samples: %s\n", snd_strerror(n));
                                        goto finish;
                                }

                                n = 0;
                        }

                        rindex += n;
                        if (rindex == rate) {
                                rindex = 0;

                                if (skip)
                                        skip = 0;
                                else {
                                        double level;

                                        iteration++;
                                        level = compute_level(buffer, sum, rate, iteration);
                                        fprintf(stderr, "Iteration %u, level is %g (%g dB).\n", iteration, level, linear_to_dB(level));

                                        if (needed_iterations == 0) {
                                                if (linear_to_dB(level) < noise_level_dB) {
                                                        fprintf(stderr, "%u iterations necessary to push noise level below %g dB\n", iteration, noise_level_dB);

                                                        needed_iterations = iteration;
                                                        current = signal;
                                                        windex = 0;
                                                        skip = 1;
                                                        iteration = 0;
                                                        memset(sum, 0, rate * sizeof(float));

                                                        fprintf(stderr, "Generating signal.\n");
                                                }
                                        } else {

                                                if (iteration >= needed_iterations) {

                                                        if (level_count <= 0) {

                                                                if (level < initial_level_min ||
                                                                    level > initial_level_max) {
                                                                        fprintf(stderr,
                                                                                "Volume level measured (%g) was too high or too low.\n"
                                                                                "Please adjust mixer so that initial level is between %g and %g.\n"
                                                                                "Test run canceled.\n", level, initial_level_min, initial_level_max);
                                                                        goto finish;
                                                                }

                                                                reference_level = level;
                                                        }

                                                        fprintf(log, "%i\t%g\t%g\t%g\t%g\n",
                                                                level_count,
                                                                level, linear_to_dB(level),
                                                                level/reference_level, linear_to_dB(level/reference_level));

                                                        fflush(log);

                                                        level_count++;

                                                        windex = 0;
                                                        skip = 1;
                                                        iteration = 0;
                                                        memset(sum, 0, rate * sizeof(float));

                                                        if (!(prompt("Please reduce volume by one step and press return. Press C-D when finished.\n")))
                                                                break;
                                                }
                                        }
                                }
                        }
                }
        }

        ret = 0;

finish:

        if (input)
                snd_pcm_close(input);

        if (output)
                snd_pcm_close(output);

        if (log)
                fclose(log);

        free(signal);
        free(silence);
        free(buffer);
        free(sum);

        return ret;
}