2 Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
24 #include "mathutils.h"
25 #include "spectral/fft.h"
26 #include "spectral/phasevoc.h"
28 /** phasevocoder internal object */
29 struct _aubio_pvoc_t {
30 uint_t win_s; /** grain length */
31 uint_t hop_s; /** overlap step */
32 aubio_fft_t * fft; /** fft object */
33 fvec_t * data; /** current input grain, [win_s] frames */
34 fvec_t * dataold; /** memory of past grain, [win_s-hop_s] frames */
35 fvec_t * synth; /** current output grain, [win_s] frames */
36 fvec_t * synthold; /** memory of past grain, [win_s-hop_s] frames */
37 fvec_t * w; /** grain window [win_s] */
38 uint_t start; /** where to start additive synthesis */
39 uint_t end; /** where to end it */
40 smpl_t scale; /** scaling factor for synthesis */
41 uint_t end_datasize; /** size of memory to end */
42 uint_t hop_datasize; /** size of memory to hop_s */
46 /** returns data and dataold slided by hop_s */
47 static void aubio_pvoc_swapbuffers(aubio_pvoc_t *pv, fvec_t *new);
49 /** do additive synthesis from 'old' and 'cur' */
50 static void aubio_pvoc_addsynth(aubio_pvoc_t *pv, fvec_t * synthnew);
52 void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) {
54 aubio_pvoc_swapbuffers(pv, datanew);
56 fvec_weight(pv->data, pv->w);
60 aubio_fft_do (pv->fft,pv->data,fftgrain);
63 void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) {
65 aubio_fft_rdo(pv->fft,fftgrain,pv->synth);
67 fvec_shift(pv->synth);
68 /* additive synthesis */
69 aubio_pvoc_addsynth(pv, synthnew);
72 aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s) {
73 aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t);
75 /* if (win_s < 2*hop_s) {
76 AUBIO_WRN("Hop size bigger than half the window size!\n");
79 if ((sint_t)hop_s < 1) {
80 AUBIO_ERR("got hop_size %d, but can not be < 1\n", hop_s);
82 } else if ((sint_t)win_s < 1) {
83 AUBIO_ERR("got buffer_size %d, but can not be < 1\n", win_s);
85 } else if (win_s < hop_s) {
86 AUBIO_ERR("hop size (%d) is larger than win size (%d)\n", win_s, hop_s);
90 pv->fft = new_aubio_fft (win_s);
93 pv->data = new_fvec (win_s);
94 pv->synth = new_fvec (win_s);
96 /* new input output */
98 pv->dataold = new_fvec (win_s-hop_s);
99 pv->synthold = new_fvec (win_s-hop_s);
101 pv->dataold = new_fvec (1);
102 pv->synthold = new_fvec (1);
104 pv->w = new_aubio_window ("hanningz", win_s);
109 /* more than 50% overlap, overlap anyway */
110 if (win_s < 2 * hop_s) pv->start = 0;
111 /* less than 50% overlap, reset latest grain trail */
112 else pv->start = win_s - hop_s - hop_s;
114 if (win_s > hop_s) pv->end = win_s - hop_s;
117 pv->end_datasize = pv->end * sizeof(smpl_t);
118 pv->hop_datasize = pv->hop_s * sizeof(smpl_t);
120 pv->scale = pv->hop_s * 2. / pv->win_s;
129 void del_aubio_pvoc(aubio_pvoc_t *pv) {
132 del_fvec(pv->dataold);
133 del_fvec(pv->synthold);
135 del_aubio_fft(pv->fft);
139 static void aubio_pvoc_swapbuffers(aubio_pvoc_t *pv, fvec_t *new)
141 /* some convenience pointers */
142 smpl_t * data = pv->data->data;
143 smpl_t * dataold = pv->dataold->data;
144 smpl_t * datanew = new->data;
145 #if !HAVE_MEMCPY_HACKS
147 for (i = 0; i < pv->end; i++)
148 data[i] = dataold[i];
149 for (i = 0; i < pv->hop_s; i++)
150 data[pv->end + i] = datanew[i];
151 for (i = 0; i < pv->end; i++)
152 dataold[i] = data[i + pv->hop_s];
154 memcpy(data, dataold, pv->end_datasize);
156 memcpy(data, datanew, pv->hop_datasize);
159 memcpy(dataold, data, pv->end_datasize);
163 static void aubio_pvoc_addsynth(aubio_pvoc_t *pv, fvec_t *synth_new)
166 /* some convenience pointers */
167 smpl_t * synth = pv->synth->data;
168 smpl_t * synthold = pv->synthold->data;
169 smpl_t * synthnew = synth_new->data;
171 /* put new result in synthnew */
172 for (i = 0; i < pv->hop_s; i++)
173 synthnew[i] = synth[i] * pv->scale;
175 /* no overlap, nothing else to do */
176 if (pv->end == 0) return;
178 /* add new synth to old one */
179 for (i = 0; i < pv->hop_s; i++)
180 synthnew[i] += synthold[i];
183 for (i = 0; i < pv->start; i++)
184 synthold[i] = synthold[i + pv->hop_s];
186 /* erase last frame in synthold */
187 for (i = pv->start; i < pv->end; i++)
191 for (i = 0; i < pv->end; i++)
192 synthold[i] += synth[i + pv->hop_s] * pv->scale;