source: src/effects/timestretch_rubberband.c @ fb5bd55

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

src/effects/timestretch_rubberband.c: invert stretch ratio

  • Property mode set to 100644
File size: 5.9 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  fvec_t *zeros;
47  uint_t eof;
48
49  RubberBandState rb;
50  RubberBandOptions rboptions;
51};
52
53extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode);
54
55aubio_timestretch_t *
56new_aubio_timestretch (const char_t * uri, const char_t * mode,
57    smpl_t stretchratio, uint_t hopsize, uint_t samplerate)
58{
59  aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t);
60  p->samplerate = samplerate;
61  p->hopsize = hopsize;
62  p->pitchscale = 1.;
63  p->eof = 0;
64
65  p->source = new_aubio_source(uri, samplerate, hopsize);
66  if (!p->source) goto beach;
67  if (samplerate == 0 ) p->samplerate = aubio_source_get_samplerate(p->source);
68
69  p->in = new_fvec(hopsize);
70  p->zeros = new_fvec(hopsize);
71
72  if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) {
73    p->stretchratio = stretchratio;
74  } else {
75    AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n",
76        MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio);
77    goto beach;
78  }
79
80  p->rboptions = aubio_get_rubberband_opts(mode);
81  if (p->rboptions < 0) {
82    AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode);
83    goto beach;
84  }
85
86  p->rb = rubberband_new(p->samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale);
87  rubberband_set_max_process_size(p->rb, p->hopsize);
88  //rubberband_set_debug_level(p->rb, 10);
89
90#if 1
91  // warm up rubber band
92  uint_t source_read = 0;
93  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
94  int available = rubberband_available(p->rb);
95  while (available <= (int)latency) {
96    aubio_source_do(p->source, p->in, &source_read);
97    // for very short samples
98    if (source_read < p->hopsize) p->eof = 1;
99    rubberband_process(p->rb, (const float* const*)&(p->in->data), p->hopsize, p->eof);
100    available = rubberband_available(p->rb);
101  }
102#endif
103
104  return p;
105
106beach:
107  del_aubio_timestretch(p);
108  return NULL;
109}
110
111void
112del_aubio_timestretch (aubio_timestretch_t * p)
113{
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_ERR("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_ERR("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_ERR("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
187void
188aubio_timestretch_do (aubio_timestretch_t * p, fvec_t * out, uint_t * read)
189{
190  uint_t source_read = p->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)p->hopsize) && (p->eof == 0)) {
194    aubio_source_do(p->source, p->in, &source_read);
195    if (source_read < p->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  // now retrieve the samples and write them into out->data
202  if (available >= (int)p->hopsize) {
203    rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize);
204    *read = p->hopsize;
205  } else {
206    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
207    *read = available;
208  }
209}
210
211uint_t
212aubio_timestretch_seek (aubio_timestretch_t *p, uint_t pos)
213{
214  p->eof = 0;
215  rubberband_reset(p->rb);
216  return aubio_source_seek(p->source, pos);
217}
218
219#endif
Note: See TracBrowser for help on using the repository browser.