5979152415a8c5667386001960676690c737e70c
[aubio.git] / tests / src / tempo / test-tempo.c
1 #include <aubio.h>
2 #include "utils_tests.h"
3
4 int test_wrong_params(void);
5
6 int main (int argc, char **argv)
7 {
8   uint_t err = 0;
9   if (argc < 2) {
10     PRINT_WRN("no arguments, running tests\n");
11     err = test_wrong_params();
12     PRINT_MSG("usage: %s <source_path> [samplerate] [win_size] [hop_size]\n",
13         argv[0]);
14     return err;
15   }
16   uint_t samplerate = 0;
17   if ( argc >= 3 ) samplerate = atoi(argv[2]);
18   uint_t win_size = 1024; // window size
19   if ( argc >= 4 ) win_size = atoi(argv[3]);
20   uint_t hop_size = win_size / 4;
21   if ( argc >= 5 ) hop_size = atoi(argv[4]);
22   uint_t n_frames = 0, read = 0;
23
24   char_t *source_path = argv[1];
25   aubio_source_t * source = new_aubio_source(source_path, samplerate,
26       hop_size);
27   if (!source) { err = 1; goto beach; }
28
29   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
30
31   // create some vectors
32   fvec_t * in = new_fvec (hop_size); // input audio buffer
33   fvec_t * out = new_fvec (1); // output position
34
35   // create tempo object
36   aubio_tempo_t * o = new_aubio_tempo("default", win_size, hop_size,
37       samplerate);
38
39   if (!o) { err = 1; goto beach_tempo; }
40
41   do {
42     // put some fresh data in input vector
43     aubio_source_do(source, in, &read);
44     // execute tempo
45     aubio_tempo_do(o,in,out);
46     // do something with the beats
47     if (out->data[0] != 0) {
48       PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
49           "with confidence %.2f\n",
50           aubio_tempo_get_last_ms(o), aubio_tempo_get_last_s(o),
51           aubio_tempo_get_last(o), aubio_tempo_get_bpm(o),
52           aubio_tempo_get_confidence(o));
53     }
54     n_frames += read;
55   } while ( read == hop_size );
56
57   PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
58       n_frames * 1. / samplerate,
59       n_frames, samplerate,
60       n_frames / hop_size, source_path);
61
62   // clean up memory
63   del_aubio_tempo(o);
64 beach_tempo:
65   del_fvec(in);
66   del_fvec(out);
67   del_aubio_source(source);
68 beach:
69   aubio_cleanup();
70
71   return err;
72 }
73
74 int test_wrong_params(void)
75 {
76   uint_t win_size = 1024;
77   uint_t hop_size = 256;
78   uint_t samplerate = 44100;
79   aubio_tempo_t *t;
80   fvec_t* in, *out;
81   uint_t i;
82
83   // test wrong method fails
84   if (new_aubio_tempo("unexisting_method", win_size, hop_size, samplerate))
85     return 1;
86
87   // test hop > win fails
88   if (new_aubio_tempo("default", hop_size, win_size, samplerate))
89     return 1;
90
91   // test null hop_size fails
92   if (new_aubio_tempo("default", win_size, 0, samplerate))
93     return 1;
94
95   // test 1 buf_size fails
96   if (new_aubio_tempo("default", 1, 1, samplerate))
97     return 1;
98
99   // test null samplerate fails
100   if (new_aubio_tempo("default", win_size, hop_size, 0))
101     return 1;
102
103   // test short sizes workaround
104   t = new_aubio_tempo("default", 2048, 2048, 500);
105   if (!t)
106     return 1;
107
108   del_aubio_tempo(t);
109
110   t = new_aubio_tempo("default", win_size, hop_size, samplerate);
111   if (!t)
112     return 1;
113
114   in = new_fvec(hop_size);
115   out = new_fvec(1);
116
117   // up to step = (next_power_of_two(5.8 * samplerate / hop_size ) / 4 )
118   for (i = 0; i < 256 + 1; i++)
119   {
120     aubio_tempo_do(t,in,out);
121     PRINT_MSG("beat at %.3fms, %.3fs, frame %d, %.2f bpm "
122         "with confidence %.2f, was tatum %d\n",
123         aubio_tempo_get_last_ms(t), aubio_tempo_get_last_s(t),
124         aubio_tempo_get_last(t), aubio_tempo_get_bpm(t),
125         aubio_tempo_get_confidence(t), aubio_tempo_was_tatum(t));
126   }
127
128   del_aubio_tempo(t);
129   del_fvec(in);
130   del_fvec(out);
131
132   return run_on_default_source(main);
133 }