source: src/effects/timestretch_rubberband.c @ 4acee97

samplertimestretch
Last change on this file since 4acee97 was 4acee97, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/effects/timestretch_rubberband.c: fix eof when last available == hopsize, change _set messages to warnings

  • Property mode set to 100644
File size: 6.1 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  uint_t source_hopsize;          /**< hop size at which the source is read */
46  fvec_t *in;
47  uint_t eof;
48
49  RubberBandState rb;
50  RubberBandOptions rboptions;
51};
52
53extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode);
54
55static void aubio_timestretch_warmup (aubio_timestretch_t * p);
56static sint_t aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t fetch);
57
58aubio_timestretch_t *
59new_aubio_timestretch (const char_t * uri, const char_t * mode,
60    smpl_t stretchratio, uint_t hopsize, uint_t samplerate)
61{
62  aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t);
63  p->samplerate = samplerate;
64  p->hopsize = hopsize;
65  p->source_hopsize = hopsize;
66  p->pitchscale = 1.;
67  p->eof = 0;
68
69  p->source = new_aubio_source(uri, samplerate, p->source_hopsize);
70  if (!p->source) goto beach;
71  if (samplerate == 0 ) p->samplerate = aubio_source_get_samplerate(p->source);
72
73  p->in = new_fvec(p->source_hopsize);
74
75  if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) {
76    p->stretchratio = stretchratio;
77  } else {
78    AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n",
79        MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio);
80    goto beach;
81  }
82
83  p->rboptions = aubio_get_rubberband_opts(mode);
84  if (p->rboptions < 0) {
85    AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode);
86    goto beach;
87  }
88
89  p->rb = rubberband_new(p->samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale);
90  rubberband_set_max_process_size(p->rb, p->source_hopsize);
91  //rubberband_set_debug_level(p->rb, 10);
92
93  aubio_timestretch_warmup(p);
94
95  return p;
96
97beach:
98  del_aubio_timestretch(p);
99  return NULL;
100}
101
102static void
103aubio_timestretch_warmup (aubio_timestretch_t * p)
104{
105  // warm up rubber band
106  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
107  aubio_timestretch_fetch(p, latency);
108}
109
110void
111del_aubio_timestretch (aubio_timestretch_t * p)
112{
113  if (p->in) del_fvec(p->in);
114  if (p->source) del_aubio_source(p->source);
115  if (p->rb) {
116    rubberband_delete(p->rb);
117  }
118  AUBIO_FREE (p);
119}
120
121uint_t
122aubio_timestretch_get_samplerate (aubio_timestretch_t * p)
123{
124  return p->samplerate;
125}
126
127uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) {
128  return rubberband_get_latency(p->rb);
129}
130
131uint_t
132aubio_timestretch_set_stretch (aubio_timestretch_t * p, smpl_t stretch)
133{
134  if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) {
135    p->stretchratio = stretch;
136    rubberband_set_time_ratio(p->rb, 1./p->stretchratio);
137    return AUBIO_OK;
138  } else {
139    AUBIO_WRN("timestretch: could not set stretch ratio to %.2f\n", stretch);
140    return AUBIO_FAIL;
141  }
142}
143
144smpl_t
145aubio_timestretch_get_stretch (aubio_timestretch_t * p)
146{
147  return p->stretchratio;
148}
149
150uint_t
151aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale)
152{
153  if (pitchscale >= 0.0625  && pitchscale <= 4.) {
154    p->pitchscale = pitchscale;
155    rubberband_set_pitch_scale(p->rb, p->pitchscale);
156    return AUBIO_OK;
157  } else {
158    AUBIO_WRN("timestretch: could not set pitchscale to %.2f\n", pitchscale);
159    return AUBIO_FAIL;
160  }
161}
162
163smpl_t
164aubio_timestretch_get_pitchscale (aubio_timestretch_t * p)
165{
166  return p->pitchscale;
167}
168
169uint_t
170aubio_timestretch_set_transpose(aubio_timestretch_t * p, smpl_t transpose)
171{
172  if (transpose >= -24. && transpose <= 24.) {
173    smpl_t pitchscale = POW(2., transpose / 12.);
174    return aubio_timestretch_set_pitchscale(p, pitchscale);
175  } else {
176    AUBIO_WRN("timestretch: could not set transpose to %.2f\n", transpose);
177    return AUBIO_FAIL;
178  }
179}
180
181smpl_t
182aubio_timestretch_get_transpose(aubio_timestretch_t * p)
183{
184  return 12. * LOG(p->pitchscale) / LOG(2.0);
185}
186
187sint_t
188aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t length)
189{
190  uint_t source_read = p->source_hopsize;
191  // read more samples from source until we have enough available or eof is reached
192  int available = rubberband_available(p->rb);
193  while ((available < (int)length) && (p->eof == 0)) {
194    aubio_source_do(p->source, p->in, &source_read);
195    if (source_read < p->source_hopsize) {
196      p->eof = 1;
197    }
198    rubberband_process(p->rb, (const float* const*)&(p->in->data), source_read, p->eof);
199    available = rubberband_available(p->rb);
200  }
201  return available;
202}
203
204void
205aubio_timestretch_do (aubio_timestretch_t * p, fvec_t * out, uint_t * read)
206{
207  int available = aubio_timestretch_fetch(p, p->hopsize);
208  // now retrieve the samples and write them into out->data
209  if (available >= (int)p->hopsize) {
210    rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize);
211    *read = p->hopsize;
212  } else if (available > 0) {
213    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
214    *read = available;
215  } else {
216    fvec_zeros(out);
217    *read = 0;
218  }
219}
220
221uint_t
222aubio_timestretch_seek (aubio_timestretch_t *p, uint_t pos)
223{
224  p->eof = 0;
225  rubberband_reset(p->rb);
226  return aubio_source_seek(p->source, pos);
227}
228
229#endif
Note: See TracBrowser for help on using the repository browser.