tests/src/io/test-source_seek.c: add simple test, read() / seek(0) / read()
[aubio.git] / tests / src / io / test-source_seek.c
1 #include <aubio.h>
2 #include "utils_tests.h"
3
4 int main (int argc, char **argv)
5 {
6   uint_t err = 0;
7   if (argc < 2) {
8     err = 2;
9     PRINT_ERR("not enough arguments\n");
10     PRINT_MSG("read a wave file as a mono vector\n");
11     PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
12     PRINT_MSG("examples:\n");
13     PRINT_MSG(" - read file.wav at original samplerate\n");
14     PRINT_MSG("       %s file.wav\n", argv[0]);
15     PRINT_MSG(" - read file.wav at 32000Hz\n");
16     PRINT_MSG("       %s file.aif 32000\n", argv[0]);
17     PRINT_MSG(" - read file.wav at original samplerate with 4096 blocks\n");
18     PRINT_MSG("       %s file.wav 0 4096 \n", argv[0]);
19     return err;
20   }
21
22   uint_t samplerate = 0;
23   uint_t hop_size = 256;
24   uint_t n_frames = 0, read = 0;
25   uint_t old_n_frames;
26   if ( argc == 3 ) samplerate = atoi(argv[2]);
27   if ( argc == 4 ) hop_size = atoi(argv[3]);
28
29   char_t *source_path = argv[1];
30
31   fvec_t *vec = new_fvec(hop_size);
32
33   aubio_source_t* s = new_aubio_source(source_path, samplerate, hop_size);
34   if (!s) { err = 1; goto beach; }
35
36   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(s);
37
38   do {
39     aubio_source_do(s, vec, &read);
40     //fvec_print (vec);
41     n_frames += read;
42   } while ( read == hop_size );
43
44   PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
45     n_frames / hop_size, source_path);
46
47   aubio_source_seek (s, 0);
48
49   old_n_frames = n_frames;
50
51   n_frames = 0;
52   do {
53     aubio_source_do(s, vec, &read);
54     //fvec_print (vec);
55     n_frames += read;
56   } while ( read == hop_size );
57
58   PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
59     n_frames / hop_size, source_path);
60
61   del_aubio_source (s);
62 beach:
63   del_fvec (vec);
64
65   assert ( n_frames == old_n_frames );
66   return err;
67 }