diff --git a/lib/filters.c b/lib/filters.c index c32558d..3483052 100644 --- a/lib/filters.c +++ b/lib/filters.c @@ -12,32 +12,15 @@ float apply_pre_emphasis(Emphasis *pe, float sample) { } void init_low_pass_filter(LowPassFilter *lp, float cutoff_frequency, float sample_rate) { - for (int i = 0; i < FIR_TAPS; i++) { - for (int j = 0; j < FIR_PHASES; j++) { - int mi = i * FIR_PHASES + j + 1; - float sincpos = mi - (((FIR_TAPS * FIR_PHASES) + 1.0f) / 2.0f); - float firlowpass = (sincpos == 0.0f) ? 1.0f : sinf(M_2PI * cutoff_frequency * sincpos / sample_rate) / (PI * sincpos); - float window = 0.54f - 0.46f * cosf(M_2PI * mi / (FIR_TAPS * FIR_PHASES)); // Hamming window - lp->low_pass_fir[j][i] = firlowpass * window; - } - } - memset(lp->sample_buffer, 0, sizeof(lp->sample_buffer)); - lp->buffer_index = 0; + float rc = 1/(M_2PI*cutoff_frequency); + lp->alpha = sample_rate/(sample_rate+rc); + lp->prev_sample = 0.0f; } float apply_low_pass_filter(LowPassFilter *lp, float sample) { - // Update the sample buffer - lp->sample_buffer[lp->buffer_index] = sample; - lp->buffer_index = (lp->buffer_index + 1) % FIR_TAPS; - - // Apply the filter - float result = 0.0f; - int index = lp->buffer_index; - for (int i = 0; i < FIR_TAPS; i++) { - result += lp->low_pass_fir[0][i] * lp->sample_buffer[index]; - index = (index + 1) % FIR_TAPS; - } - return result*6; + float output = lp->alpha*sample+(1-lp->alpha)*lp->prev_sample; + lp->prev_sample = output; + return output; } void init_delay_line(DelayLine *delay_line, int max_delay) { diff --git a/lib/filters.h b/lib/filters.h index c1fe1f2..7b80352 100644 --- a/lib/filters.h +++ b/lib/filters.h @@ -15,9 +15,8 @@ void init_emphasis(Emphasis *pe, float tau, float sample_rate); float apply_pre_emphasis(Emphasis *pe, float sample); typedef struct { - float low_pass_fir[FIR_PHASES][FIR_TAPS]; - float sample_buffer[FIR_TAPS]; - int buffer_index; + float alpha; + float prev_sample; } LowPassFilter; void init_low_pass_filter(LowPassFilter *lp, float cutoff_frequency, float sample_rate); diff --git a/src/crosby_stereo_coder.c b/src/crosby_stereo_coder.c index 86c10b6..5530da3 100644 --- a/src/crosby_stereo_coder.c +++ b/src/crosby_stereo_coder.c @@ -63,12 +63,12 @@ int main() { printf("CrosbySTCode : Stereo encoder (using the crosby system) made by radio95 (with help of ChatGPT and Claude, thanks!)\n"); // Define formats and buffer atributes pa_sample_spec stereo_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, //Float32 NE, or Float32 Native Endian, the float in c uses the endianess of your pc, or native endian, and float is float32, and double is float64 .channels = 2, .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better }; pa_sample_spec mono_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, .channels = 1, .rate = SAMPLE_RATE }; diff --git a/src/polar_stereo_coder.c b/src/polar_stereo_coder.c index 4faa023..daa4331 100644 --- a/src/polar_stereo_coder.c +++ b/src/polar_stereo_coder.c @@ -62,12 +62,12 @@ int main() { printf("PSTCode : (Polar) Stereo encoder made by radio95 (with help of ChatGPT and Claude, thanks!). Note that this version is for the OIRT band which is in use in Russia, Belarus and other CIS countries\n"); // Define formats and buffer atributes pa_sample_spec stereo_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, //Float32 NE, or Float32 Native Endian, the float in c uses the endianess of your pc, or native endian, and float is float32, and double is float64 .channels = 2, .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better }; pa_sample_spec mono_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, .channels = 1, .rate = SAMPLE_RATE }; diff --git a/src/sca_mod.c b/src/sca_mod.c index 25e7404..1a080b7 100644 --- a/src/sca_mod.c +++ b/src/sca_mod.c @@ -57,7 +57,7 @@ int main() { // Define formats and buffer atributes pa_sample_spec audio_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, //Float32 NE, or Float32 Native Endian, the float in c uses the endianess of your pc, or native endian, and float is float32, and double is float64 .channels = 1, .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better }; diff --git a/src/ssb_stereo_coder.c b/src/ssb_stereo_coder.c index 4de4915..65a3024 100644 --- a/src/ssb_stereo_coder.c +++ b/src/ssb_stereo_coder.c @@ -65,12 +65,12 @@ int main() { printf("SSB-STCode : Stereo encoder made by radio95 (with help of ChatGPT and Claude, thanks!)\n"); // Define formats and buffer atributes pa_sample_spec stereo_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, //Float32 NE, or Float32 Native Endian, the float in c uses the endianess of your pc, or native endian, and float is float32, and double is float64 .channels = 2, .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better }; pa_sample_spec mono_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, .channels = 1, .rate = SAMPLE_RATE }; diff --git a/src/stereo_coder.c b/src/stereo_coder.c index 9fd0c53..22d5119 100644 --- a/src/stereo_coder.c +++ b/src/stereo_coder.c @@ -63,12 +63,12 @@ int main() { printf("STCode : Stereo encoder made by radio95 (with help of ChatGPT and Claude, thanks!)\n"); // Define formats and buffer atributes pa_sample_spec stereo_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, //Float32 NE, or Float32 Native Endian, the float in c uses the endianess of your pc, or native endian, and float is float32, and double is float64 .channels = 2, .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better }; pa_sample_spec mono_format = { - .format = PA_SAMPLE_FLOAT32LE, + .format = PA_SAMPLE_FLOAT32NE, .channels = 1, .rate = SAMPLE_RATE };