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