source: src/spectral/phasevoc.c @ 8a5148e

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

src/spectral/phasevoc.c: improve, accept that hop_s == buf_s

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
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.
10
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/>.
18
19*/
20
21#include "aubio_priv.h"
22#include "fvec.h"
23#include "cvec.h"
24#include "mathutils.h"
25#include "spectral/fft.h"
26#include "spectral/phasevoc.h"
27
28/** phasevocoder internal object */
29struct _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};
39
40
41/** returns data and dataold slided by hop_s */
42static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, const
43    smpl_t * datanew, uint_t win_s, uint_t hop_s);
44
45/** do additive synthesis from 'old' and 'cur' */
46static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold,
47    smpl_t * synthnew, uint_t win_s, uint_t hop_s);
48
49void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) {
50  /* slide  */
51  aubio_pvoc_swapbuffers(pv->data->data,pv->dataold->data,
52      datanew->data,pv->win_s,pv->hop_s);
53  /* windowing */
54  fvec_weight(pv->data, pv->w);
55  /* shift */
56  fvec_shift(pv->data);
57  /* calculate fft */
58  aubio_fft_do (pv->fft,pv->data,fftgrain);
59}
60
61void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) {
62  /* calculate rfft */
63  aubio_fft_rdo(pv->fft,fftgrain,pv->synth);
64  /* unshift */
65  fvec_shift(pv->synth);
66  aubio_pvoc_addsynth(pv->synth->data,pv->synthold->data,
67      synthnew->data,pv->win_s,pv->hop_s);
68}
69
70aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s) {
71  aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t);
72
73  /* if (win_s < 2*hop_s) {
74    AUBIO_WRN("Hop size bigger than half the window size!\n");
75  } */
76
77  if (hop_s < 1) {
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) {
84    AUBIO_ERR("hop size (%d) is larger than win size (%d)\n", win_s, hop_s);
85    goto beach;
86  }
87
88  pv->fft      = new_aubio_fft (win_s);
89
90  /* remember old */
91  pv->data     = new_fvec (win_s);
92  pv->synth    = new_fvec (win_s);
93
94  /* new input output */
95  if (win_s > hop_s) {
96    pv->dataold  = new_fvec  (win_s-hop_s);
97    pv->synthold = new_fvec (win_s-hop_s);
98  } else {
99    pv->dataold  = new_fvec  (1);
100    pv->synthold = new_fvec (1);
101  }
102  pv->w        = new_aubio_window ("hanningz", win_s);
103
104  pv->hop_s    = hop_s;
105  pv->win_s    = win_s;
106
107  return pv;
108
109beach:
110  AUBIO_FREE (pv);
111  return NULL;
112}
113
114void del_aubio_pvoc(aubio_pvoc_t *pv) {
115  del_fvec(pv->data);
116  del_fvec(pv->synth);
117  del_fvec(pv->dataold);
118  del_fvec(pv->synthold);
119  del_fvec(pv->w);
120  del_aubio_fft(pv->fft);
121  AUBIO_FREE(pv);
122}
123
124static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, 
125    const smpl_t * datanew, uint_t win_s, uint_t hop_s)
126{
127#if !HAVE_MEMCPY_HACKS
128  uint_t i;
129  for (i = 0; i < win_s - hop_s; i++)
130    data[i] = dataold[i];
131  for (i = 0; i < hop_s; i++)
132    data[win_s - hop_s + i] = datanew[i];
133  for (i = 0; i < win_s - hop_s; i++)
134    dataold[i] = data[i + hop_s];
135#else
136  memcpy(data, dataold, (win_s - hop_s) * sizeof(smpl_t));
137  data += win_s - hop_s;
138  memcpy(data, datanew, hop_s * sizeof(smpl_t));
139  data -= win_s - hop_s;
140  data += hop_s;
141  memcpy(dataold, data, (win_s - hop_s) * sizeof(smpl_t));
142#endif
143}
144
145static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, 
146                smpl_t * synthnew, uint_t win_s, uint_t hop_s)
147{
148  uint_t i, start;
149  smpl_t scale = hop_s * 2. / win_s;
150
151  /* put new result in synthnew */
152  for (i = 0; i < hop_s; i++)
153    synthnew[i] = synth[i] * scale;
154  /* no overlap, nothing else to do */
155  if (win_s <= hop_s) return;
156
157  /* add new synth to old one and */
158  for (i = 0; i < hop_s; i++)
159    synthnew[i] += synthold[i];
160
161  /* shift synthold */
162  for (i = hop_s; i < win_s - hop_s; i++)
163    synthold[i - hop_s] = synthold[i];
164
165  /* more than 50% overlap, overlap anyway */
166  if (win_s < 2 * hop_s) start = 0;
167  /* less than 50% overlap, reset latest grain trail */
168  else start = win_s - hop_s - hop_s;
169  /* erase last frame in synthold */
170  for (i = start; i < win_s - hop_s; i++)
171    synthold[i] = 0.;
172
173  /* additive synth */
174  for (i = 0; i < win_s - hop_s; i++)
175    synthold[i] += synth[i + hop_s] * scale;
176}
177
Note: See TracBrowser for help on using the repository browser.