Debian Codegraph @

src/biquad.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #pragma once
5
6 #include <glib.h>
7
8 // Biquad filter types
9 typedef enum {
10 BIQUAD_TYPE_PEAKING,
11 BIQUAD_TYPE_LOW_SHELF,
12 BIQUAD_TYPE_HIGH_SHELF,
13 BIQUAD_TYPE_LOWPASS,
14 BIQUAD_TYPE_HIGHPASS,
15 BIQUAD_TYPE_BANDPASS,
16 BIQUAD_TYPE_NOTCH,
17 BIQUAD_TYPE_GAIN,
18 // First-order filters (6 dB/octave)
19 BIQUAD_TYPE_LOWPASS_1,
20 BIQUAD_TYPE_HIGHPASS_1,
21 BIQUAD_TYPE_LOW_SHELF_1,
22 BIQUAD_TYPE_HIGH_SHELF_1,
23 BIQUAD_TYPE_COUNT
24 } BiquadFilterType;
25
26 // Gain limits
27 #define GAIN_DB_LIMIT 24.0
28 #define DB_RANGE_NARROW 12.0
29
30 // Filter parameters
31 struct biquad_params {
32 BiquadFilterType type;
33 double freq; // Frequency in Hz (20-20000)
34 double q; // Q factor (0.1-10)
35 double gain_db; // Gain in dB for peaking/shelving
36 };
37
38 // Biquad coefficients (normalized so a0=1)
39 struct biquad_coeffs {
40 double b0, b1, b2; // numerator
41 double a1, a2; // denominator (a0 implicit = 1)
42 };
43
44 // Fixed-point conversion constants
45 #define BIQUAD_FIXED_POINT_SHIFT 28
46 #define BIQUAD_FIXED_POINT_SCALE (1L << BIQUAD_FIXED_POINT_SHIFT) // 268435456
47
48 // Calculate biquad coefficients from parameters
49 void biquad_calculate(
50 const struct biquad_params *params,
51 double sample_rate,
52 struct biquad_coeffs *coeffs
53 );
54
55 // Convert coefficients to fixed-point format for hardware
56 // Output: [b0, b1, b2, -a1, -a2] * 2^28
57 void biquad_to_fixed_point(
58 const struct biquad_coeffs *coeffs,
59 long fixed[5]
60 );
61
62 // Calculate frequency response magnitude in dB at given frequency
63 double biquad_response_db(
64 const struct biquad_coeffs *coeffs,
65 double freq,
66 double sample_rate
67 );
68
69 // Get human-readable filter type name
70 const char *biquad_type_name(BiquadFilterType type);
71
72 // Get array of filter type names (for creating enum elements)
73 const char **biquad_get_type_names(void);
74
75 // Check if filter type uses gain parameter
76 gboolean biquad_type_uses_gain(BiquadFilterType type);
77
78 // Check if filter type uses Q parameter
79 gboolean biquad_type_uses_q(BiquadFilterType type);
80
81 // Convert fixed-point coefficients back to floating-point
82 void biquad_from_fixed_point(
83 const long fixed[5],
84 struct biquad_coeffs *coeffs
85 );
86
87 // Analyze coefficients to determine filter parameters
88 // Returns TRUE if successful, FALSE if coefficients don't match a known filter
89 gboolean biquad_analyze(
90 const struct biquad_coeffs *coeffs,
91 double sample_rate,
92 struct biquad_params *params
93 );