source: src/effects/pitchshift_rubberband.c @ 78a564b

feature/cnnfeature/crepefeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 78a564b was 78a564b, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[pitchshift] do not include config.h directly

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