Home | History | Annotate | Download | only in tests
      1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  */
      5 
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <assert.h>
      9 #include <time.h>
     10 #include <memory.h>
     11 
     12 #include "crossover.h"
     13 #include "dsp_test_util.h"
     14 #include "dsp_util.h"
     15 #include "raw.h"
     16 
     17 #ifndef min
     18 #define min(a, b) ({ __typeof__(a) _a = (a);	\
     19 			__typeof__(b) _b = (b);	\
     20 			_a < _b ? _a : _b; })
     21 #endif
     22 
     23 static double tp_diff(struct timespec *tp2, struct timespec *tp1)
     24 {
     25 	return (tp2->tv_sec - tp1->tv_sec)
     26 		+ (tp2->tv_nsec - tp1->tv_nsec) * 1e-9;
     27 }
     28 
     29 void process(struct crossover *xo, int count, float *data0, float *data1,
     30 	     float *data2)
     31 {
     32 	int start;
     33 	for (start = 0; start < count; start += 2048)
     34 		crossover_process(xo, min(2048, count - start),
     35 				  data0 + start, data1 + start, data2 + start);
     36 }
     37 
     38 int main(int argc, char **argv)
     39 {
     40 	size_t frames;
     41 	float *data0, *data1, *data2;
     42 	double NQ = 44100 / 2;
     43 	struct timespec tp1, tp2;
     44 	struct crossover xo;
     45 
     46 	if (argc != 3 && argc != 6) {
     47 		printf("Usage: crossover_test input.raw output.raw "
     48 		       "[low.raw mid.raw high.raw]\n");
     49 		return 1;
     50 	}
     51 
     52 	dsp_enable_flush_denormal_to_zero();
     53 	dsp_util_clear_fp_exceptions();
     54 
     55 	data0 = read_raw(argv[1], &frames);
     56 	data1 = (float *)malloc(sizeof(float) * frames * 2);
     57 	data2 = (float *)malloc(sizeof(float) * frames * 2);
     58 
     59 	crossover_init(&xo, 400 / NQ, 4000 / NQ);
     60 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
     61 	process(&xo, frames, data0, data1, data2);
     62 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
     63 	printf("processing takes %g seconds for %zu samples\n",
     64 	       tp_diff(&tp2, &tp1), frames);
     65 
     66 	crossover_init(&xo, 400 / NQ, 4000 / NQ);
     67 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
     68 	process(&xo, frames, data0 + frames, data1 + frames,
     69 		data2 + frames);
     70 	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
     71 	printf("processing takes %g seconds for %zu samples\n",
     72 	       tp_diff(&tp2, &tp1), frames);
     73 
     74 	if (argc == 6) {
     75 		write_raw(argv[3], data0, frames);
     76 		write_raw(argv[4], data1, frames);
     77 		write_raw(argv[5], data2, frames);
     78 	}
     79 
     80 	int i;
     81 	for (i = 0; i < frames * 2; i++)
     82 		data0[i] += data1[i] + data2[i];
     83 	write_raw(argv[2], data0, frames);
     84 
     85 	free(data0);
     86 	free(data1);
     87 	free(data2);
     88 
     89 	dsp_util_print_fp_exceptions();
     90 	return 0;
     91 }
     92