diff options
Diffstat (limited to 'tests/test5/compute.c')
-rw-r--r-- | tests/test5/compute.c | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/tests/test5/compute.c b/tests/test5/compute.c index e8b03fc..d64f8c1 100644 --- a/tests/test5/compute.c +++ b/tests/test5/compute.c @@ -3,8 +3,9 @@ #include "fft.h" #include <math.h> +#define MIN_SAMPLES 256 #define MAX_SAMPLES 2048 -static inline float todB_a(const float *x); +//static inline float todB_a(const float *x); void compute_spectrum(float * data, int width, double rate, float *output); @@ -13,42 +14,49 @@ gfloat compute_level(const float *data, size_t nsamples, size_t nchan) { double rate=44100; //TODO dynamique size_t i; float input[MAX_SAMPLES], output[128]; - float level; + float value; + int gain=20, range=80; if (nsamples >= MAX_SAMPLES) { printf("WARN : nsamples >= MAX_SAMPLES : %i >= %i\n", nsamples, MAX_SAMPLES); nsamples=MAX_SAMPLES; } -/* Just return the max peak - for (i=0;i<nsamples;i+=nchan) { - val=((float *)data)[i]; - //printf("val==%i\n", val); - if (val<0) val=-val; - if (level<val) level=val; + if (nsamples < MIN_SAMPLES) { + printf("WARN : nsamples < MIN_SAMPLES : %i >= %i\n", nsamples, MIN_SAMPLES); + for (i=0;i<MIN_SAMPLES;i++) { + if ( (i/nsamples)%2==1 ) + input[i]=data[i/**nchan*/]; + else + input[i]=data[nsamples-i-1]; + } + nsamples=MIN_SAMPLES; + } else { + for (i=0;i<nsamples;i++) { + input[i]=data[i/**nchan*/]; + } } -*/ - for (i=0;i<nsamples;i++) { - input[i]=data[i/**nchan*/]; -/* printf("\r%f ", input[i]); - fflush(stdout); -*/ - } -// printf("\n"); compute_spectrum(input, nsamples, rate, output); //printf("%f\n", output[0]); - level=0.f; + value=0.f; for (i=1;i<128;i++) { - level+=output[i]; + value+=output[i]; } - level/=127.f; - return level; + // Mean value + value/=127.f; + // 0.0 to 1.0 range + value=(value + gain + range) / (double)range; + value=MAX(0.f,value); + value=MIN(value,1.f); + +// printf("DEBUG: nsamples=%8i value==%f\n", nsamples, value); + return value; } - +/* static inline float todB_a(const float *x){ return (float)((*(int32_t *)x)&0x7fffffff) * 7.17711438e-7f -764.6161886f; } - +*/ // Adapted from Audacity void compute_spectrum(float * data, int width, double rate, float *output) { |