annotate src/init.c @ 152:07aa4d2af390

removed extraneous files
author Jamie Bullock <jamie@jamiebullock.com>
date Wed, 09 Jan 2013 23:22:07 +0000
parents 9283aaf1ffb8
children 71870680f7c1
rev   line source
jamie@141 1 /*
jamie@141 2 * Copyright (C) 2012 Jamie Bullock
jamie@140 3 *
jamie@141 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jamie@141 5 * of this software and associated documentation files (the "Software"), to
jamie@141 6 * deal in the Software without restriction, including without limitation the
jamie@141 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
jamie@141 8 * sell copies of the Software, and to permit persons to whom the Software is
jamie@141 9 * furnished to do so, subject to the following conditions:
jamie@1 10 *
jamie@141 11 * The above copyright notice and this permission notice shall be included in
jamie@141 12 * all copies or substantial portions of the Software.
jamie@1 13 *
jamie@141 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jamie@141 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jamie@141 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jamie@141 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jamie@141 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jamie@141 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
jamie@141 20 * IN THE SOFTWARE.
jamie@1 21 *
jamie@1 22 */
jamie@1 23
jamie@107 24 /* init.c: defines initialisation and free functions. Also contains library constructor routine. */
jamie@1 25
jamie@98 26 #ifdef HAVE_CONFIG_H
jamie@150 27 #include <config.h>
jamie@98 28 #endif
jamie@98 29
jamie@1 30 #include <math.h>
jamie@26 31 #include <stdlib.h>
jamie@140 32 #include <stdio.h>
jamie@140 33
jamie@150 34 #include "fft.h"
jamie@1 35
jamie@98 36 #include "xtract/libxtract.h"
jamie@107 37 #include "xtract_window_private.h"
jamie@102 38 #define DEFINE_GLOBALS
jamie@98 39 #include "xtract_globals_private.h"
jamie@98 40
jamie@98 41
jamie@150 42
jamie@150 43 #ifdef USE_OOURA
jamie@150 44 void xtract_init_ooura_data(xtract_ooura_data *ooura_data, unsigned int N)
jamie@150 45 {
jamie@150 46 ooura_data->ooura_ip = (int *)calloc((2 + sqrt(N)), sizeof(int));
jamie@150 47 ooura_data->ooura_w = (double *)calloc((N - 1), sizeof(double));
jamie@150 48 ooura_data->initialised = true;
jamie@150 49 }
jamie@150 50
jamie@150 51 void xtract_free_ooura_data(xtract_ooura_data *ooura_data)
jamie@150 52 {
jamie@150 53 free(ooura_data->ooura_ip);
jamie@150 54 free(ooura_data->ooura_w);
jamie@150 55 ooura_data->ooura_ip = NULL;
jamie@150 56 ooura_data->ooura_w = NULL;
jamie@150 57 ooura_data->initialised = false;
jamie@150 58 }
jamie@150 59
jamie@150 60 int xtract_init_ooura_(int N, int feature_name)
jamie@150 61 {
jamie@150 62
jamie@150 63 int M = N >> 1;
jamie@150 64
jamie@150 65 if(feature_name == XTRACT_AUTOCORRELATION_FFT)
jamie@150 66 {
jamie@150 67 M = N; /* allow for zero padding */
jamie@150 68 }
jamie@150 69
jamie@150 70 switch(feature_name)
jamie@150 71 {
jamie@150 72 case XTRACT_SPECTRUM:
jamie@150 73 if(ooura_data_spectrum.initialised)
jamie@150 74 {
jamie@150 75 xtract_free_ooura_data(&ooura_data_spectrum);
jamie@150 76 }
jamie@150 77 xtract_init_ooura_data(&ooura_data_spectrum, M);
jamie@150 78 break;
jamie@150 79 case XTRACT_AUTOCORRELATION_FFT:
jamie@150 80 if(ooura_data_autocorrelation_fft.initialised)
jamie@150 81 {
jamie@150 82 xtract_free_ooura_data(&ooura_data_autocorrelation_fft);
jamie@150 83 }
jamie@150 84 xtract_init_ooura_data(&ooura_data_autocorrelation_fft, M);
jamie@150 85 break;
jamie@150 86 case XTRACT_DCT:
jamie@150 87 if(ooura_data_dct.initialised)
jamie@150 88 {
jamie@150 89 xtract_free_ooura_data(&ooura_data_dct);
jamie@150 90 }
jamie@150 91 xtract_init_ooura_data(&ooura_data_dct, M);
jamie@150 92 case XTRACT_MFCC:
jamie@150 93 if(ooura_data_mfcc.initialised)
jamie@150 94 {
jamie@150 95 xtract_free_ooura_data(&ooura_data_mfcc);
jamie@150 96 }
jamie@150 97 xtract_init_ooura_data(&ooura_data_mfcc, M);
jamie@150 98 break;
jamie@150 99 }
jamie@150 100
jamie@150 101 return XTRACT_SUCCESS;
jamie@150 102 }
jamie@150 103
jamie@150 104 void xtract_free_ooura_(void)
jamie@150 105 {
jamie@150 106 if(ooura_data_spectrum.initialised)
jamie@150 107 {
jamie@150 108 xtract_free_ooura_data(&ooura_data_spectrum);
jamie@150 109 }
jamie@150 110 if(ooura_data_autocorrelation_fft.initialised)
jamie@150 111 {
jamie@150 112 xtract_free_ooura_data(&ooura_data_autocorrelation_fft);
jamie@150 113 }
jamie@150 114 if(ooura_data_dct.initialised)
jamie@150 115 {
jamie@150 116 xtract_free_ooura_data(&ooura_data_dct);
jamie@150 117 }
jamie@150 118 if(ooura_data_mfcc.initialised)
jamie@150 119 {
jamie@150 120 xtract_free_ooura_data(&ooura_data_mfcc);
jamie@150 121 }
jamie@150 122 }
jamie@150 123
jamie@150 124 #else
jamie@150 125
jamie@150 126 void xtract_init_vdsp_data(xtract_vdsp_data *vdsp_data, unsigned int N)
jamie@150 127 {
jamie@150 128 vdsp_data->setup = vDSP_create_fftsetupD(log2f(N), FFT_RADIX2);
jamie@150 129 vdsp_data->fft.realp = (double *) malloc((N >> 1) * sizeof(double));
jamie@150 130 vdsp_data->fft.imagp = (double *) malloc((N >> 1) * sizeof(double));
jamie@150 131 vdsp_data->log2N = log2f(N);
jamie@150 132 vdsp_data->initialised = true;
jamie@150 133 }
jamie@150 134
jamie@150 135 void xtract_free_vdsp_data(xtract_vdsp_data *vdsp_data)
jamie@150 136 {
jamie@150 137 free(vdsp_data->fft.realp);
jamie@150 138 free(vdsp_data->fft.imagp);
jamie@150 139 vDSP_destroy_fftsetupD(vdsp_data->setup);
jamie@150 140 vdsp_data->fft.realp = NULL;
jamie@150 141 vdsp_data->fft.imagp = NULL;
jamie@150 142 vdsp_data->initialised = false;
jamie@150 143 }
jamie@150 144
jamie@150 145 int xtract_init_vdsp_(int N, int feature_name)
jamie@150 146 {
jamie@150 147
jamie@150 148 int M = N >> 1;
jamie@150 149
jamie@150 150 if(feature_name == XTRACT_AUTOCORRELATION_FFT)
jamie@150 151 {
jamie@150 152 M = N; /* allow for zero padding */
jamie@150 153 }
jamie@150 154
jamie@150 155 switch(feature_name)
jamie@150 156 {
jamie@150 157 case XTRACT_SPECTRUM:
jamie@150 158 if(vdsp_data_spectrum.initialised)
jamie@150 159 {
jamie@150 160 xtract_free_vdsp_data(&vdsp_data_spectrum);
jamie@150 161 }
jamie@150 162 xtract_init_vdsp_data(&vdsp_data_spectrum, M);
jamie@150 163 break;
jamie@150 164 case XTRACT_AUTOCORRELATION_FFT:
jamie@150 165 if(vdsp_data_autocorrelation_fft.initialised)
jamie@150 166 {
jamie@150 167 xtract_free_vdsp_data(&vdsp_data_autocorrelation_fft);
jamie@150 168 }
jamie@150 169 xtract_init_vdsp_data(&vdsp_data_autocorrelation_fft, M);
jamie@150 170 break;
jamie@150 171 case XTRACT_DCT:
jamie@150 172 if(vdsp_data_dct.initialised)
jamie@150 173 {
jamie@150 174 xtract_free_vdsp_data(&vdsp_data_dct);
jamie@150 175 }
jamie@150 176 xtract_init_vdsp_data(&vdsp_data_dct, M);
jamie@150 177 case XTRACT_MFCC:
jamie@150 178 if(vdsp_data_mfcc.initialised)
jamie@150 179 {
jamie@150 180 xtract_free_vdsp_data(&vdsp_data_mfcc);
jamie@150 181 }
jamie@150 182 xtract_init_vdsp_data(&vdsp_data_mfcc, M);
jamie@150 183 break;
jamie@150 184 }
jamie@150 185
jamie@150 186 return XTRACT_SUCCESS;
jamie@150 187 }
jamie@150 188
jamie@150 189 void xtract_free_vdsp_(void)
jamie@150 190 {
jamie@150 191 if(vdsp_data_spectrum.initialised)
jamie@150 192 {
jamie@150 193 xtract_free_vdsp_data(&vdsp_data_spectrum);
jamie@150 194 }
jamie@150 195 if(vdsp_data_autocorrelation_fft.initialised)
jamie@150 196 {
jamie@150 197 xtract_free_vdsp_data(&vdsp_data_autocorrelation_fft);
jamie@150 198 }
jamie@150 199 if(vdsp_data_dct.initialised)
jamie@150 200 {
jamie@150 201 xtract_free_vdsp_data(&vdsp_data_dct);
jamie@150 202 }
jamie@150 203 if(vdsp_data_mfcc.initialised)
jamie@150 204 {
jamie@150 205 xtract_free_vdsp_data(&vdsp_data_mfcc);
jamie@150 206 }
jamie@150 207 }
jamie@150 208
jamie@150 209
jamie@150 210 #endif
jamie@150 211
jamie@150 212 int xtract_init_fft(int N, int feature_name)
jamie@150 213 {
jamie@150 214 if(!xtract_is_poweroftwo(N))
jamie@150 215 {
jamie@150 216 fprintf(stderr,
jamie@150 217 "libxtract: error: only power-of-two FFT sizes are supported by Ooura FFT.\n");
jamie@150 218 exit(EXIT_FAILURE);
jamie@150 219 }
jamie@150 220 #ifdef USE_OOURA
jamie@150 221 return xtract_init_ooura_(N, feature_name);
jamie@150 222 #else
jamie@150 223 return xtract_init_vdsp_(N, feature_name);
jamie@150 224 #endif
jamie@150 225 }
jamie@150 226
jamie@150 227 void xtract_free_fft(void)
jamie@150 228 {
jamie@150 229 #ifdef USE_OOURA
jamie@150 230 xtract_free_ooura_();
jamie@150 231 #else
jamie@150 232 xtract_free_vdsp_();
jamie@150 233 #endif
jamie@150 234 }
jamie@150 235
jamie@150 236
jamie@150 237 int xtract_init_bark(int N, double sr, int *band_limits)
jamie@150 238 {
jamie@150 239
jamie@150 240 double edges[] = {0, 100, 200, 300, 400, 510, 630, 770, 920, 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150, 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500, 20500, 27000}; /* Takes us up to sr = 54kHz (CCRMA: JOS)*/
jamie@150 241
jamie@150 242 int bands = XTRACT_BARK_BANDS;
jamie@150 243
jamie@150 244 while(bands--)
jamie@150 245 band_limits[bands] = edges[bands] / sr * N;
jamie@150 246 /*FIX shohuld use rounding, but couldn't get it to work */
jamie@150 247
jamie@150 248 return XTRACT_SUCCESS;
jamie@150 249 }
jamie@150 250
jamie@146 251 int xtract_init_mfcc(int N, double nyquist, int style, double freq_min, double freq_max, int freq_bands, double **fft_tables)
jamie@140 252 {
jamie@98 253
jamie@140 254 int n, i, k, *fft_peak, M, next_peak;
jamie@146 255 double norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val,
jamie@107 256 freq_bw_mel, *mel_peak, *height_norm, *lin_peak;
jamie@1 257
jamie@1 258 mel_peak = height_norm = lin_peak = NULL;
jamie@1 259 fft_peak = NULL;
jamie@140 260 norm = 1;
jamie@1 261
jamie@1 262 mel_freq_max = 1127 * log(1 + freq_max / 700);
jamie@1 263 mel_freq_min = 1127 * log(1 + freq_min / 700);
jamie@1 264 freq_bw_mel = (mel_freq_max - mel_freq_min) / freq_bands;
jamie@1 265
jamie@146 266 mel_peak = (double *)malloc((freq_bands + 2) * sizeof(double));
jamie@1 267 /* +2 for zeros at start and end */
jamie@146 268 lin_peak = (double *)malloc((freq_bands + 2) * sizeof(double));
jamie@1 269 fft_peak = (int *)malloc((freq_bands + 2) * sizeof(int));
jamie@146 270 height_norm = (double *)malloc(freq_bands * sizeof(double));
jamie@1 271
jamie@140 272 if(mel_peak == NULL || height_norm == NULL ||
jamie@107 273 lin_peak == NULL || fft_peak == NULL)
jamie@107 274 return XTRACT_MALLOC_FAILED;
jamie@107 275
jamie@1 276 M = N >> 1;
jamie@1 277
jamie@1 278 mel_peak[0] = mel_freq_min;
danstowell@95 279 lin_peak[0] = freq_min; // === 700 * (exp(mel_peak[0] / 1127) - 1);
jamie@1 280 fft_peak[0] = lin_peak[0] / nyquist * M;
jamie@1 281
jamie@1 282
jamie@140 283 for (n = 1; n < freq_bands + 2; n++)
jamie@140 284 {
jamie@140 285 //roll out peak locations - mel, linear and linear on fft window scale
jamie@1 286 mel_peak[n] = mel_peak[n - 1] + freq_bw_mel;
jamie@1 287 lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1);
jamie@1 288 fft_peak[n] = lin_peak[n] / nyquist * M;
jamie@1 289 }
jamie@1 290
jamie@140 291 for (n = 0; n < freq_bands; n++)
jamie@140 292 {
danstowell@100 293 //roll out normalised gain of each peak
jamie@140 294 if (style == XTRACT_EQUAL_GAIN)
jamie@140 295 {
jamie@140 296 height = 1;
jamie@1 297 norm_fact = norm;
jamie@1 298 }
jamie@140 299 else
jamie@140 300 {
jamie@1 301 height = 2 / (lin_peak[n + 2] - lin_peak[n]);
jamie@1 302 norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0]));
jamie@1 303 }
jamie@1 304 height_norm[n] = height * norm_fact;
jamie@1 305 }
jamie@1 306
jamie@1 307 i = 0;
jamie@107 308
jamie@140 309 for(n = 0; n < freq_bands; n++)
jamie@140 310 {
jamie@107 311
jamie@107 312 // calculate the rise increment
danstowell@95 313 if(n==0)
danstowell@95 314 inc = height_norm[n] / fft_peak[n];
danstowell@95 315 else
jamie@1 316 inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]);
jamie@140 317 val = 0;
jamie@107 318
jamie@107 319 // zero the start of the array
jamie@107 320 for(k = 0; k < i; k++)
jamie@146 321 fft_tables[n][k] = 0.0;
jamie@107 322
jamie@107 323 // fill in the rise
jamie@140 324 for(; i <= fft_peak[n]; i++)
jamie@140 325 {
jamie@1 326 fft_tables[n][i] = val;
jamie@1 327 val += inc;
jamie@1 328 }
jamie@107 329
danstowell@95 330 // calculate the fall increment
jamie@1 331 inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]);
jamie@107 332
jamie@1 333 val = 0;
jamie@107 334 next_peak = fft_peak[n + 1];
jamie@107 335
jamie@140 336 // reverse fill the 'fall'
jamie@140 337 for(i = next_peak; i > fft_peak[n]; i--)
jamie@140 338 {
jamie@1 339 fft_tables[n][i] = val;
jamie@1 340 val += inc;
jamie@1 341 }
jamie@39 342
jamie@107 343 // zero the rest of the array
jamie@107 344 for(k = next_peak + 1; k < N; k++)
jamie@146 345 fft_tables[n][k] = 0.0;
jamie@1 346 }
jamie@1 347
jamie@98 348
jamie@98 349 /* Initialise the fft_plan for the DCT */
jamie@140 350 /*
jamie@140 351 * Ooura doesn't support non power-of-two DCT
jamie@98 352 xtract_init_fft(freq_bands, XTRACT_MFCC);
jamie@140 353 */
jamie@98 354
jamie@1 355 free(mel_peak);
jamie@1 356 free(lin_peak);
jamie@1 357 free(height_norm);
jamie@1 358 free(fft_peak);
jamie@1 359
jamie@56 360 return XTRACT_SUCCESS;
jamie@1 361
jamie@1 362 }
jamie@1 363
jamie@146 364 double *xtract_init_window(const int N, const int type)
jamie@140 365 {
jamie@146 366 double *window;
jamie@107 367
jamie@146 368 window = malloc(N * sizeof(double));
jamie@107 369
jamie@140 370 switch (type)
jamie@140 371 {
jamie@140 372 case XTRACT_GAUSS:
jamie@140 373 gauss(window, N, 0.4);
jamie@140 374 break;
jamie@140 375 case XTRACT_HAMMING:
jamie@140 376 hamming(window, N);
jamie@140 377 break;
jamie@140 378 case XTRACT_HANN:
jamie@140 379 hann(window, N);
jamie@140 380 break;
jamie@140 381 case XTRACT_BARTLETT:
jamie@140 382 bartlett(window, N);
jamie@140 383 break;
jamie@140 384 case XTRACT_TRIANGULAR:
jamie@140 385 triangular(window, N);
jamie@140 386 break;
jamie@140 387 case XTRACT_BARTLETT_HANN:
jamie@140 388 bartlett_hann(window, N);
jamie@140 389 break;
jamie@140 390 case XTRACT_BLACKMAN:
jamie@140 391 blackman(window, N);
jamie@140 392 break;
jamie@140 393 case XTRACT_KAISER:
jamie@140 394 kaiser(window, N, 3 * PI);
jamie@140 395 break;
jamie@140 396 case XTRACT_BLACKMAN_HARRIS:
jamie@140 397 blackman_harris(window, N);
jamie@140 398 break;
jamie@140 399 default:
jamie@140 400 hann(window, N);
jamie@140 401 break;
jamie@107 402 }
jamie@107 403
jamie@107 404 return window;
jamie@107 405 }
jamie@107 406
jamie@146 407 void xtract_free_window(double *window)
jamie@140 408 {
jamie@107 409 free(window);
jamie@107 410 }
jamie@107 411
jamie@102 412 #ifdef __GNUC__
jamie@102 413 __attribute__((constructor)) void init()
jamie@102 414 #else
jamie@140 415 void _init()ยท
jamie@102 416 #endif
jamie@102 417 {
jamie@150 418 #ifdef USE_OOURA
jamie@140 419 ooura_data_dct.initialised = false;
jamie@140 420 ooura_data_spectrum.initialised = false;
jamie@140 421 ooura_data_autocorrelation_fft.initialised = false;
jamie@140 422 ooura_data_mfcc.initialised = false;
jamie@150 423 #else
jamie@150 424 vdsp_data_dct.initialised = false;
jamie@150 425 vdsp_data_spectrum.initialised = false;
jamie@150 426 vdsp_data_autocorrelation_fft.initialised = false;
jamie@150 427 vdsp_data_mfcc.initialised = false;
jamie@150 428 #endif
jamie@102 429 }