source: src/effects/pitchshift.c @ 6e8aa74

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

src/effects/pitchshift.c: avoid signed/unsigned comparison

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