source: src/effects/timestretch_rubberband.c @ a874c48

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

src/effects/timestretch_rubberband.c: split into functions

  • Property mode set to 100644
File size: 6.2 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/** generic time stretching structure */
37struct _aubio_timestretch_t
38{
39  uint_t samplerate;              /**< samplerate */
40  uint_t hopsize;                 /**< hop size */
41  smpl_t stretchratio;            /**< time ratio */
42  smpl_t pitchscale;              /**< pitch scale */
43
44  aubio_source_t *source;
45  fvec_t *in;
46  uint_t eof;
47
48  RubberBandState rb;
49  RubberBandOptions rboptions;
50};
51
52extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode);
53
54static void aubio_timestretch_warmup (aubio_timestretch_t * p);
55static sint_t aubio_timestretch_fetch(aubio_timestretch_t *p);
56
57aubio_timestretch_t *
58new_aubio_timestretch (const char_t * uri, const char_t * mode,
59    smpl_t stretchratio, uint_t hopsize, uint_t samplerate)
60{
61  aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t);
62  p->samplerate = samplerate;
63  p->hopsize = hopsize;
64  p->pitchscale = 1.;
65  p->eof = 0;
66
67  p->source = new_aubio_source(uri, samplerate, hopsize);
68  if (!p->source) goto beach;
69  if (samplerate == 0 ) p->samplerate = aubio_source_get_samplerate(p->source);
70
71  p->in = new_fvec(hopsize);
72
73  if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) {
74    p->stretchratio = stretchratio;
75  } else {
76    AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n",
77        MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio);
78    goto beach;
79  }
80
81  p->rboptions = aubio_get_rubberband_opts(mode);
82  if (p->rboptions < 0) {
83    AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode);
84    goto beach;
85  }
86
87  p->rb = rubberband_new(p->samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale);
88  rubberband_set_max_process_size(p->rb, p->hopsize);
89  //rubberband_set_debug_level(p->rb, 10);
90
91  aubio_timestretch_warmup(p);
92
93  return p;
94
95beach:
96  del_aubio_timestretch(p);
97  return NULL;
98}
99
100static void
101aubio_timestretch_warmup (aubio_timestretch_t * p)
102{
103  // warm up rubber band
104  uint_t source_read = 0;
105  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
106  int available = rubberband_available(p->rb);
107  while (available <= (int)latency) {
108    aubio_source_do(p->source, p->in, &source_read);
109    // for very short samples
110    if (source_read < p->hopsize) p->eof = 1;
111    rubberband_process(p->rb, (const float* const*)&(p->in->data), p->hopsize, p->eof);
112    available = rubberband_available(p->rb);
113  }
114}
115
116void
117del_aubio_timestretch (aubio_timestretch_t * p)
118{
119  if (p->in) del_fvec(p->in);
120  if (p->source) del_aubio_source(p->source);
121  if (p->rb) {
122    rubberband_delete(p->rb);
123  }
124  AUBIO_FREE (p);
125}
126
127uint_t
128aubio_timestretch_get_samplerate (aubio_timestretch_t * p)
129{
130  return p->samplerate;
131}
132
133uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) {
134  return rubberband_get_latency(p->rb);
135}
136
137uint_t
138aubio_timestretch_set_stretch (aubio_timestretch_t * p, smpl_t stretch)
139{
140  if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) {
141    p->stretchratio = stretch;
142    rubberband_set_time_ratio(p->rb, 1./p->stretchratio);
143    return AUBIO_OK;
144  } else {
145    AUBIO_ERR("timestretch: could not set stretch ratio to %.2f\n", stretch);
146    return AUBIO_FAIL;
147  }
148}
149
150smpl_t
151aubio_timestretch_get_stretch (aubio_timestretch_t * p)
152{
153  return p->stretchratio;
154}
155
156uint_t
157aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale)
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_ERR("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_ERR("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_fetch(aubio_timestretch_t *p)
195{
196  uint_t source_read = p->hopsize;
197  // read more samples from source until we have enough available or eof is reached
198  int available = rubberband_available(p->rb);
199  while ((available < (int)p->hopsize) && (p->eof == 0)) {
200    aubio_source_do(p->source, p->in, &source_read);
201    if (source_read < p->hopsize) {
202      p->eof = 1;
203    }
204    rubberband_process(p->rb, (const float* const*)&(p->in->data), source_read, p->eof);
205    available = rubberband_available(p->rb);
206  }
207  return source_read;
208}
209
210void
211aubio_timestretch_do (aubio_timestretch_t * p, fvec_t * out, uint_t * read)
212{
213  int available = aubio_timestretch_fetch(p);
214  // now retrieve the samples and write them into out->data
215  if (available >= (int)p->hopsize) {
216    rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize);
217    *read = p->hopsize;
218  } else {
219    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
220    *read = available;
221  }
222}
223
224uint_t
225aubio_timestretch_seek (aubio_timestretch_t *p, uint_t pos)
226{
227  p->eof = 0;
228  rubberband_reset(p->rb);
229  return aubio_source_seek(p->source, pos);
230}
231
232#endif
Note: See TracBrowser for help on using the repository browser.