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