[tests] improve test-sink
[aubio.git] / tests / src / io / test-sink.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   uint_t err = 0;
9   if (argc < 3 || argc >= 6) {
10     PRINT_ERR("wrong number of arguments, running tests\n");
11     err = test_wrong_params();
12     PRINT_MSG("usage: %s <input_path> <output_path> [samplerate] [hop_size]\n",
13         argv[0]);
14     return err;
15   }
16
17   uint_t samplerate = 0;
18   uint_t hop_size = 512;
19   uint_t n_frames = 0, read = 0;
20
21   char_t *source_path = argv[1];
22   char_t *sink_path = argv[2];
23
24   if ( argc >= 4 ) samplerate = atoi(argv[3]);
25   if ( argc >= 5 ) hop_size = atoi(argv[4]);
26
27   fvec_t *vec = new_fvec(hop_size);
28
29   aubio_source_t *i = new_aubio_source(source_path, samplerate, hop_size);
30   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(i);
31
32   aubio_sink_t *o = new_aubio_sink(sink_path, samplerate);
33
34   if (!vec || !i || !o) { err = 1; goto failure; }
35
36   do {
37     aubio_source_do(i, vec, &read);
38     aubio_sink_do(o, vec, read);
39     n_frames += read;
40   } while ( read == hop_size );
41
42   PRINT_MSG("%d frames read at %dHz (%d blocks) from %s and written to %s\n",
43       n_frames, samplerate, n_frames / hop_size,
44       source_path, sink_path);
45
46   // close sink now (optional)
47   aubio_sink_close(o);
48
49 failure:
50   if (o)
51     del_aubio_sink(o);
52   if (i)
53     del_aubio_source(i);
54   if (vec)
55     del_fvec(vec);
56
57   return err;
58 }
59
60 int test_wrong_params(void)
61 {
62   fvec_t *vec;
63   fmat_t *mat;
64   aubio_sink_t *s;
65   char_t sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
66   uint_t samplerate = 44100;
67   uint_t hop_size = 256;
68   uint_t oversized_hop_size = 4097;
69   uint_t oversized_samplerate = 192000 * 8 + 1;
70   uint_t channels = 3;
71   uint_t oversized_channels = 1025;
72   // create temp file
73   int fd = create_temp_sink(sink_path);
74
75   if (!fd) return 1;
76
77   if (new_aubio_sink(   0,   samplerate)) return 1;
78   if (new_aubio_sink("\0",   samplerate)) return 1;
79   if (new_aubio_sink(sink_path,      -1)) return 1;
80
81   s = new_aubio_sink(sink_path, 0);
82
83   // check setting wrong parameters fails
84   if (!aubio_sink_preset_samplerate(s, oversized_samplerate)) return 1;
85   if (!aubio_sink_preset_channels(s, oversized_channels)) return 1;
86   if (!aubio_sink_preset_channels(s, -1)) return 1;
87
88   // check setting valid parameters passes
89   if (aubio_sink_preset_samplerate(s, samplerate)) return 1;
90   if (aubio_sink_preset_channels(s, 1)) return 1;
91
92   // check writing a vector with valid length
93   vec = new_fvec(hop_size);
94   aubio_sink_do(s, vec, hop_size);
95   // check writing more than in the input
96   aubio_sink_do(s, vec, hop_size+1);
97   // check write 0 frames
98   aubio_sink_do(s, vec, 0);
99   del_fvec(vec);
100
101   // check writing an oversized vector
102   vec = new_fvec(oversized_hop_size);
103   aubio_sink_do(s, vec, oversized_hop_size);
104   del_fvec(vec);
105
106   // test delete without closing
107   del_aubio_sink(s);
108
109   s = new_aubio_sink(sink_path, 0);
110
111   // preset channels first
112   if (aubio_sink_preset_channels(s, channels)) return 1;
113   if (aubio_sink_preset_samplerate(s, samplerate)) return 1;
114
115   if (aubio_sink_get_samplerate(s) != samplerate) return 1;
116   if (aubio_sink_get_channels(s) != channels) return 1;
117
118   mat = new_fmat(channels, hop_size);
119   // check writing a vector with valid length
120   aubio_sink_do_multi(s, mat, hop_size);
121   // check writing 0 frames
122   aubio_sink_do_multi(s, mat, 0);
123   // check writing more than in the input
124   aubio_sink_do_multi(s, mat, hop_size+1);
125   del_fmat(mat);
126
127   // check writing oversized input
128   mat = new_fmat(channels, oversized_hop_size);
129   aubio_sink_do_multi(s, mat, oversized_hop_size);
130   del_fmat(mat);
131
132   // check writing undersized input
133   mat = new_fmat(channels - 1, hop_size);
134   aubio_sink_do_multi(s, mat, hop_size);
135   del_fmat(mat);
136
137   aubio_sink_close(s);
138   // test closing twice
139   aubio_sink_close(s);
140
141   del_aubio_sink(s);
142
143   // delete temp file
144   close_temp_sink(sink_path, fd);
145
146   return run_on_default_source_and_sink(main);
147 }