[c41637b] | 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 | |
---|
[799e05b] | 21 | #include "aubio_priv.h" |
---|
[c41637b] | 22 | |
---|
| 23 | #ifdef HAVE_RUBBERBAND |
---|
| 24 | |
---|
| 25 | #include "fvec.h" |
---|
| 26 | #include "fmat.h" |
---|
| 27 | #include "io/source.h" |
---|
| 28 | #include "effects/timestretch.h" |
---|
| 29 | |
---|
[799e05b] | 30 | #include <rubberband/rubberband-c.h> |
---|
[c41637b] | 31 | |
---|
| 32 | #define MIN_STRETCH_RATIO 0.025 |
---|
[85755eb] | 33 | #define MAX_STRETCH_RATIO 40. |
---|
[c41637b] | 34 | |
---|
[8856791] | 35 | #define HAVE_THREADS 1 |
---|
| 36 | #if 0 |
---|
| 37 | #undef HAVE_THREADS |
---|
| 38 | #endif |
---|
| 39 | |
---|
| 40 | #ifdef HAVE_THREADS |
---|
| 41 | #include <pthread.h> |
---|
| 42 | #endif |
---|
| 43 | |
---|
[c41637b] | 44 | /** generic time stretching structure */ |
---|
| 45 | struct _aubio_timestretch_t |
---|
| 46 | { |
---|
| 47 | uint_t samplerate; /**< samplerate */ |
---|
| 48 | uint_t hopsize; /**< hop size */ |
---|
| 49 | smpl_t stretchratio; /**< time ratio */ |
---|
| 50 | smpl_t pitchscale; /**< pitch scale */ |
---|
| 51 | |
---|
| 52 | RubberBandState rb; |
---|
| 53 | RubberBandOptions rboptions; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode); |
---|
| 57 | |
---|
[a874c48] | 58 | static void aubio_timestretch_warmup (aubio_timestretch_t * p); |
---|
| 59 | |
---|
[c41637b] | 60 | aubio_timestretch_t * |
---|
[284fe8a] | 61 | new_aubio_timestretch (const char_t * mode, smpl_t stretchratio, uint_t hopsize, |
---|
| 62 | uint_t samplerate) |
---|
[c41637b] | 63 | { |
---|
| 64 | aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t); |
---|
| 65 | p->hopsize = hopsize; |
---|
| 66 | p->pitchscale = 1.; |
---|
| 67 | |
---|
| 68 | if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) { |
---|
| 69 | p->stretchratio = stretchratio; |
---|
| 70 | } else { |
---|
| 71 | AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n", |
---|
| 72 | MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio); |
---|
| 73 | goto beach; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | p->rboptions = aubio_get_rubberband_opts(mode); |
---|
| 77 | if (p->rboptions < 0) { |
---|
| 78 | AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode); |
---|
| 79 | goto beach; |
---|
| 80 | } |
---|
| 81 | |
---|
[284fe8a] | 82 | p->rb = rubberband_new(samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale); |
---|
| 83 | if (!p->rb) goto beach; |
---|
| 84 | |
---|
| 85 | p->samplerate = samplerate; |
---|
| 86 | |
---|
| 87 | //aubio_timestretch_warmup(p); |
---|
[a874c48] | 88 | |
---|
| 89 | return p; |
---|
| 90 | |
---|
| 91 | beach: |
---|
| 92 | del_aubio_timestretch(p); |
---|
| 93 | return NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static void |
---|
| 97 | aubio_timestretch_warmup (aubio_timestretch_t * p) |
---|
| 98 | { |
---|
[c41637b] | 99 | // warm up rubber band |
---|
[4559863] | 100 | //AUBIO_WRN("timestretch: warming-up\n"); |
---|
[c41637b] | 101 | unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb)); |
---|
[284fe8a] | 102 | fvec_t *input = new_fvec(p->hopsize); |
---|
| 103 | while (aubio_timestretch_push(p, input, input->length) < (int)latency) { |
---|
| 104 | //sint_t available = aubio_timestretch_get_available(p); |
---|
| 105 | //AUBIO_WRN("timestretch: warmup got %d, latency: %d\n", available, latency); |
---|
| 106 | } |
---|
| 107 | del_fvec(input); |
---|
[c41637b] | 108 | } |
---|
| 109 | |
---|
| 110 | void |
---|
| 111 | del_aubio_timestretch (aubio_timestretch_t * p) |
---|
| 112 | { |
---|
| 113 | if (p->rb) { |
---|
| 114 | rubberband_delete(p->rb); |
---|
| 115 | } |
---|
| 116 | AUBIO_FREE (p); |
---|
| 117 | } |
---|
| 118 | |
---|
[fd99f0d] | 119 | uint_t |
---|
| 120 | aubio_timestretch_get_samplerate (aubio_timestretch_t * p) |
---|
| 121 | { |
---|
| 122 | return p->samplerate; |
---|
| 123 | } |
---|
| 124 | |
---|
[c41637b] | 125 | uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) { |
---|
| 126 | return rubberband_get_latency(p->rb); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | uint_t |
---|
| 130 | aubio_timestretch_set_stretch (aubio_timestretch_t * p, smpl_t stretch) |
---|
| 131 | { |
---|
[4559863] | 132 | if (!p->rb) { |
---|
[a77f0c6] | 133 | AUBIO_ERR("timestretch: could not set stretch ratio," |
---|
| 134 | " rubberband not created\n"); |
---|
[4559863] | 135 | return AUBIO_FAIL; |
---|
| 136 | } |
---|
[c41637b] | 137 | if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) { |
---|
| 138 | p->stretchratio = stretch; |
---|
[85755eb] | 139 | rubberband_set_time_ratio(p->rb, 1./p->stretchratio); |
---|
[c41637b] | 140 | return AUBIO_OK; |
---|
| 141 | } else { |
---|
[a77f0c6] | 142 | AUBIO_ERR("timestretch: could not set stretch ratio to '%f'," |
---|
| 143 | " should be in the range [%.2f, %.2f].\n", stretch, |
---|
| 144 | MIN_STRETCH_RATIO, MAX_STRETCH_RATIO); |
---|
[c41637b] | 145 | return AUBIO_FAIL; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | smpl_t |
---|
| 150 | aubio_timestretch_get_stretch (aubio_timestretch_t * p) |
---|
| 151 | { |
---|
| 152 | return p->stretchratio; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | uint_t |
---|
| 156 | aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale) |
---|
| 157 | { |
---|
[4559863] | 158 | if (!p->rb) { |
---|
[a77f0c6] | 159 | AUBIO_ERR("timestretch: could not set pitch scale," |
---|
| 160 | " rubberband not created\n"); |
---|
[4559863] | 161 | return AUBIO_FAIL; |
---|
| 162 | } |
---|
[c41637b] | 163 | if (pitchscale >= 0.0625 && pitchscale <= 4.) { |
---|
| 164 | p->pitchscale = pitchscale; |
---|
| 165 | rubberband_set_pitch_scale(p->rb, p->pitchscale); |
---|
| 166 | return AUBIO_OK; |
---|
| 167 | } else { |
---|
[a77f0c6] | 168 | AUBIO_ERR("timestretch: could not set pitchscale to '%f'," |
---|
| 169 | " should be in the range [0.0625, 4.].\n", pitchscale); |
---|
[c41637b] | 170 | return AUBIO_FAIL; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | smpl_t |
---|
| 175 | aubio_timestretch_get_pitchscale (aubio_timestretch_t * p) |
---|
| 176 | { |
---|
| 177 | return p->pitchscale; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | uint_t |
---|
| 181 | aubio_timestretch_set_transpose(aubio_timestretch_t * p, smpl_t transpose) |
---|
| 182 | { |
---|
| 183 | if (transpose >= -24. && transpose <= 24.) { |
---|
| 184 | smpl_t pitchscale = POW(2., transpose / 12.); |
---|
| 185 | return aubio_timestretch_set_pitchscale(p, pitchscale); |
---|
| 186 | } else { |
---|
[a77f0c6] | 187 | AUBIO_ERR("timestretch: could not set transpose to '%f'," |
---|
| 188 | " should be in the range [-24; 24].\n", transpose); |
---|
[c41637b] | 189 | return AUBIO_FAIL; |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | smpl_t |
---|
| 194 | aubio_timestretch_get_transpose(aubio_timestretch_t * p) |
---|
| 195 | { |
---|
| 196 | return 12. * LOG(p->pitchscale) / LOG(2.0); |
---|
| 197 | } |
---|
| 198 | |
---|
[a874c48] | 199 | sint_t |
---|
[284fe8a] | 200 | aubio_timestretch_push(aubio_timestretch_t *p, fvec_t *input, uint_t length) |
---|
[c41637b] | 201 | { |
---|
[284fe8a] | 202 | // push new samples to rubberband, return available |
---|
| 203 | int available; |
---|
| 204 | int eof = (input->length != length) ? 1 : 0; |
---|
| 205 | rubberband_process(p->rb, (const float* const*)&(input->data), length, eof); |
---|
| 206 | available = rubberband_available(p->rb); |
---|
| 207 | //AUBIO_WRN("timestretch: processed %d, %d available, eof: %d\n", |
---|
| 208 | // length, available, eof); |
---|
[992ac88] | 209 | return available; |
---|
[a874c48] | 210 | } |
---|
| 211 | |
---|
[284fe8a] | 212 | sint_t |
---|
| 213 | aubio_timestretch_get_available(aubio_timestretch_t *p) { |
---|
| 214 | return rubberband_available(p->rb); |
---|
| 215 | } |
---|
| 216 | |
---|
[a874c48] | 217 | void |
---|
[284fe8a] | 218 | aubio_timestretch_do(aubio_timestretch_t * p, fvec_t * out, uint_t * read) |
---|
[a874c48] | 219 | { |
---|
[c41637b] | 220 | // now retrieve the samples and write them into out->data |
---|
[284fe8a] | 221 | int available = rubberband_available(p->rb); |
---|
| 222 | if (available >= (int)out->length) { |
---|
| 223 | rubberband_retrieve(p->rb, (float* const*)&(out->data), out->length); |
---|
| 224 | *read = out->length; |
---|
[9bd769a] | 225 | } else if (available > 0) { |
---|
[4559863] | 226 | // this occurs each time the end of file is reached |
---|
| 227 | //AUBIO_WRN("timestretch: short read\n"); |
---|
[c41637b] | 228 | rubberband_retrieve(p->rb, (float* const*)&(out->data), available); |
---|
[284fe8a] | 229 | fvec_t zeros; zeros.length = out->length - available; zeros.data = out->data + available; |
---|
| 230 | fvec_zeros(&zeros); |
---|
[c41637b] | 231 | *read = available; |
---|
[9bd769a] | 232 | } else { |
---|
[4559863] | 233 | // this may occur if the previous was a short read available == hopsize |
---|
[9bd769a] | 234 | fvec_zeros(out); |
---|
| 235 | *read = 0; |
---|
[c41637b] | 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
[a0a4d01] | 239 | uint_t |
---|
[284fe8a] | 240 | aubio_timestretch_reset(aubio_timestretch_t *p) |
---|
[a0a4d01] | 241 | { |
---|
[8856791] | 242 | uint_t err = AUBIO_OK; |
---|
[4559863] | 243 | if (p->rb) { |
---|
| 244 | rubberband_reset(p->rb); |
---|
| 245 | } |
---|
[8856791] | 246 | return err; |
---|
[a0a4d01] | 247 | } |
---|
| 248 | |
---|
[c41637b] | 249 | #endif |
---|