diff --git a/.vscode/.server-controller-port.log b/.vscode/.server-controller-port.log index 28663d9..92acaf7 100644 --- a/.vscode/.server-controller-port.log +++ b/.vscode/.server-controller-port.log @@ -1,5 +1,5 @@ { "port": 13452, - "time": 1735749319488, + "time": 1735839820567, "version": "0.0.3" } \ No newline at end of file diff --git a/README.md b/README.md index 786a478..301b630 100644 --- a/README.md +++ b/README.md @@ -36,4 +36,7 @@ Also it doesnsn't sound bad, how may ask where did i find a decoder for it? Made # SCAMod SCAMod is a simple FM modulator which can be used to modulate a secondary audio stream, has similiar cpu usage and latency as STCode -Has a fine quality, but as it goes for 12 khz fm signals \ No newline at end of file +Has a fine quality, but as it goes for 12 khz fm signals + +# SSAPCoder +This "standard" was made by me, it encoder stereo am-quality audio into the FM carrier \ No newline at end of file diff --git a/src/stereo_sap_coder.c b/src/stereo_sap_coder.c new file mode 100644 index 0000000..f99a429 --- /dev/null +++ b/src/stereo_sap_coder.c @@ -0,0 +1,200 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../lib/constants.h" +#include "../lib/oscillator.h" +#include "../lib/filters.h" + +// Features +#include "features.h" + +#define SAMPLE_RATE 192000 + +#define INPUT_DEVICE "real_real_tx_audio_input.monitor" +#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" +#define BUFFER_SIZE 512 +#define CLIPPER_THRESHOLD 0.75 // Adjust this as needed, this also limits deviation, so if you set this to 0.5 then the deviation will be limited to half + +#define MONO_VOLUME 0.03f // Mono Volume +#define STEREO_VOLUME 0.01f // Stereo Volume + +#ifdef PREEMPHASIS +#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america +#endif + +#ifdef LPF +#define LPF_CUTOFF 8000 +#endif + +volatile sig_atomic_t to_run = 1; + +void uninterleave(const float *input, float *left, float *right, size_t num_samples) { + // For stereo, usually it is like this: LEFT RIGHT LEFT RIGHT LEFT RIGHT so this is used to get LEFT LEFT LEFT and RIGHT RIGHT RIGHT + for (size_t i = 0; i < num_samples/2; i++) { + left[i] = input[i * 2]; + right[i] = input[i * 2 + 1]; + } +} + +float clip(float sample) { + if (sample > CLIPPER_THRESHOLD) { + return CLIPPER_THRESHOLD; // Clip to the upper threshold + } else if (sample < -CLIPPER_THRESHOLD) { + return -CLIPPER_THRESHOLD; // Clip to the lower threshold + } else { + return sample; // No clipping + } +} + +static void stop(int signum) { + (void)signum; + printf("\nReceived stop signal. Cleaning up...\n"); + to_run = 0; +} + +int main() { + printf("SSCAPMod : Stereo SAP Modulator (based on the SCA encoder SCAMod) made by radio95 (with help of ChatGPT and Claude, thanks!)\n"); + + // Define formats and buffer atributes + pa_sample_spec mono_audio_format = { + .format = PA_SAMPLE_FLOAT32LE, + .channels = 1, + .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better + }; + pa_sample_spec stereo_audio_format = { + .format = PA_SAMPLE_FLOAT32LE, + .channels = 2, + .rate = SAMPLE_RATE // Same sample rate makes it easy, leave the resampling to pipewire, it should know better + }; + + 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 + }; + pa_buffer_attr output_buffer_atr = { + .maxlength = 4096, + .tlength = 2048, + .prebuf = 0 + }; + + printf("Connecting to input device... (%s)\n", INPUT_DEVICE); + + pa_simple *input_device = pa_simple_new( + NULL, + "SSAPMod", + PA_STREAM_RECORD, + INPUT_DEVICE, + "Audio Input", + &stereo_audio_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); + + pa_simple *output_device = pa_simple_new( + NULL, + "SSAPMod", + PA_STREAM_PLAYBACK, + OUTPUT_DEVICE, + "Signal", + &mono_audio_format, + NULL, + &output_buffer_atr, + NULL + ); + if (!output_device) { + fprintf(stderr, "Error: cannot open output device.\n"); + pa_simple_free(input_device); + return 1; + } + + Oscillator osc_mono, osc_stereo; + init_oscillator(&osc_mono, 68000, SAMPLE_RATE); + init_oscillator(&osc_mono, 77000, SAMPLE_RATE); +#ifdef PREEMPHASIS + Emphasis preemp_l, premp_r; + init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE); + init_emphasis(&premp_r, PREEMPHASIS_TAU, SAMPLE_RATE); +#endif +#ifdef LPF + LowPassFilter lpf_l, lpf_r; + init_low_pass_filter(&lpf_l, LPF_CUTOFF, SAMPLE_RATE); + init_low_pass_filter(&lpf_r, LPF_CUTOFF, SAMPLE_RATE); +#endif + + signal(SIGINT, stop); + signal(SIGTERM, stop); + + int pulse_error; + float input[BUFFER_SIZE*2]; // Input from device + float left[BUFFER_SIZE+64], right[BUFFER_SIZE+64]; // Audio, same thing as in input but ininterleaved, ai told be there could be a buffer overflow here + float signal[BUFFER_SIZE]; // this goes to the output + while (to_run) { + if (pa_simple_read(input_device, input, sizeof(input), &pulse_error) < 0) { + fprintf(stderr, "Error reading from input device: %s\n", pa_strerror(pulse_error)); + to_run = 0; + break; + } + uninterleave(input, left, right, BUFFER_SIZE*2); + + for (int i = 0; i < BUFFER_SIZE; i++) { + float l_in = left[i]; + float r_in = right[i]; + +#ifdef PREEMPHASIS +#ifdef LPF + float lowpassed_left = apply_low_pass_filter(&lpf_l, l_in); + float lowpassed_right = apply_low_pass_filter(&lpf_r, r_in); + float preemphasized_left = apply_pre_emphasis(&preemp_l, lowpassed_left); + float preemphasized_right = apply_pre_emphasis(&preemp_r, lowpassed_right); + float current_left_input = clip(preemphasized_left); + float current_right_input = clip(preemphasized_right); +#else + float preemphasized_left = apply_pre_emphasis(&preemp_l, l_in); + float preemphasized_right = apply_pre_emphasis(&preemp_r, r_in); + float current_left_input = clip(preemphasized_left); + float current_right_input = clip(preemphasized_right); +#endif +#else +#ifdef LPF + float lowpassed_left = apply_low_pass_filter(&lpf_l, l_in); + float lowpassed_right = apply_low_pass_filter(&lpf_r, r_in); + float current_left_input = clip(lowpassed_left); + float current_right_input = clip(lowpassed_right); +#else + float current_left_input = clip(l_in); + float current_right_input = clip(r_in); +#endif +#endif + 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, (68000+(mono*8000))); + change_oscillator_frequency(&osc_stereo, (77000+(stereo*8000))); + signal[i] = get_oscillator_sin_sample(&osc_mono)*MONO_VOLUME+ + get_oscillator_sin_sample(&osc_stereo)*STEREO_VOLUME; + } + + if (pa_simple_write(output_device, signal, sizeof(signal), &pulse_error) < 0) { + fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error)); + to_run = 0; + break; + } + } + printf("Cleaning up...\n"); + pa_simple_free(input_device); + pa_simple_free(output_device); + return 0; +}