Mercurial > hg > libxtract
changeset 102:7a5859764ccd
Fix for 'multiple symbol definitions' vs 'symbol not defined' dilemna. I think the solution is to wrap the globals in a struct, declare it in a header, and wrap with a definition guard, then define _once_ at library init time. (Sounds like a recipe for something...)
author | Jamie Bullock <jamie@postlude.co.uk> |
---|---|
date | Tue, 16 Oct 2007 09:37:06 +0000 |
parents | 6bbc25d9a34f |
children | 1cbbe5b5e461 |
files | src/fini.c src/init.c src/vector.c src/xtract_globals_private.h |
diffstat | 4 files changed, 58 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/src/fini.c Tue Oct 09 16:33:06 2007 +0000 +++ b/src/fini.c Tue Oct 16 09:37:06 2007 +0000 @@ -32,14 +32,14 @@ #endif { #ifdef XTRACT_FFT - if(spectrum_plan != NULL) - fftwf_destroy_plan(spectrum_plan); - if(autocorrelation_fft_plan_1 != NULL) - fftwf_destroy_plan(autocorrelation_fft_plan_1); - if(autocorrelation_fft_plan_2 != NULL) - fftwf_destroy_plan(autocorrelation_fft_plan_2); - if(dct_plan != NULL) - fftwf_destroy_plan(dct_plan); + if(fft_plans.spectrum_plan != NULL) + fftwf_destroy_plan(fft_plans.spectrum_plan); + if(fft_plans.autocorrelation_fft_plan_1 != NULL) + fftwf_destroy_plan(fft_plans.autocorrelation_fft_plan_1); + if(fft_plans.autocorrelation_fft_plan_2 != NULL) + fftwf_destroy_plan(fft_plans.autocorrelation_fft_plan_2); + if(fft_plans.dct_plan != NULL) + fftwf_destroy_plan(fft_plans.dct_plan); fftwf_cleanup(); #endif }
--- a/src/init.c Tue Oct 09 16:33:06 2007 +0000 +++ b/src/init.c Tue Oct 16 09:37:06 2007 +0000 @@ -28,6 +28,7 @@ #include <stdlib.h> #include "xtract/libxtract.h" +#define DEFINE_GLOBALS #include "xtract_globals_private.h" #ifdef XTRACT_FFT @@ -166,30 +167,30 @@ switch(feature_name){ case XTRACT_SPECTRUM: - if(spectrum_plan != NULL) - fftwf_destroy_plan(spectrum_plan); - spectrum_plan = + if(fft_plans.spectrum_plan != NULL) + fftwf_destroy_plan(fft_plans.spectrum_plan); + fft_plans.spectrum_plan = fftwf_plan_r2r_1d(N, input, output, FFTW_R2HC, optimisation); break; case XTRACT_AUTOCORRELATION_FFT: - if(autocorrelation_fft_plan_1 != NULL) - fftwf_destroy_plan(autocorrelation_fft_plan_1); - if(autocorrelation_fft_plan_2 != NULL) - fftwf_destroy_plan(autocorrelation_fft_plan_2); - autocorrelation_fft_plan_1 = + if(fft_plans.autocorrelation_fft_plan_1 != NULL) + fftwf_destroy_plan(fft_plans.autocorrelation_fft_plan_1); + if(fft_plans.autocorrelation_fft_plan_2 != NULL) + fftwf_destroy_plan(fft_plans.autocorrelation_fft_plan_2); + fft_plans.autocorrelation_fft_plan_1 = fftwf_plan_r2r_1d(N, input, output, FFTW_R2HC, optimisation); - autocorrelation_fft_plan_2 = + fft_plans.autocorrelation_fft_plan_2 = fftwf_plan_r2r_1d(N, input, output, FFTW_HC2R, optimisation); break; case XTRACT_DCT: - if(dct_plan != NULL) - fftwf_destroy_plan(dct_plan); - dct_plan = + if(fft_plans.dct_plan != NULL) + fftwf_destroy_plan(fft_plans.dct_plan); + fft_plans.dct_plan = fftwf_plan_r2r_1d(N, input, output, FFTW_REDFT00, optimisation); case XTRACT_MFCC: - if(dct_plan != NULL) - fftwf_destroy_plan(dct_plan); - dct_plan = + if(fft_plans.dct_plan != NULL) + fftwf_destroy_plan(fft_plans.dct_plan); + fft_plans.dct_plan = fftwf_plan_r2r_1d(N, output, output, FFTW_REDFT00, optimisation); break; } @@ -216,3 +217,14 @@ return XTRACT_SUCCESS; } +#ifdef __GNUC__ +__attribute__((constructor)) void init() +#else + void _init()ยท +#endif +{ + fft_plans.spectrum_plan = NULL; + fft_plans.autocorrelation_fft_plan_1 = NULL; + fft_plans.autocorrelation_fft_plan_2 = NULL; + fft_plans.dct_plan = NULL; +}
--- a/src/vector.c Tue Oct 09 16:33:06 2007 +0000 +++ b/src/vector.c Tue Oct 16 09:37:06 2007 +0000 @@ -65,7 +65,7 @@ XTRACT_CHECK_q; - if(spectrum_plan == NULL){ + if(fft_plans.spectrum_plan == NULL){ /* FIX: Not sure this should really be here. Might introduce * DEBUG_POST macro, or some kind of error handler, or leave it to the * caller... */ @@ -74,7 +74,7 @@ return XTRACT_NO_RESULT; } - fftwf_execute_r2r(spectrum_plan, input, rfft); + fftwf_execute_r2r(fft_plans.spectrum_plan, input, rfft); switch(vector){ @@ -186,7 +186,7 @@ time = (float *)calloc(M, sizeof(float)); time = memcpy(time, data, N * sizeof(float)); - fftwf_execute_r2r(autocorrelation_fft_plan_1, time, freq); + fftwf_execute_r2r(fft_plans.autocorrelation_fft_plan_1, time, freq); //plan = fftwf_plan_r2r_1d(M, time, freq, FFTW_R2HC, FFTW_ESTIMATE); //fftwf_execute(plan); @@ -203,7 +203,7 @@ //fftwf_execute(plan); - fftwf_execute_r2r(autocorrelation_fft_plan_2, freq, time); + fftwf_execute_r2r(fft_plans.autocorrelation_fft_plan_2, freq, time); /* Normalisation factor */ M = M * N; @@ -246,7 +246,7 @@ //plan = // fftwf_plan_r2r_1d(N, (float *) data, result, FFTW_REDFT00, FFTW_ESTIMATE); - fftwf_execute_r2r(dct_plan, (float *)data, result); + fftwf_execute_r2r(fft_plans.dct_plan, (float *)data, result); //fftwf_execute(plan); //fftwf_destroy_plan(plan);
--- a/src/xtract_globals_private.h Tue Oct 09 16:33:06 2007 +0000 +++ b/src/xtract_globals_private.h Tue Oct 16 09:37:06 2007 +0000 @@ -26,11 +26,24 @@ #ifdef XTRACT_FFT #include <fftw3.h> -extern fftwf_plan spectrum_plan, - autocorrelation_fft_plan_1, - autocorrelation_fft_plan_2, - dct_plan; +struct xtract_fft_plans_ { + + fftwf_plan spectrum_plan; + fftwf_plan autocorrelation_fft_plan_1; + fftwf_plan autocorrelation_fft_plan_2; + fftwf_plan dct_plan; + +}; + +#ifdef DEFINE_GLOBALS +#define GLOBAL +#else +#define GLOBAL extern #endif -#endif +GLOBAL struct xtract_fft_plans_ fft_plans; +#endif /* FFT */ + +#endif /* Header guard */ +