source: src/effects/timestretch_rubberband.c @ ff87a67

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

[timestretch] validate input hopsize and samplerate

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