[tests] increase awhitening coverage
[aubio.git] / tests / src / spectral / test-awhitening.c
1 #include <aubio.h>
2 #include "utils_tests.h"
3
4 int test_wrong_params(void);
5
6 int main (int argc, char **argv)
7 {
8   sint_t err = 0;
9
10   if (argc < 3) {
11     err = 2;
12     PRINT_WRN("no arguments, running tests\n");
13     if (test_wrong_params() != 0) {
14       PRINT_ERR("tests failed!\n");
15       err = 1;
16     } else {
17       err = 0;
18     }
19     PRINT_MSG("usage: %s <input_path> <output_path> [samplerate] [hop_size]\n", argv[0]);
20     return err;
21   }
22
23   uint_t samplerate = 0;
24   uint_t win_size = 1024;
25   uint_t hop_size = 512;
26   uint_t n_frames = 0, read = 0;
27
28   char_t *source_path = argv[1];
29   char_t *sink_path = argv[2];
30
31   if ( argc >= 4 ) samplerate = atoi(argv[3]);
32   if ( argc >= 5 ) hop_size = atoi(argv[4]);
33   if ( argc >= 6 ) {
34     err = 2;
35     PRINT_ERR("too many arguments\n");
36     return err;
37   }
38
39   fvec_t *vec = new_fvec(hop_size);
40   fvec_t *out = new_fvec(hop_size); // output buffer
41   fvec_t *scale = new_fvec(hop_size);
42   cvec_t *fftgrain = new_cvec(win_size); // fft norm and phase
43   if (!vec) { err = 1; goto beach_fvec; }
44
45   aubio_source_t *i = new_aubio_source(source_path, samplerate, hop_size);
46   if (!i) { err = 1; goto beach_source; }
47
48   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(i);
49
50   aubio_sink_t *o = new_aubio_sink(sink_path, samplerate);
51   if (!o) { err = 1; goto beach_sink; }
52
53   aubio_pvoc_t *pv = new_aubio_pvoc(win_size, hop_size);
54
55   aubio_spectral_whitening_t *awhitening =
56     new_aubio_spectral_whitening (win_size, hop_size, samplerate);
57
58   aubio_spectral_whitening_set_relax_time(awhitening, 20.);
59   fvec_set_all(scale, 3.);
60
61   PRINT_MSG("spectral whitening relaxation time is %f\n",
62       aubio_spectral_whitening_get_relax_time(awhitening));
63
64   do {
65     aubio_source_do(i, vec, &read);
66     aubio_pvoc_do(pv, vec, fftgrain);
67     // apply spectral whitening
68     aubio_spectral_whitening_do(awhitening, fftgrain);
69     // rebuild the signal
70     aubio_pvoc_rdo(pv, fftgrain, out);
71     // make louder
72     fvec_weight(out, scale);
73     // make sure we dont saturate
74     fvec_clamp(out, 1.);
75     // write output
76     aubio_sink_do(o, out, read);
77     n_frames += read;
78   } while ( read == hop_size );
79
80   PRINT_MSG("read %d frames at %dHz (%d blocks) from %s written to %s\n",
81       n_frames, samplerate, n_frames / hop_size,
82       source_path, sink_path);
83
84   del_aubio_sink(o);
85 beach_sink:
86   del_aubio_source(i);
87 beach_source:
88   del_fvec(vec);
89 beach_fvec:
90   return err;
91 }
92
93 int test_wrong_params(void)
94 {
95   uint_t buf_size = 512;
96   uint_t hop_size = 256;
97   uint_t samplerate = 44100;
98   aubio_spectral_whitening_t *o;
99
100   if (new_aubio_spectral_whitening(       0, hop_size, samplerate)) return 1;
101   if (new_aubio_spectral_whitening(buf_size,        0, samplerate)) return 1;
102   if (new_aubio_spectral_whitening(buf_size, hop_size,          0)) return 1;
103
104   o = new_aubio_spectral_whitening(buf_size, hop_size, samplerate);
105
106   aubio_spectral_whitening_get_relax_time(o);
107   aubio_spectral_whitening_get_floor(o);
108
109   del_aubio_spectral_whitening(o);
110
111   return 0;
112 }