[96fb8ad] | 1 | /* |
---|
| 2 | Copyright (C) 2003 Paul Brossier |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 17 | |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #include "aubio_priv.h" |
---|
| 21 | #include "sample.h" |
---|
| 22 | #include "fft.h" |
---|
| 23 | #include "mathutils.h" |
---|
| 24 | #include "phasevoc.h" |
---|
| 25 | |
---|
| 26 | /** phasevocoder internal object */ |
---|
| 27 | struct _aubio_pvoc_t { |
---|
| 28 | /** grain length */ |
---|
| 29 | uint_t win_s; |
---|
| 30 | /** overlap step */ |
---|
| 31 | uint_t hop_s; |
---|
| 32 | /** number of channels */ |
---|
| 33 | uint_t channels; |
---|
| 34 | /** spectral data */ |
---|
[f88a326] | 35 | aubio_mfft_t * fft; |
---|
[96fb8ad] | 36 | /**cur output grain [win_s] */ |
---|
| 37 | fvec_t * synth; |
---|
| 38 | /**last input frame [win_s-hop_s] */ |
---|
| 39 | fvec_t * synthold; |
---|
| 40 | /**current input grain [win_s] */ |
---|
| 41 | fvec_t * data; |
---|
| 42 | /**last input frame [win_s-hop_s] */ |
---|
| 43 | fvec_t * dataold; |
---|
| 44 | /** grain window [win_s] */ |
---|
| 45 | float * w; |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | /** returns data and dataold slided by hop_s */ |
---|
| 50 | static void aubio_pvoc_swapbuffers( |
---|
| 51 | smpl_t * data, |
---|
| 52 | smpl_t * dataold, |
---|
| 53 | const smpl_t * datanew, |
---|
| 54 | uint_t win_s, uint_t hop_s); |
---|
| 55 | /** do additive synthesis from 'old' and 'cur' */ |
---|
| 56 | static void aubio_pvoc_addsynth( |
---|
| 57 | const smpl_t * synth, |
---|
| 58 | smpl_t * synthold, |
---|
| 59 | smpl_t * synthnew, |
---|
| 60 | uint_t win_s, uint_t hop_s); |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) { |
---|
| 64 | uint_t i,j; |
---|
| 65 | for (i=0; i<pv->channels; i++) { |
---|
| 66 | /* slide */ |
---|
| 67 | aubio_pvoc_swapbuffers(pv->data->data[i],pv->dataold->data[i], |
---|
| 68 | datanew->data[i],pv->win_s,pv->hop_s); |
---|
| 69 | /* windowing */ |
---|
| 70 | for (j=0; j<pv->win_s; j++) pv->data->data[i][j] *= pv->w[j]; |
---|
[f88a326] | 71 | } |
---|
| 72 | /* shift */ |
---|
| 73 | vec_shift(pv->data); |
---|
| 74 | /* calculate fft */ |
---|
| 75 | aubio_mfft_do (pv->fft,pv->data,fftgrain); |
---|
[96fb8ad] | 76 | } |
---|
| 77 | |
---|
| 78 | void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) { |
---|
| 79 | uint_t i,j; |
---|
[f88a326] | 80 | /* calculate rfft */ |
---|
| 81 | aubio_mfft_rdo(pv->fft,fftgrain,pv->synth); |
---|
| 82 | /* unshift */ |
---|
| 83 | vec_shift(pv->synth); |
---|
[96fb8ad] | 84 | for (i=0; i<pv->channels; i++) { |
---|
| 85 | for (j=0; j<pv->win_s; j++) pv->synth->data[i][j] *= pv->w[j]; |
---|
| 86 | aubio_pvoc_addsynth(pv->synth->data[i],pv->synthold->data[i], |
---|
| 87 | synthnew->data[i],pv->win_s,pv->hop_s); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s, uint_t channels) { |
---|
| 92 | aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t); |
---|
| 93 | |
---|
| 94 | if (win_s < 2*hop_s) { |
---|
[f88a326] | 95 | AUBIO_ERR("Hop size bigger than half the window size!\n"); |
---|
| 96 | AUBIO_ERR("Resetting hop size to half the window size.\n"); |
---|
| 97 | hop_s = win_s / 2; |
---|
[96fb8ad] | 98 | } |
---|
| 99 | |
---|
| 100 | if (hop_s < 1) { |
---|
| 101 | AUBIO_ERR("Hop size is smaller than 1!\n"); |
---|
[f88a326] | 102 | AUBIO_ERR("Resetting hop size to half the window size.\n"); |
---|
| 103 | hop_s = win_s / 2; |
---|
[96fb8ad] | 104 | } |
---|
| 105 | |
---|
[f88a326] | 106 | pv->fft = new_aubio_mfft(win_s,channels); |
---|
[96fb8ad] | 107 | |
---|
| 108 | /* remember old */ |
---|
| 109 | pv->data = new_fvec (win_s, channels); |
---|
| 110 | pv->synth = new_fvec (win_s, channels); |
---|
| 111 | |
---|
| 112 | /* new input output */ |
---|
| 113 | pv->dataold = new_fvec (win_s-hop_s, channels); |
---|
| 114 | pv->synthold = new_fvec (win_s-hop_s, channels); |
---|
| 115 | pv->w = AUBIO_ARRAY(smpl_t,win_s); |
---|
[f88a326] | 116 | window(pv->w,win_s,hanningz); |
---|
[96fb8ad] | 117 | |
---|
| 118 | pv->channels = channels; |
---|
| 119 | pv->hop_s = hop_s; |
---|
| 120 | pv->win_s = win_s; |
---|
| 121 | |
---|
| 122 | return pv; |
---|
| 123 | } |
---|
| 124 | |
---|
[f88a326] | 125 | void del_aubio_pvoc(aubio_pvoc_t *pv) { |
---|
[96fb8ad] | 126 | del_fvec(pv->data); |
---|
| 127 | del_fvec(pv->synth); |
---|
| 128 | del_fvec(pv->dataold); |
---|
| 129 | del_fvec(pv->synthold); |
---|
| 130 | AUBIO_FREE(pv->w); |
---|
| 131 | AUBIO_FREE(pv); |
---|
| 132 | } |
---|
| 133 | |
---|
[f88a326] | 134 | static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, |
---|
| 135 | const smpl_t * datanew, uint_t win_s, uint_t hop_s) |
---|
[96fb8ad] | 136 | { |
---|
| 137 | uint_t i; |
---|
| 138 | for (i=0;i<win_s-hop_s;i++) |
---|
| 139 | data[i] = dataold[i]; |
---|
| 140 | for (i=0;i<hop_s;i++) |
---|
| 141 | data[win_s-hop_s+i] = datanew[i]; |
---|
| 142 | for (i=0;i<win_s-hop_s;i++) |
---|
| 143 | dataold[i] = data[i+hop_s]; |
---|
| 144 | } |
---|
| 145 | |
---|
[f88a326] | 146 | static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, |
---|
| 147 | smpl_t * synthnew, uint_t win_s, uint_t hop_s) |
---|
[96fb8ad] | 148 | { |
---|
| 149 | uint_t i; |
---|
| 150 | smpl_t scale = 2*hop_s/(win_s+.0); |
---|
| 151 | /* add new synth to old one and put result in synthnew */ |
---|
| 152 | for (i=0;i<hop_s;i++) |
---|
| 153 | synthnew[i] = synthold[i]+synth[i]*scale; |
---|
| 154 | /* shift synthold */ |
---|
| 155 | for (i=0;i<win_s-2*hop_s;i++) |
---|
| 156 | synthold[i] = synthold[i+hop_s]; |
---|
| 157 | /* erase last frame in synthold */ |
---|
| 158 | for (i=win_s-hop_s;i<win_s;i++) |
---|
| 159 | synthold[i-hop_s]=0.; |
---|
| 160 | /* additive synth */ |
---|
| 161 | for (i=0;i<win_s-hop_s;i++) |
---|
| 162 | synthold[i] += synth[i+hop_s]*scale; |
---|
| 163 | } |
---|
| 164 | |
---|