From 1908a4efb022cac93500d629cc87bdb2a32c271c Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Sat, 25 Jan 2025 20:04:59 +0100 Subject: [PATCH] now it works, also added alsa out for mono --- CMakeLists.txt | 2 +- src/mono_passthrough.c | 48 ++++++++++++++++++++++++++++++++++++++-- src/polar_stereo_coder.c | 11 ++++++--- src/sca_mod.c | 2 +- src/stereo_coder.c | 2 +- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b60ee49..f2d05b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ file(GLOB LIB_FILES "lib/*.c") add_library(libfm OBJECT ${LIB_FILES}) # Linker flags for libraries -set(LINK_LIBS "-lpulse -lpulse-simple -lm") +set(LINK_LIBS "-lpulse -lpulse-simple -lm -lasound") # Loop through each file in src and create an executable foreach(SRC_FILE ${SRC_FILES}) diff --git a/src/mono_passthrough.c b/src/mono_passthrough.c index e3ec00a..3aadab1 100644 --- a/src/mono_passthrough.c +++ b/src/mono_passthrough.c @@ -1,6 +1,4 @@ #include -#include -#include #include #include #include @@ -16,9 +14,16 @@ #define INPUT_DEVICE "real_real_tx_audio_input.monitor" #define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" +// #define ALSA_OUTPUT // Output, not input or both #define BUFFER_SIZE 512 #define CLIPPER_THRESHOLD 0.525 // Adjust this as needed +#include +#include +#ifdef ALSA_OUTPUT +#include +#endif + #define MONO_VOLUME 0.45f // L+R Signal #ifdef PREEMPHASIS @@ -60,11 +65,13 @@ int main() { .maxlength = buffer_maxlength, .fragsize = buffer_tlength_fragsize }; +#ifndef ALSA_OUTPUT pa_buffer_attr output_buffer_atr = { .maxlength = buffer_maxlength, .tlength = buffer_tlength_fragsize, .prebuf = buffer_prebuf }; +#endif printf("Connecting to input device... (%s)\n", INPUT_DEVICE); @@ -86,6 +93,7 @@ int main() { printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE); + #ifndef ALSA_OUTPUT pa_simple *output_device = pa_simple_new( NULL, "MonoPass", @@ -102,6 +110,33 @@ int main() { pa_simple_free(input_device); return 1; } + #else + snd_pcm_hw_params_t *output_params; + snd_pcm_t *output_handle; + int output_error = snd_pcm_open(&output_handle, OUTPUT_DEVICE, SND_PCM_STREAM_PLAYBACK, 0); + if(output_error < 0) { + fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error)); + pa_simple_free(input_device); + return 1; + } + snd_pcm_hw_params_malloc(&output_params); + snd_pcm_hw_params_any(output_handle, output_params); + snd_pcm_hw_params_set_access(output_handle, output_params, SND_PCM_ACCESS_RW_INTERLEAVED); + snd_pcm_hw_params_set_format(output_handle, output_params, SND_PCM_FORMAT_FLOAT); // Same as pulse's Float32NE + snd_pcm_hw_params_set_channels(output_handle, output_params, 1); + unsigned int rate = SAMPLE_RATE; + int dir; + snd_pcm_hw_params_set_rate_near(output_handle, output_params, &rate, &dir); + snd_pcm_uframes_t frames = BUFFER_SIZE; + snd_pcm_hw_params_set_period_size_near(output_handle, output_params, &frames, &dir); // i don't have a clue why like this + output_error = snd_pcm_hw_params(output_handle, output_params); + if(output_error < 0) { + fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error)); + snd_pcm_close(output_handle); + pa_simple_free(input_device); + return 1; + } + #endif #ifdef PREEMPHASIS ResistorCapacitor preemp; @@ -149,14 +184,23 @@ int main() { mpx[i] = current_input * MONO_VOLUME; } +#ifndef ALSA_OUTPUT if (pa_simple_write(output_device, mpx, sizeof(mpx), &pulse_error) < 0) { fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error)); to_run = 0; break; } +#else + snd_pcm_writei(output_handle, mpx, sizeof(mpx)); +#endif } printf("Cleaning up...\n"); pa_simple_free(input_device); + #ifndef ALSA_OUTPUT pa_simple_free(output_device); + #else + snd_pcm_drain(output_handle); + snd_pcm_close(output_handle); + #endif return 0; } diff --git a/src/polar_stereo_coder.c b/src/polar_stereo_coder.c index f3799cf..cab7448 100644 --- a/src/polar_stereo_coder.c +++ b/src/polar_stereo_coder.c @@ -1,6 +1,4 @@ #include -#include -#include #include #include #include @@ -21,6 +19,12 @@ #define BUFFER_SIZE 512 #define CLIPPER_THRESHOLD 0.525 // Adjust this as needed +#include +#include +#ifdef ALSA_OUTPUT +#include +#endif + #define MONO_VOLUME 0.45f // L+R Signal #define STEREO_VOLUME 0.45f // L-R signal @@ -233,6 +237,7 @@ int main() { pa_simple_free(output_device); #else snd_pcm_drain(output_handle); - snd_pcm_free(output_handle); + snd_pcm_close(output_handle); + #endif return 0; } diff --git a/src/sca_mod.c b/src/sca_mod.c index a53a6f5..c0dc2b5 100644 --- a/src/sca_mod.c +++ b/src/sca_mod.c @@ -207,7 +207,7 @@ int main() { pa_simple_free(output_device); #else snd_pcm_drain(output_handle); - snd_pcm_free(output_handle); + snd_pcm_close(output_handle); #endif return 0; } diff --git a/src/stereo_coder.c b/src/stereo_coder.c index 384e896..f06b8b2 100644 --- a/src/stereo_coder.c +++ b/src/stereo_coder.c @@ -271,7 +271,7 @@ int main() { pa_simple_free(output_device); #else snd_pcm_drain(output_handle); - snd_pcm_free(output_handle); + snd_pcm_close(output_handle); #endif #ifdef SSB exit_hilbert(&hilbert);