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