summaryrefslogtreecommitdiffstats
path: root/spa/plugins/audioconvert/crossover.c
blob: 7575833cdbe5e96759144865ce92b3224008706c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <float.h>
#include <string.h>

#include "crossover.h"

void lr4_set(struct lr4 *lr4, enum biquad_type type, float freq)
{
	biquad_set(&lr4->bq, type, freq);
	lr4->x1 = 0;
	lr4->x2 = 0;
	lr4->y1 = 0;
	lr4->y2 = 0;
	lr4->z1 = 0;
	lr4->z2 = 0;
	lr4->active = true;
}

void lr4_process(struct lr4 *lr4, float *dst, const float *src, const float vol, int samples)
{
	float lx1 = lr4->x1;
	float lx2 = lr4->x2;
	float ly1 = lr4->y1;
	float ly2 = lr4->y2;
	float lz1 = lr4->z1;
	float lz2 = lr4->z2;
	float lb0 = lr4->bq.b0;
	float lb1 = lr4->bq.b1;
	float lb2 = lr4->bq.b2;
	float la1 = lr4->bq.a1;
	float la2 = lr4->bq.a2;
	int i;

	if (vol == 0.0f) {
		memset(dst, 0, samples * sizeof(float));
		return;
	} else if (!lr4->active) {
		if (src != dst || vol != 1.0f) {
			for (i = 0; i < samples; i++)
				dst[i] = src[i] * vol;
		}
		return;
	}

	for (i = 0; i < samples; i++) {
		float x, y, z;
		x = src[i];
		y = lb0*x + lb1*lx1 + lb2*lx2 - la1*ly1 - la2*ly2;
		z = lb0*y + lb1*ly1 + lb2*ly2 - la1*lz1 - la2*lz2;
		lx2 = lx1;
		lx1 = x;
		ly2 = ly1;
		ly1 = y;
		lz2 = lz1;
		lz1 = z;
		dst[i] = z * vol;
	}
#define F(x) (-FLT_MIN < (x) && (x) < FLT_MIN ? 0.0f : (x))
	lr4->x1 = F(lx1);
	lr4->x2 = F(lx2);
	lr4->y1 = F(ly1);
	lr4->y2 = F(ly2);
	lr4->z1 = F(lz1);
	lr4->z2 = F(lz2);
#undef F
}