[tests] check if tempo creation suceeded
[aubio.git] / tests / src / tempo / test-tempo.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] [win_size] [hop_size]\n", argv[0]);
12     return err;
13   }
14   uint_t samplerate = 0;
15   if ( argc >= 3 ) samplerate = atoi(argv[2]);
16   uint_t win_size = 1024; // window size
17   if ( argc >= 4 ) win_size = atoi(argv[3]);
18   uint_t hop_size = win_size / 4;
19   if ( argc >= 5 ) hop_size = atoi(argv[4]);
20   uint_t n_frames = 0, read = 0;
21
22   char_t *source_path = argv[1];
23   aubio_source_t * source = new_aubio_source(source_path, samplerate, hop_size);
24   if (!source) { err = 1; goto beach; }
25
26   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
27
28   // create some vectors
29   fvec_t * in = new_fvec (hop_size); // input audio buffer
30   fvec_t * out = new_fvec (1); // output position
31
32   // create tempo object
33   aubio_tempo_t * o = new_aubio_tempo("default", win_size, hop_size, samplerate);
34
35   if (!o) { err = 1; goto beach_tempo; }
36
37   do {
38     // put some fresh data in input vector
39     aubio_source_do(source, in, &read);
40     // execute tempo
41     aubio_tempo_do(o,in,out);
42     // do something with the beats
43     if (out->data[0] != 0) {
44       PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2fbpm with confidence %.2f\n",
45           aubio_tempo_get_last_ms(o), aubio_tempo_get_last_s(o),
46           aubio_tempo_get_last(o), aubio_tempo_get_bpm(o), aubio_tempo_get_confidence(o));
47     }
48     n_frames += read;
49   } while ( read == hop_size );
50
51   PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
52       n_frames * 1. / samplerate,
53       n_frames, samplerate,
54       n_frames / hop_size, source_path);
55
56   // clean up memory
57   del_aubio_tempo(o);
58 beach_tempo:
59   del_fvec(in);
60   del_fvec(out);
61   del_aubio_source(source);
62 beach:
63   aubio_cleanup();
64
65   return err;
66 }