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