[c41637b] | 1 | /* |
---|
| 2 | Copyright (C) 2016 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 | |
---|
| 21 | #include "config.h" |
---|
| 22 | |
---|
| 23 | #ifdef HAVE_RUBBERBAND |
---|
| 24 | |
---|
| 25 | #include "aubio_priv.h" |
---|
| 26 | #include "fvec.h" |
---|
| 27 | #include "fmat.h" |
---|
| 28 | #include "io/source.h" |
---|
| 29 | #include "effects/timestretch.h" |
---|
| 30 | |
---|
| 31 | #include "rubberband/rubberband-c.h" |
---|
| 32 | |
---|
| 33 | #define MIN_STRETCH_RATIO 0.025 |
---|
[85755eb] | 34 | #define MAX_STRETCH_RATIO 40. |
---|
[c41637b] | 35 | |
---|
[8856791] | 36 | #define HAVE_THREADS 1 |
---|
| 37 | #if 0 |
---|
| 38 | #undef HAVE_THREADS |
---|
| 39 | #endif |
---|
| 40 | |
---|
| 41 | #ifdef HAVE_THREADS |
---|
| 42 | #include <pthread.h> |
---|
| 43 | #endif |
---|
| 44 | |
---|
[c41637b] | 45 | /** generic time stretching structure */ |
---|
| 46 | struct _aubio_timestretch_t |
---|
| 47 | { |
---|
| 48 | uint_t samplerate; /**< samplerate */ |
---|
| 49 | uint_t hopsize; /**< hop size */ |
---|
| 50 | smpl_t stretchratio; /**< time ratio */ |
---|
| 51 | smpl_t pitchscale; /**< pitch scale */ |
---|
| 52 | |
---|
| 53 | aubio_source_t *source; |
---|
[992ac88] | 54 | uint_t source_hopsize; /**< hop size at which the source is read */ |
---|
[c41637b] | 55 | fvec_t *in; |
---|
| 56 | uint_t eof; |
---|
| 57 | |
---|
| 58 | RubberBandState rb; |
---|
| 59 | RubberBandOptions rboptions; |
---|
[8856791] | 60 | |
---|
| 61 | #ifdef HAVE_THREADS |
---|
| 62 | pthread_t read_thread; |
---|
| 63 | pthread_mutex_t read_mutex; |
---|
| 64 | pthread_cond_t read_avail; |
---|
| 65 | pthread_cond_t read_request; |
---|
| 66 | sint_t available; |
---|
[bdb249b] | 67 | uint_t started; |
---|
| 68 | uint_t finish; |
---|
[8856791] | 69 | #endif |
---|
[c41637b] | 70 | }; |
---|
| 71 | |
---|
| 72 | extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode); |
---|
| 73 | |
---|
[a874c48] | 74 | static void aubio_timestretch_warmup (aubio_timestretch_t * p); |
---|
[992ac88] | 75 | static sint_t aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t fetch); |
---|
[8856791] | 76 | #ifdef HAVE_THREADS |
---|
| 77 | static void *aubio_timestretch_readfn(void *p); |
---|
| 78 | #endif |
---|
[a874c48] | 79 | |
---|
[c41637b] | 80 | aubio_timestretch_t * |
---|
| 81 | new_aubio_timestretch (const char_t * uri, const char_t * mode, |
---|
| 82 | smpl_t stretchratio, uint_t hopsize, uint_t samplerate) |
---|
| 83 | { |
---|
| 84 | aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t); |
---|
| 85 | p->samplerate = samplerate; |
---|
| 86 | p->hopsize = hopsize; |
---|
[8856791] | 87 | //p->source_hopsize = 2048; |
---|
[992ac88] | 88 | p->source_hopsize = hopsize; |
---|
[c41637b] | 89 | p->pitchscale = 1.; |
---|
| 90 | p->eof = 0; |
---|
| 91 | |
---|
[992ac88] | 92 | p->source = new_aubio_source(uri, samplerate, p->source_hopsize); |
---|
[c41637b] | 93 | if (!p->source) goto beach; |
---|
| 94 | if (samplerate == 0 ) p->samplerate = aubio_source_get_samplerate(p->source); |
---|
| 95 | |
---|
[992ac88] | 96 | p->in = new_fvec(p->source_hopsize); |
---|
[c41637b] | 97 | |
---|
| 98 | if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) { |
---|
| 99 | p->stretchratio = stretchratio; |
---|
| 100 | } else { |
---|
| 101 | AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n", |
---|
| 102 | MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio); |
---|
| 103 | goto beach; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | p->rboptions = aubio_get_rubberband_opts(mode); |
---|
| 107 | if (p->rboptions < 0) { |
---|
| 108 | AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode); |
---|
| 109 | goto beach; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | p->rb = rubberband_new(p->samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale); |
---|
[992ac88] | 113 | rubberband_set_max_process_size(p->rb, p->source_hopsize); |
---|
[c41637b] | 114 | //rubberband_set_debug_level(p->rb, 10); |
---|
| 115 | |
---|
[8856791] | 116 | #ifdef HAVE_THREADS |
---|
[bdb249b] | 117 | p->started = 0; |
---|
| 118 | p->finish = 0; |
---|
[8856791] | 119 | pthread_mutex_init(&p->read_mutex, 0); |
---|
| 120 | pthread_cond_init (&p->read_avail, 0); |
---|
| 121 | pthread_cond_init (&p->read_request, 0); |
---|
[bdb249b] | 122 | //AUBIO_WRN("timestretch: creating thread\n"); |
---|
[8856791] | 123 | pthread_create(&p->read_thread, 0, aubio_timestretch_readfn, p); |
---|
| 124 | //AUBIO_DBG("timestretch: new_ waiting for warmup, got %d available\n", p->available); |
---|
| 125 | pthread_mutex_lock(&p->read_mutex); |
---|
| 126 | pthread_cond_wait(&p->read_avail, &p->read_mutex); |
---|
| 127 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 128 | //AUBIO_DBG("timestretch: new_ warm up success, got %d available\n", p->available); |
---|
| 129 | #else |
---|
[a874c48] | 130 | aubio_timestretch_warmup(p); |
---|
[8856791] | 131 | #endif |
---|
[a874c48] | 132 | |
---|
| 133 | return p; |
---|
| 134 | |
---|
| 135 | beach: |
---|
| 136 | del_aubio_timestretch(p); |
---|
| 137 | return NULL; |
---|
| 138 | } |
---|
| 139 | |
---|
[8856791] | 140 | #ifdef HAVE_THREADS |
---|
| 141 | void * |
---|
| 142 | aubio_timestretch_readfn(void *z) |
---|
| 143 | { |
---|
| 144 | aubio_timestretch_t *p = z; |
---|
| 145 | while(1) { //p->available < (int)p->hopsize && p->eof != 1) { |
---|
| 146 | //AUBIO_WRN("timestretch: locking in readfn\n"); |
---|
| 147 | pthread_mutex_lock(&p->read_mutex); |
---|
[bdb249b] | 148 | if (!p->started && !p->eof) { |
---|
| 149 | // fetch the first few samples and mark as started |
---|
| 150 | //AUBIO_WRN("timestretch: fetching first samples\n"); |
---|
| 151 | aubio_timestretch_warmup(p); |
---|
| 152 | p->started = 1; |
---|
| 153 | } else if (!p->eof) { |
---|
| 154 | // fetch at least p->hopsize stretched samples |
---|
| 155 | p->available = aubio_timestretch_fetch(p, p->hopsize); |
---|
| 156 | // signal available frames |
---|
| 157 | pthread_cond_signal(&p->read_avail); |
---|
| 158 | if (p->eof != 1) { |
---|
| 159 | // the end of file was not reached yet, wait for the next read_request |
---|
| 160 | pthread_cond_wait(&p->read_request, &p->read_mutex); |
---|
| 161 | } else { |
---|
| 162 | // eof was reached, do not wait for a read request and mark as stopped |
---|
| 163 | p->started = 0; |
---|
| 164 | } |
---|
| 165 | } else { |
---|
| 166 | //pthread_cond_signal(&p->read_avail); |
---|
[8856791] | 167 | pthread_cond_wait(&p->read_request, &p->read_mutex); |
---|
[bdb249b] | 168 | //AUBIO_WRN("timestretch: finished idle in readfn\n"); |
---|
| 169 | if (p->finish) pthread_exit(NULL); |
---|
[8856791] | 170 | } |
---|
| 171 | //AUBIO_WRN("timestretch: unlocking in readfn\n"); |
---|
| 172 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 173 | } |
---|
| 174 | //AUBIO_WRN("timestretch: exiting readfn\n"); |
---|
| 175 | pthread_exit(NULL); |
---|
| 176 | } |
---|
| 177 | #endif |
---|
| 178 | |
---|
[a874c48] | 179 | static void |
---|
| 180 | aubio_timestretch_warmup (aubio_timestretch_t * p) |
---|
| 181 | { |
---|
[c41637b] | 182 | // warm up rubber band |
---|
| 183 | unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb)); |
---|
[8856791] | 184 | #ifdef HAVE_THREADS |
---|
| 185 | p->available = aubio_timestretch_fetch(p, latency); |
---|
| 186 | #else |
---|
[992ac88] | 187 | aubio_timestretch_fetch(p, latency); |
---|
[8856791] | 188 | #endif |
---|
[c41637b] | 189 | } |
---|
| 190 | |
---|
| 191 | void |
---|
| 192 | del_aubio_timestretch (aubio_timestretch_t * p) |
---|
| 193 | { |
---|
[8856791] | 194 | #ifdef HAVE_THREADS |
---|
[bdb249b] | 195 | void *threadfn; |
---|
| 196 | //AUBIO_WRN("timestretch: entering delete\n"); |
---|
[8856791] | 197 | pthread_mutex_lock(&p->read_mutex); |
---|
[bdb249b] | 198 | p->finish = 1; |
---|
[8856791] | 199 | pthread_cond_signal(&p->read_request); |
---|
| 200 | //pthread_cond_wait(&p->read_avail, &p->read_mutex); |
---|
| 201 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 202 | if ((p->eof == 0) && (pthread_cancel(p->read_thread))) { |
---|
| 203 | AUBIO_WRN("timestretch: cancelling thread failed\n"); |
---|
| 204 | } |
---|
| 205 | if (pthread_join(p->read_thread, &threadfn)) { |
---|
| 206 | AUBIO_WRN("timestretch: joining thread failed\n"); |
---|
| 207 | } |
---|
| 208 | pthread_mutex_destroy(&p->read_mutex); |
---|
| 209 | pthread_cond_destroy(&p->read_avail); |
---|
| 210 | pthread_cond_destroy(&p->read_request); |
---|
| 211 | #endif |
---|
[456a784] | 212 | if (p->in) del_fvec(p->in); |
---|
[c41637b] | 213 | if (p->source) del_aubio_source(p->source); |
---|
| 214 | if (p->rb) { |
---|
| 215 | rubberband_delete(p->rb); |
---|
| 216 | } |
---|
| 217 | AUBIO_FREE (p); |
---|
| 218 | } |
---|
| 219 | |
---|
[fd99f0d] | 220 | uint_t |
---|
| 221 | aubio_timestretch_get_samplerate (aubio_timestretch_t * p) |
---|
| 222 | { |
---|
| 223 | return p->samplerate; |
---|
| 224 | } |
---|
| 225 | |
---|
[c41637b] | 226 | uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) { |
---|
| 227 | return rubberband_get_latency(p->rb); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | uint_t |
---|
| 231 | aubio_timestretch_set_stretch (aubio_timestretch_t * p, smpl_t stretch) |
---|
| 232 | { |
---|
| 233 | if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) { |
---|
| 234 | p->stretchratio = stretch; |
---|
[85755eb] | 235 | rubberband_set_time_ratio(p->rb, 1./p->stretchratio); |
---|
[c41637b] | 236 | return AUBIO_OK; |
---|
| 237 | } else { |
---|
[9bd769a] | 238 | AUBIO_WRN("timestretch: could not set stretch ratio to %.2f\n", stretch); |
---|
[c41637b] | 239 | return AUBIO_FAIL; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | smpl_t |
---|
| 244 | aubio_timestretch_get_stretch (aubio_timestretch_t * p) |
---|
| 245 | { |
---|
| 246 | return p->stretchratio; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | uint_t |
---|
| 250 | aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale) |
---|
| 251 | { |
---|
| 252 | if (pitchscale >= 0.0625 && pitchscale <= 4.) { |
---|
| 253 | p->pitchscale = pitchscale; |
---|
| 254 | rubberband_set_pitch_scale(p->rb, p->pitchscale); |
---|
| 255 | return AUBIO_OK; |
---|
| 256 | } else { |
---|
[9bd769a] | 257 | AUBIO_WRN("timestretch: could not set pitchscale to %.2f\n", pitchscale); |
---|
[c41637b] | 258 | return AUBIO_FAIL; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | smpl_t |
---|
| 263 | aubio_timestretch_get_pitchscale (aubio_timestretch_t * p) |
---|
| 264 | { |
---|
| 265 | return p->pitchscale; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | uint_t |
---|
| 269 | aubio_timestretch_set_transpose(aubio_timestretch_t * p, smpl_t transpose) |
---|
| 270 | { |
---|
| 271 | if (transpose >= -24. && transpose <= 24.) { |
---|
| 272 | smpl_t pitchscale = POW(2., transpose / 12.); |
---|
| 273 | return aubio_timestretch_set_pitchscale(p, pitchscale); |
---|
| 274 | } else { |
---|
[9bd769a] | 275 | AUBIO_WRN("timestretch: could not set transpose to %.2f\n", transpose); |
---|
[c41637b] | 276 | return AUBIO_FAIL; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | smpl_t |
---|
| 281 | aubio_timestretch_get_transpose(aubio_timestretch_t * p) |
---|
| 282 | { |
---|
| 283 | return 12. * LOG(p->pitchscale) / LOG(2.0); |
---|
| 284 | } |
---|
| 285 | |
---|
[a874c48] | 286 | sint_t |
---|
[992ac88] | 287 | aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t length) |
---|
[c41637b] | 288 | { |
---|
[992ac88] | 289 | uint_t source_read = p->source_hopsize; |
---|
[c41637b] | 290 | // read more samples from source until we have enough available or eof is reached |
---|
| 291 | int available = rubberband_available(p->rb); |
---|
[992ac88] | 292 | while ((available < (int)length) && (p->eof == 0)) { |
---|
[c41637b] | 293 | aubio_source_do(p->source, p->in, &source_read); |
---|
[992ac88] | 294 | if (source_read < p->source_hopsize) { |
---|
[c41637b] | 295 | p->eof = 1; |
---|
| 296 | } |
---|
| 297 | rubberband_process(p->rb, (const float* const*)&(p->in->data), source_read, p->eof); |
---|
| 298 | available = rubberband_available(p->rb); |
---|
| 299 | } |
---|
[992ac88] | 300 | return available; |
---|
[a874c48] | 301 | } |
---|
| 302 | |
---|
| 303 | void |
---|
| 304 | aubio_timestretch_do (aubio_timestretch_t * p, fvec_t * out, uint_t * read) |
---|
| 305 | { |
---|
[8856791] | 306 | #ifndef HAVE_THREADS |
---|
[992ac88] | 307 | int available = aubio_timestretch_fetch(p, p->hopsize); |
---|
[8856791] | 308 | #else /* HAVE_THREADS */ |
---|
| 309 | int available; |
---|
| 310 | pthread_mutex_lock(&p->read_mutex); |
---|
| 311 | if (p->eof != 1) { |
---|
| 312 | // signal a read request |
---|
| 313 | pthread_cond_signal(&p->read_request); |
---|
| 314 | // wait for an available signal |
---|
| 315 | pthread_cond_wait(&p->read_avail, &p->read_mutex); |
---|
| 316 | } else { |
---|
| 317 | available = rubberband_available(p->rb); |
---|
| 318 | } |
---|
| 319 | #endif /* HAVE_THREADS */ |
---|
[c41637b] | 320 | // now retrieve the samples and write them into out->data |
---|
| 321 | if (available >= (int)p->hopsize) { |
---|
| 322 | rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize); |
---|
| 323 | *read = p->hopsize; |
---|
[9bd769a] | 324 | } else if (available > 0) { |
---|
[c41637b] | 325 | rubberband_retrieve(p->rb, (float* const*)&(out->data), available); |
---|
| 326 | *read = available; |
---|
[9bd769a] | 327 | } else { |
---|
| 328 | fvec_zeros(out); |
---|
| 329 | *read = 0; |
---|
[c41637b] | 330 | } |
---|
[8856791] | 331 | #ifdef HAVE_THREADS |
---|
| 332 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 333 | #endif |
---|
[c41637b] | 334 | } |
---|
| 335 | |
---|
[a0a4d01] | 336 | uint_t |
---|
| 337 | aubio_timestretch_seek (aubio_timestretch_t *p, uint_t pos) |
---|
| 338 | { |
---|
[8856791] | 339 | uint_t err = AUBIO_OK; |
---|
| 340 | #if HAVE_THREADS |
---|
| 341 | pthread_mutex_lock(&p->read_mutex); |
---|
| 342 | #endif |
---|
[a0a4d01] | 343 | p->eof = 0; |
---|
| 344 | rubberband_reset(p->rb); |
---|
[8856791] | 345 | err = aubio_source_seek(p->source, pos); |
---|
| 346 | #if HAVE_THREADS |
---|
| 347 | p->available = 0; |
---|
[bdb249b] | 348 | p->started = 1; |
---|
[8856791] | 349 | pthread_mutex_unlock(&p->read_mutex); |
---|
| 350 | #endif |
---|
| 351 | return err; |
---|
[a0a4d01] | 352 | } |
---|
| 353 | |
---|
[c41637b] | 354 | #endif |
---|