From 7a20c89cb4b4a94f99c7dd6899a7e6b370d091bf Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Fri, 3 Jan 2025 10:21:36 +0100 Subject: [PATCH] updates --- lib/fm_modulator.c | 13 +++++++++++++ lib/fm_modulator.h | 11 +++++++++++ src/crosby_stereo_coder.c | 8 ++++---- src/features.h | 2 +- src/polar_stereo_coder.c | 8 ++++---- src/sca_mod.c | 16 ++++++++-------- src/ssb_stereo_coder.c | 8 ++++---- src/stereo_coder.c | 8 ++++---- src/stereo_sca_mod.c | 21 ++++++++++----------- 9 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 lib/fm_modulator.c create mode 100644 lib/fm_modulator.h diff --git a/lib/fm_modulator.c b/lib/fm_modulator.c new file mode 100644 index 0000000..ac6447d --- /dev/null +++ b/lib/fm_modulator.c @@ -0,0 +1,13 @@ +#include "fm_modulator.h" + +void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate) { + fm->frequency = frequency; + fm->deviation = deviation; + init_oscillator(&fm->osc, frequency, sample_rate); +} + +float modulate_fm(FMModulator *fm, float sample) { + float inst_freq = fm->frequency+(sample*fm->deviation); + change_oscillator_frequency(&fm->osc, inst_freq); + return get_oscillator_sin_sample(&fm->osc); +} \ No newline at end of file diff --git a/lib/fm_modulator.h b/lib/fm_modulator.h new file mode 100644 index 0000000..2d57e59 --- /dev/null +++ b/lib/fm_modulator.h @@ -0,0 +1,11 @@ +#include "oscillator.h" + +typedef struct +{ + float frequency; + float deviation; + Oscillator osc; +} FMModulator; + +void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float sample_rate); +float modulate_fm(FMModulator *fm, float sample); \ No newline at end of file diff --git a/src/crosby_stereo_coder.c b/src/crosby_stereo_coder.c index 80b0124..296f043 100644 --- a/src/crosby_stereo_coder.c +++ b/src/crosby_stereo_coder.c @@ -74,12 +74,12 @@ int main() { }; pa_buffer_attr input_buffer_atr = { - .maxlength = 4096, // You can lower this to 512, but this is fine, it's sub-second delay, you're probably not gonna notice unless you're looking for it - .fragsize = 2048 + .maxlength = 8192, + .fragsize = 4096 }; pa_buffer_attr output_buffer_atr = { - .maxlength = 4096, - .tlength = 2048, + .maxlength = 8192, + .tlength = 4096, .prebuf = 0 }; diff --git a/src/features.h b/src/features.h index 6d0867f..4e3379a 100644 --- a/src/features.h +++ b/src/features.h @@ -1,2 +1,2 @@ // #define PREEMPHASIS -#define LPF \ No newline at end of file +// #define LPF \ No newline at end of file diff --git a/src/polar_stereo_coder.c b/src/polar_stereo_coder.c index 5958e00..55d18f4 100644 --- a/src/polar_stereo_coder.c +++ b/src/polar_stereo_coder.c @@ -73,12 +73,12 @@ int main() { }; pa_buffer_attr input_buffer_atr = { - .maxlength = 4096, // You can lower this to 512, but this is fine, it's sub-second delay, you're probably not gonna notice unless you're looking for it - .fragsize = 2048 + .maxlength = 8192, + .fragsize = 4096 }; pa_buffer_attr output_buffer_atr = { - .maxlength = 4096, - .tlength = 2048, + .maxlength = 8192, + .tlength = 4096, .prebuf = 0 }; diff --git a/src/sca_mod.c b/src/sca_mod.c index e85344d..3b25534 100644 --- a/src/sca_mod.c +++ b/src/sca_mod.c @@ -10,6 +10,7 @@ #include "../lib/constants.h" #include "../lib/oscillator.h" #include "../lib/filters.h" +#include "../lib/fm_modulator.h" // Features #include "features.h" @@ -63,12 +64,12 @@ int main() { }; pa_buffer_attr input_buffer_atr = { - .maxlength = 4096, // You can lower this to 512, but this is fine, it's sub-second delay, you're probably not gonna notice unless you're looking for it - .fragsize = 2048 + .maxlength = 8192, + .fragsize = 4096 }; pa_buffer_attr output_buffer_atr = { - .maxlength = 4096, - .tlength = 2048, + .maxlength = 8192, + .tlength = 4096, .prebuf = 0 }; @@ -109,8 +110,8 @@ int main() { return 1; } - Oscillator osc; - init_oscillator(&osc, FREQUENCY, SAMPLE_RATE); + FMModulator mod; + init_fm_modulator(&mod, FREQUENCY, DEVIATION, SAMPLE_RATE); #ifdef PREEMPHASIS Emphasis preemp; init_emphasis(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE); @@ -154,8 +155,7 @@ int main() { #endif #endif - change_oscillator_frequency(&osc, (FREQUENCY+((current_input*VOLUME_AUDIO)*DEVIATION))); - signal[i] = get_oscillator_sin_sample(&osc)*VOLUME; + signal[i] = modulate_fm(&mod, current_input)*VOLUME; } if (pa_simple_write(output_device, signal, sizeof(signal), &pulse_error) < 0) { diff --git a/src/ssb_stereo_coder.c b/src/ssb_stereo_coder.c index 90e94b8..151082b 100644 --- a/src/ssb_stereo_coder.c +++ b/src/ssb_stereo_coder.c @@ -76,12 +76,12 @@ int main() { }; pa_buffer_attr input_buffer_atr = { - .maxlength = 4096, // You can lower this to 512, but this is fine, it's sub-second delay, you're probably not gonna notice unless you're looking for it - .fragsize = 2048 + .maxlength = 8192, + .fragsize = 4096 }; pa_buffer_attr output_buffer_atr = { - .maxlength = 4096, - .tlength = 2048, + .maxlength = 8192, + .tlength = 4096, .prebuf = 0 }; diff --git a/src/stereo_coder.c b/src/stereo_coder.c index fcd2d11..b0fd9c7 100644 --- a/src/stereo_coder.c +++ b/src/stereo_coder.c @@ -74,12 +74,12 @@ int main() { }; pa_buffer_attr input_buffer_atr = { - .maxlength = 4096, // You can lower this to 512, but this is fine, it's sub-second delay, you're probably not gonna notice unless you're looking for it - .fragsize = 2048 + .maxlength = 8192, + .fragsize = 4096 }; pa_buffer_attr output_buffer_atr = { - .maxlength = 4096, - .tlength = 2048, + .maxlength = 8192, + .tlength = 4096, .prebuf = 0 }; diff --git a/src/stereo_sca_mod.c b/src/stereo_sca_mod.c index fecae61..b9a687e 100644 --- a/src/stereo_sca_mod.c +++ b/src/stereo_sca_mod.c @@ -10,6 +10,7 @@ #include "../lib/constants.h" #include "../lib/oscillator.h" #include "../lib/filters.h" +#include "../lib/fm_modulator.h" // Features #include "features.h" @@ -74,12 +75,12 @@ int main() { }; pa_buffer_attr input_buffer_atr = { - .maxlength = 4096, // You can lower this to 512, but this is fine, it's sub-second delay, you're probably not gonna notice unless you're looking for it - .fragsize = 2048 + .maxlength = 8192, + .fragsize = 4096 }; pa_buffer_attr output_buffer_atr = { - .maxlength = 4096, - .tlength = 2048, + .maxlength = 8192, + .tlength = 4096, .prebuf = 0 }; @@ -120,9 +121,9 @@ int main() { return 1; } - Oscillator osc_mono, osc_stereo; - init_oscillator(&osc_mono, 67000, SAMPLE_RATE); - init_oscillator(&osc_stereo, 80000, SAMPLE_RATE); + FMModulator mod_mono, mod_stereo; + init_fm_modulator(&mod_mono, 67000, 6000, SAMPLE_RATE); + init_fm_modulator(&mod_stereo, 80000, 6000, SAMPLE_RATE); #ifdef PREEMPHASIS Emphasis preemp_l, preemp_r; init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE); @@ -181,10 +182,8 @@ int main() { float mono = (current_left_input+current_right_input)/2.0f; float stereo = (current_left_input-current_right_input)/2.0f; - change_oscillator_frequency(&osc_mono, (67000+(mono*6000))); - change_oscillator_frequency(&osc_stereo, (80000+(stereo*6000))); - signal[i] = get_oscillator_sin_sample(&osc_mono)*MONO_VOLUME+ - get_oscillator_sin_sample(&osc_stereo)*STEREO_VOLUME; + signal[i] = modulate_fm(&mod_mono, mono)*MONO_VOLUME+ + modulate_fm(&mod_stereo, stereo)*STEREO_VOLUME; } if (pa_simple_write(output_device, signal, sizeof(signal), &pulse_error) < 0) {