From 62e698ea526c2a4614d2499696ed0e07302fdc0d Mon Sep 17 00:00:00 2001 From: kuba Date: Wed, 1 Jan 2025 12:18:43 +0100 Subject: [PATCH] add error checking --- .vscode/.server-controller-port.log | 2 +- src/sca_mod.c | 11 +++++++---- src/ssb_stereo_coder.c | 20 ++++++++++++++------ src/stereo_coder.c | 11 +++++++---- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/.vscode/.server-controller-port.log b/.vscode/.server-controller-port.log index c52b3f3..5b79385 100644 --- a/.vscode/.server-controller-port.log +++ b/.vscode/.server-controller-port.log @@ -1,5 +1,5 @@ { "port": 13452, - "time": 1735683786793, + "time": 1735729771954, "version": "0.0.3" } \ No newline at end of file diff --git a/src/sca_mod.c b/src/sca_mod.c index 72005e4..842f5b2 100644 --- a/src/sca_mod.c +++ b/src/sca_mod.c @@ -122,11 +122,13 @@ int main() { 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), NULL) < 0) { - fprintf(stderr, "Error reading from input device.\n"); + 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; } @@ -155,8 +157,9 @@ int main() { 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"); + 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; } } diff --git a/src/ssb_stereo_coder.c b/src/ssb_stereo_coder.c index ce63b4f..f43b516 100644 --- a/src/ssb_stereo_coder.c +++ b/src/ssb_stereo_coder.c @@ -13,6 +13,7 @@ // Features #include "features.h" +#define USB #define SAMPLE_RATE 192000 // Don't go lower than 108 KHz, becuase it (53000*2) and (38000+15000) @@ -140,12 +141,14 @@ int main() { signal(SIGINT, stop); signal(SIGTERM, stop); + int pulse_error; 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"); + 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); @@ -188,15 +191,20 @@ int main() { float stereo = (current_left_input - current_right_input) / 2.0f; // Also Stereo to Mono but a bit diffrent float stereo_i, stereo_q; apply_hilbert(&hilbert, stereo, &stereo_i, &stereo_q); // I/Q, the Quadrature data is 90 degrees apart from the In-phase data - float lsb = (stereo_i*cos38-stereo_q*(sin38*0.73f)); // Compute LSB, as the Hilbert isn't perfect, i'll have to a bit silence down the Q carrier in order to make it better, also, it is just perfect as FM Stereo LSB shouldn't be fully LSB +#ifdef USB + float signal = (stereo_i*cos38+stereo_q*(sin38*0.73f)); // Compute LSB/USB, as the Hilbert isn't perfect, i'll have to a bit silence down the Q carrier in order to make it better, also, it is just perfect as FM Stereo LSB shouldn't be fully LSB +#else + float signal = (stereo_i*cos38-stereo_q*(sin38*0.73f)); // Compute LSB/USB, as the Hilbert isn't perfect, i'll have to a bit silence down the Q carrier in order to make it better, also, it is just perfect as FM Stereo LSB shouldn't be fully LSB +#endif mpx[i] = delay_line(&monoDelay, mono) * MONO_VOLUME + pilot * PILOT_VOLUME + - lsb *STEREO_VOLUME; + signal*STEREO_VOLUME; } - if (pa_simple_write(output_device, mpx, sizeof(mpx), NULL) < 0) { - fprintf(stderr, "Error writing to output device.\n"); + if (pa_simple_write(output_device, mpx, sizeof(mpx), &pulse_error) < 0) { + fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error)); + to_run = 0; break; } } diff --git a/src/stereo_coder.c b/src/stereo_coder.c index 7954c91..bb91b6e 100644 --- a/src/stereo_coder.c +++ b/src/stereo_coder.c @@ -135,12 +135,14 @@ int main() { signal(SIGINT, stop); signal(SIGTERM, stop); + int pulse_error; 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"); + 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); @@ -185,8 +187,9 @@ int main() { (stereo * stereo_carrier) * STEREO_VOLUME; // DSB-SC modulation } - if (pa_simple_write(output_device, mpx, sizeof(mpx), NULL) < 0) { - fprintf(stderr, "Error writing to output device.\n"); + if (pa_simple_write(output_device, mpx, sizeof(mpx), &pulse_error) < 0) { + fprintf(stderr, "Error writing to output device: %s\n", pa_strerror(pulse_error)); + to_run = 0; break; } }