source: src/effects/timestretch_rubberband.c @ 986e4b7

feature/crepe
Last change on this file since 986e4b7 was 6e157df, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[timestretch] comment out unused warmup

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