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 < 2) { |
---|
18 | err = 2; |
---|
19 | PRINT_ERR("not enough arguments\n"); |
---|
20 | PRINT_MSG("usage: %s <sample_path> [samplerate] [blocksize] [output_path]\n", argv[0]); |
---|
21 | return err; |
---|
22 | } |
---|
23 | |
---|
24 | uint_t samplerate = 44100; // default is 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 | char_t *sample_path = argv[1]; |
---|
32 | if ( argc > 2 ) samplerate = atoi(argv[2]); |
---|
33 | if ( argc > 3 ) hop_size = atoi(argv[3]); |
---|
34 | if ( argc > 4 ) sink_path = argv[4]; |
---|
35 | |
---|
36 | fvec_t *vec = new_fvec(hop_size); |
---|
37 | |
---|
38 | aubio_sampler_t * sampler = new_aubio_sampler (hop_size, samplerate); |
---|
39 | if (!vec) goto beach; |
---|
40 | if (!sampler) goto beach_sampler; |
---|
41 | // load source file |
---|
42 | aubio_sampler_load (sampler, sample_path); |
---|
43 | // load source file (asynchronously) |
---|
44 | //aubio_sampler_queue (sampler, sample_path); |
---|
45 | samplerate = aubio_sampler_get_samplerate (sampler); |
---|
46 | if (samplerate == 0) { |
---|
47 | PRINT_ERR("starting with samplerate = 0\n"); |
---|
48 | //goto beach_sink; |
---|
49 | } |
---|
50 | |
---|
51 | if (sink_path) { |
---|
52 | sink = new_aubio_sink(sink_path, samplerate); |
---|
53 | if (!sink) goto beach_sink; |
---|
54 | } |
---|
55 | |
---|
56 | smpl_t sample_duration = 2.953; |
---|
57 | uint_t sample_repeat = 10; |
---|
58 | smpl_t t1 = 1., |
---|
59 | t2 = t1 + sample_duration * sample_repeat - .1, |
---|
60 | t3 = t2 - sample_duration + .1, |
---|
61 | t4 = t3 + sample_duration + .1, |
---|
62 | t5 = t4 + sample_duration + .1, |
---|
63 | total_duration = t5 + sample_duration + .1; |
---|
64 | |
---|
65 | //aubio_sampler_set_transpose(sampler, 0.); |
---|
66 | //aubio_sampler_set_stretch(sampler, .8); |
---|
67 | |
---|
68 | do { |
---|
69 | if (time_was_reached(t1, n_frames, hop_size, samplerate)) { |
---|
70 | PRINT_MSG("`-test one shot play of loaded sample\n"); |
---|
71 | aubio_sampler_set_loop( sampler, 1); |
---|
72 | aubio_sampler_play ( sampler ); |
---|
73 | } else if (time_was_reached(t2, n_frames, hop_size, samplerate)) { |
---|
74 | PRINT_MSG("`-test queueing while playing after eof was reached\n"); |
---|
75 | //aubio_sampler_queue (sampler, sample_path); |
---|
76 | //aubio_sampler_play (sampler); |
---|
77 | aubio_sampler_set_loop( sampler, 0); |
---|
78 | #if 0 |
---|
79 | } else if (time_was_reached(t3, n_frames, hop_size, samplerate)) { |
---|
80 | PRINT_MSG("`-test queueing twice cancels the first one\n"); |
---|
81 | aubio_sampler_queue (sampler, sample_path); |
---|
82 | aubio_sampler_queue (sampler, sample_path); |
---|
83 | aubio_sampler_play (sampler); |
---|
84 | } else if (time_was_reached(t4, n_frames, hop_size, samplerate)) { |
---|
85 | PRINT_MSG("`-test queueing a corrupt file\n"); |
---|
86 | aubio_sampler_queue (sampler, "/dev/null"); |
---|
87 | aubio_sampler_play (sampler); |
---|
88 | } else if (time_was_reached(t5, n_frames, hop_size, samplerate)) { |
---|
89 | aubio_sampler_stop ( sampler ); |
---|
90 | PRINT_MSG("`-test queueing a correct file after a corrupt one\n"); |
---|
91 | uint_t i; |
---|
92 | for (i = 0; i < 4; i++) |
---|
93 | aubio_sampler_queue (sampler, "/dev/null"); |
---|
94 | aubio_sampler_queue (sampler, "/dev/null1"); |
---|
95 | aubio_sampler_queue (sampler, "/dev/null2"); |
---|
96 | aubio_sampler_queue (sampler, sample_path); |
---|
97 | aubio_sampler_play (sampler); |
---|
98 | #endif |
---|
99 | } |
---|
100 | /* |
---|
101 | if (n_frames / hop_size == 40) { |
---|
102 | aubio_sampler_queue (sampler, sample_path); |
---|
103 | aubio_sampler_queue (sampler, sample_path); |
---|
104 | aubio_sampler_seek ( sampler, 0); |
---|
105 | } |
---|
106 | if (n_frames / hop_size == 70) { |
---|
107 | aubio_sampler_seek ( sampler, 0); |
---|
108 | } |
---|
109 | */ |
---|
110 | aubio_sampler_do (sampler, vec, &read); |
---|
111 | if (sink) aubio_sink_do(sink, vec, hop_size); |
---|
112 | n_frames += hop_size; |
---|
113 | frames_played += read; |
---|
114 | //} while ( read == hop_size ); |
---|
115 | // last for 40 seconds |
---|
116 | } while ( n_frames <= total_duration * samplerate ); |
---|
117 | PRINT_MSG("reached %.2f sec at %d samples, sampler played %d frames\n", |
---|
118 | total_duration, n_frames, frames_played); |
---|
119 | |
---|
120 | if (sink) del_aubio_sink(sink); |
---|
121 | beach_sink: |
---|
122 | del_aubio_sampler(sampler); |
---|
123 | beach_sampler: |
---|
124 | del_fvec(vec); |
---|
125 | beach: |
---|
126 | aubio_cleanup(); |
---|
127 | return 0; |
---|
128 | } |
---|