[tests] run some tests in onset if no arguments passed
[aubio.git] / tests / src / onset / test-onset.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     err = 2;
11     PRINT_WRN("no arguments, running tests\n");
12     if (test_wrong_params() != 0) {
13       PRINT_ERR("tests failed!\n");
14       err = 1;
15     } else {
16       err = 0;
17     }
18     PRINT_MSG("usage: %s <source_path> [samplerate] [hop_size]\n", argv[0]);
19     return err;
20   }
21   uint_t samplerate = 0;
22   uint_t win_s = 1024; // window size
23   uint_t hop_size = win_s / 4;
24   uint_t n_frames = 0, read = 0;
25   if ( argc == 3 ) samplerate = atoi(argv[2]);
26   if ( argc == 4 ) hop_size = atoi(argv[3]);
27
28   char_t *source_path = argv[1];
29   aubio_source_t * source = new_aubio_source(source_path, samplerate, hop_size);
30   if (!source) { err = 1; goto beach; }
31
32   if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(source);
33
34   // create some vectors
35   fvec_t * in = new_fvec (hop_size); // input audio buffer
36   fvec_t * out = new_fvec (2); // output position
37
38   // create onset object
39   aubio_onset_t * o = new_aubio_onset("default", win_s, hop_size, samplerate);
40
41   do {
42     // put some fresh data in input vector
43     aubio_source_do(source, in, &read);
44     // execute onset
45     aubio_onset_do(o,in,out);
46     // do something with the onsets
47     if (out->data[0] != 0) {
48       PRINT_MSG("onset at %.3fms, %.3fs, frame %d\n",
49           aubio_onset_get_last_ms(o), aubio_onset_get_last_s(o),
50           aubio_onset_get_last(o));
51     }
52     n_frames += read;
53   } while ( read == hop_size );
54
55   PRINT_MSG("read %.2fs, %d frames at %dHz (%d blocks) from %s\n",
56       n_frames * 1. / samplerate,
57       n_frames, samplerate,
58       n_frames / hop_size, source_path);
59
60   // clean up memory
61   del_aubio_source(source);
62   del_aubio_onset(o);
63   del_fvec(in);
64   del_fvec(out);
65 beach:
66   aubio_cleanup();
67
68   return err;
69 }
70
71 int test_wrong_params(void)
72 {
73   uint_t win_size = 1024;
74   uint_t hop_size = win_size / 2;
75   uint_t samplerate = 44100;
76   // hop_size < 1
77   if (new_aubio_onset("default", 5, 0, samplerate))
78     return 1;
79   // buf_size < 2
80   if (new_aubio_onset("default", 1, 1, samplerate))
81     return 1;
82   // buf_size < hop_size
83   if (new_aubio_onset("default", hop_size, win_size, samplerate))
84     return 1;
85   // samplerate < 1
86   if (new_aubio_onset("default", 1024, 512, 0))
87     return 1;
88
89   // specdesc creation failed
90   if (new_aubio_onset("abcd", win_size, win_size/2, samplerate))
91     return 1;
92   // pv creation failed
93   if (new_aubio_onset("default", 5, 2, samplerate))
94     return 1;
95
96   aubio_onset_t *o;
97   o = new_aubio_onset("default", win_size, hop_size, samplerate);
98   if (!aubio_onset_set_default_parameters(o, "wrong_type"))
99     return 1;
100   del_aubio_onset(o);
101
102   return 0;
103 }