1 | #include <aubio.h> |
---|
2 | #include "utils_tests.h" |
---|
3 | |
---|
4 | int time_was_reached (smpl_t time_s, uint_t n_frames, uint_t hop_size, uint_t samplerate) { |
---|
5 | if ((n_frames / hop_size) == (uint_t)(time_s * samplerate) / hop_size) { |
---|
6 | PRINT_MSG("reached %.2f sec at %d samples\n", time_s, n_frames); |
---|
7 | return 1; |
---|
8 | } else { |
---|
9 | return 0; |
---|
10 | } |
---|
11 | } |
---|
12 | |
---|
13 | int main (int argc, char **argv) |
---|
14 | { |
---|
15 | sint_t err = 0; |
---|
16 | |
---|
17 | if (argc < 1) { |
---|
18 | err = 2; |
---|
19 | PRINT_ERR("not enough arguments\n"); |
---|
20 | PRINT_MSG("usage: %s [samplerate] [blocksize] [output_path]\n", argv[0]); |
---|
21 | return err; |
---|
22 | } |
---|
23 | |
---|
24 | uint_t samplerate = 44100; |
---|
25 | uint_t hop_size = 64; //256; |
---|
26 | uint_t n_frames = 0, frames_played = 0; |
---|
27 | uint_t read = 0; |
---|
28 | char_t *sink_path = NULL; |
---|
29 | aubio_sink_t *sink = NULL; |
---|
30 | |
---|
31 | if ( argc > 1 ) samplerate = atoi(argv[1]); |
---|
32 | if ( argc > 2 ) hop_size = atoi(argv[2]); |
---|
33 | if ( argc > 3 ) sink_path = argv[3]; |
---|
34 | |
---|
35 | fvec_t *vec = new_fvec(hop_size); |
---|
36 | |
---|
37 | aubio_sampler_t * sampler = new_aubio_sampler (hop_size, samplerate); |
---|
38 | if (!vec) goto beach; |
---|
39 | if (!sampler) goto beach_sampler; |
---|
40 | // load source file |
---|
41 | //aubio_sampler_load (sampler, sample_path); |
---|
42 | // load source file (asynchronously) |
---|
43 | //aubio_sampler_queue (sampler, sample_path); |
---|
44 | samplerate = aubio_sampler_get_samplerate (sampler); |
---|
45 | if (samplerate == 0) { |
---|
46 | PRINT_ERR("starting with samplerate = 0\n"); |
---|
47 | //goto beach_sink; |
---|
48 | } |
---|
49 | |
---|
50 | //fvec_t *table = new_fvec(1234560); |
---|
51 | fvec_t *table = new_fvec(123456); |
---|
52 | |
---|
53 | aubio_sampler_set_table(sampler, table); |
---|
54 | |
---|
55 | if (sink_path) { |
---|
56 | sink = new_aubio_sink(sink_path, samplerate); |
---|
57 | if (!sink) goto beach_sink; |
---|
58 | } |
---|
59 | |
---|
60 | smpl_t sample_duration = table->length/(smpl_t) samplerate; |
---|
61 | uint_t sample_repeat = 10; |
---|
62 | smpl_t t1 = 1., |
---|
63 | t2 = t1 + sample_duration * sample_repeat - .1, |
---|
64 | t3 = t2 - sample_duration + .1, |
---|
65 | t4 = t3 + sample_duration + .1, |
---|
66 | t5 = t4 + sample_duration + .1, |
---|
67 | total_duration = t5 + sample_duration + .1; |
---|
68 | |
---|
69 | //aubio_sampler_set_transpose(sampler, 0.); |
---|
70 | //aubio_sampler_set_stretch(sampler, .8); |
---|
71 | |
---|
72 | do { |
---|
73 | if (time_was_reached(t1, n_frames, hop_size, samplerate)) { |
---|
74 | PRINT_MSG("`-test one shot play of loaded sample\n"); |
---|
75 | aubio_sampler_set_loop( sampler, 1); |
---|
76 | aubio_sampler_play ( sampler ); |
---|
77 | } else if (time_was_reached(t2, n_frames, hop_size, samplerate)) { |
---|
78 | PRINT_MSG("`-test queueing while playing after eof was reached\n"); |
---|
79 | //aubio_sampler_queue (sampler, sample_path); |
---|
80 | //aubio_sampler_play (sampler); |
---|
81 | aubio_sampler_set_loop( sampler, 0); |
---|
82 | } |
---|
83 | aubio_sampler_do (sampler, vec, &read); |
---|
84 | if (sink) aubio_sink_do(sink, vec, hop_size); |
---|
85 | n_frames += hop_size; |
---|
86 | frames_played += read; |
---|
87 | } while ( n_frames <= total_duration * samplerate ); |
---|
88 | PRINT_MSG("reached %.2f sec at %d samples, sampler played %d frames\n", |
---|
89 | total_duration, n_frames, frames_played); |
---|
90 | |
---|
91 | if (sink) del_aubio_sink(sink); |
---|
92 | beach_sink: |
---|
93 | del_aubio_sampler(sampler); |
---|
94 | beach_sampler: |
---|
95 | del_fvec(vec); |
---|
96 | beach: |
---|
97 | aubio_cleanup(); |
---|
98 | return 0; |
---|
99 | } |
---|