source: src/effects/timestretch_rubberband.c @ bdb249b

feature/timestretch
Last change on this file since bdb249b was bdb249b, checked in by Paul Brossier <piem@piem.org>, 12 months ago

src/effects/timestretch_rubberband.c: do not reopen thread in _seek, add flags to mark warmed-up and finished states

  • Property mode set to 100644
File size: 9.8 KB
Line 
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
34#define MAX_STRETCH_RATIO 40.
35
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
45/** generic time stretching structure */
46struct _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;
54  uint_t source_hopsize;          /**< hop size at which the source is read */
55  fvec_t *in;
56  uint_t eof;
57
58  RubberBandState rb;
59  RubberBandOptions rboptions;
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;
67  uint_t started;
68  uint_t finish;
69#endif
70};
71
72extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode);
73
74static void aubio_timestretch_warmup (aubio_timestretch_t * p);
75static sint_t aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t fetch);
76#ifdef HAVE_THREADS
77static void *aubio_timestretch_readfn(void *p);
78#endif
79
80aubio_timestretch_t *
81new_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;
87  //p->source_hopsize = 2048;
88  p->source_hopsize = hopsize;
89  p->pitchscale = 1.;
90  p->eof = 0;
91
92  p->source = new_aubio_source(uri, samplerate, p->source_hopsize);
93  if (!p->source) goto beach;
94  if (samplerate == 0 ) p->samplerate = aubio_source_get_samplerate(p->source);
95
96  p->in = new_fvec(p->source_hopsize);
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);
113  rubberband_set_max_process_size(p->rb, p->source_hopsize);
114  //rubberband_set_debug_level(p->rb, 10);
115
116#ifdef HAVE_THREADS
117  p->started = 0;
118  p->finish = 0;
119  pthread_mutex_init(&p->read_mutex, 0);
120  pthread_cond_init (&p->read_avail, 0);
121  pthread_cond_init (&p->read_request, 0);
122  //AUBIO_WRN("timestretch: creating thread\n");
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
130  aubio_timestretch_warmup(p);
131#endif
132
133  return p;
134
135beach:
136  del_aubio_timestretch(p);
137  return NULL;
138}
139
140#ifdef HAVE_THREADS
141void *
142aubio_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);
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);
167      pthread_cond_wait(&p->read_request, &p->read_mutex);
168      //AUBIO_WRN("timestretch: finished idle in readfn\n");
169      if (p->finish) pthread_exit(NULL);
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
179static void
180aubio_timestretch_warmup (aubio_timestretch_t * p)
181{
182  // warm up rubber band
183  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
184#ifdef HAVE_THREADS
185  p->available = aubio_timestretch_fetch(p, latency);
186#else
187  aubio_timestretch_fetch(p, latency);
188#endif
189}
190
191void
192del_aubio_timestretch (aubio_timestretch_t * p)
193{
194#ifdef HAVE_THREADS
195  void *threadfn;
196  //AUBIO_WRN("timestretch: entering delete\n");
197  pthread_mutex_lock(&p->read_mutex);
198  p->finish = 1;
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
212  if (p->in) del_fvec(p->in);
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
220uint_t
221aubio_timestretch_get_samplerate (aubio_timestretch_t * p)
222{
223  return p->samplerate;
224}
225
226uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) {
227  return rubberband_get_latency(p->rb);
228}
229
230uint_t
231aubio_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;
235    rubberband_set_time_ratio(p->rb, 1./p->stretchratio);
236    return AUBIO_OK;
237  } else {
238    AUBIO_WRN("timestretch: could not set stretch ratio to %.2f\n", stretch);
239    return AUBIO_FAIL;
240  }
241}
242
243smpl_t
244aubio_timestretch_get_stretch (aubio_timestretch_t * p)
245{
246  return p->stretchratio;
247}
248
249uint_t
250aubio_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 {
257    AUBIO_WRN("timestretch: could not set pitchscale to %.2f\n", pitchscale);
258    return AUBIO_FAIL;
259  }
260}
261
262smpl_t
263aubio_timestretch_get_pitchscale (aubio_timestretch_t * p)
264{
265  return p->pitchscale;
266}
267
268uint_t
269aubio_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 {
275    AUBIO_WRN("timestretch: could not set transpose to %.2f\n", transpose);
276    return AUBIO_FAIL;
277  }
278}
279
280smpl_t
281aubio_timestretch_get_transpose(aubio_timestretch_t * p)
282{
283  return 12. * LOG(p->pitchscale) / LOG(2.0);
284}
285
286sint_t
287aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t length)
288{
289  uint_t source_read = p->source_hopsize;
290  // read more samples from source until we have enough available or eof is reached
291  int available = rubberband_available(p->rb);
292  while ((available < (int)length) && (p->eof == 0)) {
293    aubio_source_do(p->source, p->in, &source_read);
294    if (source_read < p->source_hopsize) {
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  }
300  return available;
301}
302
303void
304aubio_timestretch_do (aubio_timestretch_t * p, fvec_t * out, uint_t * read)
305{
306#ifndef HAVE_THREADS
307  int available = aubio_timestretch_fetch(p, p->hopsize);
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 */
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;
324  } else if (available > 0) {
325    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
326    *read = available;
327  } else {
328    fvec_zeros(out);
329    *read = 0;
330  }
331#ifdef HAVE_THREADS
332  pthread_mutex_unlock(&p->read_mutex);
333#endif
334}
335
336uint_t
337aubio_timestretch_seek (aubio_timestretch_t *p, uint_t pos)
338{
339  uint_t err = AUBIO_OK;
340#if HAVE_THREADS
341  pthread_mutex_lock(&p->read_mutex);
342#endif
343  p->eof = 0;
344  rubberband_reset(p->rb);
345  err = aubio_source_seek(p->source, pos);
346#if HAVE_THREADS
347  p->available = 0;
348  p->started = 1;
349  pthread_mutex_unlock(&p->read_mutex);
350#endif
351  return err;
352}
353
354#endif
Note: See TracBrowser for help on using the repository browser.