source: src/effects/pitchshift_rubberband.c @ 218f670

sampler
Last change on this file since 218f670 was ea3a113, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/effects/: include aubio_priv.h, not config.h, use <> for external includes

  • Property mode set to 100644
File size: 4.1 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 "aubio_priv.h"
22
23#ifdef HAVE_RUBBERBAND
24
25#include "fvec.h"
26#include "effects/pitchshift.h"
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 pitchscale;              /**< pitch scale */
36
37  RubberBandState rb;
38  RubberBandOptions rboptions;
39};
40
41extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode);
42
43aubio_pitchshift_t *
44new_aubio_pitchshift (const char_t * mode,
45    smpl_t transpose, uint_t hopsize, uint_t samplerate)
46{
47  aubio_pitchshift_t *p = AUBIO_NEW (aubio_pitchshift_t);
48  p->samplerate = samplerate;
49  p->hopsize = hopsize;
50  p->pitchscale = 1.;
51  if (transpose >= -24. && transpose <= 24.) {
52    p->pitchscale = POW(2., transpose / 12.);
53  } else {
54    AUBIO_ERR("pitchshift: transpose should be in the range [-24, 24], got %f\n", transpose);
55    goto beach;
56  }
57
58  p->rboptions = aubio_get_rubberband_opts(mode);
59  if (p->rboptions < 0) {
60    AUBIO_ERR("pitchshift: unknown pitch shifting method %s\n", mode);
61    goto beach;
62  }
63  //AUBIO_MSG("pitchshift: using pitch shifting method %s\n", mode);
64
65  p->rb = rubberband_new(samplerate, 1, p->rboptions, 1., p->pitchscale);
66  rubberband_set_max_process_size(p->rb, p->hopsize);
67  //rubberband_set_debug_level(p->rb, 10);
68
69#if 1
70  // warm up rubber band
71  unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb));
72  int available = rubberband_available(p->rb);
73  fvec_t *zeros = new_fvec(p->hopsize);
74  while (available <= (int)latency) {
75    rubberband_process(p->rb, (const float* const*)&(zeros->data), p->hopsize, 0);
76    available = rubberband_available(p->rb);
77  }
78  del_fvec(zeros);
79#endif
80
81  return p;
82
83beach:
84  del_aubio_pitchshift(p);
85  return NULL;
86}
87
88void
89del_aubio_pitchshift (aubio_pitchshift_t * p)
90{
91  if (p->rb) {
92    rubberband_delete(p->rb);
93  }
94  AUBIO_FREE (p);
95}
96
97uint_t aubio_pitchshift_get_latency (aubio_pitchshift_t * p) {
98  return rubberband_get_latency(p->rb);
99}
100
101uint_t
102aubio_pitchshift_set_pitchscale (aubio_pitchshift_t * p, smpl_t pitchscale)
103{
104  if (pitchscale >= 0.25  && pitchscale <= 4.) {
105    p->pitchscale = pitchscale;
106    rubberband_set_pitch_scale(p->rb, p->pitchscale);
107    return AUBIO_OK;
108  } else {
109    AUBIO_ERR("pitchshift: could not set pitchscale to %.2f\n", pitchscale);
110    return AUBIO_FAIL;
111  }
112}
113
114smpl_t
115aubio_pitchshift_get_pitchscale (aubio_pitchshift_t * p)
116{
117  return p->pitchscale;
118}
119
120uint_t
121aubio_pitchshift_set_transpose(aubio_pitchshift_t * p, smpl_t transpose)
122{
123  if (transpose >= -24. && transpose <= 24.) {
124    smpl_t pitchscale = POW(2., transpose / 12.);
125    return aubio_pitchshift_set_pitchscale(p, pitchscale);
126  } else {
127    AUBIO_ERR("pitchshift: could not set transpose to %.2f\n", transpose);
128    return AUBIO_FAIL;
129  }
130}
131
132smpl_t
133aubio_pitchshift_get_transpose(aubio_pitchshift_t * p)
134{
135  return 12. * LOG(p->pitchscale) / LOG(2.0);
136}
137
138void
139aubio_pitchshift_do (aubio_pitchshift_t * p, const fvec_t * in, fvec_t * out)
140{
141  int output = 0;
142  rubberband_process(p->rb, (const float* const*)&(in->data), p->hopsize, output);
143  if (rubberband_available(p->rb) >= (int)p->hopsize) {
144    rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize);
145  } else {
146    AUBIO_WRN("pitchshift: catching up with zeros, only %d available, needed: %d, "
147        "current pitchscale: %f\n",
148        rubberband_available(p->rb), p->hopsize, p->pitchscale);
149    fvec_zeros(out);
150  }
151}
152
153#endif
Note: See TracBrowser for help on using the repository browser.