0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 03:23:54 +01:00
This commit is contained in:
2024-12-31 16:10:21 +01:00
parent c96fe78618
commit 1868cd98ca
16 changed files with 199 additions and 279 deletions

2
src/features.h Normal file
View File

@@ -0,0 +1,2 @@
// #define PREEMPHASIS
#define LPF

232
src/quadro_encoder.c Normal file
View File

@@ -0,0 +1,232 @@
// what am i doing with my life, writing some quadro encoders? (https://en.wikipedia.org/wiki/FM_broadcasting#Quadraphonic_FM)
#include <stdio.h>
#include <pulse/simple.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include "../lib/constants.h"
#include "../lib/oscillator.h"
#include "../lib/filters.h"
// Features
#include "features.h"
#define SAMPLE_RATE 192000 // Don't go lower than 182 KHz (91*2)
#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.425 // Adjust this as needed
#define MONO_VOLUME 0.45f // L+R Signal
#define PILOT_VOLUME 0.0175f // 19 KHz Pilot
#define SIN38_VOLUME 0.35f
#define COS38_VOLUME 0.35f
#define SIN76_VOLUME 0.35f
#ifdef PREEMPHASIS
#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america
#endif
#ifdef LPF
#define LPF_CUTOFF 15000
#endif
volatile sig_atomic_t to_run = 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
}
}
void uninterleave(const float *input, float *front_left, float *front_right, float *rear_left, float *rear_right, size_t num_samples) {
for (size_t i = 0; i < num_samples / 4; i++) {
front_left[i] = input[i * 4];
front_right[i] = input[i * 4 + 1];
rear_left[i] = input[i * 4 + 2];
rear_right[i] = input[i * 4 + 3];
}
}
static void stop(int signum) {
(void)signum;
printf("\nReceived stop signal. Cleaning up...\n");
to_run = 0;
}
int main() {
printf("QDCode : Quad 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_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 = 4,
.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_FLOAT32NE,
.channels = 1,
.rate = SAMPLE_RATE
};
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,
"QuadCoder",
PA_STREAM_RECORD,
INPUT_DEVICE,
"Audio Input",
&stereo_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,
"QuadCoder",
PA_STREAM_PLAYBACK,
OUTPUT_DEVICE,
"MPX",
&mono_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 pilot_osc;
init_oscillator(&pilot_osc, 19000.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier
#ifdef PREEMPHASIS
Emphasis preemp_lf, preemp_lr, preemp_rf, preemp_rr;
init_emphasis(&preemp_lf, PREEMPHASIS_TAU, SAMPLE_RATE);
init_emphasis(&preemp_lr, PREEMPHASIS_TAU, SAMPLE_RATE);
init_emphasis(&preemp_rf, PREEMPHASIS_TAU, SAMPLE_RATE);
init_emphasis(&preemp_rr, PREEMPHASIS_TAU, SAMPLE_RATE);
#endif
#ifdef LPF
LowPassFilter lpf_lf, lpf_lr, lpf_rf, lpf_rr;
init_low_pass_filter(&lpf_lf, LPF_CUTOFF, SAMPLE_RATE);
init_low_pass_filter(&lpf_lr, LPF_CUTOFF, SAMPLE_RATE);
init_low_pass_filter(&lpf_rf, LPF_CUTOFF, SAMPLE_RATE);
init_low_pass_filter(&lpf_rr, LPF_CUTOFF, SAMPLE_RATE);
#endif
signal(SIGINT, stop);
signal(SIGTERM, stop);
float input[BUFFER_SIZE*4]; // Input from device, interleaved
float left_front[BUFFER_SIZE+64], left_rear[BUFFER_SIZE+64]; // Audio, same thing as in input but ininterleaved, ai told be there could be a buffer overflow here
float right_front[BUFFER_SIZE+64], right_rear[BUFFER_SIZE+64]; // Audio, same thing as in input but ininterleaved, ai told be there could be a buffer overflow here
float mpx[BUFFER_SIZE]; // MPX, this goes to the output
while (to_run) {
if (pa_simple_read(input_device, input, sizeof(input), NULL) < 0) {
fprintf(stderr, "Error reading from input device.\n");
break;
}
uninterleave(input, left_front, right_front, left_rear, right_rear, BUFFER_SIZE*4);
for (int i = 0; i < BUFFER_SIZE; i++) {
float sin38 = sinf((pilot_osc.phase+(0.5*PI))*2);
float cos38 = cosf((pilot_osc.phase+(0.5*PI))*2);
float sin76 = sinf((pilot_osc.phase+(0.5*PI))*4);
float pilot = get_oscillator_sin_sample(&pilot_osc);
float lf_in = left_front[i];
float lr_in = left_rear[i];
float rf_in = right_front[i];
float rr_in = right_rear[i];
#ifdef PREEMPHASIS
#ifdef LPF
float lowpassed_frontleft = apply_low_pass_filter(&lpf_lf, lf_in);
float lowpassed_frontright = apply_low_pass_filter(&lpf_rf, rf_in);
float lowpassed_rearleft = apply_low_pass_filter(&lpf_lr, lr_in);
float lowpassed_rearright = apply_low_pass_filter(&lpf_rr, rr_in);
float preemphasized_frontleft = apply_pre_emphasis(&preemp_lf, lowpassed_frontleft);
float preemphasized_frontright = apply_pre_emphasis(&preemp_rf, lowpassed_frontright);
float preemphasized_rearleft = apply_pre_emphasis(&preemp_lr, lowpassed_rearleft);
float preemphasized_rearright = apply_pre_emphasis(&preemp_rr, lowpassed_rearright);
float current_lf_input = clip(preemphasized_frontleft);
float current_rf_input = clip(preemphasized_frontright);
float current_lr_input = clip(preemphasized_rearleft);
float current_rr_input = clip(preemphasized_rearright);
#else
float preemphasized_frontleft = apply_pre_emphasis(&preemp_lf, lf_in);
float preemphasized_frontright = apply_pre_emphasis(&preemp_rf, rf_in);
float preemphasized_rearleft = apply_pre_emphasis(&preemp_lr, lr_in);
float preemphasized_rearright = apply_pre_emphasis(&preemp_rr, rr_in);
float current_lf_input = clip(preemphasized_frontleft);
float current_rf_input = clip(preemphasized_frontright);
float current_lr_input = clip(preemphasized_rearleft);
float current_rr_input = clip(preemphasized_rearright);
#endif
#else
#ifdef LPF
float lowpassed_frontleft = apply_low_pass_filter(&lpf_lf, lf_in);
float lowpassed_frontright = apply_low_pass_filter(&lpf_rf, rf_in);
float lowpassed_rearleft = apply_low_pass_filter(&lpf_lr, lr_in);
float lowpassed_rearright = apply_low_pass_filter(&lpf_rr, rr_in);
float current_lf_input = clip(lowpassed_frontleft);
float current_rf_input = clip(lowpassed_frontright);
float current_lr_input = clip(lowpassed_rearleft);
float current_rr_input = clip(lowpassed_rearright);
#else
float current_lf_input = clip(lf_in);
float current_rf_input = clip(rf_in);
float current_lr_input = clip(lr_in);
float current_rr_input = clip(rr_in);
#endif
#endif
float mono = (current_lf_input+current_rf_input+current_lr_input+current_rr_input)/4;
float signal_sin38 = ((current_lf_input+current_lr_input)-(current_rf_input+current_rr_input))/4;
float signal_cos38 = ((current_lf_input+current_rr_input)-(current_lr_input+current_rf_input))/4;
float signal_sin76 = ((current_lf_input+current_rf_input)-(current_lr_input+current_rr_input))/4;
mpx[i] = mono * MONO_VOLUME +
pilot * PILOT_VOLUME +
(sin38*signal_sin38)*SIN38_VOLUME +
(cos38*signal_cos38)*COS38_VOLUME +
(sin76*signal_sin76)*SIN76_VOLUME;
}
if (pa_simple_write(output_device, mpx, sizeof(mpx), 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;
}

166
src/sca_mod.c Normal file
View File

@@ -0,0 +1,166 @@
#include <stdio.h>
#include <pulse/simple.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#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.425 // 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 VOLUME 0.03f // SCA Volume
#define FREQUENCY 67000 // SCA Frequency
#define DEVIATION 6000 // SCA Deviation
#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;
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("SCAMod : SCA Modulator (based on the Stereo encoder STCode) made by radio95 (with help of ChatGPT and Claude, thanks!)\n");
// Define formats and buffer atributes
pa_sample_spec audio_format = {
.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
};
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,
"SCAMod",
PA_STREAM_RECORD,
INPUT_DEVICE,
"Audio Input",
&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,
"SCAMod",
PA_STREAM_PLAYBACK,
OUTPUT_DEVICE,
"Signal",
&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;
init_oscillator(&osc, FREQUENCY, SAMPLE_RATE);
#ifdef PREEMPHASIS
Emphasis preemp;
init_emphasis(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE);
#endif
#ifdef LPF
LowPassFilter lpf;
init_low_pass_filter(&lpf, LPF_CUTOFF, SAMPLE_RATE);
#endif
signal(SIGINT, stop);
signal(SIGTERM, stop);
float input[BUFFER_SIZE]; // Input from device
float signal[BUFFER_SIZE]; // this goes to the output
while (to_run) {
if (pa_simple_read(input_device, input, sizeof(input), NULL) < 0) {
fprintf(stderr, "Error reading from input device.\n");
break;
}
for (int i = 0; i < BUFFER_SIZE; i++) {
float in = input[i];
#ifdef PREEMPHASIS
#ifdef LPF
float lowpassed = apply_low_pass_filter(&lpf, in);
float preemphasized = apply_pre_emphasis(&preemp, lowpassed);
float current_input = clip(preemphasized);
#else
float preemphasized = apply_pre_emphasis(&preemp, in);
float current_input = clip(preemphasized);
#endif
#else
#ifdef LPF
float lowpassed = apply_low_pass_filter(&lpf, in);
float current_input = clip(lowpassed);
#else
float current_input = clip(in);
#endif
#endif
change_oscillator_frequency(&osc, (FREQUENCY+(current_input*DEVIATION)));
signal[i] = get_oscillator_sin_sample(&osc)*VOLUME;
}
if (pa_simple_write(output_device, signal, sizeof(signal), 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;
}

197
src/stereo_coder.c Normal file
View File

@@ -0,0 +1,197 @@
#include <stdio.h>
#include <pulse/simple.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include "../lib/constants.h"
#include "../lib/oscillator.h"
#include "../lib/filters.h"
// Features
#include "features.h"
#define SAMPLE_RATE 192000 // Don't go lower than 108 KHz, becuase it (53000*2) and (38000+15000)
#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.425 // Adjust this as needed
#define MONO_VOLUME 0.45f // L+R Signal
#define PILOT_VOLUME 0.0175f // 19 KHz Pilot
#define STEREO_VOLUME 0.35f // L-R signal
#ifdef PREEMPHASIS
#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america
#endif
#ifdef LPF
#define LPF_CUTOFF 15000
#endif
volatile sig_atomic_t to_run = 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
}
}
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];
}
}
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");
// Define formats and buffer atributes
pa_sample_spec stereo_format = {
.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_FLOAT32NE,
.channels = 1,
.rate = SAMPLE_RATE
};
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,
"StereoEncoder",
PA_STREAM_RECORD,
INPUT_DEVICE,
"Audio Input",
&stereo_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,
"StereoEncoder",
PA_STREAM_PLAYBACK,
OUTPUT_DEVICE,
"MPX",
&mono_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 pilot_osc;
init_oscillator(&pilot_osc, 19000.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier
#ifdef PREEMPHASIS
Emphasis preemp_l, preemp_r;
init_emphasis(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE);
init_emphasis(&preemp_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);
float input[BUFFER_SIZE*2]; // Input from device, interleaved stereo
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 mpx[BUFFER_SIZE]; // MPX, this goes to the output
while (to_run) {
if (pa_simple_read(input_device, input, sizeof(input), NULL) < 0) {
fprintf(stderr, "Error reading from input device.\n");
break;
}
uninterleave(input, left, right, BUFFER_SIZE*2);
for (int i = 0; i < BUFFER_SIZE; i++) {
float stereo_carrier = sinf(pilot_osc.phase*2); // Stereo carrier should be a harmonic of the pilot which is in phase, best way to generate the harmonic is to multiply the pilot's phase by two, so it is mathematically impossible for them to not be in phase
float pilot = get_oscillator_sin_sample(&pilot_osc); // This is after because if it was before then the stereo would be out of phase by one increment, so [GET STEREO] ([GET PILOT] [INCREMENT PHASE])
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; // Stereo to Mono
float stereo = (current_left_input - current_right_input) / 2.0f; // Also Sterreo to Mono but a bit diffrent
mpx[i] = mono * MONO_VOLUME +
pilot * PILOT_VOLUME +
(stereo * stereo_carrier) * STEREO_VOLUME; // DSB-SC modulate
}
if (pa_simple_write(output_device, mpx, sizeof(mpx), 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;
}