source: src/effects/timestretch_rubberband.c @ 799e05b

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

src/effects/: include aubio_priv.h, not config.h, use <> for external includes

  • Property mode set to 100644
File size: 6.3 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_WRN("timestretch: could not set stretch ratio, rubberband not created\n");
134    return AUBIO_FAIL;
135  }
136  if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) {
137    p->stretchratio = stretch;
138    rubberband_set_time_ratio(p->rb, 1./p->stretchratio);
139    return AUBIO_OK;
140  } else {
141    AUBIO_WRN("timestretch: could not set stretch ratio to %.2f\n", stretch);
142    return AUBIO_FAIL;
143  }
144}
145
146smpl_t
147aubio_timestretch_get_stretch (aubio_timestretch_t * p)
148{
149  return p->stretchratio;
150}
151
152uint_t
153aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale)
154{
155  if (!p->rb) {
156    AUBIO_WRN("timestretch: could not set pitch scale, rubberband not created\n");
157    return AUBIO_FAIL;
158  }
159  if (pitchscale >= 0.0625  && pitchscale <= 4.) {
160    p->pitchscale = pitchscale;
161    rubberband_set_pitch_scale(p->rb, p->pitchscale);
162    return AUBIO_OK;
163  } else {
164    AUBIO_WRN("timestretch: could not set pitchscale to %.2f\n", pitchscale);
165    return AUBIO_FAIL;
166  }
167}
168
169smpl_t
170aubio_timestretch_get_pitchscale (aubio_timestretch_t * p)
171{
172  return p->pitchscale;
173}
174
175uint_t
176aubio_timestretch_set_transpose(aubio_timestretch_t * p, smpl_t transpose)
177{
178  if (transpose >= -24. && transpose <= 24.) {
179    smpl_t pitchscale = POW(2., transpose / 12.);
180    return aubio_timestretch_set_pitchscale(p, pitchscale);
181  } else {
182    AUBIO_WRN("timestretch: could not set transpose to %.2f\n", transpose);
183    return AUBIO_FAIL;
184  }
185}
186
187smpl_t
188aubio_timestretch_get_transpose(aubio_timestretch_t * p)
189{
190  return 12. * LOG(p->pitchscale) / LOG(2.0);
191}
192
193sint_t
194aubio_timestretch_push(aubio_timestretch_t *p, fvec_t *input, uint_t length)
195{
196  // push new samples to rubberband, return available
197  int available;
198  int eof = (input->length != length) ? 1 : 0;
199  rubberband_process(p->rb, (const float* const*)&(input->data), length, eof);
200  available = rubberband_available(p->rb);
201  //AUBIO_WRN("timestretch: processed %d, %d available, eof: %d\n",
202  //    length, available, eof);
203  return available;
204}
205
206sint_t
207aubio_timestretch_get_available(aubio_timestretch_t *p) {
208  return rubberband_available(p->rb);
209}
210
211void
212aubio_timestretch_do(aubio_timestretch_t * p, fvec_t * out, uint_t * read)
213{
214  // now retrieve the samples and write them into out->data
215  int available = rubberband_available(p->rb);
216  if (available >= (int)out->length) {
217    rubberband_retrieve(p->rb, (float* const*)&(out->data), out->length);
218    *read = out->length;
219  } else if (available > 0) {
220    // this occurs each time the end of file is reached
221    //AUBIO_WRN("timestretch: short read\n");
222    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
223    fvec_t zeros; zeros.length = out->length - available; zeros.data = out->data + available;
224    fvec_zeros(&zeros);
225    *read = available;
226  } else {
227    // this may occur if the previous was a short read available == hopsize
228    fvec_zeros(out);
229    *read = 0;
230  }
231}
232
233uint_t
234aubio_timestretch_reset(aubio_timestretch_t *p)
235{
236  uint_t err = AUBIO_OK;
237  if (p->rb) {
238    rubberband_reset(p->rb);
239  }
240  return err;
241}
242
243#endif
Note: See TracBrowser for help on using the repository browser.