diff --git a/bat/analyze.c b/bat/analyze.c index 60e2d1c..5cfdac3 100644 --- a/bat/analyze.c +++ b/bat/analyze.c @@ -256,6 +256,20 @@ static int reorder_data(struct bat *bat) return 0; } +/* truncate sample frames for faster FFT analysis process */ +static int truncate_frames(struct bat *bat) +{ + int shift = SHIFT_MAX; + + for (; shift > SHIFT_MIN; shift--) + if (bat->frames & (1 << shift)) { + bat->frames = 1 << shift; + return 0; + } + + return -EINVAL; +} + int analyze_capture(struct bat *bat) { int err = 0; @@ -263,6 +277,13 @@ int analyze_capture(struct bat *bat) int c; struct analyze a; + err = truncate_frames(bat); + if (err < 0) { + fprintf(bat->err, _("Invalid frame number for analysis: %d\n"), + bat->frames); + return err; + } + fprintf(bat->log, _("\nBAT analysis: signal has %d frames at %d Hz,"), bat->frames, bat->rate); fprintf(bat->log, _(" %d channels, %d bytes per sample.\n"), diff --git a/bat/common.h b/bat/common.h index c04452d..7346d9f 100644 --- a/bat/common.h +++ b/bat/common.h @@ -63,6 +63,12 @@ #define FOUND_DC (1<<1) #define FOUND_WRONG_PEAK (1<<0) +/* Truncate sample frames to (1 << N), for faster FFT analysis process. The + * valid range of N is (SHIFT_MIN, SHIFT_MAX). When N increases, the analysis + * will be more time-consuming, and the result will be more accurate. */ +#define SHIFT_MAX (sizeof(int) * 8 - 2) +#define SHIFT_MIN 8 + struct wav_header { unsigned int magic; /* 'RIFF' */ unsigned int length; /* file len */