[tests] factorise sink tests
[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   if ( argc >= 4 ) samplerate = atoi(argv[3]);
29   if ( argc >= 5 ) hop_size = atoi(argv[4]);
30
31   fvec_t *vec = new_fvec(hop_size);
32
33   aubio_source_t *i = new_aubio_source(source_path, samplerate, hop_size);
34   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(i);
35
36   aubio_sink_custom_t *o = new_aubio_sink_custom(sink_path, samplerate);
37
38   if (!vec || !i || !o) { err = 1; goto failure; }
39
40   do {
41     aubio_source_do(i, vec, &read);
42     aubio_sink_custom_do(o, vec, read);
43     n_frames += read;
44   } while ( read == hop_size );
45
46   PRINT_MSG("%d frames at %dHz (%d blocks) read from %s, wrote to %s\n",
47       n_frames, samplerate, n_frames / hop_size,
48       source_path, sink_path);
49
50   // close sink now (optional)
51   aubio_sink_custom_close(o);
52
53 failure:
54   if (o)
55     del_aubio_sink_custom(o);
56   if (i)
57     del_aubio_source(i);
58   if (vec)
59     del_fvec(vec);
60
61   return err;
62 }
63
64 int test_wrong_params(void)
65 {
66   return run_on_default_source_and_sink(base_main);
67 }
68
69 #else /* HAVE_AUBIO_SINK_CUSTOM */
70
71 int base_main(int argc, char** argv)
72 {
73   PRINT_ERR("aubio was not compiled with aubio_sink_"
74           aubio_sink_custom ", failed running %s with %d args\n",
75           argv[0], argc);
76   return 0;
77 }
78
79 #endif /* HAVE_AUBIO_SINK_CUSTOM */