source: src/effects/pitchshift.c @ e2645cb

feature/cnnfeature/crepefeature/pitchshiftfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretch
Last change on this file since e2645cb was e2645cb, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/effects/pitchshift.c: remove unused timeratio

  • Property mode set to 100644
File size: 6.6 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#include "aubio_priv.h"
23#include "fvec.h"
24#include "effects/pitchshift.h"
25
26#ifdef HAVE_RUBBERBAND
27
28#include "rubberband/rubberband-c.h"
29
30// check rubberband is 1.8.1, warn if 1.3
31#if !((RUBBERBAND_API_MAJOR_VERSION >= 2) && \
32    (RUBBERBAND_API_MINOR_VERSION >= 5))
33#warning RubberBandOptionDetectorSoft not available, \
34 please upgrade rubberband to version 1.8.1 or higher
35#define RubberBandOptionDetectorSoft 0x00000000
36#endif
37
38/** generic pitch shifting structure */
39struct _aubio_pitchshift_t
40{
41  uint_t samplerate;              /**< samplerate */
42  uint_t hopsize;                 /**< hop size */
43  smpl_t pitchscale;              /**< pitch scale */
44
45  RubberBandState rb;
46  RubberBandOptions rboptions;
47};
48
49aubio_pitchshift_t *
50new_aubio_pitchshift (const char_t * mode,
51    smpl_t pitchscale, uint_t hopsize, uint_t samplerate)
52{
53  aubio_pitchshift_t *p = AUBIO_NEW (aubio_pitchshift_t);
54  p->samplerate = samplerate;
55  p->hopsize = hopsize;
56  p->timeratio = 1.;
57  p->pitchscale = pitchscale;
58
59  p->rboptions = RubberBandOptionProcessRealTime;
60
61  if ( strcmp(mode,"crispness:0") == 0 ) {
62    p->rboptions |= RubberBandOptionTransientsSmooth;
63    p->rboptions |= RubberBandOptionWindowLong;
64    p->rboptions |= RubberBandOptionPhaseIndependent;
65  } else if ( strcmp(mode, "crispness:1") == 0 ) {
66    p->rboptions |= RubberBandOptionDetectorSoft;
67    p->rboptions |= RubberBandOptionTransientsSmooth;
68    p->rboptions |= RubberBandOptionWindowLong;
69    p->rboptions |= RubberBandOptionPhaseIndependent;
70  } else if ( strcmp(mode, "crispness:2") == 0 ) {
71    p->rboptions |= RubberBandOptionTransientsSmooth;
72    p->rboptions |= RubberBandOptionPhaseIndependent;
73  } else if ( strcmp(mode, "crispness:3") == 0 ) {
74    p->rboptions |= RubberBandOptionTransientsSmooth;
75  } else if ( strcmp(mode, "crispness:4") == 0 ) {
76    // same as "default"
77  } else if ( strcmp(mode, "crispness:5") == 0 ) {
78    p->rboptions |= RubberBandOptionTransientsCrisp;
79  } else if ( strcmp(mode, "crispness:6") == 0 ) {
80    p->rboptions |= RubberBandOptionTransientsCrisp;
81    p->rboptions |= RubberBandOptionWindowShort;
82    p->rboptions |= RubberBandOptionPhaseIndependent;
83  } else if ( strcmp(mode, "default") == 0 ) {
84    // nothing to do
85  } else {
86    AUBIO_ERR("pitchshift: unknown pitch shifting method %s\n", mode);
87    goto beach;
88  }
89  //AUBIO_MSG("pitchshift: using pitch shifting method %s\n", mode);
90
91  //p->rboptions |= RubberBandOptionTransientsCrisp;
92  //p->rboptions |= RubberBandOptionWindowStandard;
93  //p->rboptions |= RubberBandOptionSmoothingOff;
94  //p->rboptions |= RubberBandOptionFormantShifted;
95  //p->rboptions |= RubberBandOptionPitchHighConsistency;
96  p->rb = rubberband_new(samplerate, 1, p->rboptions, 1., p->pitchscale);
97  rubberband_set_max_process_size(p->rb, p->hopsize);
98  //rubberband_set_debug_level(p->rb, 10);
99
100#if 1
101  // warm up rubber band
102  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
103  int available = rubberband_available(p->rb);
104  fvec_t *zeros = new_fvec(p->hopsize);
105  while (available <= (int)latency) {
106    rubberband_process(p->rb, (const float* const*)&(zeros->data), p->hopsize, 0);
107    available = rubberband_available(p->rb);
108  }
109  del_fvec(zeros);
110#endif
111
112  return p;
113
114beach:
115  del_aubio_pitchshift(p);
116  return NULL;
117}
118
119void
120del_aubio_pitchshift (aubio_pitchshift_t * p)
121{
122  if (p->rb) {
123    rubberband_delete(p->rb);
124  }
125  AUBIO_FREE (p);
126}
127
128uint_t aubio_pitchshift_get_latency (aubio_pitchshift_t * p) {
129  return rubberband_get_latency(p->rb);
130}
131
132uint_t
133aubio_pitchshift_set_pitchscale (aubio_pitchshift_t * p, smpl_t pitchscale)
134{
135  if (pitchscale >= 0.0625  && pitchscale <= 4.) {
136    p->pitchscale = pitchscale;
137    rubberband_set_pitch_scale(p->rb, p->pitchscale);
138    return AUBIO_OK;
139  } else {
140    AUBIO_ERR("pitchshift: could not set pitchscale to %.2f\n", pitchscale);
141    return AUBIO_FAIL;
142  }
143}
144
145smpl_t
146aubio_pitchshift_get_pitchscale (aubio_pitchshift_t * p)
147{
148  return p->pitchscale;
149}
150
151uint_t
152aubio_pitchshift_set_transpose(aubio_pitchshift_t * p, smpl_t transpose)
153{
154  if (transpose >= -24. && transpose <= 24.) {
155    smpl_t pitchscale = POW(2., transpose / 12.);
156    return aubio_pitchshift_set_pitchscale(p, pitchscale);
157  } else {
158    AUBIO_ERR("pitchshift: could not set transpose to %.2f\n", transpose);
159    return AUBIO_FAIL;
160  }
161}
162
163smpl_t
164aubio_pitchshift_get_transpose(aubio_pitchshift_t * p)
165{
166  return 12. * LOG(p->pitchscale) / LOG(2.0);
167}
168
169void
170aubio_pitchshift_do (aubio_pitchshift_t * p, const fvec_t * in, fvec_t * out)
171{
172  int output = 0;
173  rubberband_process(p->rb, (const float* const*)&(in->data), p->hopsize, output);
174  if (rubberband_available(p->rb) >= (int)p->hopsize) {
175    rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize);
176  } else {
177    AUBIO_WRN("pitchshift: catching up with zeros, only %d available, needed: %d, "
178        "current pitchscale: %f\n",
179        rubberband_available(p->rb), p->hopsize, p->pitchscale);
180    fvec_zeros(out);
181  }
182}
183
184#else
185
186// TODO fallback pitch shifting implementation
187
188struct _aubio_pitchshift_t
189{
190  void *dummy;
191};
192
193void aubio_pitchshift_do (aubio_pitchshift_t * o UNUSED, const fvec_t * in UNUSED,
194    fvec_t * out UNUSED) {
195}
196
197void del_aubio_pitchshift (aubio_pitchshift_t * o UNUSED) {
198}
199
200aubio_pitchshift_t *new_aubio_pitchshift (const char_t * method UNUSED,
201    smpl_t pitchscale UNUSED, uint_t hop_size UNUSED, uint_t samplerate UNUSED)
202{
203  AUBIO_ERR ("aubio was not compiled with rubberband\n");
204  return NULL;
205}
206
207uint_t aubio_pitchshift_set_pitchscale (aubio_pitchshift_t * o UNUSED, smpl_t pitchscale UNUSED)
208{
209  return AUBIO_FAIL;
210}
211
212smpl_t aubio_pitchshift_get_pitchscale (aubio_pitchshift_t * o UNUSED)
213{
214  return 1.;
215}
216
217uint_t aubio_pitchshift_set_transpose (aubio_pitchshift_t * o UNUSED, smpl_t transpose UNUSED) {
218  return AUBIO_FAIL;
219}
220
221smpl_t aubio_pitchshift_get_transpose (aubio_pitchshift_t * o UNUSED) {
222  return 0.;
223}
224
225uint_t aubio_pitchshift_get_latency (aubio_pitchshift_t * o UNUSED) {
226  return 0.;
227}
228
229// end of dummy implementation
230
231#endif /* HAVE_RUBBERBAND */
Note: See TracBrowser for help on using the repository browser.