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