commit c699ae929abfb052b102bf0c7e8a4855a9ae8f75 Author: KubaPro010 Date: Mon Dec 30 11:18:09 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90a169a --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +stereo_coder +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/compile_stereo b/compile_stereo new file mode 100755 index 0000000..4dc9703 --- /dev/null +++ b/compile_stereo @@ -0,0 +1 @@ +gcc stereo_coder.c -lpulse -lpulse-simple -lm -o stereo_coder diff --git a/stereo_coder.c b/stereo_coder.c new file mode 100644 index 0000000..05a3c0b --- /dev/null +++ b/stereo_coder.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include + +#define INPUT_DEVICE "real_real_tx_audio_input.monitor" +#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" +#define BUFFER_SIZE 512 + +#define MONO_VOLUME 0.5f +#define PILOT_VOLUME 0.025f +#define STEREO_VOLUME 0.275f + + +#define TWO_BUFFER_SIZE (BUFFER_SIZE*2) // Don't touch this +volatile int to_run = 1; + +void s16le_to_mono_float(const int16_t *input, float *left, float *right, size_t num_samples) { + const float scale = 1.0f / 32768.0f; + for (size_t i = 0; i < num_samples/2; i++) { + left[i] = input[i * 2] * scale; + right[i] = input[i * 2 + 1] * scale; + } +} + +void float_array_to_s16le(const float *input, int16_t *output, size_t num_samples) { + for (size_t i = 0; i < num_samples; i++) { + output[i] = (int16_t)((fminf(fmaxf(input[i], -1.0f), 1.0f)) * 32767.0f); + } +} + +#define M_2PI (3.14159265358979323846 * 2.0) + +// Track phase continuously to maintain frequency accuracy +typedef struct { + float phase; + float phase_increment; +} Oscillator; + +void init_oscillator(Oscillator *osc, float frequency, float sample_rate) { + osc->phase = 0.0f; + osc->phase_increment = (M_2PI * frequency) / sample_rate; +} + +float get_next_sample(Oscillator *osc) { + float sample = sinf(osc->phase); + osc->phase += osc->phase_increment; + if (osc->phase >= M_2PI) { + osc->phase -= M_2PI; + } + return sample; +} + +static void stop(int signum) { + (void)signum; + printf("\nReceived stop signal. Cleaning up...\n"); + to_run = 0; +} + +int main() { + printf("STCode : Stereo encoder made by radio95 (with help of ChatGPT and Claude, thanks!)\n"); + const float SAMPLE_RATE = 192000.0f; + const float PILOT_FREQ = 19000.0f; + const float STEREO_FREQ = 38000.0f; + + printf("Connecting to input device... (%s)\n", INPUT_DEVICE); + + // Set up input device + pa_sample_spec input_format = { + .format = PA_SAMPLE_S16LE, + .channels = 2, + .rate = SAMPLE_RATE + }; + pa_buffer_attr input_buffer_atr = { + .maxlength = 4096, + .fragsize = 2048 + }; + pa_simple *input_device = pa_simple_new( + NULL, + "StereoEncoder", + PA_STREAM_RECORD, + INPUT_DEVICE, + "Audio Input", + &input_format, + NULL, + &input_buffer_atr, + NULL + ); + if (!input_device) { + fprintf(stderr, "Error: cannot open input device.\n"); + return 1; + } + + printf("Connecting to output device... (%s)\n", OUTPUT_DEVICE); + + // Set up output device + pa_sample_spec output_format = { + .format = PA_SAMPLE_S16LE, + .channels = 1, + .rate = SAMPLE_RATE + }; + pa_buffer_attr output_buffer_atr = { + .maxlength = 4096, + .tlength = 2048, + .prebuf = 0 + }; + pa_simple *output_device = pa_simple_new( + NULL, + "StereoEncoder", + PA_STREAM_PLAYBACK, + OUTPUT_DEVICE, + "MPX", + &output_format, + NULL, + &output_buffer_atr, + NULL + ); + if (!output_device) { + fprintf(stderr, "Error: cannot open output device.\n"); + pa_simple_free(input_device); + return 1; + } + // Initialize oscillators + Oscillator pilot_osc, stereo_osc; + init_oscillator(&pilot_osc, PILOT_FREQ, SAMPLE_RATE); + init_oscillator(&stereo_osc, STEREO_FREQ, SAMPLE_RATE); + // Set up signal handlers + signal(SIGINT, stop); + signal(SIGTERM, stop); + // Processing buffers + int16_t input[TWO_BUFFER_SIZE]; + float left[BUFFER_SIZE], right[BUFFER_SIZE]; + float mpx[BUFFER_SIZE]; + int16_t output[BUFFER_SIZE]; + while (to_run) { + if (pa_simple_read(input_device, input, sizeof(input), NULL) < 0) { + fprintf(stderr, "Error reading from input device.\n"); + break; + } + // Convert input to float and separate channels + s16le_to_mono_float(input, left, right, TWO_BUFFER_SIZE); + // Process audio + for (int i = 0; i < BUFFER_SIZE; i++) { + float pilot = get_next_sample(&pilot_osc); + float stereo_carrier = get_next_sample(&stereo_osc); + // Create MPX signal + float mono = (left[i] + right[i]) / 2.0f; + float stereo = (left[i] - right[i]) / 2.0f; + mpx[i] = mono*MONO_VOLUME + + (stereo * stereo_carrier)*STEREO_VOLUME + + (pilot * PILOT_VOLUME); + } + // Convert to output format + float_array_to_s16le(mpx, output, BUFFER_SIZE); + // Write to output + if (pa_simple_write(output_device, output, sizeof(output), NULL) < 0) { + fprintf(stderr, "Error writing to output device.\n"); + break; + } + } + printf("Cleaning up...\n"); + pa_simple_free(input_device); + pa_simple_free(output_device); + return 0; +}