0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-26 19:23:51 +01:00

some optimalizations

This commit is contained in:
2025-03-09 17:13:46 +01:00
parent eda100d8a1
commit 1cbc8e7903
4 changed files with 31 additions and 31 deletions

View File

@@ -27,6 +27,7 @@ foreach(SRC_FILE ${SRC_FILES})
# Create the executable from each source file
add_executable(${EXEC_NAME} ${SRC_FILE})
target_compile_options(${EXEC_NAME} PRIVATE -O1)
# Link the necessary libraries and object files from lib/
target_link_libraries(${EXEC_NAME} PRIVATE libfm ${LINK_LIBS})

View File

@@ -10,15 +10,9 @@ float apply_preemphasis(ResistorCapacitor *filter, float sample) {
return out;
}
float hard_clip(float sample, float threshold) {
if (sample > threshold) {
return threshold; // Clip to the upper threshold
} else if (sample < -threshold) {
return -threshold; // Clip to the lower threshold
} else {
return sample; // No clipping
}
float hard_clip_fast(float sample, float threshold) {
// Branchless clipping
return fmaxf(-threshold, fminf(threshold, sample));
}
float voltage_db_to_voltage(float db) {

View File

@@ -10,6 +10,7 @@ void init_fm_modulator(FMModulator *fm, float frequency, float deviation, float
float modulate_fm(FMModulator *fm, float sample) {
float inst_freq = fm->frequency+(sample*fm->deviation);
if (inst_freq < 0.0f) inst_freq = 0.0f;
float out = sinf(fm->osc_phase);
fm->osc_phase += fmodf(fm->osc_phase + ((M_2PI * inst_freq) / fm->sample_rate), M_2PI);
return sinf(fm->osc_phase);
return out;
}

View File

@@ -238,6 +238,10 @@ int main(int argc, char **argv) {
}
// #endregion
int mpx_on = (strlen(audio_mpx_device) != 0);
int rds_on = (strlen(audio_rds_device) != 0);
int sca_on = (strlen(audio_sca_device) != 0);
// #region Setup devices
// Define formats and buffer atributes
@@ -282,7 +286,7 @@ int main(int argc, char **argv) {
return 1;
}
if(strlen(audio_mpx_device) != 0) {
if(mpx_on) {
printf("Connecting to MPX device... (%s)\n", audio_mpx_device);
mpx_device = pa_simple_new(
@@ -302,7 +306,7 @@ int main(int argc, char **argv) {
return 1;
}
}
if(strlen(audio_rds_device) != 0) {
if(rds_on) {
printf("Connecting to RDS device... (%s)\n", audio_rds_device);
rds_device = pa_simple_new(
@@ -323,7 +327,7 @@ int main(int argc, char **argv) {
return 1;
}
}
if(strlen(audio_sca_device) != 0) {
if(sca_on) {
printf("Connecting to SCA device... (%s)\n", audio_sca_device);
sca_device = pa_simple_new(
@@ -340,8 +344,8 @@ int main(int argc, char **argv) {
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);
if(strlen(audio_rds_device) != 0) pa_simple_free(rds_device);
if(mpx_on) pa_simple_free(mpx_device);
if(rds_on) pa_simple_free(rds_device);
return 1;
}
}
@@ -362,9 +366,9 @@ int main(int argc, char **argv) {
if (!output_device) {
fprintf(stderr, "Error: cannot open output device: %s\n", pa_strerror(opentime_pulse_error));
pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
if(strlen(audio_rds_device) != 0) pa_simple_free(rds_device);
if(strlen(audio_sca_device) != 0) pa_simple_free(sca_device);
if(mpx_on) pa_simple_free(mpx_device);
if(rds_on) pa_simple_free(rds_device);
if(sca_on) pa_simple_free(sca_device);
return 1;
}
// #endregion
@@ -390,9 +394,9 @@ int main(int argc, char **argv) {
}
printf("Cleaning up...\n");
pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
if(strlen(audio_rds_device) != 0) pa_simple_free(rds_device);
if(strlen(audio_sca_device) != 0) pa_simple_free(sca_device);
if(mpx_on) pa_simple_free(mpx_device);
if(rds_on) pa_simple_free(rds_device);
if(sca_on) pa_simple_free(sca_device);
pa_simple_free(output_device);
return 0;
}
@@ -418,7 +422,7 @@ int main(int argc, char **argv) {
float mpx_in[BUFFER_SIZE] = {0}; // Input from MPX device
float rds_in[BUFFER_SIZE] = {0}; // Input from RDS device
float sca_in[BUFFER_SIZE] = {0}; // 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], right[BUFFER_SIZE]; // Audio, same thing as in input but uninterleaved
float output[BUFFER_SIZE]; // MPX, this goes to the output
while (to_run) {
if (pa_simple_read(input_device, audio_stereo_input, sizeof(audio_stereo_input), &pulse_error) < 0) {
@@ -427,21 +431,21 @@ int main(int argc, char **argv) {
break;
}
uninterleave(audio_stereo_input, left, right, BUFFER_SIZE*2);
if(strlen(audio_mpx_device) != 0) {
if(mpx_on) {
if (pa_simple_read(mpx_device, mpx_in, sizeof(mpx_in), &pulse_error) < 0) {
fprintf(stderr, "Error reading from MPX device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
}
if(strlen(audio_rds_device) != 0) {
if(rds_on) {
if (pa_simple_read(rds_device, rds_in, sizeof(rds_in), &pulse_error) < 0) {
fprintf(stderr, "Error reading from RDS device: %s\n", pa_strerror(pulse_error));
to_run = 0;
break;
}
}
if(strlen(audio_sca_device) != 0) {
if(sca_on) {
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;
@@ -466,7 +470,7 @@ int main(int argc, char **argv) {
if(stereo == 1) {
float stereo = (ready_l - ready_r) / 2.0f; // Also Stereo to Mono but a bit diffrent
float stereo_carrier = get_oscillator_sin_multiplier_ni(&osc, polar_stereo ? 1 : 2);
if((strlen(audio_rds_device) != 0) && polar_stereo == 0) {
if(rds_on && polar_stereo == 0) {
float rds_carrier = get_oscillator_sin_multiplier_ni(&osc, 3);
output[i] += (current_rds_in*rds_carrier)*RDS_VOLUME;
}
@@ -479,8 +483,8 @@ int main(int argc, char **argv) {
}
advance_oscillator(&osc);
}
if(strlen(audio_mpx_device) != 0) output[i] += hard_clip(current_mpx_in, MPX_CLIPPER_THRESHOLD)*MPX_VOLUME;
if(strlen(audio_sca_device) != 0) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
if(mpx_on) output[i] += hard_clip(current_mpx_in, MPX_CLIPPER_THRESHOLD)*MPX_VOLUME;
if(sca_on) output[i] += modulate_fm(&sca_mod, hard_clip(current_sca_in, sca_clipper_threshold))*SCA_VOLUME;
output[i] *= master_volume;
}
@@ -492,9 +496,9 @@ int main(int argc, char **argv) {
}
printf("Cleaning up...\n");
pa_simple_free(input_device);
if(strlen(audio_mpx_device) != 0) pa_simple_free(mpx_device);
if(strlen(audio_rds_device) != 0) pa_simple_free(rds_device);
if(strlen(audio_sca_device) != 0) pa_simple_free(sca_device);
if(mpx_on) pa_simple_free(mpx_device);
if(rds_on) pa_simple_free(rds_device);
if(sca_on) pa_simple_free(sca_device);
pa_simple_free(output_device);
return 0;
}