source: src/effects/timestretch_rubberband.c @ a77f0c6

feature/cnnfeature/crepefeature/timestretchfix/ffmpeg5
Last change on this file since a77f0c6 was a77f0c6, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[effects] convert warnings to error, improve messages

  • Property mode set to 100644
File size: 6.5 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 "aubio_priv.h"
22
23#ifdef HAVE_RUBBERBAND
24
25#include "fvec.h"
26#include "fmat.h"
27#include "io/source.h"
28#include "effects/timestretch.h"
29
30#include <rubberband/rubberband-c.h>
31
32#define MIN_STRETCH_RATIO 0.025
33#define MAX_STRETCH_RATIO 40.
34
35#define HAVE_THREADS 1
36#if 0
37#undef HAVE_THREADS
38#endif
39
40#ifdef HAVE_THREADS
41#include <pthread.h>
42#endif
43
44/** generic time stretching structure */
45struct _aubio_timestretch_t
46{
47  uint_t samplerate;              /**< samplerate */
48  uint_t hopsize;                 /**< hop size */
49  smpl_t stretchratio;            /**< time ratio */
50  smpl_t pitchscale;              /**< pitch scale */
51
52  RubberBandState rb;
53  RubberBandOptions rboptions;
54};
55
56extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode);
57
58static void aubio_timestretch_warmup (aubio_timestretch_t * p);
59
60aubio_timestretch_t *
61new_aubio_timestretch (const char_t * mode, smpl_t stretchratio, uint_t hopsize,
62    uint_t samplerate)
63{
64  aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t);
65  p->hopsize = hopsize;
66  p->pitchscale = 1.;
67
68  if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) {
69    p->stretchratio = stretchratio;
70  } else {
71    AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n",
72        MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio);
73    goto beach;
74  }
75
76  p->rboptions = aubio_get_rubberband_opts(mode);
77  if (p->rboptions < 0) {
78    AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode);
79    goto beach;
80  }
81
82  p->rb = rubberband_new(samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale);
83  if (!p->rb) goto beach;
84
85  p->samplerate = samplerate;
86
87  //aubio_timestretch_warmup(p);
88
89  return p;
90
91beach:
92  del_aubio_timestretch(p);
93  return NULL;
94}
95
96static void
97aubio_timestretch_warmup (aubio_timestretch_t * p)
98{
99  // warm up rubber band
100  //AUBIO_WRN("timestretch: warming-up\n");
101  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
102  fvec_t *input = new_fvec(p->hopsize);
103  while (aubio_timestretch_push(p, input, input->length) < (int)latency) {
104    //sint_t available = aubio_timestretch_get_available(p);
105    //AUBIO_WRN("timestretch: warmup got %d, latency: %d\n", available, latency);
106  }
107  del_fvec(input);
108}
109
110void
111del_aubio_timestretch (aubio_timestretch_t * p)
112{
113  if (p->rb) {
114    rubberband_delete(p->rb);
115  }
116  AUBIO_FREE (p);
117}
118
119uint_t
120aubio_timestretch_get_samplerate (aubio_timestretch_t * p)
121{
122  return p->samplerate;
123}
124
125uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) {
126  return rubberband_get_latency(p->rb);
127}
128
129uint_t
130aubio_timestretch_set_stretch (aubio_timestretch_t * p, smpl_t stretch)
131{
132  if (!p->rb) {
133    AUBIO_ERR("timestretch: could not set stretch ratio,"
134       " rubberband not created\n");
135    return AUBIO_FAIL;
136  }
137  if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) {
138    p->stretchratio = stretch;
139    rubberband_set_time_ratio(p->rb, 1./p->stretchratio);
140    return AUBIO_OK;
141  } else {
142    AUBIO_ERR("timestretch: could not set stretch ratio to '%f',"
143        " should be in the range [%.2f, %.2f].\n", stretch,
144        MIN_STRETCH_RATIO, MAX_STRETCH_RATIO);
145    return AUBIO_FAIL;
146  }
147}
148
149smpl_t
150aubio_timestretch_get_stretch (aubio_timestretch_t * p)
151{
152  return p->stretchratio;
153}
154
155uint_t
156aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale)
157{
158  if (!p->rb) {
159    AUBIO_ERR("timestretch: could not set pitch scale,"
160       " rubberband not created\n");
161    return AUBIO_FAIL;
162  }
163  if (pitchscale >= 0.0625  && pitchscale <= 4.) {
164    p->pitchscale = pitchscale;
165    rubberband_set_pitch_scale(p->rb, p->pitchscale);
166    return AUBIO_OK;
167  } else {
168    AUBIO_ERR("timestretch: could not set pitchscale to '%f',"
169        " should be in the range [0.0625, 4.].\n", pitchscale);
170    return AUBIO_FAIL;
171  }
172}
173
174smpl_t
175aubio_timestretch_get_pitchscale (aubio_timestretch_t * p)
176{
177  return p->pitchscale;
178}
179
180uint_t
181aubio_timestretch_set_transpose(aubio_timestretch_t * p, smpl_t transpose)
182{
183  if (transpose >= -24. && transpose <= 24.) {
184    smpl_t pitchscale = POW(2., transpose / 12.);
185    return aubio_timestretch_set_pitchscale(p, pitchscale);
186  } else {
187    AUBIO_ERR("timestretch: could not set transpose to '%f',"
188        " should be in the range [-24; 24].\n", transpose);
189    return AUBIO_FAIL;
190  }
191}
192
193smpl_t
194aubio_timestretch_get_transpose(aubio_timestretch_t * p)
195{
196  return 12. * LOG(p->pitchscale) / LOG(2.0);
197}
198
199sint_t
200aubio_timestretch_push(aubio_timestretch_t *p, fvec_t *input, uint_t length)
201{
202  // push new samples to rubberband, return available
203  int available;
204  int eof = (input->length != length) ? 1 : 0;
205  rubberband_process(p->rb, (const float* const*)&(input->data), length, eof);
206  available = rubberband_available(p->rb);
207  //AUBIO_WRN("timestretch: processed %d, %d available, eof: %d\n",
208  //    length, available, eof);
209  return available;
210}
211
212sint_t
213aubio_timestretch_get_available(aubio_timestretch_t *p) {
214  return rubberband_available(p->rb);
215}
216
217void
218aubio_timestretch_do(aubio_timestretch_t * p, fvec_t * out, uint_t * read)
219{
220  // now retrieve the samples and write them into out->data
221  int available = rubberband_available(p->rb);
222  if (available >= (int)out->length) {
223    rubberband_retrieve(p->rb, (float* const*)&(out->data), out->length);
224    *read = out->length;
225  } else if (available > 0) {
226    // this occurs each time the end of file is reached
227    //AUBIO_WRN("timestretch: short read\n");
228    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
229    fvec_t zeros; zeros.length = out->length - available; zeros.data = out->data + available;
230    fvec_zeros(&zeros);
231    *read = available;
232  } else {
233    // this may occur if the previous was a short read available == hopsize
234    fvec_zeros(out);
235    *read = 0;
236  }
237}
238
239uint_t
240aubio_timestretch_reset(aubio_timestretch_t *p)
241{
242  uint_t err = AUBIO_OK;
243  if (p->rb) {
244    rubberband_reset(p->rb);
245  }
246  return err;
247}
248
249#endif
Note: See TracBrowser for help on using the repository browser.