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:
2025-01-26 12:41:00 +01:00
parent d4c7334a96
commit 0326f2e75d
5 changed files with 189 additions and 278 deletions

View File

@@ -1,5 +1,5 @@
{ {
"port": 13452, "port": 13452,
"time": 1737845288156, "time": 1737886041050,
"version": "0.0.3" "version": "0.0.3"
} }

View File

@@ -1,6 +1,3 @@
# FMTools
FMTools is a repository of apps you can use to make your FM broadcast better, pirate or not this will help you if you don't have something, maybe you want a better stereo encoder? SCA? We have what you need, for RDS just use MiniRDS
# fm95 # fm95
FM95 is a audio processor for FM, it does: FM95 is a audio processor for FM, it does:
- Pre-Emphasis - Pre-Emphasis
@@ -9,7 +6,7 @@ FM95 is a audio processor for FM, it does:
- SSB Stereo - SSB Stereo
- Polar Stereo - Polar Stereo
- Polar SSB Stereo (huh) - Polar SSB Stereo (huh)
- TODO: SCA - SCA
Supports 2 inputs: Supports 2 inputs:
- Audio (via Pulse) - Audio (via Pulse)
@@ -18,13 +15,6 @@ Supports 2 inputs:
and one output: and one output:
- MPX (via Pulse or ALSA) - MPX (via Pulse or ALSA)
Note that i haven't tested it, but i will on monday (29-01-25)
# 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
# How to compile? # How to compile?
To compile you need `cmake`, `libasound2-dev` and `libpulse-dev`, if you have those then do these commands: To compile you need `cmake`, `libasound2-dev` and `libpulse-dev`, if you have those then do these commands:
``` ```

View File

@@ -1,18 +1,22 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include <getopt.h> #include <getopt.h>
#include "options.h" #define PREEMPHASIS
#define buffer_maxlength 12288
#define buffer_tlength_fragsize 8192
#define buffer_prebuf 16
#define DEFAULT_STEREO 1 #define DEFAULT_STEREO 1
#define DEFAULT_STEREO_POLAR 0 #define DEFAULT_STEREO_POLAR 0
#define DEFAULT_STEREO_SSB 0 #define DEFAULT_STEREO_SSB 0
#define DEFAULT_CLIPPER_THRESHOLD 1.0f #define DEFAULT_CLIPPER_THRESHOLD 1.0f
#define DEFAULT_ALSA_OUTPUT 0 #define DEFAULT_ALSA_OUTPUT 0
#define DEFAULT_SCA_FREQUENCY 67000.0f
#define DEFAULT_SCA_DEVIATION 7000.0f
#define DEFAULT_SCA_CLIPPER_THRESHOLD 1.0f
#define DEFAULT_PREEMPHASIS_TAU 50e-6
//#define USB //#define USB
@@ -20,12 +24,14 @@
#include "../lib/oscillator.h" #include "../lib/oscillator.h"
#include "../lib/filters.h" #include "../lib/filters.h"
#include "../lib/hilbert.h" #include "../lib/hilbert.h"
#include "../lib/fm_modulator.h"
#define SAMPLE_RATE 192000 // Don't go lower than 108 KHz, becuase it (53000*2) and (38000+15000) #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 INPUT_DEVICE "real_real_tx_audio_input.monitor"
#define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback" #define OUTPUT_DEVICE "alsa_output.platform-soc_sound.stereo-fallback"
// #define MPX_DEVICE "" // #define MPX_DEVICE ""
// #define SCA_DEVICE ""
#define BUFFER_SIZE 512 #define BUFFER_SIZE 512
@@ -36,11 +42,9 @@
#define MONO_VOLUME 0.45f // L+R Signal #define MONO_VOLUME 0.45f // L+R Signal
#define PILOT_VOLUME 0.09f // 19 KHz Pilot #define PILOT_VOLUME 0.09f // 19 KHz Pilot
#define STEREO_VOLUME 0.45f // L-R signal #define STEREO_VOLUME 0.45f // L-R signal
#define SCA_VOLUME 0.1f
#define MPX_VOLUME 1.0f #define MPX_VOLUME 1.0f
#ifdef PREEMPHASIS
#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america
#endif
volatile sig_atomic_t to_run = 1; volatile sig_atomic_t to_run = 1;
@@ -71,64 +75,115 @@ static void stop(int signum) {
void show_version() { void show_version() {
printf("fm95 (an FM Processor by radio95) version 1.0\n"); printf("fm95 (an FM Processor by radio95) version 1.0\n");
} }
void show_help(char *name) { void show_help(char *name) {
printf( printf(
"fm95 (an FM Processor by radio95)\n" "fm95 (an FM Processor by radio95)\n"
"Usage: %s\n\n" "Usage: %s\n\n"
" -m,--mono Force Mono\n" " -m,--mono Force Mono [default: %d]\n"
" -s,--stereo Force Stereo\n" " -s,--stereo Force Stereo [default: %d]\n"
" -i,--input Override input device\n" " -i,--input Override input device [default: %s]\n"
" -o,--output Override output device\n" " -o,--output Override output device [default: %s]\n"
" -M,--mpx Override MPX input device\n" " -a,--alsa_out Force alsa output [default: %d]\n"
" -c,--clipper Override the clipper threshold\n" " -p,--pulse_out Force pulse output [default: %d]\n"
" -P,--polar Force Polar Stereo (does not take effect with -m)\n" " -M,--mpx Override MPX input device [default: %s]\n"
" -g,--ge Force Zenith/GE stereo (does not take effect with -m, default)\n" " -C,--sca Override the SCA input device [default: %s]\n"
" -S,--ssb Force SSB\n" " -f,--sca_freq Override the SCA frequency [default: %f]\n"
" -D,--dsb Force DSB\n" " -F,--sca_dev Override the SCA deviation [default: %f]\n"
" -L,--sca_clip Override the SCA clipper threshold [default: %f]\n"
" -c,--clipper Override the clipper threshold [default: %f]\n"
" -P,--polar Force Polar Stereo (does not take effect with -m%s)\n"
" -g,--ge Force Zenith/GE stereo (does not take effect with -m%s)\n"
" -S,--ssb Force SSB [default: %d]\n"
" -D,--dsb Force DSB [default: %d]\n"
" -R,--preemp Override preemphasis [default: %f]\n"
,name ,name
,DEFAULT_STEREO^1
,DEFAULT_STEREO
,INPUT_DEVICE
,OUTPUT_DEVICE
,DEFAULT_ALSA_OUTPUT
,DEFAULT_ALSA_OUTPUT^1
#ifdef MPX_DEVICE
,MPX_DEVICE
#else
,"not set"
#endif
#ifdef SCA_DEVICE
,SCA_DEVICE
#else
,"not set"
#endif
,DEFAULT_SCA_FREQUENCY
,DEFAULT_SCA_DEVIATION
,DEFAULT_SCA_CLIPPER_THRESHOLD
,DEFAULT_CLIPPER_THRESHOLD
,(DEFAULT_STEREO_POLAR == 1) ? ", default" : ""
,(DEFAULT_STEREO_POLAR == 1) ? "" : ", default"
,DEFAULT_STEREO_SSB
,DEFAULT_STEREO_SSB^1
,DEFAULT_PREEMPHASIS_TAU
); );
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
show_version(); show_version();
int stereo = DEFAULT_STEREO;
#ifndef MPX_DEVICE
char audio_mpx_device[64] = "\0";
#else
char audio_mpx_device[64] = MPX_DEVICE;
#endif
pa_simple *mpx_device; pa_simple *mpx_device;
pa_simple *sca_device;
pa_simple *output_device; pa_simple *output_device;
snd_pcm_hw_params_t *output_params; snd_pcm_hw_params_t *output_params;
snd_pcm_t *output_handle; snd_pcm_t *output_handle;
float clipper_threshold = DEFAULT_CLIPPER_THRESHOLD; float clipper_threshold = DEFAULT_CLIPPER_THRESHOLD;
int stereo = DEFAULT_STEREO;
int polar_stereo = DEFAULT_STEREO_POLAR; int polar_stereo = DEFAULT_STEREO_POLAR;
int ssb = DEFAULT_STEREO_SSB; int ssb = DEFAULT_STEREO_SSB;
float sca_frequency = DEFAULT_SCA_FREQUENCY;
float sca_deviation = DEFAULT_SCA_DEVIATION;
float sca_clipper_threshold = DEFAULT_SCA_CLIPPER_THRESHOLD;
char audio_input_device[64] = INPUT_DEVICE; char audio_input_device[64] = INPUT_DEVICE;
char audio_output_device[64] = OUTPUT_DEVICE; char audio_output_device[64] = OUTPUT_DEVICE;
#ifndef MPX_DEVICE
char audio_mpx_device[64] = "\0";
#else
char audio_mpx_device[64] = MPX_DEVICE;
#endif
#ifndef SCA_DEVICE
char audio_sca_device[64] = "\0";
#else
char audio_sca_device[64] = SCA_DEVICE;
#endif
int alsa_output = DEFAULT_ALSA_OUTPUT; int alsa_output = DEFAULT_ALSA_OUTPUT;
float preemphasis_tau = DEFAULT_PREEMPHASIS_TAU;
// #region Parse Arguments
int opt; int opt;
const char *short_opt = "msi:o:apM:c:PgSDhv"; const char *short_opt = "msi:o:apM:C:f:F:L:c:PgSDR:hv";
struct option long_opt[] = struct option long_opt[] =
{ {
{"mono", no_argument, NULL, 'm'}, {"mono", no_argument, NULL, 'm'},
{"stereo", no_argument, NULL, 's'}, {"stereo", no_argument, NULL, 's'},
{"input", optional_argument, NULL, 'i'}, {"input", required_argument, NULL, 'i'},
{"output", optional_argument, NULL, 'o'}, {"output", required_argument, NULL, 'o'},
{"alsa_out", no_argument, NULL, 'a'}, {"alsa_out", no_argument, NULL, 'a'},
{"pulse_put", no_argument, NULL, 'p'}, {"pulse_out", no_argument, NULL, 'p'},
{"mpx", optional_argument, NULL, 'M'}, {"mpx", required_argument, NULL, 'M'},
{"clipper", optional_argument, NULL, 'c'}, {"sca", required_argument, NULL, 'C'},
{"polar", no_argument, NULL, 'P'}, {"sca_freq", required_argument, NULL, 'f'},
{"ge", no_argument, NULL, 'g'}, {"sca_dev", required_argument, NULL, 'F'},
{"ssb", no_argument, NULL, 'S'}, {"sca_clip", required_argument, NULL, 'L'},
{"dsb", no_argument, NULL, 'D'}, {"clipper", required_argument, NULL, 'c'},
{"polar", no_argument, NULL, 'P'},
{"help", no_argument, NULL, 'h'}, {"ge", no_argument, NULL, 'g'},
{"version", no_argument, NULL, 'v'}, {"ssb", no_argument, NULL, 'S'},
{ 0, 0, 0, 0 } {"dsb", no_argument, NULL, 'D'},
{"preemp", no_argument, NULL, 'R'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0} // No trailing comma here
}; };
while((opt = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) { while((opt = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) {
@@ -158,6 +213,21 @@ int main(int argc, char **argv) {
case 'M': //MPX in case 'M': //MPX in
memcpy(audio_mpx_device, optarg, 63); memcpy(audio_mpx_device, optarg, 63);
break; break;
case 'C': //SCA in
memcpy(audio_sca_device, optarg, 63);
break;
case 'f': //SCA freq
sca_frequency = strtof(optarg, NULL);
printf("Running with a SCA frequency of %f\n", sca_frequency);
break;
case 'F': //SCA deviation
sca_deviation = strtof(optarg, NULL);
printf("Running with a SCA deviation of %f\n", sca_deviation);
break;
case 'L': //SCA clip
sca_clipper_threshold = strtof(optarg, NULL);
printf("Running with a SCA clipper threshold of %f\n", sca_clipper_threshold);
break;
case 'c': //Clipper case 'c': //Clipper
clipper_threshold = strtof(optarg, NULL); clipper_threshold = strtof(optarg, NULL);
printf("Running with a clipper threshold of %f\n", clipper_threshold); printf("Running with a clipper threshold of %f\n", clipper_threshold);
@@ -178,6 +248,10 @@ int main(int argc, char **argv) {
ssb = 0; ssb = 0;
printf("Using DSB\n"); printf("Using DSB\n");
break; break;
case 'R': // Preemp
preemphasis_tau = strtof(optarg, NULL)*0.000001;
printf("Running with a premp of %f\n", preemphasis_tau);
break;
case 'v': // Version case 'v': // Version
show_version(); show_version();
return 0; return 0;
@@ -186,6 +260,9 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
} }
// #endregion
// #region Setup devices
// Define formats and buffer atributes // Define formats and buffer atributes
pa_sample_spec stereo_format = { pa_sample_spec stereo_format = {
@@ -215,10 +292,10 @@ int main(int argc, char **argv) {
pa_simple *input_device = pa_simple_new( pa_simple *input_device = pa_simple_new(
NULL, NULL,
"StereoEncoder", "fm95",
PA_STREAM_RECORD, PA_STREAM_RECORD,
audio_input_device, audio_input_device,
"Audio Input", "Main Audio Input",
&stereo_format, &stereo_format,
NULL, NULL,
&input_buffer_atr, &input_buffer_atr,
@@ -234,7 +311,7 @@ int main(int argc, char **argv) {
mpx_device = pa_simple_new( mpx_device = pa_simple_new(
NULL, NULL,
"StereoEncoder", "fm95",
PA_STREAM_RECORD, PA_STREAM_RECORD,
audio_mpx_device, audio_mpx_device,
"MPX Input", "MPX Input",
@@ -245,6 +322,28 @@ int main(int argc, char **argv) {
); );
if (!mpx_device) { if (!mpx_device) {
fprintf(stderr, "Error: cannot open MPX device: %s\n", pa_strerror(opentime_pulse_error)); fprintf(stderr, "Error: cannot open MPX device: %s\n", pa_strerror(opentime_pulse_error));
pa_simple_free(input_device);
return 1;
}
}
if(strlen(audio_sca_device) != 0) {
printf("Connecting to SCA device... (%s)\n", audio_sca_device);
sca_device = pa_simple_new(
NULL,
"fm95",
PA_STREAM_RECORD,
audio_sca_device,
"SCA Input",
&mono_format,
NULL,
&input_buffer_atr,
&opentime_pulse_error
);
if (!sca_device) {
fprintf(stderr, "Error: cannot open SCA device: %s\n", pa_strerror(opentime_pulse_error));
pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
return 1; return 1;
} }
} }
@@ -266,6 +365,8 @@ int main(int argc, char **argv) {
if (!output_device) { if (!output_device) {
fprintf(stderr, "Error: cannot open output device: %s\n", pa_strerror(opentime_pulse_error)); fprintf(stderr, "Error: cannot open output device: %s\n", pa_strerror(opentime_pulse_error));
pa_simple_free(input_device); pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
if(strlen(audio_sca_device) != 0) pa_simple_free(sca_device);
return 1; return 1;
} }
} else { } else {
@@ -273,6 +374,8 @@ int main(int argc, char **argv) {
if(output_error < 0) { if(output_error < 0) {
fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error)); fprintf(stderr, "Error: cannot open output device: %s\n", snd_strerror(output_error));
pa_simple_free(input_device); pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
if(strlen(audio_sca_device) != 0) pa_simple_free(sca_device);
return 1; return 1;
} }
snd_pcm_hw_params_malloc(&output_params); snd_pcm_hw_params_malloc(&output_params);
@@ -294,29 +397,35 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
} }
// #endregion
Oscillator pilot_osc; Oscillator pilot_osc;
if(polar_stereo == 1) { if(polar_stereo == 1) {
init_oscillator(&pilot_osc, 31250.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier init_oscillator(&pilot_osc, 31250.0, SAMPLE_RATE); // The stereo carrier itself, the stereo carrier in polar is modulated directly on 31.25 khz with a bit of a carrier
} else { } else {
init_oscillator(&pilot_osc, 19000.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier init_oscillator(&pilot_osc, 19000.0, SAMPLE_RATE); // Pilot, it's there to indicate stereo and as a refrence signal with the stereo carrier
} }
FMModulator sca_mod;
init_fm_modulator(&sca_mod, sca_frequency, sca_deviation, SAMPLE_RATE);
HilbertTransformer hilbert; // An Hilbert shifts a signal in quadrature, generating the I/Q data HilbertTransformer hilbert; // An Hilbert shifts a signal in quadrature, generating the I/Q data
init_hilbert(&hilbert); init_hilbert(&hilbert);
DelayLine monoDelay; // Hilbert introduces a delay of 99 samples, this should be here to sync the mono with stereo to a sample DelayLine monoDelay; // Hilbert introduces a delay of 99 samples, this should be here to sync the mono with stereo to a sample
init_delay_line(&monoDelay, 99); init_delay_line(&monoDelay, 99);
#ifdef PREEMPHASIS
ResistorCapacitor preemp_l, preemp_r; ResistorCapacitor preemp_l, preemp_r;
init_rc_tau(&preemp_l, PREEMPHASIS_TAU, SAMPLE_RATE); init_rc_tau(&preemp_l, preemphasis_tau, SAMPLE_RATE);
init_rc_tau(&preemp_r, PREEMPHASIS_TAU, SAMPLE_RATE); init_rc_tau(&preemp_r, preemphasis_tau, SAMPLE_RATE);
#endif
signal(SIGINT, stop); signal(SIGINT, stop);
signal(SIGTERM, stop); signal(SIGTERM, stop);
int pulse_error; int pulse_error;
float audio_stereo_input[BUFFER_SIZE*2]; // Input from device, interleaved stereo float audio_stereo_input[BUFFER_SIZE*2]; // Input from device, interleaved stereo
float mpx_in[BUFFER_SIZE]; // Input from MPX device float mpx_in[BUFFER_SIZE]; // Input from MPX device
float sca_in[BUFFER_SIZE]; // Input from SCA device
float left[BUFFER_SIZE+64], right[BUFFER_SIZE+64]; // Audio, same thing as in input but uninterleaved, ai told be there could be a buffer overflow here float left[BUFFER_SIZE+64], right[BUFFER_SIZE+64]; // Audio, same thing as in input but uninterleaved, ai told be there could be a buffer overflow here
float output[BUFFER_SIZE]; // MPX, this goes to the output float output[BUFFER_SIZE]; // MPX, this goes to the output
while (to_run) { while (to_run) {
@@ -333,25 +442,30 @@ int main(int argc, char **argv) {
break; break;
} }
} }
if(strlen(audio_sca_device) != 0) {
if (pa_simple_read(sca_device, sca_in, sizeof(sca_in), &pulse_error) < 0) {
fprintf(stderr, "Error reading from SCA device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
}
for (int i = 0; i < BUFFER_SIZE; i++) { for (int i = 0; i < BUFFER_SIZE; i++) {
float l_in = left[i]; float l_in = left[i];
float r_in = right[i]; float r_in = right[i];
float multiplex_in = mpx_in[i]; float current_mpx_in = mpx_in[i];
float current_sca_in = sca_in[i];
#ifdef PREEMPHASIS float ready_l = l_in;
float preemphasized_left = apply_pre_emphasis(&preemp_l, l_in)*2; float ready_r = r_in;
float preemphasized_right = apply_pre_emphasis(&preemp_r, r_in)*2; ready_l = apply_pre_emphasis(&preemp_l, ready_l)*2;
float current_left_input = hard_clip(preemphasized_left, clipper_threshold); ready_r = apply_pre_emphasis(&preemp_r, ready_r)*2;
float current_right_input = hard_clip(preemphasized_right, clipper_threshold); ready_l = hard_clip(ready_l, clipper_threshold);
#else ready_r = hard_clip(ready_r, clipper_threshold);
float current_left_input = hard_clip(l_in, clipper_threshold);
float current_right_input = hard_clip(r_in, clipper_threshold);
#endif
float mono = (current_left_input + current_right_input) / 2.0f; // Stereo to Mono float mono = (ready_l + ready_r) / 2.0f; // Stereo to Mono
if(stereo == 1) { if(stereo == 1) {
float stereo = (current_left_input - current_right_input) / 2.0f; // Also Stereo to Mono but a bit diffrent float stereo = (ready_l - ready_r) / 2.0f; // Also Stereo to Mono but a bit diffrent
if(polar_stereo == 1) { if(polar_stereo == 1) {
if(ssb) { if(ssb) {
float stereo_carrier = get_oscillator_sin_multiplier_ni(&pilot_osc, 1); // Get stereo carrier via multiplication float stereo_carrier = get_oscillator_sin_multiplier_ni(&pilot_osc, 1); // Get stereo carrier via multiplication
@@ -367,12 +481,14 @@ int main(int argc, char **argv) {
#endif #endif
output[i] = delay_line(&monoDelay, mono)*MONO_VOLUME + output[i] = delay_line(&monoDelay, mono)*MONO_VOLUME +
signal*STEREO_VOLUME; signal*STEREO_VOLUME;
if(strlen(audio_mpx_device) != 0) output[i] += multiplex_in*MPX_VOLUME; if(strlen(audio_mpx_device) != 0) output[i] += current_mpx_in*MPX_VOLUME;
if(strlen(audio_sca_device) != 0) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
} else { } else {
float stereo_carrier = get_oscillator_sin_sample(&pilot_osc); float stereo_carrier = get_oscillator_sin_sample(&pilot_osc);
output[i] = mono*MONO_VOLUME + output[i] = mono*MONO_VOLUME +
((stereo+0.2)*stereo_carrier)*STEREO_VOLUME; ((stereo+0.2)*stereo_carrier)*STEREO_VOLUME;
if(strlen(audio_mpx_device) != 0) output[i] += multiplex_in*MPX_VOLUME; if(strlen(audio_mpx_device) != 0) output[i] += current_mpx_in*MPX_VOLUME;
if(strlen(audio_sca_device) != 0) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
} }
} else { } else {
if(ssb) { if(ssb) {
@@ -390,18 +506,22 @@ int main(int argc, char **argv) {
output[i] = delay_line(&monoDelay, mono)*MONO_VOLUME + output[i] = delay_line(&monoDelay, mono)*MONO_VOLUME +
pilot*PILOT_VOLUME + pilot*PILOT_VOLUME +
signal*STEREO_VOLUME; signal*STEREO_VOLUME;
if(strlen(audio_mpx_device) != 0) output[i] += multiplex_in*MPX_VOLUME; if(strlen(audio_mpx_device) != 0) output[i] += current_mpx_in*MPX_VOLUME;
if(strlen(audio_sca_device) != 0) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
} else { } else {
float stereo_carrier = get_oscillator_sin_multiplier_ni(&pilot_osc,2); float stereo_carrier = get_oscillator_sin_multiplier_ni(&pilot_osc,2);
float pilot = get_oscillator_sin_sample(&pilot_osc); float pilot = get_oscillator_sin_sample(&pilot_osc);
output[i] = mono*MONO_VOLUME + output[i] = mono*MONO_VOLUME +
pilot*PILOT_VOLUME + pilot*PILOT_VOLUME +
(stereo*stereo_carrier)*STEREO_VOLUME; (stereo*stereo_carrier)*STEREO_VOLUME;
if(strlen(audio_mpx_device) != 0) output[i] += multiplex_in*MPX_VOLUME; if(strlen(audio_mpx_device) != 0) output[i] += current_mpx_in*MPX_VOLUME;
if(strlen(audio_sca_device) != 0) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
} }
} }
} else { } else {
output[i] = mono*MONO_VOLUME; // Only Mono output[i] = mono*MONO_VOLUME; // Only Mono
if(strlen(audio_mpx_device) != 0) output[i] += current_mpx_in*MPX_VOLUME;
if(strlen(audio_sca_device) != 0) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
} }
} }
@@ -418,6 +538,7 @@ int main(int argc, char **argv) {
printf("Cleaning up...\n"); printf("Cleaning up...\n");
pa_simple_free(input_device); pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device); if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
if(strlen(audio_sca_device) != 0) pa_simple_free(sca_device);
if(alsa_output == 0) { if(alsa_output == 0) {
pa_simple_free(output_device); pa_simple_free(output_device);
} else { } else {

View File

@@ -1,5 +0,0 @@
#define PREEMPHASIS
#define buffer_maxlength 12288
#define buffer_tlength_fragsize 8192
#define buffer_prebuf 16

View File

@@ -1,195 +0,0 @@
#include <stdio.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"
#include "../lib/fm_modulator.h"
#include "options.h"
#define SAMPLE_RATE 192000
#define INPUT_DEVICE "SCA.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 1 // Adjust this as needed, this also limits deviation, so if you set this to 0.5 then the deviation will be limited to half
#include <pulse/simple.h>
#include <pulse/error.h>
#ifdef ALSA_OUTPUT
#include <alsa/asoundlib.h>
#endif
#define VOLUME 0.1f // SCA Volume
#define VOLUME_AUDIO 1.0f // SCA Audio volume
#define FREQUENCY 67000 // SCA Frequency
#define DEVIATION 7000 // SCA Deviation
#ifdef PREEMPHASIS
#define PREEMPHASIS_TAU 0.00005 // 50 microseconds, use 0.000075 if in america
#endif
volatile sig_atomic_t to_run = 1;
float hard_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 = 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);
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);
#ifndef ALSA_OUTPUT
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;
}
#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
FMModulator mod;
init_fm_modulator(&mod, FREQUENCY, DEVIATION, SAMPLE_RATE);
#ifdef PREEMPHASIS
ResistorCapacitor preemp;
init_rc_tau(&preemp, PREEMPHASIS_TAU, SAMPLE_RATE);
#endif
signal(SIGINT, stop);
signal(SIGTERM, stop);
int pulse_error;
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), &pulse_error) < 0) {
fprintf(stderr, "Error reading from input device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
for (int i = 0; i < BUFFER_SIZE; i++) {
float in = input[i];
#ifdef PREEMPHASIS
float preemphasized = apply_pre_emphasis(&preemp, in)*2;
float current_input = hard_clip(preemphasized);
#else
float current_input = hard_clip(in);
#endif
signal[i] = modulate_fm(&mod, current_input)*VOLUME;
}
#ifndef ALSA_OUTPUT
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;
}
#else
snd_pcm_writei(output_handle, signal, sizeof(signal));
#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);
snd_pcm_hw_params_free(&output_params);
#endif
return 0;
}