[96fb8ad] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[e6a78ea] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[e6a78ea] | 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. |
---|
[96fb8ad] | 10 | |
---|
[e6a78ea] | 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[8b2dc90] | 22 | #include "fvec.h" |
---|
| 23 | #include "cvec.h" |
---|
[96fb8ad] | 24 | #include "mathutils.h" |
---|
[32d6958] | 25 | #include "spectral/fft.h" |
---|
| 26 | #include "spectral/phasevoc.h" |
---|
[96fb8ad] | 27 | |
---|
| 28 | /** phasevocoder internal object */ |
---|
| 29 | struct _aubio_pvoc_t { |
---|
[685412e] | 30 | uint_t win_s; /** grain length */ |
---|
| 31 | uint_t hop_s; /** overlap step */ |
---|
[8b2dc90] | 32 | aubio_fft_t * fft; /** fft object */ |
---|
| 33 | fvec_t * synth; /** cur output grain [win_s] */ |
---|
| 34 | fvec_t * synthold; /** last input frame [win_s-hop_s] */ |
---|
| 35 | fvec_t * data; /** current input grain [win_s] */ |
---|
| 36 | fvec_t * dataold; /** last input frame [win_s-hop_s] */ |
---|
[d84d19e] | 37 | fvec_t * w; /** grain window [win_s] */ |
---|
[96fb8ad] | 38 | }; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /** returns data and dataold slided by hop_s */ |
---|
[685412e] | 42 | static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, const |
---|
| 43 | smpl_t * datanew, uint_t win_s, uint_t hop_s); |
---|
[96fb8ad] | 44 | |
---|
[685412e] | 45 | /** do additive synthesis from 'old' and 'cur' */ |
---|
| 46 | static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, |
---|
| 47 | smpl_t * synthnew, uint_t win_s, uint_t hop_s); |
---|
[96fb8ad] | 48 | |
---|
| 49 | void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) { |
---|
[d95ff38] | 50 | /* slide */ |
---|
| 51 | aubio_pvoc_swapbuffers(pv->data->data,pv->dataold->data, |
---|
| 52 | datanew->data,pv->win_s,pv->hop_s); |
---|
[bab0a12] | 53 | /* windowing */ |
---|
| 54 | fvec_weight(pv->data, pv->w); |
---|
[685412e] | 55 | /* shift */ |
---|
[5c4ec3c] | 56 | fvec_shift(pv->data); |
---|
[685412e] | 57 | /* calculate fft */ |
---|
[8b2dc90] | 58 | aubio_fft_do (pv->fft,pv->data,fftgrain); |
---|
[96fb8ad] | 59 | } |
---|
| 60 | |
---|
| 61 | void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) { |
---|
[685412e] | 62 | /* calculate rfft */ |
---|
[8b2dc90] | 63 | aubio_fft_rdo(pv->fft,fftgrain,pv->synth); |
---|
[685412e] | 64 | /* unshift */ |
---|
[5c4ec3c] | 65 | fvec_shift(pv->synth); |
---|
[d95ff38] | 66 | aubio_pvoc_addsynth(pv->synth->data,pv->synthold->data, |
---|
| 67 | synthnew->data,pv->win_s,pv->hop_s); |
---|
[96fb8ad] | 68 | } |
---|
| 69 | |
---|
[d95ff38] | 70 | aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s) { |
---|
[685412e] | 71 | aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t); |
---|
| 72 | |
---|
[cd7d733] | 73 | /* if (win_s < 2*hop_s) { |
---|
| 74 | AUBIO_WRN("Hop size bigger than half the window size!\n"); |
---|
| 75 | } */ |
---|
[685412e] | 76 | |
---|
| 77 | if (hop_s < 1) { |
---|
[5958d1e] | 78 | AUBIO_ERR("got hop_size %d, but can not be < 1\n", hop_s); |
---|
| 79 | goto beach; |
---|
| 80 | } else if (win_s < 1) { |
---|
| 81 | AUBIO_ERR("got buffer_size %d, but can not be < 2\n", win_s); |
---|
| 82 | goto beach; |
---|
| 83 | } else if (win_s < hop_s + 1) { |
---|
| 84 | AUBIO_ERR("hop size (%d) is larger than or equal to win size (%d)\n", win_s, hop_s); |
---|
| 85 | goto beach; |
---|
[685412e] | 86 | } |
---|
| 87 | |
---|
[d95ff38] | 88 | pv->fft = new_aubio_fft (win_s); |
---|
[685412e] | 89 | |
---|
| 90 | /* remember old */ |
---|
[d95ff38] | 91 | pv->data = new_fvec (win_s); |
---|
| 92 | pv->synth = new_fvec (win_s); |
---|
[685412e] | 93 | |
---|
| 94 | /* new input output */ |
---|
[d95ff38] | 95 | pv->dataold = new_fvec (win_s-hop_s); |
---|
| 96 | pv->synthold = new_fvec (win_s-hop_s); |
---|
[407bba9] | 97 | pv->w = new_aubio_window ("hanningz", win_s); |
---|
[685412e] | 98 | |
---|
| 99 | pv->hop_s = hop_s; |
---|
| 100 | pv->win_s = win_s; |
---|
| 101 | |
---|
| 102 | return pv; |
---|
[5958d1e] | 103 | |
---|
| 104 | beach: |
---|
| 105 | AUBIO_FREE (pv); |
---|
| 106 | return NULL; |
---|
[96fb8ad] | 107 | } |
---|
| 108 | |
---|
[f88a326] | 109 | void del_aubio_pvoc(aubio_pvoc_t *pv) { |
---|
[685412e] | 110 | del_fvec(pv->data); |
---|
| 111 | del_fvec(pv->synth); |
---|
| 112 | del_fvec(pv->dataold); |
---|
| 113 | del_fvec(pv->synthold); |
---|
[d84d19e] | 114 | del_fvec(pv->w); |
---|
[8b2dc90] | 115 | del_aubio_fft(pv->fft); |
---|
[685412e] | 116 | AUBIO_FREE(pv); |
---|
[96fb8ad] | 117 | } |
---|
| 118 | |
---|
[f88a326] | 119 | static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, |
---|
[685412e] | 120 | const smpl_t * datanew, uint_t win_s, uint_t hop_s) |
---|
[96fb8ad] | 121 | { |
---|
[4087d54] | 122 | #if !HAVE_MEMCPY_HACKS |
---|
[685412e] | 123 | uint_t i; |
---|
[9778d9a] | 124 | for (i = 0; i < win_s - hop_s; i++) |
---|
[685412e] | 125 | data[i] = dataold[i]; |
---|
[9778d9a] | 126 | for (i = 0; i < hop_s; i++) |
---|
| 127 | data[win_s - hop_s + i] = datanew[i]; |
---|
| 128 | for (i = 0; i < win_s - hop_s; i++) |
---|
| 129 | dataold[i] = data[i + hop_s]; |
---|
[4087d54] | 130 | #else |
---|
| 131 | memcpy(data, dataold, (win_s - hop_s) * sizeof(smpl_t)); |
---|
| 132 | data += win_s - hop_s; |
---|
| 133 | memcpy(data, datanew, hop_s * sizeof(smpl_t)); |
---|
| 134 | data -= win_s - hop_s; |
---|
| 135 | data += hop_s; |
---|
| 136 | memcpy(dataold, data, (win_s - hop_s) * sizeof(smpl_t)); |
---|
| 137 | #endif |
---|
[96fb8ad] | 138 | } |
---|
| 139 | |
---|
[f88a326] | 140 | static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, |
---|
| 141 | smpl_t * synthnew, uint_t win_s, uint_t hop_s) |
---|
[96fb8ad] | 142 | { |
---|
[685412e] | 143 | uint_t i; |
---|
[9778d9a] | 144 | smpl_t scale = 2 * hop_s / (win_s + .0); |
---|
[685412e] | 145 | /* add new synth to old one and put result in synthnew */ |
---|
[9778d9a] | 146 | for (i = 0; i < hop_s; i++) |
---|
| 147 | synthnew[i] = synthold[i] + synth[i] * scale; |
---|
[685412e] | 148 | /* shift synthold */ |
---|
[9778d9a] | 149 | for (i = 0; i < win_s - 2 * hop_s; i++) |
---|
| 150 | synthold[i] = synthold[i + hop_s]; |
---|
[685412e] | 151 | /* erase last frame in synthold */ |
---|
[9778d9a] | 152 | for (i = win_s - hop_s; i < win_s; i++) |
---|
| 153 | synthold[i - hop_s] = 0.; |
---|
[685412e] | 154 | /* additive synth */ |
---|
[9778d9a] | 155 | for (i = 0; i < win_s - hop_s; i++) |
---|
| 156 | synthold[i] += synth[i + hop_s] * scale; |
---|
[96fb8ad] | 157 | } |
---|
| 158 | |
---|