source: src/spectral/phasevoc.c @ afceccd

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since afceccd was ffc1f79, checked in by Paul Brossier <piem@piem.org>, 10 years ago

src/spectral/phasevoc.c: improve error messages

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[96fb8ad]1/*
[687eead]2  Copyright (C) 2003-2014 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 */
29struct _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 */
[8a5148e]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 */
[d84d19e]37  fvec_t * w;         /** grain window [win_s] */
[687eead]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 */
[96fb8ad]43};
44
45
46/** returns data and dataold slided by hop_s */
[687eead]47static void aubio_pvoc_swapbuffers(aubio_pvoc_t *pv, fvec_t *new);
[96fb8ad]48
[685412e]49/** do additive synthesis from 'old' and 'cur' */
[687eead]50static void aubio_pvoc_addsynth(aubio_pvoc_t *pv, fvec_t * synthnew);
[96fb8ad]51
52void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) {
[d95ff38]53  /* slide  */
[687eead]54  aubio_pvoc_swapbuffers(pv, datanew);
[bab0a12]55  /* windowing */
56  fvec_weight(pv->data, pv->w);
[685412e]57  /* shift */
[5c4ec3c]58  fvec_shift(pv->data);
[685412e]59  /* calculate fft */
[8b2dc90]60  aubio_fft_do (pv->fft,pv->data,fftgrain);
[96fb8ad]61}
62
63void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) {
[685412e]64  /* calculate rfft */
[8b2dc90]65  aubio_fft_rdo(pv->fft,fftgrain,pv->synth);
[685412e]66  /* unshift */
[5c4ec3c]67  fvec_shift(pv->synth);
[687eead]68  /* additive synthesis */
69  aubio_pvoc_addsynth(pv, synthnew);
[96fb8ad]70}
71
[d95ff38]72aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s) {
[685412e]73  aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t);
74
[cd7d733]75  /* if (win_s < 2*hop_s) {
76    AUBIO_WRN("Hop size bigger than half the window size!\n");
77  } */
[685412e]78
[7c8d32a]79  if ((sint_t)hop_s < 1) {
[ffc1f79]80    AUBIO_ERR("pvoc: got hop_size %d, but can not be < 1\n", hop_s);
[5958d1e]81    goto beach;
[7c8d32a]82  } else if ((sint_t)win_s < 1) {
[ffc1f79]83    AUBIO_ERR("pvoc: got buffer_size %d, but can not be < 1\n", win_s);
[5958d1e]84    goto beach;
[8a5148e]85  } else if (win_s < hop_s) {
[ffc1f79]86    AUBIO_ERR("pvoc: hop size (%d) is larger than win size (%d)\n", win_s, hop_s);
[5958d1e]87    goto beach;
[685412e]88  }
89
[d95ff38]90  pv->fft      = new_aubio_fft (win_s);
[685412e]91
92  /* remember old */
[d95ff38]93  pv->data     = new_fvec (win_s);
94  pv->synth    = new_fvec (win_s);
[685412e]95
96  /* new input output */
[8a5148e]97  if (win_s > hop_s) {
98    pv->dataold  = new_fvec  (win_s-hop_s);
99    pv->synthold = new_fvec (win_s-hop_s);
100  } else {
101    pv->dataold  = new_fvec  (1);
102    pv->synthold = new_fvec (1);
103  }
[407bba9]104  pv->w        = new_aubio_window ("hanningz", win_s);
[685412e]105
106  pv->hop_s    = hop_s;
107  pv->win_s    = win_s;
108
[687eead]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;
113
[e2eee03]114  if (win_s > hop_s) pv->end = win_s - hop_s;
115  else pv->end = 0;
[687eead]116
117  pv->end_datasize = pv->end * sizeof(smpl_t);
118  pv->hop_datasize = pv->hop_s * sizeof(smpl_t);
119
120  pv->scale = pv->hop_s * 2. / pv->win_s;
121
[685412e]122  return pv;
[5958d1e]123
124beach:
125  AUBIO_FREE (pv);
126  return NULL;
[96fb8ad]127}
128
[f88a326]129void del_aubio_pvoc(aubio_pvoc_t *pv) {
[685412e]130  del_fvec(pv->data);
131  del_fvec(pv->synth);
132  del_fvec(pv->dataold);
133  del_fvec(pv->synthold);
[d84d19e]134  del_fvec(pv->w);
[8b2dc90]135  del_aubio_fft(pv->fft);
[685412e]136  AUBIO_FREE(pv);
[96fb8ad]137}
138
[687eead]139static void aubio_pvoc_swapbuffers(aubio_pvoc_t *pv, fvec_t *new)
[96fb8ad]140{
[687eead]141  /* some convenience pointers */
142  smpl_t * data = pv->data->data;
143  smpl_t * dataold = pv->dataold->data;
144  smpl_t * datanew = new->data;
[4087d54]145#if !HAVE_MEMCPY_HACKS
[685412e]146  uint_t i;
[687eead]147  for (i = 0; i < pv->end; i++)
[685412e]148    data[i] = dataold[i];
[687eead]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];
[4087d54]153#else
[687eead]154  memcpy(data, dataold, pv->end_datasize);
155  data += pv->end;
156  memcpy(data, datanew, pv->hop_datasize);
157  data -= pv->end;
158  data += pv->hop_s;
159  memcpy(dataold, data, pv->end_datasize);
[4087d54]160#endif
[96fb8ad]161}
162
[687eead]163static void aubio_pvoc_addsynth(aubio_pvoc_t *pv, fvec_t *synth_new)
[96fb8ad]164{
[687eead]165  uint_t i;
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;
[8a5148e]170
171  /* put new result in synthnew */
[687eead]172  for (i = 0; i < pv->hop_s; i++)
173    synthnew[i] = synth[i] * pv->scale;
174
[8a5148e]175  /* no overlap, nothing else to do */
[687eead]176  if (pv->end == 0) return;
[8a5148e]177
[687eead]178  /* add new synth to old one */
179  for (i = 0; i < pv->hop_s; i++)
[8a5148e]180    synthnew[i] += synthold[i];
181
[685412e]182  /* shift synthold */
[687eead]183  for (i = 0; i < pv->start; i++)
184    synthold[i] = synthold[i + pv->hop_s];
[8a5148e]185
[685412e]186  /* erase last frame in synthold */
[687eead]187  for (i = pv->start; i < pv->end; i++)
[8a5148e]188    synthold[i] = 0.;
189
[685412e]190  /* additive synth */
[687eead]191  for (i = 0; i < pv->end; i++)
192    synthold[i] += synth[i + pv->hop_s] * pv->scale;
[96fb8ad]193}
Note: See TracBrowser for help on using the repository browser.