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