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 "fvec.h" |
---|
22 | #include "cvec.h" |
---|
23 | #include "fft.h" |
---|
24 | #include "mathutils.h" |
---|
25 | #include "phasevoc.h" |
---|
26 | |
---|
27 | /** phasevocoder internal object */ |
---|
28 | struct _aubio_pvoc_t { |
---|
29 | uint_t win_s; /** grain length */ |
---|
30 | uint_t hop_s; /** overlap step */ |
---|
31 | uint_t channels; /** number of channels */ |
---|
32 | aubio_fft_t * fft; /** fft object */ |
---|
33 | fvec_t * synth; /** cur output grain [win_s] */ |
---|
34 | fvec_t * synthold; /** last input frame [win_s-hop_s] */ |
---|
35 | fvec_t * data; /** current input grain [win_s] */ |
---|
36 | fvec_t * dataold; /** last input frame [win_s-hop_s] */ |
---|
37 | smpl_t * w; /** grain window [win_s] */ |
---|
38 | }; |
---|
39 | |
---|
40 | |
---|
41 | /** returns data and dataold slided by hop_s */ |
---|
42 | static 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' */ |
---|
46 | static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, |
---|
47 | smpl_t * synthnew, uint_t win_s, uint_t hop_s); |
---|
48 | |
---|
49 | void aubio_pvoc_do(aubio_pvoc_t *pv, fvec_t * datanew, cvec_t *fftgrain) { |
---|
50 | uint_t i,j; |
---|
51 | for (i=0; i<pv->channels; i++) { |
---|
52 | /* slide */ |
---|
53 | aubio_pvoc_swapbuffers(pv->data->data[i],pv->dataold->data[i], |
---|
54 | datanew->data[i],pv->win_s,pv->hop_s); |
---|
55 | /* windowing */ |
---|
56 | for (j=0; j<pv->win_s; j++) pv->data->data[i][j] *= pv->w[j]; |
---|
57 | } |
---|
58 | /* shift */ |
---|
59 | vec_shift(pv->data); |
---|
60 | /* calculate fft */ |
---|
61 | aubio_fft_do (pv->fft,pv->data,fftgrain); |
---|
62 | } |
---|
63 | |
---|
64 | void aubio_pvoc_rdo(aubio_pvoc_t *pv,cvec_t * fftgrain, fvec_t * synthnew) { |
---|
65 | uint_t i; |
---|
66 | /* calculate rfft */ |
---|
67 | aubio_fft_rdo(pv->fft,fftgrain,pv->synth); |
---|
68 | /* unshift */ |
---|
69 | vec_shift(pv->synth); |
---|
70 | for (i=0; i<pv->channels; i++) { |
---|
71 | aubio_pvoc_addsynth(pv->synth->data[i],pv->synthold->data[i], |
---|
72 | synthnew->data[i],pv->win_s,pv->hop_s); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | aubio_pvoc_t * new_aubio_pvoc (uint_t win_s, uint_t hop_s, uint_t channels) { |
---|
77 | aubio_pvoc_t * pv = AUBIO_NEW(aubio_pvoc_t); |
---|
78 | |
---|
79 | if (win_s < 2*hop_s) { |
---|
80 | AUBIO_ERR("Hop size bigger than half the window size!\n"); |
---|
81 | AUBIO_ERR("Resetting hop size to half the window size.\n"); |
---|
82 | hop_s = win_s / 2; |
---|
83 | } |
---|
84 | |
---|
85 | if (hop_s < 1) { |
---|
86 | AUBIO_ERR("Hop size is smaller than 1!\n"); |
---|
87 | AUBIO_ERR("Resetting hop size to half the window size.\n"); |
---|
88 | hop_s = win_s / 2; |
---|
89 | } |
---|
90 | |
---|
91 | pv->fft = new_aubio_fft(win_s,channels); |
---|
92 | |
---|
93 | /* remember old */ |
---|
94 | pv->data = new_fvec (win_s, channels); |
---|
95 | pv->synth = new_fvec (win_s, channels); |
---|
96 | |
---|
97 | /* new input output */ |
---|
98 | pv->dataold = new_fvec (win_s-hop_s, channels); |
---|
99 | pv->synthold = new_fvec (win_s-hop_s, channels); |
---|
100 | pv->w = AUBIO_ARRAY(smpl_t,win_s); |
---|
101 | aubio_window(pv->w,win_s,aubio_win_hanningz); |
---|
102 | |
---|
103 | pv->channels = channels; |
---|
104 | pv->hop_s = hop_s; |
---|
105 | pv->win_s = win_s; |
---|
106 | |
---|
107 | return pv; |
---|
108 | } |
---|
109 | |
---|
110 | void del_aubio_pvoc(aubio_pvoc_t *pv) { |
---|
111 | del_fvec(pv->data); |
---|
112 | del_fvec(pv->synth); |
---|
113 | del_fvec(pv->dataold); |
---|
114 | del_fvec(pv->synthold); |
---|
115 | del_aubio_fft(pv->fft); |
---|
116 | AUBIO_FREE(pv->w); |
---|
117 | AUBIO_FREE(pv); |
---|
118 | } |
---|
119 | |
---|
120 | static void aubio_pvoc_swapbuffers(smpl_t * data, smpl_t * dataold, |
---|
121 | const smpl_t * datanew, uint_t win_s, uint_t hop_s) |
---|
122 | { |
---|
123 | uint_t i; |
---|
124 | for (i=0;i<win_s-hop_s;i++) |
---|
125 | data[i] = dataold[i]; |
---|
126 | for (i=0;i<hop_s;i++) |
---|
127 | data[win_s-hop_s+i] = datanew[i]; |
---|
128 | for (i=0;i<win_s-hop_s;i++) |
---|
129 | dataold[i] = data[i+hop_s]; |
---|
130 | } |
---|
131 | |
---|
132 | static void aubio_pvoc_addsynth(const smpl_t * synth, smpl_t * synthold, |
---|
133 | smpl_t * synthnew, uint_t win_s, uint_t hop_s) |
---|
134 | { |
---|
135 | uint_t i; |
---|
136 | smpl_t scale = 2*hop_s/(win_s+.0); |
---|
137 | /* add new synth to old one and put result in synthnew */ |
---|
138 | for (i=0;i<hop_s;i++) |
---|
139 | synthnew[i] = synthold[i]+synth[i]*scale; |
---|
140 | /* shift synthold */ |
---|
141 | for (i=0;i<win_s-2*hop_s;i++) |
---|
142 | synthold[i] = synthold[i+hop_s]; |
---|
143 | /* erase last frame in synthold */ |
---|
144 | for (i=win_s-hop_s;i<win_s;i++) |
---|
145 | synthold[i-hop_s]=0.; |
---|
146 | /* additive synth */ |
---|
147 | for (i=0;i<win_s-hop_s;i++) |
---|
148 | synthold[i] += synth[i+hop_s]*scale; |
---|
149 | } |
---|
150 | |
---|