[a682dbe] | 1 | /* |
---|
| 2 | Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org> |
---|
| 3 | |
---|
| 4 | This file is part of aubio. |
---|
| 5 | |
---|
| 6 | aubio is free software: you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | (at your option) any later version. |
---|
| 10 | |
---|
| 11 | aubio is distributed in the hope that it will be useful, |
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | GNU General Public License for more details. |
---|
| 15 | |
---|
| 16 | You should have received a copy of the GNU General Public License |
---|
| 17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
[88a774c] | 21 | #include <assert.h> |
---|
[a682dbe] | 22 | |
---|
| 23 | #include "aubio_priv.h" |
---|
| 24 | #include "fvec.h" |
---|
| 25 | #include "fmat.h" |
---|
| 26 | #include "io/source.h" |
---|
[88a774c] | 27 | #include "utils/ringbuffer.h" |
---|
[a682dbe] | 28 | #include "synth/sampler.h" |
---|
| 29 | |
---|
[88042ef] | 30 | #define HAVE_THREADS 1 |
---|
[88a774c] | 31 | #define READER_THREAD_ON 1 |
---|
[88042ef] | 32 | #if 0 |
---|
| 33 | #undef HAVE_THREADS |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | #ifdef HAVE_THREADS |
---|
| 37 | #include <pthread.h> |
---|
| 38 | #endif |
---|
| 39 | |
---|
[88a774c] | 40 | typedef enum { |
---|
| 41 | aubio_sampler_reading_from_source, |
---|
| 42 | aubio_sampler_reading_from_table, |
---|
| 43 | aubio_sampler_n_reading_methods |
---|
| 44 | } aubio_sampler_reading_method; |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | typedef enum { |
---|
| 48 | aubio_sampler_interp_pitchtime, |
---|
| 49 | aubio_sampler_interp_quad, |
---|
| 50 | aubio_sampler_interp_lin, |
---|
| 51 | aubio_sampler_n_interp_methods |
---|
| 52 | } aubio_sampler_interp_method; |
---|
| 53 | |
---|
[a682dbe] | 54 | struct _aubio_sampler_t { |
---|
| 55 | uint_t samplerate; |
---|
| 56 | uint_t blocksize; |
---|
[88a774c] | 57 | // current reading mode (can be a file or an array) |
---|
| 58 | uint_t reading_from; |
---|
| 59 | // current interpolation mode (can be quadratic, timestretch, ...) |
---|
| 60 | uint_t interp; |
---|
| 61 | aubio_ringbuffer_t *ring; |
---|
| 62 | uint_t perfectloop; |
---|
[f253b03] | 63 | uint_t eof_remaining; |
---|
[2ca4b59] | 64 | // reading from a table |
---|
| 65 | fvec_t *table; |
---|
| 66 | uint_t table_index; |
---|
[88a774c] | 67 | // reading from a source |
---|
[a682dbe] | 68 | aubio_source_t *source; |
---|
[88042ef] | 69 | const char_t *uri; |
---|
[a682dbe] | 70 | uint_t playing; |
---|
[88042ef] | 71 | uint_t opened; |
---|
| 72 | uint_t loop; |
---|
[0a756ea] | 73 | uint_t finished; // end of file was reached |
---|
| 74 | uint_t eof; // end of file is now |
---|
[88042ef] | 75 | #ifdef HAVE_THREADS |
---|
[b959a8f] | 76 | // file reading thread |
---|
| 77 | pthread_t read_thread; |
---|
| 78 | uint_t threaded_read; // use reading thread? |
---|
[0a756ea] | 79 | pthread_mutex_t read_mutex; |
---|
| 80 | pthread_cond_t read_avail; |
---|
| 81 | pthread_cond_t read_request; |
---|
[88a774c] | 82 | uint_t source_blocksize; |
---|
| 83 | fvec_t *source_output; |
---|
| 84 | fmat_t *source_moutput; |
---|
| 85 | uint_t channels; |
---|
| 86 | // file opening thread |
---|
| 87 | pthread_t open_thread; |
---|
[88042ef] | 88 | pthread_mutex_t open_mutex; |
---|
| 89 | uint_t waited; // number of frames skipped while opening |
---|
| 90 | const char_t *next_uri; |
---|
| 91 | uint_t open_thread_running; |
---|
| 92 | sint_t available; // number of samples currently available |
---|
| 93 | uint_t started; // source warmed up |
---|
[0a756ea] | 94 | uint_t read_thread_finish; // flag to tell reading thread to exit |
---|
[88042ef] | 95 | #endif |
---|
[a682dbe] | 96 | }; |
---|
| 97 | |
---|
[88a774c] | 98 | static sint_t aubio_sampler_pull_from_source(aubio_sampler_t *s); |
---|
| 99 | |
---|
| 100 | static void aubio_sampler_do_eof(aubio_sampler_t *s); |
---|
| 101 | |
---|
| 102 | static void aubio_sampler_read(aubio_sampler_t *s, fvec_t *output, uint_t *read); |
---|
| 103 | static void aubio_sampler_read_from_source(aubio_sampler_t *s, fvec_t *output, uint_t *read); |
---|
| 104 | static void aubio_sampler_read_from_table(aubio_sampler_t *s, fvec_t *output, uint_t *read); |
---|
| 105 | |
---|
[0a756ea] | 106 | #ifdef HAVE_THREADS |
---|
[b959a8f] | 107 | static void *aubio_sampler_openfn(void *p); |
---|
[0a756ea] | 108 | static void *aubio_sampler_readfn(void *p); |
---|
[b959a8f] | 109 | static void aubio_sampler_open_opening_thread(aubio_sampler_t *o); |
---|
| 110 | static void aubio_sampler_open_reading_thread(aubio_sampler_t *o); |
---|
| 111 | static void aubio_sampler_close_opening_thread(aubio_sampler_t *o); |
---|
| 112 | static void aubio_sampler_close_reading_thread(aubio_sampler_t *o); |
---|
[0a756ea] | 113 | #endif |
---|
| 114 | |
---|
[88042ef] | 115 | aubio_sampler_t *new_aubio_sampler(uint_t blocksize, uint_t samplerate) |
---|
[a682dbe] | 116 | { |
---|
| 117 | aubio_sampler_t *s = AUBIO_NEW(aubio_sampler_t); |
---|
[79b54ea] | 118 | if ((sint_t)blocksize < 1) { |
---|
| 119 | AUBIO_ERR("sampler: got blocksize %d, but can not be < 1\n", blocksize); |
---|
| 120 | goto beach; |
---|
| 121 | } |
---|
[a682dbe] | 122 | s->samplerate = samplerate; |
---|
| 123 | s->blocksize = blocksize; |
---|
| 124 | s->source = NULL; |
---|
| 125 | s->playing = 0; |
---|
[88042ef] | 126 | s->loop = 0; |
---|
| 127 | s->uri = NULL; |
---|
| 128 | s->finished = 1; |
---|
| 129 | s->eof = 0; |
---|
| 130 | s->opened = 0; |
---|
[b959a8f] | 131 | s->available = 0; |
---|
[88042ef] | 132 | |
---|
[fefbbd8] | 133 | s->threaded_read = 0; |
---|
| 134 | s->perfectloop = 0; |
---|
[88a774c] | 135 | #if 0 // naive mode |
---|
| 136 | s->source_blocksize = s->blocksize; |
---|
[fefbbd8] | 137 | #elif 0 // threaded mode, no ringbuffer |
---|
[88a774c] | 138 | s->source_blocksize = s->blocksize; |
---|
[fefbbd8] | 139 | s->threaded_read = 1; |
---|
| 140 | #elif 0 // unthreaded, with ringbuffer |
---|
| 141 | s->source_blocksize = 2048; //32 * s->blocksize; |
---|
| 142 | s->perfectloop = 1; |
---|
| 143 | #elif 1 // threaded with ringhbuffer |
---|
[88a774c] | 144 | s->source_blocksize = 2048; //32 * s->blocksize; |
---|
| 145 | s->perfectloop = 1; |
---|
[fefbbd8] | 146 | s->threaded_read = 1; |
---|
[88a774c] | 147 | #endif |
---|
[a006c5f] | 148 | |
---|
| 149 | if (s->source_blocksize < s->blocksize) { |
---|
| 150 | s->source_blocksize = s->blocksize; |
---|
| 151 | } |
---|
[fefbbd8] | 152 | // FIXME: perfectloop fails if source_blocksize > 2048 with source_avcodec |
---|
| 153 | //s->source_blocksize = 8192; |
---|
[88a774c] | 154 | |
---|
[f253b03] | 155 | if (s->perfectloop || s->source_blocksize != s->blocksize) { |
---|
| 156 | s->ring = new_aubio_ringbuffer(s->source_blocksize * 2, s->blocksize); |
---|
[88a774c] | 157 | } |
---|
[a69e7aa] | 158 | if (s->threaded_read || s->perfectloop || s->ring) |
---|
| 159 | s->source_output = new_fvec(s->source_blocksize); |
---|
[fefbbd8] | 160 | //s->channels = 1; |
---|
| 161 | //s->source_moutput = new_fmat(s->source_blocksize, s->channels); |
---|
[88a774c] | 162 | |
---|
[88042ef] | 163 | #ifdef HAVE_THREADS |
---|
[b959a8f] | 164 | aubio_sampler_open_opening_thread(s); |
---|
[88a774c] | 165 | |
---|
[b959a8f] | 166 | if (s->threaded_read) { |
---|
[fefbbd8] | 167 | //AUBIO_WRN("sampler: starting reading thread\n"); |
---|
[b959a8f] | 168 | aubio_sampler_open_reading_thread(s); |
---|
| 169 | } |
---|
| 170 | #endif |
---|
| 171 | |
---|
[2ca4b59] | 172 | #if 0 |
---|
| 173 | s->reading_from = aubio_sampler_reading_from_table; |
---|
| 174 | s->perfectloop = 1; |
---|
| 175 | s->threaded_read = 0; |
---|
| 176 | s->opened = 1; |
---|
| 177 | s->finished = 1; |
---|
| 178 | s->table_index = 0; |
---|
| 179 | #endif |
---|
| 180 | |
---|
[b959a8f] | 181 | return s; |
---|
| 182 | beach: |
---|
| 183 | AUBIO_FREE(s); |
---|
| 184 | return NULL; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | #ifdef HAVE_THREADS |
---|
| 188 | void aubio_sampler_open_opening_thread(aubio_sampler_t *s) { |
---|
[88042ef] | 189 | pthread_mutex_init(&s->open_mutex, 0); |
---|
| 190 | s->waited = 0; |
---|
| 191 | s->open_thread = 0; |
---|
| 192 | s->open_thread_running = 0; |
---|
[b959a8f] | 193 | } |
---|
[0a756ea] | 194 | |
---|
[b959a8f] | 195 | void aubio_sampler_open_reading_thread(aubio_sampler_t *s) { |
---|
[0a756ea] | 196 | s->read_thread_finish = 0; |
---|
| 197 | pthread_mutex_init(&s->read_mutex, 0); |
---|
| 198 | pthread_cond_init (&s->read_avail, 0); |
---|
| 199 | pthread_cond_init (&s->read_request, 0); |
---|
| 200 | pthread_create(&s->read_thread, 0, aubio_sampler_readfn, s); |
---|
[a682dbe] | 201 | } |
---|
| 202 | |
---|
[b959a8f] | 203 | void aubio_sampler_close_opening_thread(aubio_sampler_t *o) { |
---|
| 204 | // clean up opening thread |
---|
| 205 | void *threadret; |
---|
[2ca4b59] | 206 | if (!o->open_thread) return; |
---|
[b959a8f] | 207 | pthread_mutex_destroy(&o->open_mutex); |
---|
| 208 | if (o->open_thread_running) { |
---|
| 209 | if (pthread_cancel(o->open_thread)) { |
---|
| 210 | AUBIO_WRN("sampler: cancelling file opening thread failed\n"); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | if (o->open_thread && pthread_join(o->open_thread, &threadret)) { |
---|
| 214 | AUBIO_WRN("sampler: joining file opening thread failed\n"); |
---|
| 215 | } |
---|
| 216 | pthread_mutex_destroy(&o->open_mutex); |
---|
[2ca4b59] | 217 | o->open_thread = 0; |
---|
[b959a8f] | 218 | } |
---|
| 219 | |
---|
| 220 | void aubio_sampler_close_reading_thread(aubio_sampler_t *o) { |
---|
| 221 | // clean up reading thread |
---|
| 222 | void *threadret; |
---|
| 223 | if (!o->read_thread) return; |
---|
| 224 | o->read_thread_finish = 1; |
---|
| 225 | pthread_cond_signal(&o->read_request); |
---|
| 226 | if (pthread_cancel(o->read_thread)) { |
---|
| 227 | AUBIO_WRN("sampler: cancelling file reading thread failed\n"); |
---|
| 228 | } |
---|
| 229 | if (pthread_join(o->read_thread, &threadret)) { |
---|
| 230 | AUBIO_WRN("sampler: joining file reading thread failed\n"); |
---|
| 231 | } |
---|
| 232 | pthread_mutex_destroy(&o->read_mutex); |
---|
| 233 | pthread_cond_destroy(&o->read_avail); |
---|
| 234 | pthread_cond_destroy(&o->read_request); |
---|
[2ca4b59] | 235 | o->read_thread = 0; |
---|
[b959a8f] | 236 | } |
---|
| 237 | #endif |
---|
| 238 | |
---|
[eaee767] | 239 | uint_t aubio_sampler_load( aubio_sampler_t * o, const char_t * uri ) |
---|
[a682dbe] | 240 | { |
---|
[88042ef] | 241 | uint_t ret = AUBIO_FAIL; |
---|
| 242 | aubio_source_t *oldsource = o->source, *newsource = NULL; |
---|
[88a774c] | 243 | newsource = new_aubio_source(uri, o->samplerate, o->source_blocksize); |
---|
[88042ef] | 244 | if (newsource) { |
---|
[a006c5f] | 245 | uint_t duration = aubio_source_get_duration(newsource); |
---|
| 246 | if (duration < o->blocksize) { |
---|
| 247 | AUBIO_WRN("sampler: %s is %d frames long, but blocksize is %d\n", |
---|
| 248 | uri, duration, o->blocksize); |
---|
| 249 | } |
---|
[88042ef] | 250 | o->source = newsource; |
---|
| 251 | if (oldsource) del_aubio_source(oldsource); |
---|
| 252 | if (o->samplerate == 0) { |
---|
| 253 | o->samplerate = aubio_source_get_samplerate(o->source); |
---|
| 254 | } |
---|
| 255 | o->uri = uri; |
---|
| 256 | o->finished = 0; |
---|
| 257 | o->eof = 0; |
---|
[f253b03] | 258 | o->eof_remaining = 0; |
---|
[88042ef] | 259 | o->opened = 1; |
---|
| 260 | ret = AUBIO_OK; |
---|
[584bfd0] | 261 | AUBIO_WRN("sampler: loaded %s\n", uri); |
---|
[88a774c] | 262 | if (o->waited) { |
---|
[584bfd0] | 263 | AUBIO_WRN("sampler: %.2fms (%d samples) taken to load %s\n", 1000. * |
---|
| 264 | o->waited / (smpl_t)o->samplerate, o->waited, o->uri); |
---|
[88a774c] | 265 | } |
---|
[88042ef] | 266 | } else { |
---|
| 267 | o->source = NULL; |
---|
| 268 | if (oldsource) del_aubio_source(oldsource); |
---|
| 269 | o->playing = 0; |
---|
| 270 | o->uri = NULL; |
---|
| 271 | o->finished = 1; |
---|
| 272 | o->eof = 0; |
---|
[f253b03] | 273 | o->eof_remaining = 0; |
---|
[88042ef] | 274 | o->opened = 0; |
---|
| 275 | AUBIO_WRN("sampler: failed loading %s\n", uri); |
---|
| 276 | } |
---|
[88a774c] | 277 | if (o->ring) { |
---|
[f253b03] | 278 | //AUBIO_WRN("sampler: resetting ringbuffer\n"); |
---|
[88a774c] | 279 | aubio_ringbuffer_reset(o->ring); |
---|
| 280 | } |
---|
[88042ef] | 281 | return ret; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | #ifdef HAVE_THREADS |
---|
| 285 | static void *aubio_sampler_openfn(void *z) { |
---|
| 286 | aubio_sampler_t *p = z; |
---|
| 287 | uint_t err; |
---|
| 288 | int oldtype; |
---|
| 289 | void *ret; |
---|
| 290 | pthread_setcancelstate(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype); |
---|
| 291 | pthread_mutex_lock(&p->open_mutex); |
---|
| 292 | p->open_thread_running = 1; |
---|
| 293 | err = aubio_sampler_load(p, p->next_uri); |
---|
| 294 | p->open_thread_running = 0; |
---|
| 295 | pthread_mutex_unlock(&p->open_mutex); |
---|
| 296 | ret = &err; |
---|
| 297 | pthread_exit(ret); |
---|
| 298 | } |
---|
| 299 | #endif |
---|
[eaee767] | 300 | |
---|
[88042ef] | 301 | uint_t |
---|
| 302 | aubio_sampler_queue(aubio_sampler_t *o, const char_t *uri) |
---|
| 303 | { |
---|
| 304 | #ifdef HAVE_THREADS |
---|
| 305 | uint_t ret = AUBIO_OK; |
---|
[2ca4b59] | 306 | |
---|
| 307 | if (o->reading_from == aubio_sampler_reading_from_table) { |
---|
| 308 | o->reading_from = aubio_sampler_reading_from_source; |
---|
| 309 | o->opened = 0; |
---|
| 310 | o->finished = 1; |
---|
| 311 | } |
---|
[88042ef] | 312 | /* open uri in open_thread */ |
---|
| 313 | if (o->open_thread_running) { |
---|
| 314 | // cancel previous open_thread |
---|
| 315 | if (pthread_cancel(o->open_thread)) { |
---|
[88a774c] | 316 | AUBIO_WRN("sampler: failed queuing %s (cancelling existing open thread failed)\n", uri); |
---|
[88042ef] | 317 | return AUBIO_FAIL; |
---|
| 318 | } else { |
---|
[88a774c] | 319 | AUBIO_WRN("sampler: cancelled queuing %s (queuing %s now)\n", |
---|
[88042ef] | 320 | o->next_uri, uri); |
---|
| 321 | } |
---|
| 322 | o->open_thread_running = 0; |
---|
| 323 | } |
---|
| 324 | void *threadret; |
---|
| 325 | if (o->open_thread && pthread_join(o->open_thread, &threadret)) { |
---|
| 326 | AUBIO_WRN("sampler: joining thread failed\n"); |
---|
| 327 | } |
---|
| 328 | if (pthread_mutex_trylock(&o->open_mutex)) { |
---|
[88a774c] | 329 | AUBIO_WRN("sampler: failed queuing %s (locking failed)\n", uri); |
---|
[88042ef] | 330 | ret = AUBIO_FAIL; |
---|
| 331 | goto lock_failed; |
---|
| 332 | } |
---|
| 333 | o->opened = 0; // while opening |
---|
| 334 | o->started = 0; |
---|
| 335 | o->available = 0; |
---|
| 336 | o->next_uri = uri; |
---|
[0a756ea] | 337 | o->waited = 0; |
---|
[88042ef] | 338 | if (pthread_create(&o->open_thread, 0, aubio_sampler_openfn, o) != 0) { |
---|
| 339 | AUBIO_ERR("sampler: failed creating opening thread\n"); |
---|
| 340 | ret = AUBIO_FAIL; |
---|
| 341 | goto thread_create_failed; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | thread_create_failed: |
---|
| 345 | pthread_mutex_unlock(&o->open_mutex); |
---|
| 346 | lock_failed: |
---|
| 347 | if (ret == AUBIO_OK) { |
---|
| 348 | //AUBIO_WRN("sampler: queued %s\n", uri); |
---|
| 349 | } else { |
---|
| 350 | AUBIO_ERR("sampler: queueing %s failed\n", uri); |
---|
| 351 | } |
---|
| 352 | return ret; |
---|
| 353 | #else |
---|
| 354 | AUBIO_WRN("sampler: opening %s, not queueing (not compiled with threading)\n", uri); |
---|
| 355 | return aubio_sampler_load(o, uri); |
---|
| 356 | #endif |
---|
| 357 | } |
---|
[eaee767] | 358 | |
---|
[0a756ea] | 359 | #ifdef HAVE_THREADS |
---|
[88a774c] | 360 | |
---|
| 361 | uint_t aubio_sampler_reading_from_source_ring_fetch(aubio_sampler_t*s); |
---|
[fefbbd8] | 362 | |
---|
[0a756ea] | 363 | void *aubio_sampler_readfn(void *z) { |
---|
| 364 | aubio_sampler_t *p = z; |
---|
| 365 | while(1) { |
---|
| 366 | pthread_mutex_lock(&p->read_mutex); |
---|
[88a774c] | 367 | if (p->open_thread_running) { |
---|
| 368 | //AUBIO_WRN("sampler: readfn(): file is being opened\n"); |
---|
| 369 | pthread_cond_signal(&p->read_avail); |
---|
| 370 | //pthread_cond_wait(&p->read_request, &p->read_mutex); |
---|
| 371 | } else if (p->opened && !p->started && !p->finished) { |
---|
| 372 | //AUBIO_WRN("sampler: readfn(): file started\n"); |
---|
| 373 | if (p->ring) { |
---|
[fefbbd8] | 374 | p->available = aubio_sampler_reading_from_source_ring_fetch(p); |
---|
[88a774c] | 375 | } else { |
---|
| 376 | p->available = aubio_sampler_pull_from_source(p); |
---|
| 377 | if (p->available < (sint_t)p->source_blocksize) |
---|
| 378 | aubio_sampler_do_eof(p); |
---|
| 379 | } |
---|
| 380 | pthread_cond_signal(&p->read_avail); |
---|
| 381 | if (!p->finished) { |
---|
| 382 | pthread_cond_wait(&p->read_request, &p->read_mutex); |
---|
| 383 | } |
---|
| 384 | } else { |
---|
| 385 | //AUBIO_WRN("sampler: readfn(): idle?\n"); |
---|
[d02d8f0] | 386 | pthread_cond_signal(&p->read_avail); |
---|
[88a774c] | 387 | pthread_cond_wait(&p->read_request, &p->read_mutex); |
---|
| 388 | if (p->read_thread_finish) { |
---|
| 389 | goto done; |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 393 | } |
---|
| 394 | done: |
---|
| 395 | //AUBIO_WRN("sampler: exiting reading thread\n"); |
---|
| 396 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 397 | pthread_exit(NULL); |
---|
| 398 | } |
---|
[0a756ea] | 399 | #endif |
---|
| 400 | |
---|
[88042ef] | 401 | void |
---|
[88a774c] | 402 | aubio_sampler_read(aubio_sampler_t *s, fvec_t *output, uint_t *read) { |
---|
| 403 | if (s->reading_from == aubio_sampler_reading_from_source) { |
---|
| 404 | aubio_sampler_read_from_source(s, output, read); |
---|
| 405 | } else if (s->reading_from == aubio_sampler_reading_from_table) { |
---|
| 406 | aubio_sampler_read_from_table(s, output, read); |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | static void |
---|
| 411 | aubio_sampler_reading_from_source_naive(aubio_sampler_t *s, fvec_t * output, |
---|
| 412 | uint_t *read) |
---|
| 413 | { |
---|
| 414 | // directly read from disk |
---|
| 415 | //aubio_source_do(s->source, output, read); |
---|
| 416 | s->source_output = output; |
---|
| 417 | *read = aubio_sampler_pull_from_source(s); |
---|
| 418 | if (*read < s->source_blocksize) { |
---|
| 419 | //AUBIO_WRN("sampler: calling go_eof in _read_from_source()\n"); |
---|
| 420 | aubio_sampler_do_eof(s); |
---|
| 421 | } |
---|
[a682dbe] | 422 | } |
---|
| 423 | |
---|
[88a774c] | 424 | uint_t |
---|
| 425 | aubio_sampler_reading_from_source_ring_fetch(aubio_sampler_t*s) { |
---|
| 426 | // read source_blocksize (> blocksize) at once |
---|
| 427 | int ring_avail = aubio_ringbuffer_get_available(s->ring); |
---|
| 428 | //if (ring_avail < s->blocksize) { |
---|
| 429 | uint_t available = 0; |
---|
| 430 | if (ring_avail < (sint_t)s->blocksize) { |
---|
| 431 | available = aubio_sampler_pull_from_source(s); |
---|
| 432 | if (available > 0) { |
---|
| 433 | aubio_ringbuffer_push(s->ring, s->source_output, available); |
---|
[f253b03] | 434 | } |
---|
| 435 | if (available < s->source_blocksize) { |
---|
| 436 | if (ring_avail + available <= s->blocksize) { |
---|
| 437 | s->eof_remaining = available + ring_avail; |
---|
| 438 | if (s->eof_remaining == 0) s->eof_remaining = s->blocksize; |
---|
| 439 | //AUBIO_ERR("sampler: marking special eof got: %d, in ring: %d, %d, eof remaining %d\n", |
---|
| 440 | // available, ring_avail, s->blocksize, s->eof_remaining); |
---|
| 441 | if (s->loop) { |
---|
| 442 | aubio_sampler_seek(s,0); |
---|
| 443 | // read some frames from beginning of source for perfect looping |
---|
| 444 | if (s->perfectloop) { |
---|
| 445 | available = aubio_sampler_pull_from_source(s); |
---|
| 446 | if (available <= 0) { |
---|
| 447 | AUBIO_ERR("sampler: perfectloop but s->available = 0 !\n"); |
---|
| 448 | } else { |
---|
| 449 | aubio_ringbuffer_push(s->ring, s->source_output, available); |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | } |
---|
[88a774c] | 454 | } |
---|
| 455 | } |
---|
| 456 | return available; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | static void |
---|
| 460 | aubio_sampler_reading_from_source_ring_pull(aubio_sampler_t *s, fvec_t *output, |
---|
| 461 | uint_t *read) |
---|
| 462 | { |
---|
| 463 | // write into output |
---|
| 464 | int ring_avail = aubio_ringbuffer_get_available(s->ring); |
---|
| 465 | if (ring_avail >= (sint_t)s->blocksize) { |
---|
| 466 | //AUBIO_MSG("sampler: pulling %d / %d from ringbuffer\n", s->blocksize, ring_avail); |
---|
| 467 | aubio_ringbuffer_pull(s->ring, output, s->blocksize); |
---|
| 468 | *read = s->blocksize; |
---|
[f253b03] | 469 | if (s->eof_remaining > 0) { |
---|
| 470 | if (s->eof_remaining <= s->blocksize) { |
---|
[816932e] | 471 | //AUBIO_WRN("sampler: signaling eof\n"); |
---|
| 472 | s->eof = 1; // signal eof |
---|
[f253b03] | 473 | s->eof_remaining = 0; |
---|
| 474 | } else if (s->eof_remaining <= s->source_blocksize) { |
---|
| 475 | s->eof_remaining -= s->blocksize; |
---|
| 476 | } |
---|
| 477 | } |
---|
[88a774c] | 478 | } else { |
---|
[a006c5f] | 479 | //AUBIO_MSG("sampler: last frame, pulling remaining %d left\n", ring_avail); |
---|
[88a774c] | 480 | *read = 0; |
---|
| 481 | if (ring_avail > 0) { |
---|
[fefbbd8] | 482 | // pull remaining frames in ring buffer |
---|
[88a774c] | 483 | aubio_ringbuffer_pull(s->ring, output, ring_avail); |
---|
| 484 | *read += ring_avail; |
---|
| 485 | } |
---|
[f253b03] | 486 | // signal eof |
---|
[88a774c] | 487 | aubio_sampler_do_eof(s); |
---|
[f253b03] | 488 | // finished playing, reset ringbuffer for next read |
---|
| 489 | if (!s->playing) |
---|
| 490 | aubio_ringbuffer_reset(s->ring); |
---|
[88a774c] | 491 | } |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | static void |
---|
| 495 | aubio_sampler_reading_from_source_ring(aubio_sampler_t *s, fvec_t *output, |
---|
| 496 | uint_t *read) |
---|
| 497 | { |
---|
| 498 | aubio_sampler_reading_from_source_ring_fetch(s); |
---|
| 499 | aubio_sampler_reading_from_source_ring_pull(s, output, read); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | #ifdef HAVE_THREADS |
---|
| 503 | static void |
---|
| 504 | aubio_sampler_read_from_source_threaded(aubio_sampler_t *s, fvec_t *output, |
---|
| 505 | uint_t *read) { |
---|
| 506 | // request at least output->length |
---|
| 507 | // make sure we have enough samples read from source |
---|
| 508 | int available; |
---|
| 509 | pthread_mutex_lock(&s->read_mutex); |
---|
| 510 | if (!s->opened || s->open_thread_running) { |
---|
| 511 | //AUBIO_ERR("sampler: _read_from_source: not opened, signaling read_request\n"); |
---|
| 512 | pthread_cond_signal(&s->read_request); |
---|
| 513 | available = 0; |
---|
| 514 | } else if (!s->finished) { |
---|
| 515 | pthread_cond_signal(&s->read_request); |
---|
| 516 | pthread_cond_wait(&s->read_avail, &s->read_mutex); |
---|
| 517 | //AUBIO_ERR("sampler: _read_from_source: %d\n", s->available); |
---|
| 518 | available = s->available; |
---|
| 519 | } else { |
---|
| 520 | //AUBIO_WRN("sampler: _read_from_source: eof\n"); |
---|
| 521 | pthread_cond_signal(&s->read_request); |
---|
| 522 | available = 0; |
---|
| 523 | } |
---|
| 524 | pthread_mutex_unlock(&s->read_mutex); |
---|
| 525 | //AUBIO_WRN("sampler: got %d available in _read_from_source\n", available); |
---|
| 526 | // read -> number of samples read |
---|
[fefbbd8] | 527 | if (!s->perfectloop && s->source_blocksize == s->blocksize) { |
---|
| 528 | if (available >= (sint_t)s->blocksize) { |
---|
| 529 | fvec_copy(s->source_output, output); |
---|
| 530 | *read = s->blocksize; |
---|
| 531 | } else if (available > 0) { |
---|
| 532 | fvec_copy(s->source_output, output); |
---|
| 533 | *read = available; |
---|
| 534 | } else { |
---|
| 535 | fvec_zeros(output); |
---|
| 536 | *read = 0; |
---|
| 537 | } |
---|
[88a774c] | 538 | } else { |
---|
[fefbbd8] | 539 | aubio_sampler_reading_from_source_ring_pull(s, output, read); |
---|
[88a774c] | 540 | } |
---|
| 541 | } |
---|
| 542 | #endif |
---|
| 543 | |
---|
[88042ef] | 544 | void |
---|
[88a774c] | 545 | aubio_sampler_read_from_source(aubio_sampler_t *s, fvec_t *output, uint_t *read) { |
---|
| 546 | #ifdef HAVE_THREADS |
---|
| 547 | if (s->threaded_read) { // if threaded |
---|
[fefbbd8] | 548 | aubio_sampler_read_from_source_threaded(s, output, read); |
---|
[88a774c] | 549 | } else |
---|
| 550 | #endif |
---|
| 551 | { |
---|
| 552 | if (s->finished) { |
---|
| 553 | *read = 0; |
---|
| 554 | } |
---|
| 555 | else if (s->source_blocksize == s->blocksize && !s->perfectloop) { |
---|
| 556 | aubio_sampler_reading_from_source_naive(s, output, read); |
---|
| 557 | } else { |
---|
| 558 | aubio_sampler_reading_from_source_ring(s, output, read); |
---|
| 559 | } |
---|
| 560 | #if 1 |
---|
| 561 | if (s->loop && s->perfectloop && *read != s->blocksize) { // && s->started && !s->finished) { |
---|
| 562 | AUBIO_ERR("sampler: perfectloop but read only %d\n", *read); |
---|
| 563 | } |
---|
| 564 | #endif |
---|
| 565 | } |
---|
[88042ef] | 566 | } |
---|
| 567 | |
---|
| 568 | void |
---|
[2ca4b59] | 569 | aubio_sampler_read_from_table(aubio_sampler_t *s, fvec_t *output, uint_t *read) { |
---|
[88a774c] | 570 | *read = 0; |
---|
[2ca4b59] | 571 | if (s->table == NULL) { |
---|
| 572 | AUBIO_WRN("sampler: _pull_from_table but table not set %d, %d\n", |
---|
| 573 | output->length, *read); |
---|
| 574 | } else if (s->playing) { |
---|
| 575 | uint_t available = s->table->length - s->table_index; |
---|
| 576 | fvec_t tmp; |
---|
| 577 | tmp.data = s->table->data + s->table_index; |
---|
| 578 | if (available < s->blocksize) { |
---|
| 579 | //AUBIO_WRN("sampler: _pull_from_table: table length %d, index: %d, read %d\n", |
---|
| 580 | // s->table->length, s->table_index, *read); |
---|
| 581 | tmp.length = available; |
---|
| 582 | fvec_t tmpout; tmpout.data = output->data; tmpout.length = available; |
---|
| 583 | fvec_copy(&tmp, &tmpout); |
---|
| 584 | if (s->loop && s->perfectloop) { |
---|
| 585 | uint_t remaining = s->blocksize - available; |
---|
| 586 | tmpout.data = output->data + available; tmpout.length = remaining; |
---|
| 587 | tmp.data = s->table->data; tmp.length = remaining; |
---|
| 588 | fvec_copy(&tmp, &tmpout); |
---|
| 589 | s->table_index = remaining; |
---|
| 590 | *read = s->blocksize; |
---|
| 591 | } else { |
---|
| 592 | s->table_index = 0; |
---|
| 593 | *read = available; |
---|
| 594 | } |
---|
| 595 | aubio_sampler_do_eof(s); |
---|
| 596 | } else { |
---|
| 597 | tmp.length = s->blocksize; |
---|
| 598 | fvec_copy(&tmp, output); |
---|
| 599 | s->table_index += output->length; |
---|
| 600 | *read = s->blocksize; |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | uint_t |
---|
| 606 | aubio_sampler_set_table(aubio_sampler_t *s, fvec_t *samples) { |
---|
| 607 | if (!samples || !s) return AUBIO_FAIL; |
---|
| 608 | if (s->reading_from == aubio_sampler_reading_from_source) { |
---|
| 609 | //aubio_sampler_close_reading_thread(s); |
---|
| 610 | } |
---|
| 611 | s->table = samples; |
---|
| 612 | //AUBIO_INF("sampler: setting table (%d long)\n", s->table->length); |
---|
| 613 | s->table_index = 0; |
---|
| 614 | s->reading_from = aubio_sampler_reading_from_table; |
---|
| 615 | //s->threaded_read = 0; |
---|
| 616 | s->opened = 1; |
---|
| 617 | s->finished = 1; |
---|
| 618 | return AUBIO_OK; |
---|
[88042ef] | 619 | } |
---|
[88a774c] | 620 | |
---|
| 621 | sint_t |
---|
| 622 | aubio_sampler_pull_from_source(aubio_sampler_t *s) |
---|
| 623 | { |
---|
| 624 | // pull source_blocksize samples from source, return available frames |
---|
| 625 | uint_t source_read = s->source_blocksize; |
---|
| 626 | if (s->source == NULL) { |
---|
| 627 | AUBIO_ERR("sampler: trying to fetch on NULL source\n"); |
---|
| 628 | return -1; |
---|
| 629 | } |
---|
| 630 | aubio_source_do(s->source, s->source_output, &source_read); |
---|
| 631 | return source_read; |
---|
| 632 | } |
---|
| 633 | |
---|
[88042ef] | 634 | |
---|
| 635 | uint_t |
---|
| 636 | aubio_sampler_get_samplerate (aubio_sampler_t *o) |
---|
| 637 | { |
---|
| 638 | return o->samplerate; |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | uint_t |
---|
| 642 | aubio_sampler_get_opened (aubio_sampler_t *o) |
---|
| 643 | { |
---|
| 644 | return o->opened; //== 1 ? AUBIO_OK : AUBIO_FAIL; |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | uint_t |
---|
| 648 | aubio_sampler_get_finished(aubio_sampler_t *o) |
---|
| 649 | { |
---|
| 650 | return o->finished; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | uint_t |
---|
| 654 | aubio_sampler_get_eof (aubio_sampler_t *o) |
---|
[a682dbe] | 655 | { |
---|
[88042ef] | 656 | return o->eof; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | uint_t |
---|
| 660 | aubio_sampler_get_waited_opening (aubio_sampler_t *o, uint_t waited) { |
---|
| 661 | #ifdef HAVE_THREADS |
---|
[a682dbe] | 662 | if (o->playing) { |
---|
[88042ef] | 663 | if (!o->opened) { |
---|
| 664 | o->waited += waited; |
---|
| 665 | } else if (o->waited) { |
---|
| 666 | //AUBIO_WRN("sampler: waited %d frames (%.2fms) while opening %s\n", |
---|
| 667 | // o->waited, 1000.*o->waited/(smpl_t)o->samplerate, o->uri); |
---|
| 668 | uint_t waited = o->waited; |
---|
| 669 | o->waited = 0; |
---|
| 670 | return waited; |
---|
[a682dbe] | 671 | } |
---|
| 672 | } |
---|
[88042ef] | 673 | #endif |
---|
| 674 | return 0; |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | uint_t |
---|
| 678 | aubio_sampler_seek(aubio_sampler_t * o, uint_t pos) |
---|
| 679 | { |
---|
[88a774c] | 680 | //AUBIO_WRN("sampler: seeking to 0\n"); |
---|
[88042ef] | 681 | uint_t ret = AUBIO_FAIL; |
---|
| 682 | o->finished = 0; |
---|
| 683 | if (!o->opened) return AUBIO_OK; |
---|
| 684 | if (o->source) { |
---|
| 685 | ret = aubio_source_seek(o->source, pos); |
---|
[886a7d8] | 686 | } else if (o->table && (sint_t)pos >= 0 && pos < o->table->length) { |
---|
| 687 | o->table_index = pos < o->table->length ? pos : o->table->length - 1; |
---|
| 688 | ret = AUBIO_OK; |
---|
[88042ef] | 689 | } |
---|
| 690 | return ret; |
---|
[a682dbe] | 691 | } |
---|
| 692 | |
---|
[88042ef] | 693 | void |
---|
| 694 | aubio_sampler_do_eof (aubio_sampler_t * o) |
---|
[a682dbe] | 695 | { |
---|
[88a774c] | 696 | //AUBIO_MSG("sampler: calling _do_eof()\n"); |
---|
[88042ef] | 697 | o->finished = 1; |
---|
| 698 | o->eof = 1; |
---|
| 699 | if (!o->loop) { |
---|
| 700 | o->playing = 0; |
---|
| 701 | } else { |
---|
[2ca4b59] | 702 | if (o->reading_from == aubio_sampler_reading_from_source) |
---|
| 703 | aubio_sampler_seek(o, 0); |
---|
[88a774c] | 704 | //o->finished = 0; |
---|
[88042ef] | 705 | } |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | void aubio_sampler_do ( aubio_sampler_t * o, fvec_t * output, uint_t *read) |
---|
| 709 | { |
---|
| 710 | o->eof = 0; |
---|
| 711 | if (o->opened == 1 && o->playing) { |
---|
[88a774c] | 712 | aubio_sampler_read(o, output, read); |
---|
[88042ef] | 713 | } else { |
---|
| 714 | fvec_zeros(output); |
---|
[88a774c] | 715 | *read = 0; |
---|
[a682dbe] | 716 | } |
---|
[88042ef] | 717 | } |
---|
| 718 | |
---|
| 719 | void aubio_sampler_do_multi ( aubio_sampler_t * o, fmat_t * output, uint_t *read) |
---|
| 720 | { |
---|
| 721 | o->eof = 0; |
---|
[88a774c] | 722 | if (o->opened == 1 && o->playing) { |
---|
| 723 | //aubio_sampler_read_multi(o, output, read); |
---|
[88042ef] | 724 | } else { |
---|
| 725 | fmat_zeros(output); |
---|
| 726 | *read = 0; |
---|
[a682dbe] | 727 | } |
---|
| 728 | } |
---|
| 729 | |
---|
[eaee767] | 730 | uint_t aubio_sampler_get_playing ( const aubio_sampler_t * o ) |
---|
[a682dbe] | 731 | { |
---|
| 732 | return o->playing; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing ) |
---|
| 736 | { |
---|
| 737 | o->playing = (playing == 1) ? 1 : 0; |
---|
| 738 | return 0; |
---|
| 739 | } |
---|
| 740 | |
---|
[88042ef] | 741 | uint_t aubio_sampler_get_loop ( aubio_sampler_t * o ) |
---|
| 742 | { |
---|
| 743 | return o->loop; |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | uint_t aubio_sampler_set_loop ( aubio_sampler_t * o, uint_t loop ) |
---|
| 747 | { |
---|
| 748 | o->loop = (loop == 1) ? 1 : 0; |
---|
| 749 | return 0; |
---|
| 750 | } |
---|
| 751 | |
---|
[a682dbe] | 752 | uint_t aubio_sampler_play ( aubio_sampler_t * o ) |
---|
| 753 | { |
---|
| 754 | return aubio_sampler_set_playing (o, 1); |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | uint_t aubio_sampler_stop ( aubio_sampler_t * o ) |
---|
| 758 | { |
---|
| 759 | return aubio_sampler_set_playing (o, 0); |
---|
| 760 | } |
---|
| 761 | |
---|
[88042ef] | 762 | uint_t aubio_sampler_loop ( aubio_sampler_t * o ) |
---|
| 763 | { |
---|
| 764 | aubio_sampler_set_loop(o, 1); |
---|
| 765 | aubio_sampler_seek(o, 0); |
---|
| 766 | return aubio_sampler_set_playing (o, 1); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | uint_t aubio_sampler_trigger ( aubio_sampler_t * o ) |
---|
| 770 | { |
---|
[0840440] | 771 | if (o->ring) aubio_ringbuffer_reset(o->ring); |
---|
[88042ef] | 772 | aubio_sampler_set_loop(o, 0); |
---|
| 773 | aubio_sampler_seek(o, 0); |
---|
| 774 | return aubio_sampler_set_playing (o, 1); |
---|
| 775 | } |
---|
| 776 | |
---|
[943ef49] | 777 | uint_t aubio_sampler_set_perfectloop (aubio_sampler_t *s, uint_t perfectloop) { |
---|
| 778 | if (!s) return AUBIO_FAIL; |
---|
| 779 | s->perfectloop = perfectloop; |
---|
| 780 | return AUBIO_OK; |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | uint_t aubio_sampler_get_perfectloop (aubio_sampler_t *s) { |
---|
| 784 | if (!s) return AUBIO_FAIL; |
---|
| 785 | return s->perfectloop; |
---|
| 786 | } |
---|
| 787 | |
---|
[a682dbe] | 788 | void del_aubio_sampler( aubio_sampler_t * o ) |
---|
| 789 | { |
---|
[88042ef] | 790 | #ifdef HAVE_THREADS |
---|
[b959a8f] | 791 | // close opening thread |
---|
| 792 | aubio_sampler_close_opening_thread(o); |
---|
[0a756ea] | 793 | // close reading thread |
---|
[b959a8f] | 794 | aubio_sampler_close_reading_thread(o); |
---|
[88042ef] | 795 | #endif |
---|
[88a774c] | 796 | //if (o->source_output) { |
---|
[f253b03] | 797 | if (o->source_output && (o->threaded_read || o->perfectloop)) { |
---|
[88a774c] | 798 | del_fvec(o->source_output); |
---|
| 799 | } |
---|
| 800 | if (o->source_moutput) { |
---|
| 801 | del_fmat(o->source_moutput); |
---|
| 802 | } |
---|
| 803 | if (o->ring) { |
---|
| 804 | del_aubio_ringbuffer(o->ring); |
---|
| 805 | } |
---|
[a682dbe] | 806 | if (o->source) { |
---|
| 807 | del_aubio_source(o->source); |
---|
| 808 | } |
---|
| 809 | AUBIO_FREE(o); |
---|
| 810 | } |
---|