source: src/spectral/phasevoc.c @ 0b9a02a

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

src: update all headers to GPLv3

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[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 */
29struct _aubio_pvoc_t {
[685412e]30  uint_t win_s;       /** grain length */
31  uint_t hop_s;       /** overlap step */
32  uint_t channels;    /** number of channels */
[8b2dc90]33  aubio_fft_t * fft;  /** fft object */
34  fvec_t * synth;     /** cur output grain [win_s] */
35  fvec_t * synthold;  /** last input frame [win_s-hop_s] */
36  fvec_t * data;      /** current input grain [win_s] */
37  fvec_t * dataold;   /** last input frame [win_s-hop_s] */
[d84d19e]38  fvec_t * w;         /** grain window [win_s] */
[96fb8ad]39};
40
41
42/** returns data and dataold slided by hop_s */
[685412e]43static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, const
44    smpl_t * datanew, uint_t win_s, uint_t hop_s);
[96fb8ad]45
[685412e]46/** do additive synthesis from 'old' and 'cur' */
47static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold,
48    smpl_t * synthnew, uint_t win_s, uint_t hop_s);
[96fb8ad]49
50void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) {
[bab0a12]51  uint_t i;
[685412e]52  for (i=0; i<pv->channels; i++) {
53    /* slide  */
54    aubio_pvoc_swapbuffers(pv->data->data[i],pv->dataold->data[i],
55        datanew->data[i],pv->win_s,pv->hop_s);
56  }
[bab0a12]57  /* windowing */
58  fvec_weight(pv->data, pv->w);
[685412e]59  /* shift */
[5c4ec3c]60  fvec_shift(pv->data);
[685412e]61  /* calculate fft */
[8b2dc90]62  aubio_fft_do (pv->fft,pv->data,fftgrain);
[96fb8ad]63}
64
65void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) {
[685412e]66  uint_t i;
67  /* calculate rfft */
[8b2dc90]68  aubio_fft_rdo(pv->fft,fftgrain,pv->synth);
[685412e]69  /* unshift */
[5c4ec3c]70  fvec_shift(pv->synth);
[685412e]71  for (i=0; i<pv->channels; i++) {
72    aubio_pvoc_addsynth(pv->synth->data[i],pv->synthold->data[i],
73        synthnew->data[i],pv->win_s,pv->hop_s);
74  }
[96fb8ad]75}
76
77aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s, uint_t channels) {
[685412e]78  aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t);
79
[cd7d733]80  /* if (win_s < 2*hop_s) {
81    AUBIO_WRN("Hop size bigger than half the window size!\n");
82  } */
[685412e]83
84  if (hop_s < 1) {
85    AUBIO_ERR("Hop size is smaller than 1!\n");
86    AUBIO_ERR("Resetting hop size to half the window size.\n");
87    hop_s = win_s / 2;
88  }
89
[8b2dc90]90  pv->fft      = new_aubio_fft(win_s,channels);
[685412e]91
92  /* remember old */
93  pv->data     = new_fvec (win_s, channels);
94  pv->synth    = new_fvec (win_s, channels);
95
96  /* new input output */
97  pv->dataold  = new_fvec  (win_s-hop_s, channels);
98  pv->synthold = new_fvec (win_s-hop_s, channels);
[407bba9]99  pv->w        = new_aubio_window ("hanningz", win_s);
[685412e]100
101  pv->channels = channels;
102  pv->hop_s    = hop_s;
103  pv->win_s    = win_s;
104
105  return pv;
[96fb8ad]106}
107
[f88a326]108void del_aubio_pvoc(aubio_pvoc_t *pv) {
[685412e]109  del_fvec(pv->data);
110  del_fvec(pv->synth);
111  del_fvec(pv->dataold);
112  del_fvec(pv->synthold);
[d84d19e]113  del_fvec(pv->w);
[8b2dc90]114  del_aubio_fft(pv->fft);
[685412e]115  AUBIO_FREE(pv);
[96fb8ad]116}
117
[f88a326]118static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, 
[685412e]119    const smpl_t * datanew, uint_t win_s, uint_t hop_s)
[96fb8ad]120{
[685412e]121  uint_t i;
122  for (i=0;i<win_s-hop_s;i++)
123    data[i] = dataold[i];
124  for (i=0;i<hop_s;i++)
125    data[win_s-hop_s+i] = datanew[i];
126  for (i=0;i<win_s-hop_s;i++)
127    dataold[i] = data[i+hop_s];
[96fb8ad]128}
129
[f88a326]130static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, 
131                smpl_t * synthnew, uint_t win_s, uint_t hop_s)
[96fb8ad]132{
[685412e]133  uint_t i;
134  smpl_t scale = 2*hop_s/(win_s+.0);
135  /* add new synth to old one and put result in synthnew */
136  for (i=0;i<hop_s;i++)
137    synthnew[i] = synthold[i]+synth[i]*scale;
138  /* shift synthold */
139  for (i=0;i<win_s-2*hop_s;i++)
140    synthold[i] = synthold[i+hop_s];
141  /* erase last frame in synthold */
142  for (i=win_s-hop_s;i<win_s;i++)
143    synthold[i-hop_s]=0.;
144  /* additive synth */
145  for (i=0;i<win_s-hop_s;i++)
146    synthold[i] += synth[i+hop_s]*scale;
[96fb8ad]147}
148
Note: See TracBrowser for help on using the repository browser.