source: src/effects/pitchshift.c @ 29e8f52

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

src/effects/pitchshift.c: clean-up options, set to default

  • Property mode set to 100644
File size: 5.3 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/** generic pitch shifting structure */
31struct _aubio_pitchshift_t
32{
33  uint_t samplerate;              /**< samplerate */
34  uint_t hopsize;                 /**< hop size */
35  smpl_t timeratio;               /**< time ratio */
36  smpl_t pitchscale;              /**< pitch scale */
37
38  RubberBandState rb;
39  RubberBandOptions rboptions;
40};
41
42aubio_pitchshift_t *
43new_aubio_pitchshift (const char_t * mode,
44    smpl_t pitchscale, uint_t hopsize, uint_t samplerate)
45{
46  aubio_pitchshift_t *p = AUBIO_NEW (aubio_pitchshift_t);
47  int available = 0; unsigned int latency = 0;
48  p->samplerate = samplerate;
49  if (strcmp (mode, "default") != 0) {
50    AUBIO_ERR ("unknown pitch shifting method %s\n", mode);
51    goto beach;
52  }
53  //p->mode = pitch_type;
54  p->hopsize = hopsize;
55  p->timeratio = 1.;
56  p->pitchscale = pitchscale;
57
58  p->rboptions = RubberBandOptionProcessRealTime;
59  //p->rboptions |= RubberBandOptionTransientsCrisp;
60  //p->rboptions |= RubberBandOptionWindowStandard;
61  //p->rboptions |= RubberBandOptionSmoothingOff;
62  //p->rboptions |= RubberBandOptionFormantShifted;
63  //p->rboptions |= RubberBandOptionPitchHighConsistency;
64  p->rb = rubberband_new(samplerate, 1, p->rboptions, p->timeratio, p->pitchscale);
65  rubberband_set_max_process_size(p->rb, p->hopsize * 4);
66  //rubberband_set_debug_level(p->rb, 10);
67
68  latency = MAX(rubberband_get_latency(p->rb), p->hopsize);
69
70  // warm up
71  fvec_t *zeros = new_fvec(p->hopsize);
72  while (available <= (int)latency) {
73    rubberband_process(p->rb, (const float* const*)&(zeros->data), p->hopsize, 0);
74    available = rubberband_available(p->rb);
75#if 0
76    int samples_required = rubberband_get_samples_required(p->rb);
77    AUBIO_DBG("pitchshift: warmup "
78        "samples_required: %d, available: %d, hopsize: %d, latency: %d\n",
79        samples_required, available, p->hopsize, latency);
80#endif
81  }
82  del_fvec(zeros);
83
84  return p;
85
86beach:
87  del_aubio_pitchshift(p);
88  return NULL;
89}
90
91void
92del_aubio_pitchshift (aubio_pitchshift_t * p)
93{
94  if (p->rb) {
95    rubberband_delete(p->rb);
96  }
97  AUBIO_FREE (p);
98}
99
100uint_t
101aubio_pitchshift_set_pitchscale (aubio_pitchshift_t * p, smpl_t pitchscale)
102{
103  if (pitchscale >= 0.0625  && pitchscale <= 4.) {
104    p->pitchscale = pitchscale;
105    rubberband_set_pitch_scale(p->rb, p->pitchscale);
106    return AUBIO_OK;
107  } else {
108    AUBIO_ERR("pitchshift: could not set pitchscale to %.2f\n", pitchscale);
109    return AUBIO_FAIL;
110  }
111}
112
113smpl_t
114aubio_pitchshift_get_pitchscale (aubio_pitchshift_t * p)
115{
116  return p->pitchscale;
117}
118
119uint_t
120aubio_pitchshift_set_transpose(aubio_pitchshift_t * p, smpl_t transpose)
121{
122  if (transpose >= -24. && transpose <= 24.) {
123    smpl_t pitchscale = POW(2., transpose / 12.);
124    return aubio_pitchshift_set_pitchscale(p, pitchscale);
125  } else {
126    AUBIO_ERR("pitchshift: could not set transpose to %.2f\n", transpose);
127    return AUBIO_FAIL;
128  }
129}
130
131smpl_t
132aubio_pitchshift_get_transpose(aubio_pitchshift_t * p)
133{
134  return 12. * LOG(p->pitchscale) / LOG(2.0);
135}
136
137void
138aubio_pitchshift_do (aubio_pitchshift_t * p, const fvec_t * in, fvec_t * out)
139{
140  int output = 0;
141  // this may occur when RubberBandStretcher initialPitchScale is changed
142  while (rubberband_available(p->rb) <= (int)p->hopsize) {
143    //AUBIO_WRN("pitchshift: catching up, only %d available\n", rubberband_available(p->rb));
144    rubberband_process(p->rb, (const float* const*)&(in->data), 0, output);
145  }
146  rubberband_process(p->rb, (const float* const*)&(in->data), p->hopsize, output);
147  rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize);
148}
149
150#else
151
152// TODO fallback pitch shifting implementation
153
154struct _aubio_pitchshift_t
155{
156  void *dummy;
157};
158
159void aubio_pitchshift_do (aubio_pitchshift_t * o UNUSED, const fvec_t * in UNUSED,
160    fvec_t * out UNUSED) {
161}
162
163void del_aubio_pitchshift (aubio_pitchshift_t * o UNUSED) {
164}
165
166aubio_pitchshift_t *new_aubio_pitchshift (const char_t * method UNUSED,
167    smpl_t pitchscale UNUSED, uint_t hop_size UNUSED, uint_t samplerate UNUSED)
168{
169  AUBIO_ERR ("aubio was not compiled with rubberband\n");
170  return NULL;
171}
172
173uint_t aubio_pitchshift_set_pitchscale (aubio_pitchshift_t * o UNUSED, smpl_t pitchscale UNUSED)
174{
175  return AUBIO_FAIL;
176}
177
178smpl_t aubio_pitchshift_get_pitchscale (aubio_pitchshift_t * o UNUSED)
179{
180  return 1.;
181}
182
183uint_t aubio_pitchshift_set_transpose (aubio_pitchshift_t * o UNUSED, smpl_t transpose UNUSED) {
184  return AUBIO_FAIL;
185}
186
187smpl_t aubio_pitchshift_get_transpose (aubio_pitchshift_t * o UNUSED) {
188  return 0.;
189}
190
191// end of dummy implementation
192
193#endif /* HAVE_RUBBERBAND */
Note: See TracBrowser for help on using the repository browser.