[af195a5] | 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 | |
---|
| 23 | #ifdef HAVE_RUBBERBAND |
---|
| 24 | |
---|
| 25 | #include "aubio_priv.h" |
---|
| 26 | #include "fvec.h" |
---|
| 27 | #include "fmat.h" |
---|
| 28 | #include "io/source.h" |
---|
| 29 | #include "effects/timestretch.h" |
---|
| 30 | |
---|
| 31 | #include "rubberband/rubberband-c.h" |
---|
| 32 | |
---|
| 33 | #define MIN_STRETCH_RATIO 0.025 |
---|
[fb5bd55] | 34 | #define MAX_STRETCH_RATIO 40. |
---|
[af195a5] | 35 | |
---|
| 36 | /** generic time stretching structure */ |
---|
| 37 | struct _aubio_timestretch_t |
---|
| 38 | { |
---|
| 39 | uint_t samplerate; /**< samplerate */ |
---|
| 40 | uint_t hopsize; /**< hop size */ |
---|
| 41 | smpl_t stretchratio; /**< time ratio */ |
---|
| 42 | smpl_t pitchscale; /**< pitch scale */ |
---|
| 43 | |
---|
| 44 | aubio_source_t *source; |
---|
[30efdd0] | 45 | uint_t source_hopsize; /**< hop size at which the source is read */ |
---|
[af195a5] | 46 | fvec_t *in; |
---|
| 47 | uint_t eof; |
---|
| 48 | |
---|
| 49 | RubberBandState rb; |
---|
| 50 | RubberBandOptions rboptions; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | extern RubberBandOptions aubio_get_rubberband_opts(const char_t *mode); |
---|
| 54 | |
---|
[1e4b7f6] | 55 | static void aubio_timestretch_warmup (aubio_timestretch_t * p); |
---|
[30efdd0] | 56 | static sint_t aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t fetch); |
---|
[1e4b7f6] | 57 | |
---|
[af195a5] | 58 | aubio_timestretch_t * |
---|
| 59 | new_aubio_timestretch (const char_t * uri, const char_t * mode, |
---|
| 60 | smpl_t stretchratio, uint_t hopsize, uint_t samplerate) |
---|
| 61 | { |
---|
| 62 | aubio_timestretch_t *p = AUBIO_NEW (aubio_timestretch_t); |
---|
| 63 | p->samplerate = samplerate; |
---|
| 64 | p->hopsize = hopsize; |
---|
[30efdd0] | 65 | p->source_hopsize = hopsize; |
---|
[af195a5] | 66 | p->pitchscale = 1.; |
---|
| 67 | p->eof = 0; |
---|
| 68 | |
---|
[30efdd0] | 69 | p->source = new_aubio_source(uri, samplerate, p->source_hopsize); |
---|
[af195a5] | 70 | if (!p->source) goto beach; |
---|
| 71 | if (samplerate == 0 ) p->samplerate = aubio_source_get_samplerate(p->source); |
---|
| 72 | |
---|
[30efdd0] | 73 | p->in = new_fvec(p->source_hopsize); |
---|
[af195a5] | 74 | |
---|
| 75 | if (stretchratio <= MAX_STRETCH_RATIO && stretchratio >= MIN_STRETCH_RATIO) { |
---|
| 76 | p->stretchratio = stretchratio; |
---|
| 77 | } else { |
---|
| 78 | AUBIO_ERR("timestretch: stretchratio should be in the range [%.3f, %.3f], got %f\n", |
---|
| 79 | MIN_STRETCH_RATIO, MAX_STRETCH_RATIO, stretchratio); |
---|
| 80 | goto beach; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | p->rboptions = aubio_get_rubberband_opts(mode); |
---|
| 84 | if (p->rboptions < 0) { |
---|
| 85 | AUBIO_ERR("timestretch: unknown time stretching method %s\n", mode); |
---|
| 86 | goto beach; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | p->rb = rubberband_new(p->samplerate, 1, p->rboptions, p->stretchratio, p->pitchscale); |
---|
[30efdd0] | 90 | rubberband_set_max_process_size(p->rb, p->source_hopsize); |
---|
[af195a5] | 91 | //rubberband_set_debug_level(p->rb, 10); |
---|
| 92 | |
---|
[1e4b7f6] | 93 | aubio_timestretch_warmup(p); |
---|
| 94 | |
---|
| 95 | return p; |
---|
| 96 | |
---|
| 97 | beach: |
---|
| 98 | del_aubio_timestretch(p); |
---|
| 99 | return NULL; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | static void |
---|
| 103 | aubio_timestretch_warmup (aubio_timestretch_t * p) |
---|
| 104 | { |
---|
[af195a5] | 105 | // warm up rubber band |
---|
| 106 | unsigned int latency = MAX(p->hopsize, rubberband_get_latency(p->rb)); |
---|
[30efdd0] | 107 | aubio_timestretch_fetch(p, latency); |
---|
[af195a5] | 108 | } |
---|
| 109 | |
---|
| 110 | void |
---|
| 111 | del_aubio_timestretch (aubio_timestretch_t * p) |
---|
| 112 | { |
---|
[b4ad853] | 113 | if (p->in) del_fvec(p->in); |
---|
[af195a5] | 114 | if (p->source) del_aubio_source(p->source); |
---|
| 115 | if (p->rb) { |
---|
| 116 | rubberband_delete(p->rb); |
---|
| 117 | } |
---|
| 118 | AUBIO_FREE (p); |
---|
| 119 | } |
---|
| 120 | |
---|
[16965d5] | 121 | uint_t |
---|
| 122 | aubio_timestretch_get_samplerate (aubio_timestretch_t * p) |
---|
| 123 | { |
---|
| 124 | return p->samplerate; |
---|
| 125 | } |
---|
| 126 | |
---|
[af195a5] | 127 | uint_t aubio_timestretch_get_latency (aubio_timestretch_t * p) { |
---|
| 128 | return rubberband_get_latency(p->rb); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | uint_t |
---|
| 132 | aubio_timestretch_set_stretch (aubio_timestretch_t * p, smpl_t stretch) |
---|
| 133 | { |
---|
| 134 | if (stretch >= MIN_STRETCH_RATIO && stretch <= MAX_STRETCH_RATIO) { |
---|
| 135 | p->stretchratio = stretch; |
---|
[fb5bd55] | 136 | rubberband_set_time_ratio(p->rb, 1./p->stretchratio); |
---|
[af195a5] | 137 | return AUBIO_OK; |
---|
| 138 | } else { |
---|
| 139 | AUBIO_ERR("timestretch: could not set stretch ratio to %.2f\n", stretch); |
---|
| 140 | return AUBIO_FAIL; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | smpl_t |
---|
| 145 | aubio_timestretch_get_stretch (aubio_timestretch_t * p) |
---|
| 146 | { |
---|
| 147 | return p->stretchratio; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | uint_t |
---|
| 151 | aubio_timestretch_set_pitchscale (aubio_timestretch_t * p, smpl_t pitchscale) |
---|
| 152 | { |
---|
| 153 | if (pitchscale >= 0.0625 && pitchscale <= 4.) { |
---|
| 154 | p->pitchscale = pitchscale; |
---|
| 155 | rubberband_set_pitch_scale(p->rb, p->pitchscale); |
---|
| 156 | return AUBIO_OK; |
---|
| 157 | } else { |
---|
| 158 | AUBIO_ERR("timestretch: could not set pitchscale to %.2f\n", pitchscale); |
---|
| 159 | return AUBIO_FAIL; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | smpl_t |
---|
| 164 | aubio_timestretch_get_pitchscale (aubio_timestretch_t * p) |
---|
| 165 | { |
---|
| 166 | return p->pitchscale; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | uint_t |
---|
| 170 | aubio_timestretch_set_transpose(aubio_timestretch_t * p, smpl_t transpose) |
---|
| 171 | { |
---|
| 172 | if (transpose >= -24. && transpose <= 24.) { |
---|
| 173 | smpl_t pitchscale = POW(2., transpose / 12.); |
---|
| 174 | return aubio_timestretch_set_pitchscale(p, pitchscale); |
---|
| 175 | } else { |
---|
| 176 | AUBIO_ERR("timestretch: could not set transpose to %.2f\n", transpose); |
---|
| 177 | return AUBIO_FAIL; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | smpl_t |
---|
| 182 | aubio_timestretch_get_transpose(aubio_timestretch_t * p) |
---|
| 183 | { |
---|
| 184 | return 12. * LOG(p->pitchscale) / LOG(2.0); |
---|
| 185 | } |
---|
| 186 | |
---|
[1e4b7f6] | 187 | sint_t |
---|
[30efdd0] | 188 | aubio_timestretch_fetch(aubio_timestretch_t *p, uint_t length) |
---|
[af195a5] | 189 | { |
---|
[30efdd0] | 190 | uint_t source_read = p->source_hopsize; |
---|
[af195a5] | 191 | // read more samples from source until we have enough available or eof is reached |
---|
| 192 | int available = rubberband_available(p->rb); |
---|
[30efdd0] | 193 | while ((available < (int)length) && (p->eof == 0)) { |
---|
[af195a5] | 194 | aubio_source_do(p->source, p->in, &source_read); |
---|
[30efdd0] | 195 | if (source_read < p->source_hopsize) { |
---|
[af195a5] | 196 | p->eof = 1; |
---|
| 197 | } |
---|
| 198 | rubberband_process(p->rb, (const float* const*)&(p->in->data), source_read, p->eof); |
---|
| 199 | available = rubberband_available(p->rb); |
---|
| 200 | } |
---|
[30efdd0] | 201 | return available; |
---|
[1e4b7f6] | 202 | } |
---|
| 203 | |
---|
| 204 | void |
---|
| 205 | aubio_timestretch_do (aubio_timestretch_t * p, fvec_t * out, uint_t * read) |
---|
| 206 | { |
---|
[30efdd0] | 207 | int available = aubio_timestretch_fetch(p, p->hopsize); |
---|
[af195a5] | 208 | // now retrieve the samples and write them into out->data |
---|
| 209 | if (available >= (int)p->hopsize) { |
---|
| 210 | rubberband_retrieve(p->rb, (float* const*)&(out->data), p->hopsize); |
---|
| 211 | *read = p->hopsize; |
---|
| 212 | } else { |
---|
| 213 | rubberband_retrieve(p->rb, (float* const*)&(out->data), available); |
---|
| 214 | *read = available; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
[0d4228d] | 218 | uint_t |
---|
| 219 | aubio_timestretch_seek (aubio_timestretch_t *p, uint_t pos) |
---|
| 220 | { |
---|
| 221 | p->eof = 0; |
---|
| 222 | rubberband_reset(p->rb); |
---|
| 223 | return aubio_source_seek(p->source, pos); |
---|
| 224 | } |
---|
| 225 | |
---|
[af195a5] | 226 | #endif |
---|