[tests] test del_aubio_source on null
[aubio.git] / tests / src / io / base-sink_custom.h
1 // this should be included *after* custom functions have been defined
2
3 #ifndef aubio_sink_custom
4 #define aubio_sink_custom "undefined"
5 #endif /* aubio_sink_custom */
6
7 #ifdef HAVE_AUBIO_SINK_CUSTOM
8 int test_wrong_params(void);
9
10 int base_main(int argc, char **argv)
11 {
12   uint_t err = 0;
13   if (argc < 3 || argc >= 6) {
14     PRINT_ERR("wrong number of arguments, running tests\n");
15     err = test_wrong_params();
16     PRINT_MSG("usage: %s <input_path> <output_path> [samplerate] [hop_size]\n",
17         argv[0]);
18     return err;
19   }
20
21   uint_t samplerate = 0;
22   uint_t hop_size = 512;
23   uint_t n_frames = 0, read = 0;
24
25   char_t *source_path = argv[1];
26   char_t *sink_path = argv[2];
27
28   aubio_source_t *src = NULL;
29   aubio_sink_custom_t *snk = NULL;
30
31   if ( argc >= 4 ) samplerate = atoi(argv[3]);
32   if ( argc >= 5 ) hop_size = atoi(argv[4]);
33
34   fvec_t *vec = new_fvec(hop_size);
35   if (!vec) { err = 1; goto failure; }
36
37   src = new_aubio_source(source_path, samplerate, hop_size);
38   if (!src) { err = 1; goto failure; }
39   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(src);
40
41   snk = new_aubio_sink_custom(sink_path, samplerate);
42   if (!snk) { err = 1; goto failure; }
43
44   do {
45     aubio_source_do(src, vec, &read);
46     aubio_sink_custom_do(snk, vec, read);
47     n_frames += read;
48   } while ( read == hop_size );
49
50   PRINT_MSG("%d frames at %dHz (%d blocks) read from %s, wrote to %s\n",
51       n_frames, samplerate, n_frames / hop_size,
52       source_path, sink_path);
53
54   // close sink now (optional)
55   aubio_sink_custom_close(snk);
56
57 failure:
58   if (snk)
59     del_aubio_sink_custom(snk);
60   if (src)
61     del_aubio_source(src);
62   if (vec)
63     del_fvec(vec);
64
65   return err;
66 }
67
68 int test_wrong_params(void)
69 {
70   fvec_t *vec;
71   fmat_t *mat;
72   aubio_sink_custom_t *s;
73   char_t sink_path[PATH_MAX] = "tmp_aubio_XXXXXX";
74   uint_t samplerate = 44100;
75   uint_t hop_size = 256;
76   uint_t oversized_hop_size = 4097;
77   uint_t oversized_samplerate = 192000 * 8 + 1;
78   uint_t channels = 3;
79   uint_t oversized_channels = 1025;
80   // create temp file
81   int fd = create_temp_sink(sink_path);
82
83   if (!fd) return 1;
84
85   if (new_aubio_sink_custom(   0,   samplerate)) return 1;
86   if (new_aubio_sink_custom("\0",   samplerate)) return 1;
87   if (new_aubio_sink_custom(sink_path,      -1)) return 1;
88
89   s = new_aubio_sink_custom(sink_path, 0);
90
91   // check setting wrong parameters fails
92   if (!aubio_sink_custom_preset_samplerate(s, oversized_samplerate)) return 1;
93   if (!aubio_sink_custom_preset_channels(s, oversized_channels)) return 1;
94   if (!aubio_sink_custom_preset_channels(s, -1)) return 1;
95
96   // check setting valid parameters passes
97   if (aubio_sink_custom_preset_samplerate(s, samplerate)) return 1;
98   if (aubio_sink_custom_preset_channels(s, 1)) return 1;
99
100   // check writing a vector with valid length
101   vec = new_fvec(hop_size);
102   aubio_sink_custom_do(s, vec, hop_size);
103   // check writing more than in the input
104   aubio_sink_custom_do(s, vec, hop_size+1);
105   // check write 0 frames
106   aubio_sink_custom_do(s, vec, 0);
107   del_fvec(vec);
108
109   // check writing an oversized vector
110   vec = new_fvec(oversized_hop_size);
111   aubio_sink_custom_do(s, vec, oversized_hop_size);
112   del_fvec(vec);
113
114   // test delete without closing
115   del_aubio_sink_custom(s);
116
117   s = new_aubio_sink_custom(sink_path, 0);
118
119   // preset channels first
120   if (aubio_sink_custom_preset_channels(s, channels)) return 1;
121   if (aubio_sink_custom_preset_samplerate(s, samplerate)) return 1;
122
123   if (aubio_sink_custom_get_samplerate(s) != samplerate) return 1;
124   if (aubio_sink_custom_get_channels(s) != channels) return 1;
125
126   mat = new_fmat(channels, hop_size);
127   // check writing a vector with valid length
128   aubio_sink_custom_do_multi(s, mat, hop_size);
129   // check writing 0 frames
130   aubio_sink_custom_do_multi(s, mat, 0);
131   // check writing more than in the input
132   aubio_sink_custom_do_multi(s, mat, hop_size+1);
133   del_fmat(mat);
134
135   // check writing oversized input
136   mat = new_fmat(channels, oversized_hop_size);
137   aubio_sink_custom_do_multi(s, mat, oversized_hop_size);
138   del_fmat(mat);
139
140   // check writing undersized input
141   mat = new_fmat(channels - 1, hop_size);
142   aubio_sink_custom_do_multi(s, mat, hop_size);
143   del_fmat(mat);
144
145   aubio_sink_custom_close(s);
146   // test closing twice
147   aubio_sink_custom_close(s);
148
149   del_aubio_sink_custom(s);
150
151   // delete temp file
152   close_temp_sink(sink_path, fd);
153
154   return run_on_default_source_and_sink(base_main);
155 }
156
157 #else /* HAVE_AUBIO_SINK_CUSTOM */
158
159 int base_main(int argc, char** argv)
160 {
161   PRINT_ERR("aubio was not compiled with aubio_sink_"
162           aubio_sink_custom ", failed running %s with %d args\n",
163           argv[0], argc);
164   return 0;
165 }
166
167 #endif /* HAVE_AUBIO_SINK_CUSTOM */