source: src/effects/timestretch_rubberband.c @ 456a784

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

src/effects/timestretch_rubberband.c: remove unused variable, erase internal vector

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