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 "crossover2.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
23static 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
29void process(struct crossover2 *xo2, int count, float *data0L, float *data0R,
30	     float *data1L, float *data1R, float *data2L, float *data2R)
31{
32	int start;
33	for (start = 0; start < count; start += 2048)
34		crossover2_process(xo2, min(2048, count - start),
35				   data0L + start, data0R + start,
36				   data1L + start, data1R + start,
37				   data2L + start, data2R + start);
38}
39
40int main(int argc, char **argv)
41{
42	size_t frames;
43	float *data0, *data1, *data2;
44	double NQ = 44100 / 2;
45	struct timespec tp1, tp2;
46	struct crossover2 xo2;
47
48	if (argc != 3 && argc != 6) {
49		printf("Usage: crossover2_test input.raw output.raw "
50		       "[low.raw mid.raw high.raw]\n");
51		return 1;
52	}
53
54	dsp_enable_flush_denormal_to_zero();
55	dsp_util_clear_fp_exceptions();
56
57	data0 = read_raw(argv[1], &frames);
58	data1 = (float *)malloc(sizeof(float) * frames * 2);
59	data2 = (float *)malloc(sizeof(float) * frames * 2);
60
61	crossover2_init(&xo2, 400 / NQ, 4000 / NQ);
62	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
63	process(&xo2, frames, data0, data0 + frames, data1, data1 + frames,
64		data2, data2 + frames);
65	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
66	printf("processing takes %g seconds for %zu samples\n",
67	       tp_diff(&tp2, &tp1), frames * 2);
68
69	if (argc == 6) {
70		write_raw(argv[3], data0, frames);
71		write_raw(argv[4], data1, frames);
72		write_raw(argv[5], data2, frames);
73	}
74
75	int i;
76	for (i = 0; i < frames * 2; i++)
77		data0[i] += data1[i] + data2[i];
78	write_raw(argv[2], data0, frames);
79
80	free(data0);
81	free(data1);
82	free(data2);
83
84	dsp_util_print_fp_exceptions();
85	return 0;
86}
87