intercommit

This commit is contained in:
2025-03-08 19:30:56 +01:00
parent bae5ae5c1b
commit b1c9e28844
2 changed files with 75 additions and 70 deletions
+65 -60
View File
@@ -11,79 +11,84 @@ float apply_preemphasis(ResistorCapacitor *filter, float sample) {
} }
void init_lpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate) { void init_lpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate) {
float x = (cutoffFreq * M_2PI) / sampleRate; // Calculate intermediate values
float sinX = sin(x); float omega = 2.0f * M_PI * cutoffFreq / sampleRate;
float y = sinX / (qFactor*2.0f); float sn = sinf(omega);
float cosX = cos(x); float cs = cosf(omega);
float z = (1.0f-cosX)/2.0f; float alpha = sn / (2.0f * qFactor);
float _a0 = y + 1.0f; // Calculate coefficients
float _a1 = cosX * -2.0f; float b0 = (1.0f - cs) * 0.5f;
float _a2 = 1.0f - y; float b1 = 1.0f - cs;
float _b0 = z; float b2 = (1.0f - cs) * 0.5f;
float _b1 = 1.0f - cosX; float a0 = 1.0f + alpha;
float _b2 = z; float a1 = -2.0f * cs;
float a2 = 1.0f - alpha;
filter->y2 = 0; // Normalize by a0
filter->y1 = 0; filter->b0 = b0 / a0;
filter->x2 = 0; filter->b1 = b1 / a0;
filter->x1 = 0; filter->b2 = b2 / a0;
filter->b0 = _b0/_a0; filter->a1 = a1 / a0;
filter->b1 = _b1/_a0; filter->a2 = a2 / a0;
filter->b2 = _b2/_a0;
filter->a1 = -_a1/_a0; // Initialize state variables
filter->a2 = -_a2/_a0; filter->x1 = 0.0f;
filter->x2 = 0.0f;
filter->y1 = 0.0f;
filter->y2 = 0.0f;
} }
void init_hpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate) { void init_hpf(BiquadFilter* filter, float cutoffFreq, float qFactor, float sampleRate) {
float x = (cutoffFreq * M_2PI) / sampleRate; float omega = 2.0f * M_PI * cutoffFreq / sampleRate;
float sinX = sin(x); float alpha = sinf(omega) / (2.0f * qFactor);
float y = sinX / (qFactor*2.0f); float cosw = cosf(omega);
float cosX = cos(x);
float z = (1.0f-cosX)/2.0f;
float _a0 = y + 1.0f; float b0 = (1.0f + cosw) / 2.0f;
float _a1 = cosX * -2.0f; float b1 = -(1.0f + cosw);
float _a2 = 1.0f - y; float b2 = (1.0f + cosw) / 2.0f;
float _b0 = 1.0f - z; float a0 = 1.0f + alpha;
float _b1 = cosX * -2.0f; float a1 = -2.0f * cosw;
float _b2 = 1.0f - z; float a2 = 1.0f - alpha;
filter->y2 = 0; // Normalize by a0
filter->y1 = 0; filter->b0 = b0 / a0;
filter->x2 = 0; filter->b1 = b1 / a0;
filter->x1 = 0; filter->b2 = b2 / a0;
filter->b0 = _b0/_a0; filter->a1 = a1 / a0;
filter->b1 = _b1/_a0; filter->a2 = a2 / a0;
filter->b2 = _b2/_a0;
filter->a1 = -_a1/_a0; // Initialize state variables
filter->a2 = -_a2/_a0; filter->x1 = 0.0f;
filter->x2 = 0.0f;
filter->y1 = 0.0f;
filter->y2 = 0.0f;
} }
void init_bpf(BiquadFilter* filter, float centerFreq, float qFactor, float sampleRate) { void init_bpf(BiquadFilter* filter, float centerFreq, float qFactor, float sampleRate) {
float x = (centerFreq * M_2PI) / sampleRate; float omega = 2.0f * M_PI * centerFreq / sampleRate;
float sinX = sin(x); float alpha = sinf(omega) / (2.0f * qFactor);
float cosX = cos(x); float cosw = cosf(omega);
float alpha = sinX / (2.0f * qFactor); float b0 = alpha;
float b1 = 0.0f;
float b2 = -alpha;
float a0 = 1.0f + alpha;
float a1 = -2.0f * cosw;
float a2 = 1.0f - alpha;
float _a0 = 1.0f + alpha; // Normalize by a0
float _a1 = -2.0f * cosX; filter->b0 = b0 / a0;
float _a2 = 1.0f - alpha; filter->b1 = b1 / a0;
float _b0 = alpha; filter->b2 = b2 / a0;
float _b1 = 0.0f; filter->a1 = a1 / a0;
float _b2 = -alpha; filter->a2 = a2 / a0;
filter->y2 = 0; // Initialize state variables
filter->y1 = 0; filter->x1 = 0.0f;
filter->x2 = 0; filter->x2 = 0.0f;
filter->x1 = 0; filter->y1 = 0.0f;
filter->y2 = 0.0f;
filter->b0 = _b0 / _a0;
filter->b1 = _b1 / _a0;
filter->b2 = _b2 / _a0;
filter->a1 = -_a1 / _a0;
filter->a2 = -_a2 / _a0;
} }
float apply_frequency_filter(BiquadFilter* filter, float input) { float apply_frequency_filter(BiquadFilter* filter, float input) {
+4 -4
View File
@@ -34,7 +34,7 @@
#include <pulse/error.h> #include <pulse/error.h>
#define MASTER_VOLUME 1.0f // Volume of everything combined, for calibration #define MASTER_VOLUME 1.0f // Volume of everything combined, for calibration
#define AUDIO_VOLUME 1.5f // Audio volume, before clipper #define AUDIO_VOLUME 1.0f // Audio volume, before clipper
#define MONO_VOLUME 0.45f // L+R Signal #define MONO_VOLUME 0.45f // L+R Signal
#define PILOT_VOLUME 0.09f // 19 KHz Pilot #define PILOT_VOLUME 0.09f // 19 KHz Pilot
#define STEREO_VOLUME 0.45f // L-R signal, should be same as MONO #define STEREO_VOLUME 0.45f // L-R signal, should be same as MONO
@@ -42,7 +42,7 @@
#define MPX_VOLUME 1.0f // Passtrough #define MPX_VOLUME 1.0f // Passtrough
#define MPX_CLIPPER_THRESHOLD 1.0f #define MPX_CLIPPER_THRESHOLD 1.0f
#define LPF_CUTOFF 15000 // Should't need to be changed #define LPF_CUTOFF 10000 // Should't need to be changed
volatile sig_atomic_t to_run = 1; volatile sig_atomic_t to_run = 1;
@@ -372,8 +372,8 @@ int main(int argc, char **argv) {
init_preemphasis(&preemp_r, preemphasis_tau, SAMPLE_RATE); init_preemphasis(&preemp_r, preemphasis_tau, SAMPLE_RATE);
BiquadFilter lpf_l, lpf_r; BiquadFilter lpf_l, lpf_r;
init_lpf(&lpf_l, LPF_CUTOFF, 1.25f, SAMPLE_RATE); init_lpf(&lpf_l, LPF_CUTOFF, 0.707f, SAMPLE_RATE);
init_lpf(&lpf_r, LPF_CUTOFF, 1.25f, SAMPLE_RATE); init_lpf(&lpf_r, LPF_CUTOFF, 0.707f, SAMPLE_RATE);
// #endregion // #endregion
signal(SIGINT, stop); signal(SIGINT, stop);