Chris@337
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@337
|
2
|
Chris@337
|
3 /*
|
Chris@337
|
4 Vamp
|
Chris@337
|
5
|
Chris@337
|
6 An API for audio analysis and feature extraction plugins.
|
Chris@337
|
7
|
Chris@337
|
8 Centre for Digital Music, Queen Mary, University of London.
|
Chris@337
|
9 Copyright 2006-2012 Chris Cannam and QMUL.
|
Chris@337
|
10
|
Chris@337
|
11 Permission is hereby granted, free of charge, to any person
|
Chris@337
|
12 obtaining a copy of this software and associated documentation
|
Chris@337
|
13 files (the "Software"), to deal in the Software without
|
Chris@337
|
14 restriction, including without limitation the rights to use, copy,
|
Chris@337
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@337
|
16 of the Software, and to permit persons to whom the Software is
|
Chris@337
|
17 furnished to do so, subject to the following conditions:
|
Chris@337
|
18
|
Chris@337
|
19 The above copyright notice and this permission notice shall be
|
Chris@337
|
20 included in all copies or substantial portions of the Software.
|
Chris@337
|
21
|
Chris@337
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@337
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@337
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@337
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
Chris@337
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@337
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@337
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@337
|
29
|
Chris@337
|
30 Except as contained in this notice, the names of the Centre for
|
Chris@337
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
Chris@337
|
32 shall not be used in advertising or otherwise to promote the sale,
|
Chris@337
|
33 use or other dealings in this Software without prior written
|
Chris@337
|
34 authorization.
|
Chris@337
|
35 */
|
Chris@337
|
36
|
Chris@337
|
37 #include <vamp-sdk/FFT.h>
|
Chris@337
|
38
|
Chris@434
|
39 #include <stdlib.h>
|
Chris@434
|
40 #include <stdio.h>
|
Chris@434
|
41 #include <math.h>
|
Chris@434
|
42 #include <string.h>
|
Chris@337
|
43
|
Chris@516
|
44 #if ( VAMP_SDK_MAJOR_VERSION != 2 || VAMP_SDK_MINOR_VERSION != 8 )
|
Chris@337
|
45 #error Unexpected version of Vamp SDK header included
|
Chris@337
|
46 #endif
|
Chris@337
|
47
|
Chris@434
|
48 _VAMP_SDK_PLUGSPACE_BEGIN(FFT.cpp)
|
chris@340
|
49
|
Chris@445
|
50 #include "FFTimpl.cpp"
|
Chris@337
|
51
|
Chris@337
|
52 namespace Vamp {
|
Chris@337
|
53
|
Chris@501
|
54 using namespace Kiss;
|
Chris@501
|
55
|
Chris@337
|
56 void
|
Chris@434
|
57 FFT::forward(unsigned int un,
|
Chris@337
|
58 const double *ri, const double *ii,
|
Chris@337
|
59 double *ro, double *io)
|
Chris@337
|
60 {
|
Chris@434
|
61 int n(un);
|
Chris@501
|
62 vamp_kiss_fft_cfg c = vamp_kiss_fft_alloc(n, false, 0, 0);
|
Chris@501
|
63 vamp_kiss_fft_cpx *in = new vamp_kiss_fft_cpx[n];
|
Chris@501
|
64 vamp_kiss_fft_cpx *out = new vamp_kiss_fft_cpx[n];
|
Chris@434
|
65 for (int i = 0; i < n; ++i) {
|
Chris@434
|
66 in[i].r = ri[i];
|
Chris@434
|
67 in[i].i = 0;
|
Chris@434
|
68 }
|
Chris@434
|
69 if (ii) {
|
Chris@434
|
70 for (int i = 0; i < n; ++i) {
|
Chris@434
|
71 in[i].i = ii[i];
|
Chris@434
|
72 }
|
Chris@434
|
73 }
|
Chris@501
|
74 vamp_kiss_fft(c, in, out);
|
Chris@434
|
75 for (int i = 0; i < n; ++i) {
|
Chris@434
|
76 ro[i] = out[i].r;
|
Chris@434
|
77 io[i] = out[i].i;
|
Chris@434
|
78 }
|
Chris@501
|
79 vamp_kiss_fft_free(c);
|
Chris@434
|
80 delete[] in;
|
Chris@434
|
81 delete[] out;
|
Chris@337
|
82 }
|
Chris@337
|
83
|
Chris@337
|
84 void
|
Chris@434
|
85 FFT::inverse(unsigned int un,
|
Chris@337
|
86 const double *ri, const double *ii,
|
Chris@337
|
87 double *ro, double *io)
|
Chris@337
|
88 {
|
Chris@434
|
89 int n(un);
|
Chris@501
|
90 vamp_kiss_fft_cfg c = vamp_kiss_fft_alloc(n, true, 0, 0);
|
Chris@501
|
91 vamp_kiss_fft_cpx *in = new vamp_kiss_fft_cpx[n];
|
Chris@501
|
92 vamp_kiss_fft_cpx *out = new vamp_kiss_fft_cpx[n];
|
Chris@434
|
93 for (int i = 0; i < n; ++i) {
|
Chris@434
|
94 in[i].r = ri[i];
|
Chris@434
|
95 in[i].i = 0;
|
Chris@434
|
96 }
|
Chris@434
|
97 if (ii) {
|
Chris@434
|
98 for (int i = 0; i < n; ++i) {
|
Chris@434
|
99 in[i].i = ii[i];
|
Chris@434
|
100 }
|
Chris@434
|
101 }
|
Chris@501
|
102 vamp_kiss_fft(c, in, out);
|
Chris@434
|
103 double scale = 1.0 / double(n);
|
Chris@434
|
104 for (int i = 0; i < n; ++i) {
|
Chris@434
|
105 ro[i] = out[i].r * scale;
|
Chris@434
|
106 io[i] = out[i].i * scale;
|
Chris@434
|
107 }
|
Chris@501
|
108 vamp_kiss_fft_free(c);
|
Chris@434
|
109 delete[] in;
|
Chris@434
|
110 delete[] out;
|
Chris@434
|
111 }
|
Chris@434
|
112
|
Chris@446
|
113 class FFTComplex::D
|
Chris@446
|
114 {
|
Chris@446
|
115 public:
|
Chris@446
|
116 D(int n) :
|
Chris@446
|
117 m_n(n),
|
Chris@501
|
118 m_fconf(vamp_kiss_fft_alloc(n, false, 0, 0)),
|
Chris@501
|
119 m_iconf(vamp_kiss_fft_alloc(n, true, 0, 0)),
|
Chris@501
|
120 m_ci(new vamp_kiss_fft_cpx[m_n]),
|
Chris@501
|
121 m_co(new vamp_kiss_fft_cpx[m_n]) { }
|
Chris@446
|
122
|
Chris@446
|
123 ~D() {
|
Chris@501
|
124 vamp_kiss_fftr_free(m_fconf);
|
Chris@501
|
125 vamp_kiss_fftr_free(m_iconf);
|
Chris@446
|
126 delete[] m_ci;
|
Chris@446
|
127 delete[] m_co;
|
Chris@446
|
128 }
|
Chris@446
|
129
|
Chris@446
|
130 void forward(const double *ci, double *co) {
|
Chris@446
|
131 for (int i = 0; i < m_n; ++i) {
|
Chris@446
|
132 m_ci[i].r = ci[i*2];
|
Chris@446
|
133 m_ci[i].i = ci[i*2+1];
|
Chris@446
|
134 }
|
Chris@501
|
135 vamp_kiss_fft(m_fconf, m_ci, m_co);
|
Chris@446
|
136 for (int i = 0; i < m_n; ++i) {
|
Chris@446
|
137 co[i*2] = m_co[i].r;
|
Chris@446
|
138 co[i*2+1] = m_co[i].i;
|
Chris@446
|
139 }
|
Chris@446
|
140 }
|
Chris@446
|
141
|
Chris@446
|
142 void inverse(const double *ci, double *co) {
|
Chris@446
|
143 for (int i = 0; i < m_n; ++i) {
|
Chris@446
|
144 m_ci[i].r = ci[i*2];
|
Chris@446
|
145 m_ci[i].i = ci[i*2+1];
|
Chris@446
|
146 }
|
Chris@501
|
147 vamp_kiss_fft(m_iconf, m_ci, m_co);
|
Chris@446
|
148 double scale = 1.0 / double(m_n);
|
Chris@446
|
149 for (int i = 0; i < m_n; ++i) {
|
Chris@446
|
150 co[i*2] = m_co[i].r * scale;
|
Chris@446
|
151 co[i*2+1] = m_co[i].i * scale;
|
Chris@446
|
152 }
|
Chris@446
|
153 }
|
Chris@446
|
154
|
Chris@446
|
155 private:
|
Chris@446
|
156 int m_n;
|
Chris@501
|
157 vamp_kiss_fft_cfg m_fconf;
|
Chris@501
|
158 vamp_kiss_fft_cfg m_iconf;
|
Chris@501
|
159 vamp_kiss_fft_cpx *m_ci;
|
Chris@501
|
160 vamp_kiss_fft_cpx *m_co;
|
Chris@446
|
161 };
|
Chris@446
|
162
|
Chris@446
|
163 FFTComplex::FFTComplex(unsigned int n) :
|
Chris@446
|
164 m_d(new D(n))
|
Chris@446
|
165 {
|
Chris@446
|
166 }
|
Chris@446
|
167
|
Chris@446
|
168 FFTComplex::~FFTComplex()
|
Chris@446
|
169 {
|
Chris@446
|
170 delete m_d;
|
Chris@446
|
171 }
|
Chris@446
|
172
|
Chris@446
|
173 void
|
Chris@446
|
174 FFTComplex::forward(const double *ci, double *co)
|
Chris@446
|
175 {
|
Chris@446
|
176 m_d->forward(ci, co);
|
Chris@446
|
177 }
|
Chris@446
|
178
|
Chris@446
|
179 void
|
Chris@446
|
180 FFTComplex::inverse(const double *ci, double *co)
|
Chris@446
|
181 {
|
Chris@446
|
182 m_d->inverse(ci, co);
|
Chris@446
|
183 }
|
Chris@446
|
184
|
Chris@434
|
185 class FFTReal::D
|
Chris@434
|
186 {
|
Chris@434
|
187 public:
|
Chris@434
|
188 D(int n) :
|
Chris@434
|
189 m_n(n),
|
Chris@501
|
190 m_fconf(vamp_kiss_fftr_alloc(n, false, 0, 0)),
|
Chris@501
|
191 m_iconf(vamp_kiss_fftr_alloc(n, true, 0, 0)),
|
Chris@501
|
192 m_ri(new vamp_kiss_fft_scalar[m_n]),
|
Chris@501
|
193 m_ro(new vamp_kiss_fft_scalar[m_n]),
|
Chris@501
|
194 m_freq(new vamp_kiss_fft_cpx[n/2+1]) { }
|
Chris@434
|
195
|
Chris@434
|
196 ~D() {
|
Chris@501
|
197 vamp_kiss_fftr_free(m_fconf);
|
Chris@501
|
198 vamp_kiss_fftr_free(m_iconf);
|
Chris@445
|
199 delete[] m_ri;
|
Chris@445
|
200 delete[] m_ro;
|
Chris@434
|
201 delete[] m_freq;
|
Chris@434
|
202 }
|
Chris@434
|
203
|
Chris@445
|
204 void forward(const double *ri, double *co) {
|
Chris@445
|
205 for (int i = 0; i < m_n; ++i) {
|
Chris@501
|
206 // in case vamp_kiss_fft_scalar is float
|
Chris@445
|
207 m_ri[i] = ri[i];
|
Chris@445
|
208 }
|
Chris@501
|
209 vamp_kiss_fftr(m_fconf, m_ri, m_freq);
|
Chris@434
|
210 int hs = m_n/2 + 1;
|
Chris@434
|
211 for (int i = 0; i < hs; ++i) {
|
Chris@434
|
212 co[i*2] = m_freq[i].r;
|
Chris@434
|
213 co[i*2+1] = m_freq[i].i;
|
Chris@434
|
214 }
|
Chris@434
|
215 }
|
Chris@434
|
216
|
Chris@445
|
217 void inverse(const double *ci, double *ro) {
|
Chris@434
|
218 int hs = m_n/2 + 1;
|
Chris@434
|
219 for (int i = 0; i < hs; ++i) {
|
Chris@434
|
220 m_freq[i].r = ci[i*2];
|
Chris@434
|
221 m_freq[i].i = ci[i*2+1];
|
Chris@434
|
222 }
|
Chris@501
|
223 vamp_kiss_fftri(m_iconf, m_freq, m_ro);
|
Chris@434
|
224 double scale = 1.0 / double(m_n);
|
Chris@434
|
225 for (int i = 0; i < m_n; ++i) {
|
Chris@445
|
226 ro[i] = m_ro[i] * scale;
|
Chris@434
|
227 }
|
Chris@434
|
228 }
|
Chris@434
|
229
|
Chris@434
|
230 private:
|
Chris@434
|
231 int m_n;
|
Chris@501
|
232 vamp_kiss_fftr_cfg m_fconf;
|
Chris@501
|
233 vamp_kiss_fftr_cfg m_iconf;
|
Chris@501
|
234 vamp_kiss_fft_scalar *m_ri;
|
Chris@501
|
235 vamp_kiss_fft_scalar *m_ro;
|
Chris@501
|
236 vamp_kiss_fft_cpx *m_freq;
|
Chris@434
|
237 };
|
Chris@434
|
238
|
Chris@434
|
239 FFTReal::FFTReal(unsigned int n) :
|
Chris@434
|
240 m_d(new D(n))
|
Chris@434
|
241 {
|
Chris@434
|
242 }
|
Chris@434
|
243
|
Chris@434
|
244 FFTReal::~FFTReal()
|
Chris@434
|
245 {
|
Chris@434
|
246 delete m_d;
|
Chris@434
|
247 }
|
Chris@434
|
248
|
Chris@434
|
249 void
|
Chris@445
|
250 FFTReal::forward(const double *ri, double *co)
|
Chris@434
|
251 {
|
Chris@434
|
252 m_d->forward(ri, co);
|
Chris@434
|
253 }
|
Chris@434
|
254
|
Chris@434
|
255 void
|
Chris@445
|
256 FFTReal::inverse(const double *ci, double *ro)
|
Chris@434
|
257 {
|
Chris@434
|
258 m_d->inverse(ci, ro);
|
Chris@337
|
259 }
|
Chris@337
|
260
|
Chris@337
|
261 }
|
Chris@337
|
262
|
Chris@337
|
263 _VAMP_SDK_PLUGSPACE_END(FFT.cpp)
|
Chris@337
|
264
|