]> git.alsa-project.org Git - alsa-utils.git/commitdiff
alsabat: truncate sample frames for faster FFT analysis
authorLu, Han <han.lu@intel.com>
Mon, 29 Feb 2016 02:33:45 +0000 (10:33 +0800)
committerTakashi Iwai <tiwai@suse.de>
Tue, 1 Mar 2016 09:31:00 +0000 (10:31 +0100)
Truncate the sample frames to powers of 2, since the FFTW algorithm
runs especially fast in this case, and other sizes may be computed
by means of a slow, general-purpose algorithm.
In my test environment applying the patch, a sound clip of 33072
frames is cut off to 32768 frames before analysis, and the time
cost is reduced from 6.128s to 0.224s.

Signed-off-by: Lu, Han <han.lu@intel.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
bat/analyze.c
bat/common.h

index 60e2d1c945b37ddac4375389669531e2ce8e6642..5cfdac38f19edb45669dc66cbb361263f3645330 100644 (file)
@@ -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"),
index c04452db70b0f8ea3079ac71969844bc30c976cc..7346d9fab682b598273d08211aa3b0136b6543d7 100644 (file)
 #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 */