[5d1f716] | 1 | #include <aubio.h> |
---|
[248da64] | 2 | #include "utils_tests.h" |
---|
[5d1f716] | 3 | |
---|
[4ca4a4a] | 4 | int test_wrong_params(void); |
---|
[248da64] | 5 | |
---|
[4ca4a4a] | 6 | int main(int argc, char **argv) |
---|
| 7 | { |
---|
| 8 | uint_t err = 0; |
---|
| 9 | if (argc < 3 || argc >= 6) { |
---|
| 10 | PRINT_ERR("wrong number of arguments, running tests\n"); |
---|
| 11 | err = test_wrong_params(); |
---|
| 12 | PRINT_MSG("usage: %s <input_path> <output_path> [samplerate] [hop_size]\n", |
---|
| 13 | argv[0]); |
---|
[248da64] | 14 | return err; |
---|
| 15 | } |
---|
[5d1f716] | 16 | |
---|
[9f68f11] | 17 | uint_t samplerate = 0; |
---|
| 18 | uint_t hop_size = 512; |
---|
| 19 | uint_t n_frames = 0, read = 0; |
---|
| 20 | |
---|
[248da64] | 21 | char_t *source_path = argv[1]; |
---|
| 22 | char_t *sink_path = argv[2]; |
---|
[d67a08e] | 23 | |
---|
[43ea60d] | 24 | aubio_source_t *src = NULL; |
---|
| 25 | aubio_sink_t *snk = NULL; |
---|
[387b605] | 26 | |
---|
[d67a08e] | 27 | if ( argc >= 4 ) samplerate = atoi(argv[3]); |
---|
| 28 | if ( argc >= 5 ) hop_size = atoi(argv[4]); |
---|
[248da64] | 29 | |
---|
[5d1f716] | 30 | fvec_t *vec = new_fvec(hop_size); |
---|
[387b605] | 31 | if (!vec) { err = 1; goto failure; } |
---|
[d67a08e] | 32 | |
---|
[43ea60d] | 33 | src = new_aubio_source(source_path, samplerate, hop_size); |
---|
| 34 | if (!src) { err = 1; goto failure; } |
---|
| 35 | if (samplerate == 0 ) samplerate = aubio_source_get_samplerate(src); |
---|
[d67a08e] | 36 | |
---|
[43ea60d] | 37 | snk = new_aubio_sink(sink_path, samplerate); |
---|
| 38 | if (!snk) { err = 1; goto failure; } |
---|
[5d1f716] | 39 | |
---|
[248da64] | 40 | do { |
---|
[43ea60d] | 41 | aubio_source_do(src, vec, &read); |
---|
| 42 | aubio_sink_do(snk, vec, read); |
---|
[248da64] | 43 | n_frames += read; |
---|
| 44 | } while ( read == hop_size ); |
---|
| 45 | |
---|
[4ca4a4a] | 46 | PRINT_MSG("%d frames read at %dHz (%d blocks) from %s and written to %s\n", |
---|
[d67a08e] | 47 | n_frames, samplerate, n_frames / hop_size, |
---|
| 48 | source_path, sink_path); |
---|
[5d1f716] | 49 | |
---|
[4ca4a4a] | 50 | // close sink now (optional) |
---|
[43ea60d] | 51 | aubio_sink_close(snk); |
---|
[4ca4a4a] | 52 | |
---|
| 53 | failure: |
---|
[43ea60d] | 54 | if (snk) |
---|
| 55 | del_aubio_sink(snk); |
---|
| 56 | if (src) |
---|
| 57 | del_aubio_source(src); |
---|
[4ca4a4a] | 58 | if (vec) |
---|
| 59 | del_fvec(vec); |
---|
| 60 | |
---|
[5d1f716] | 61 | return err; |
---|
| 62 | } |
---|
[4ca4a4a] | 63 | |
---|
| 64 | int test_wrong_params(void) |
---|
| 65 | { |
---|
| 66 | fvec_t *vec; |
---|
| 67 | fmat_t *mat; |
---|
| 68 | aubio_sink_t *s; |
---|
| 69 | char_t sink_path[PATH_MAX] = "tmp_aubio_XXXXXX"; |
---|
| 70 | uint_t samplerate = 44100; |
---|
| 71 | uint_t hop_size = 256; |
---|
| 72 | uint_t oversized_hop_size = 4097; |
---|
| 73 | uint_t oversized_samplerate = 192000 * 8 + 1; |
---|
| 74 | uint_t channels = 3; |
---|
| 75 | uint_t oversized_channels = 1025; |
---|
| 76 | // create temp file |
---|
| 77 | int fd = create_temp_sink(sink_path); |
---|
| 78 | |
---|
| 79 | if (!fd) return 1; |
---|
| 80 | |
---|
| 81 | if (new_aubio_sink( 0, samplerate)) return 1; |
---|
| 82 | if (new_aubio_sink("\0", samplerate)) return 1; |
---|
| 83 | if (new_aubio_sink(sink_path, -1)) return 1; |
---|
| 84 | |
---|
| 85 | s = new_aubio_sink(sink_path, 0); |
---|
| 86 | |
---|
| 87 | // check setting wrong parameters fails |
---|
| 88 | if (!aubio_sink_preset_samplerate(s, oversized_samplerate)) return 1; |
---|
| 89 | if (!aubio_sink_preset_channels(s, oversized_channels)) return 1; |
---|
| 90 | if (!aubio_sink_preset_channels(s, -1)) return 1; |
---|
| 91 | |
---|
| 92 | // check setting valid parameters passes |
---|
| 93 | if (aubio_sink_preset_samplerate(s, samplerate)) return 1; |
---|
| 94 | if (aubio_sink_preset_channels(s, 1)) return 1; |
---|
| 95 | |
---|
| 96 | // check writing a vector with valid length |
---|
| 97 | vec = new_fvec(hop_size); |
---|
| 98 | aubio_sink_do(s, vec, hop_size); |
---|
| 99 | // check writing more than in the input |
---|
| 100 | aubio_sink_do(s, vec, hop_size+1); |
---|
| 101 | // check write 0 frames |
---|
| 102 | aubio_sink_do(s, vec, 0); |
---|
| 103 | del_fvec(vec); |
---|
| 104 | |
---|
| 105 | // check writing an oversized vector |
---|
| 106 | vec = new_fvec(oversized_hop_size); |
---|
| 107 | aubio_sink_do(s, vec, oversized_hop_size); |
---|
| 108 | del_fvec(vec); |
---|
| 109 | |
---|
| 110 | // test delete without closing |
---|
| 111 | del_aubio_sink(s); |
---|
| 112 | |
---|
| 113 | s = new_aubio_sink(sink_path, 0); |
---|
| 114 | |
---|
| 115 | // preset channels first |
---|
| 116 | if (aubio_sink_preset_channels(s, channels)) return 1; |
---|
| 117 | if (aubio_sink_preset_samplerate(s, samplerate)) return 1; |
---|
| 118 | |
---|
[05774ba3] | 119 | if (aubio_sink_get_samplerate(s) != samplerate) return 1; |
---|
| 120 | if (aubio_sink_get_channels(s) != channels) return 1; |
---|
| 121 | |
---|
[4ca4a4a] | 122 | mat = new_fmat(channels, hop_size); |
---|
| 123 | // check writing a vector with valid length |
---|
| 124 | aubio_sink_do_multi(s, mat, hop_size); |
---|
[05774ba3] | 125 | // check writing 0 frames |
---|
| 126 | aubio_sink_do_multi(s, mat, 0); |
---|
[4ca4a4a] | 127 | // check writing more than in the input |
---|
| 128 | aubio_sink_do_multi(s, mat, hop_size+1); |
---|
| 129 | del_fmat(mat); |
---|
| 130 | |
---|
| 131 | // check writing oversized input |
---|
| 132 | mat = new_fmat(channels, oversized_hop_size); |
---|
| 133 | aubio_sink_do_multi(s, mat, oversized_hop_size); |
---|
| 134 | del_fmat(mat); |
---|
| 135 | |
---|
| 136 | // check writing undersized input |
---|
| 137 | mat = new_fmat(channels - 1, hop_size); |
---|
| 138 | aubio_sink_do_multi(s, mat, hop_size); |
---|
| 139 | del_fmat(mat); |
---|
| 140 | |
---|
| 141 | aubio_sink_close(s); |
---|
| 142 | // test closing twice |
---|
| 143 | aubio_sink_close(s); |
---|
| 144 | |
---|
| 145 | del_aubio_sink(s); |
---|
| 146 | |
---|
| 147 | // delete temp file |
---|
| 148 | close_temp_sink(sink_path, fd); |
---|
| 149 | |
---|
| 150 | return run_on_default_source_and_sink(main); |
---|
| 151 | } |
---|