source: src/effects/timestretch_rubberband.c @ 49688ef

feature/crepe
Last change on this file since 49688ef 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
RevLine 
[c41637b]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
[799e05b]21#include "aubio_priv.h"
[c41637b]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
[799e05b]30#include <rubberband/rubberband-c.h>
[c41637b]31
32#define MIN_STRETCH_RATIO 0.025
[85755eb]33#define MAX_STRETCH_RATIO 40.
[c41637b]34
[8856791]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
[c41637b]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
[6e157df]58//static void aubio_timestretch_warmup (aubio_timestretch_t * p);
[a874c48]59
[c41637b]60aubio_timestretch_t *
[284fe8a]61new_aubio_timestretch (const char_t * mode, smpl_t stretchratio, uint_t hopsize,
62    uint_t samplerate)
[c41637b]63{
64  aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t);
65  p->hopsize = hopsize;
66  p->pitchscale = 1.;
67
[97a5ac08]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
[c41637b]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
[284fe8a]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);
[a874c48]98
99  return p;
100
101beach:
102  del_aubio_timestretch(p);
103  return NULL;
104}
105
[6e157df]106#if 0
[a874c48]107static void
108aubio_timestretch_warmup (aubio_timestretch_t * p)
109{
[c41637b]110  // warm up rubber band
[4559863]111  //AUBIO_WRN("timestretch: warming-up\n");
[c41637b]112  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
[284fe8a]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);
[c41637b]119}
[6e157df]120#endif
[c41637b]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
[fd99f0d]131uint_t
132aubio_timestretch_get_samplerate (aubio_timestretch_t * p)
133{
134  return p->samplerate;
135}
136
[c41637b]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{
[4559863]144  if (!p->rb) {
[a77f0c6]145    AUBIO_ERR("timestretch: could not set stretch ratio,"
146       " rubberband not created\n");
[4559863]147    return AUBIO_FAIL;
148  }
[c41637b]149  if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) {
150    p->stretchratio = stretch;
[85755eb]151    rubberband_set_time_ratio(p->rb, 1./p->stretchratio);
[c41637b]152    return AUBIO_OK;
153  } else {
[a77f0c6]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);
[c41637b]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{
[4559863]170  if (!p->rb) {
[a77f0c6]171    AUBIO_ERR("timestretch: could not set pitch scale,"
172       " rubberband not created\n");
[4559863]173    return AUBIO_FAIL;
174  }
[c41637b]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 {
[a77f0c6]180    AUBIO_ERR("timestretch: could not set pitchscale to '%f',"
181        " should be in the range [0.0625, 4.].\n", pitchscale);
[c41637b]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 {
[a77f0c6]199    AUBIO_ERR("timestretch: could not set transpose to '%f',"
200        " should be in the range [-24; 24].\n", transpose);
[c41637b]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
[a874c48]211sint_t
[284fe8a]212aubio_timestretch_push(aubio_timestretch_t *p, fvec_t *input, uint_t length)
[c41637b]213{
[284fe8a]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);
[992ac88]221  return available;
[a874c48]222}
223
[284fe8a]224sint_t
225aubio_timestretch_get_available(aubio_timestretch_t *p) {
226  return rubberband_available(p->rb);
227}
228
[a874c48]229void
[284fe8a]230aubio_timestretch_do(aubio_timestretch_t * p, fvec_t * out, uint_t * read)
[a874c48]231{
[c41637b]232  // now retrieve the samples and write them into out->data
[284fe8a]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;
[9bd769a]237  } else if (available > 0) {
[4559863]238    // this occurs each time the end of file is reached
239    //AUBIO_WRN("timestretch: short read\n");
[c41637b]240    rubberband_retrieve(p->rb, (float* const*)&(out->data), available);
[284fe8a]241    fvec_t zeros; zeros.length = out->length - available; zeros.data = out->data + available;
242    fvec_zeros(&zeros);
[c41637b]243    *read = available;
[9bd769a]244  } else {
[4559863]245    // this may occur if the previous was a short read available == hopsize
[9bd769a]246    fvec_zeros(out);
247    *read = 0;
[c41637b]248  }
249}
250
[a0a4d01]251uint_t
[284fe8a]252aubio_timestretch_reset(aubio_timestretch_t *p)
[a0a4d01]253{
[8856791]254  uint_t err = AUBIO_OK;
[4559863]255  if (p->rb) {
256    rubberband_reset(p->rb);
257  }
[8856791]258  return err;
[a0a4d01]259}
260
[c41637b]261#endif
Note: See TracBrowser for help on using the repository browser.