[tests] test-source checks for undersized/oversized output
[aubio.git] / tests / src / io / base-source_custom.h
1 // this should be included *after* custom functions have been defined
2
3 #ifndef aubio_source_custom
4 #define aubio_source_custom "undefined"
5 #endif /* aubio_source_custom */
6
7 #ifdef HAVE_AUBIO_SOURCE_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 < 2) {
14     PRINT_ERR("not enough arguments, running tests\n");
15     err = test_wrong_params();
16     PRINT_MSG("read a wave file as a mono vector\n");
17     PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
18     PRINT_MSG("examples:\n");
19     PRINT_MSG(" - read file.wav at original samplerate\n");
20     PRINT_MSG("       %s file.wav\n", argv[0]);
21     PRINT_MSG(" - read file.wav at 32000Hz\n");
22     PRINT_MSG("       %s file.aif 32000\n", argv[0]);
23     PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
24     PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
25     return err;
26   }
27
28   uint_t samplerate = 0;
29   uint_t hop_size = 256;
30   uint_t n_frames = 0, read = 0;
31   if ( argc >= 3 ) samplerate = atoi(argv[2]);
32   if ( argc >= 4 ) hop_size = atoi(argv[3]);
33
34   char_t *source_path = argv[1];
35
36   aubio_source_custom_t * s =
37     new_aubio_source_custom(source_path, samplerate, hop_size);
38   fvec_t *vec = new_fvec(hop_size);
39   if (!s || !vec) { err = 1; goto beach; }
40
41   uint_t n_frames_expected = aubio_source_custom_get_duration(s);
42
43   samplerate = aubio_source_custom_get_samplerate(s);
44
45   do {
46     aubio_source_custom_do(s, vec, &read);
47     fvec_print (vec);
48     n_frames += read;
49   } while ( read == hop_size );
50
51   PRINT_MSG("read %d frames (expected %d) at %dHz (%d blocks) from %s\n",
52             n_frames, n_frames_expected, samplerate, n_frames / hop_size,
53             source_path);
54
55   // close the file (optional)
56   aubio_source_custom_close(s);
57
58 beach:
59   if (vec)
60     del_fvec(vec);
61   if (s)
62     del_aubio_source_custom(s);
63   return err;
64 }
65
66 int test_wrong_params(void)
67 {
68   char_t *uri = DEFINEDSTRING(AUBIO_TESTS_SOURCE);
69   uint_t samplerate = 44100;
70   uint_t hop_size = 512;
71   uint_t channels, read = 0;
72   fvec_t *vec;
73   fmat_t *mat;
74   aubio_source_custom_t *s;
75
76   if (new_aubio_source_custom(0,    samplerate, hop_size)) return 1;
77   if (new_aubio_source_custom("\0", samplerate, hop_size)) return 1;
78   if (new_aubio_source_custom(uri,          -1, hop_size)) return 1;
79   if (new_aubio_source_custom(uri,           0,        0)) return 1;
80
81   s = new_aubio_source_custom(uri, samplerate, hop_size);
82   if (!s) return 1;
83   channels = aubio_source_custom_get_channels(s);
84
85   // vector to read downmixed samples
86   vec = new_fvec(hop_size);
87   // matrix to read individual channels
88   mat = new_fmat(channels, hop_size);
89
90   if (aubio_source_custom_get_samplerate(s) != samplerate) return 1;
91
92   // read first hop_size frames
93   aubio_source_custom_do(s, vec, &read);
94   if (read != hop_size) return 1;
95
96   // read again in undersized vector
97   del_fvec(vec);
98   vec = new_fvec(hop_size - 1);
99   aubio_source_custom_do(s, vec, &read);
100   if (read != hop_size - 1) return 1;
101
102   // read again in oversized vector
103   del_fvec(vec);
104   vec = new_fvec(hop_size + 1);
105   aubio_source_custom_do(s, vec, &read);
106   if (read != hop_size) return 1;
107
108   // seek to 0
109   if(aubio_source_custom_seek(s, 0)) return 1;
110
111   // read again as multiple channels
112   aubio_source_custom_do_multi(s, mat, &read);
113   if (read != hop_size) return 1;
114
115   // read again as multiple channels in an undersized matrix
116   del_fmat(mat);
117   mat = new_fmat(channels - 1, hop_size);
118   aubio_source_custom_do_multi(s, mat, &read);
119   if (read != hop_size) return 1;
120
121   // read again as multiple channels in an undersized matrix
122   del_fmat(mat);
123   mat = new_fmat(channels, hop_size - 1);
124   aubio_source_custom_do_multi(s, mat, &read);
125   if (read != hop_size - 1) return 1;
126
127   // read again as multiple channels in an oversized matrix
128   del_fmat(mat);
129   mat = new_fmat(channels + 1, hop_size);
130   aubio_source_custom_do_multi(s, mat, &read);
131   if (read != hop_size) return 1;
132
133   // read again as multiple channels in an oversized matrix
134   del_fmat(mat);
135   mat = new_fmat(channels, hop_size + 1);
136   aubio_source_custom_do_multi(s, mat, &read);
137   if (read != hop_size) return 1;
138
139   // close the file (optional)
140   aubio_source_custom_close(s);
141   // test closing the file a second time
142   aubio_source_custom_close(s);
143
144   del_aubio_source_custom(s);
145   del_fmat(mat);
146   del_fvec(vec);
147
148   return run_on_default_source(base_main);
149 }
150
151 #else /* HAVE_AUBIO_SOURCE_CUSTOM */
152
153 int base_main(int argc, char** argv)
154 {
155   PRINT_ERR("aubio was not compiled with aubio_source_"
156           aubio_source_custom ", failed running %s with %d args\n",
157           argv[0], argc);
158   return 0;
159 }
160
161 #endif /* HAVE_AUBIO_SOURCE_CUSTOM */