annotate src/vamp-sdk/FFT.cpp @ 472:79a219ba6178

Correct use of multibyte-widechar conversion routines
author Chris Cannam
date Fri, 28 Oct 2016 14:30:26 +0100
parents d132b92ec65d
children 90571dcc371a
rev   line source
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@424 44 #if ( VAMP_SDK_MAJOR_VERSION != 2 || VAMP_SDK_MINOR_VERSION != 7 )
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@337 54 void
Chris@434 55 FFT::forward(unsigned int un,
Chris@337 56 const double *ri, const double *ii,
Chris@337 57 double *ro, double *io)
Chris@337 58 {
Chris@434 59 int n(un);
Chris@445 60 Kiss::kiss_fft_cfg c = Kiss::kiss_fft_alloc(n, false, 0, 0);
Chris@445 61 Kiss::kiss_fft_cpx *in = new Kiss::kiss_fft_cpx[n];
Chris@445 62 Kiss::kiss_fft_cpx *out = new Kiss::kiss_fft_cpx[n];
Chris@434 63 for (int i = 0; i < n; ++i) {
Chris@434 64 in[i].r = ri[i];
Chris@434 65 in[i].i = 0;
Chris@434 66 }
Chris@434 67 if (ii) {
Chris@434 68 for (int i = 0; i < n; ++i) {
Chris@434 69 in[i].i = ii[i];
Chris@434 70 }
Chris@434 71 }
Chris@434 72 kiss_fft(c, in, out);
Chris@434 73 for (int i = 0; i < n; ++i) {
Chris@434 74 ro[i] = out[i].r;
Chris@434 75 io[i] = out[i].i;
Chris@434 76 }
Chris@445 77 Kiss::kiss_fft_free(c);
Chris@434 78 delete[] in;
Chris@434 79 delete[] out;
Chris@337 80 }
Chris@337 81
Chris@337 82 void
Chris@434 83 FFT::inverse(unsigned int un,
Chris@337 84 const double *ri, const double *ii,
Chris@337 85 double *ro, double *io)
Chris@337 86 {
Chris@434 87 int n(un);
Chris@445 88 Kiss::kiss_fft_cfg c = Kiss::kiss_fft_alloc(n, true, 0, 0);
Chris@445 89 Kiss::kiss_fft_cpx *in = new Kiss::kiss_fft_cpx[n];
Chris@445 90 Kiss::kiss_fft_cpx *out = new Kiss::kiss_fft_cpx[n];
Chris@434 91 for (int i = 0; i < n; ++i) {
Chris@434 92 in[i].r = ri[i];
Chris@434 93 in[i].i = 0;
Chris@434 94 }
Chris@434 95 if (ii) {
Chris@434 96 for (int i = 0; i < n; ++i) {
Chris@434 97 in[i].i = ii[i];
Chris@434 98 }
Chris@434 99 }
Chris@434 100 kiss_fft(c, in, out);
Chris@434 101 double scale = 1.0 / double(n);
Chris@434 102 for (int i = 0; i < n; ++i) {
Chris@434 103 ro[i] = out[i].r * scale;
Chris@434 104 io[i] = out[i].i * scale;
Chris@434 105 }
Chris@445 106 Kiss::kiss_fft_free(c);
Chris@434 107 delete[] in;
Chris@434 108 delete[] out;
Chris@434 109 }
Chris@434 110
Chris@446 111 class FFTComplex::D
Chris@446 112 {
Chris@446 113 public:
Chris@446 114 D(int n) :
Chris@446 115 m_n(n),
Chris@446 116 m_fconf(Kiss::kiss_fft_alloc(n, false, 0, 0)),
Chris@446 117 m_iconf(Kiss::kiss_fft_alloc(n, true, 0, 0)),
Chris@446 118 m_ci(new Kiss::kiss_fft_cpx[m_n]),
Chris@446 119 m_co(new Kiss::kiss_fft_cpx[m_n]) { }
Chris@446 120
Chris@446 121 ~D() {
Chris@446 122 Kiss::kiss_fftr_free(m_fconf);
Chris@446 123 Kiss::kiss_fftr_free(m_iconf);
Chris@446 124 delete[] m_ci;
Chris@446 125 delete[] m_co;
Chris@446 126 }
Chris@446 127
Chris@446 128 void forward(const double *ci, double *co) {
Chris@446 129 for (int i = 0; i < m_n; ++i) {
Chris@446 130 m_ci[i].r = ci[i*2];
Chris@446 131 m_ci[i].i = ci[i*2+1];
Chris@446 132 }
Chris@446 133 Kiss::kiss_fft(m_fconf, m_ci, m_co);
Chris@446 134 for (int i = 0; i < m_n; ++i) {
Chris@446 135 co[i*2] = m_co[i].r;
Chris@446 136 co[i*2+1] = m_co[i].i;
Chris@446 137 }
Chris@446 138 }
Chris@446 139
Chris@446 140 void inverse(const double *ci, double *co) {
Chris@446 141 for (int i = 0; i < m_n; ++i) {
Chris@446 142 m_ci[i].r = ci[i*2];
Chris@446 143 m_ci[i].i = ci[i*2+1];
Chris@446 144 }
Chris@446 145 Kiss::kiss_fft(m_iconf, m_ci, m_co);
Chris@446 146 double scale = 1.0 / double(m_n);
Chris@446 147 for (int i = 0; i < m_n; ++i) {
Chris@446 148 co[i*2] = m_co[i].r * scale;
Chris@446 149 co[i*2+1] = m_co[i].i * scale;
Chris@446 150 }
Chris@446 151 }
Chris@446 152
Chris@446 153 private:
Chris@446 154 int m_n;
Chris@446 155 Kiss::kiss_fft_cfg m_fconf;
Chris@446 156 Kiss::kiss_fft_cfg m_iconf;
Chris@446 157 Kiss::kiss_fft_cpx *m_ci;
Chris@446 158 Kiss::kiss_fft_cpx *m_co;
Chris@446 159 };
Chris@446 160
Chris@446 161 FFTComplex::FFTComplex(unsigned int n) :
Chris@446 162 m_d(new D(n))
Chris@446 163 {
Chris@446 164 }
Chris@446 165
Chris@446 166 FFTComplex::~FFTComplex()
Chris@446 167 {
Chris@446 168 delete m_d;
Chris@446 169 }
Chris@446 170
Chris@446 171 void
Chris@446 172 FFTComplex::forward(const double *ci, double *co)
Chris@446 173 {
Chris@446 174 m_d->forward(ci, co);
Chris@446 175 }
Chris@446 176
Chris@446 177 void
Chris@446 178 FFTComplex::inverse(const double *ci, double *co)
Chris@446 179 {
Chris@446 180 m_d->inverse(ci, co);
Chris@446 181 }
Chris@446 182
Chris@434 183 class FFTReal::D
Chris@434 184 {
Chris@434 185 public:
Chris@434 186 D(int n) :
Chris@434 187 m_n(n),
Chris@446 188 m_fconf(Kiss::kiss_fftr_alloc(n, false, 0, 0)),
Chris@446 189 m_iconf(Kiss::kiss_fftr_alloc(n, true, 0, 0)),
Chris@445 190 m_ri(new Kiss::kiss_fft_scalar[m_n]),
Chris@445 191 m_ro(new Kiss::kiss_fft_scalar[m_n]),
Chris@445 192 m_freq(new Kiss::kiss_fft_cpx[n/2+1]) { }
Chris@434 193
Chris@434 194 ~D() {
Chris@446 195 Kiss::kiss_fftr_free(m_fconf);
Chris@446 196 Kiss::kiss_fftr_free(m_iconf);
Chris@445 197 delete[] m_ri;
Chris@445 198 delete[] m_ro;
Chris@434 199 delete[] m_freq;
Chris@434 200 }
Chris@434 201
Chris@445 202 void forward(const double *ri, double *co) {
Chris@445 203 for (int i = 0; i < m_n; ++i) {
Chris@445 204 // in case kiss_fft_scalar is float
Chris@445 205 m_ri[i] = ri[i];
Chris@445 206 }
Chris@446 207 Kiss::kiss_fftr(m_fconf, m_ri, m_freq);
Chris@434 208 int hs = m_n/2 + 1;
Chris@434 209 for (int i = 0; i < hs; ++i) {
Chris@434 210 co[i*2] = m_freq[i].r;
Chris@434 211 co[i*2+1] = m_freq[i].i;
Chris@434 212 }
Chris@434 213 }
Chris@434 214
Chris@445 215 void inverse(const double *ci, double *ro) {
Chris@434 216 int hs = m_n/2 + 1;
Chris@434 217 for (int i = 0; i < hs; ++i) {
Chris@434 218 m_freq[i].r = ci[i*2];
Chris@434 219 m_freq[i].i = ci[i*2+1];
Chris@434 220 }
Chris@446 221 Kiss::kiss_fftri(m_iconf, m_freq, m_ro);
Chris@434 222 double scale = 1.0 / double(m_n);
Chris@434 223 for (int i = 0; i < m_n; ++i) {
Chris@445 224 ro[i] = m_ro[i] * scale;
Chris@434 225 }
Chris@434 226 }
Chris@434 227
Chris@434 228 private:
Chris@434 229 int m_n;
Chris@446 230 Kiss::kiss_fftr_cfg m_fconf;
Chris@446 231 Kiss::kiss_fftr_cfg m_iconf;
Chris@445 232 Kiss::kiss_fft_scalar *m_ri;
Chris@445 233 Kiss::kiss_fft_scalar *m_ro;
Chris@445 234 Kiss::kiss_fft_cpx *m_freq;
Chris@434 235 };
Chris@434 236
Chris@434 237 FFTReal::FFTReal(unsigned int n) :
Chris@434 238 m_d(new D(n))
Chris@434 239 {
Chris@434 240 }
Chris@434 241
Chris@434 242 FFTReal::~FFTReal()
Chris@434 243 {
Chris@434 244 delete m_d;
Chris@434 245 }
Chris@434 246
Chris@434 247 void
Chris@445 248 FFTReal::forward(const double *ri, double *co)
Chris@434 249 {
Chris@434 250 m_d->forward(ri, co);
Chris@434 251 }
Chris@434 252
Chris@434 253 void
Chris@445 254 FFTReal::inverse(const double *ci, double *ro)
Chris@434 255 {
Chris@434 256 m_d->inverse(ci, ro);
Chris@337 257 }
Chris@337 258
Chris@337 259 }
Chris@337 260
Chris@337 261 _VAMP_SDK_PLUGSPACE_END(FFT.cpp)
Chris@337 262