diff --git a/.vscode/.server-controller-port.log b/.vscode/.server-controller-port.log index 353bf6d..9eb6188 100644 --- a/.vscode/.server-controller-port.log +++ b/.vscode/.server-controller-port.log @@ -1,5 +1,5 @@ { "port": 13452, - "time": 1754151185891, + "time": 1754228131242, "version": "0.0.3" } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 14647cd..e3a61d9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -42,7 +42,8 @@ "queue": "c", "thread": "c", "vector": "c", - "stereo_encoder.h": "c" + "stereo_encoder.h": "c", + "iir.h": "c" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/filter/bs412.c b/filter/bs412.c index 6310a7f..c784581 100644 --- a/filter/bs412.c +++ b/filter/bs412.c @@ -36,7 +36,7 @@ float bs412_compress(BS412Compressor* mpx, float sample) { #ifdef BS412_DEBUG if(mpx->average_counter % mpx->sample_rate == 0) { - debug_printf("MPX power: %.2f dBr (%.0f)\n", modulation_power, avg_deviation); + debug_printf("MPX power: %.2f dBr (%.0f) with gain %.2fx\n", modulation_power, avg_deviation, mpx->gain); } #endif diff --git a/io/audio.c b/io/audio.c index f4e6078..39bc266 100644 --- a/io/audio.c +++ b/io/audio.c @@ -7,6 +7,8 @@ int init_PulseInputDevice(PulseInputDevice* dev, const int sample_rate, const in if (dev->initialized) return PA_ERR_BADSTATE; pa_sample_spec sample_spec = {.format = format, .channels = channels, .rate = sample_rate}; + if (!pa_sample_spec_valid(&sample_spec)) return PA_ERR_INVALID; + pa_buffer_attr new_buffer_attr = *buffer_attr; dev->sample_spec = sample_spec; dev->buffer_attr = new_buffer_attr; @@ -15,6 +17,8 @@ int init_PulseInputDevice(PulseInputDevice* dev, const int sample_rate, const in dev->stream_name = strdup(stream_name); dev->device = strdup(device); + dev->direction = 1; + int error; dev->dev = pa_simple_new(NULL, app_name, PA_STREAM_RECORD, device, stream_name, &sample_spec, NULL, &new_buffer_attr, &error); if (!dev->dev) return error; @@ -29,11 +33,12 @@ int read_PulseInputDevice(PulseInputDevice* dev, void* buffer, size_t size) { return error; } -void free_PulseInputDevice(PulseInputDevice* dev) { +void free_PulseDevice(PulseDevice* dev) { #ifdef PULSE_DEBUG - debug_printf("Freeing PulseInputDevice with app_name: %s, stream_name: %s, device: %s\n", dev->app_name, dev->stream_name, dev->device); + debug_printf("Freeing PulseDevice with app_name: %s, stream_name: %s, device: %s, direction: %d\n", dev->app_name, dev->stream_name, dev->device, dev->direction); #endif + if (!dev->direction) pa_simple_drain(dev->dev, NULL); if (dev->dev && dev->initialized) pa_simple_free(dev->dev); free(dev->app_name); free(dev->stream_name); @@ -41,6 +46,7 @@ void free_PulseInputDevice(PulseInputDevice* dev) { dev->initialized = 0; } + int init_PulseOutputDevice(PulseOutputDevice* dev, const int sample_rate, const int channels, const char* app_name, const char *stream_name, const char* device, pa_buffer_attr* buffer_attr, enum pa_sample_format format) { #ifdef PULSE_DEBUG debug_printf("Initializing PulseOutputDevice format with app_name: %s, stream_name: %s, device: %s, sample_rate: %d, channels: %d, format: %d\n", app_name, stream_name, device, sample_rate, channels, format); @@ -56,6 +62,8 @@ int init_PulseOutputDevice(PulseOutputDevice* dev, const int sample_rate, const dev->stream_name = strdup(stream_name); dev->device = strdup(device); + dev->direction = 0; + int error; dev->dev = pa_simple_new(NULL, app_name, PA_STREAM_PLAYBACK, device, stream_name, &sample_spec, NULL, &new_buffer_attr, &error); if (!dev->dev) return error; @@ -69,15 +77,3 @@ int write_PulseOutputDevice(PulseOutputDevice* dev, void* buffer, size_t size) { if(pa_simple_write(dev->dev, buffer, size, &error) == 0) return 0; return error; } - -void free_PulseOutputDevice(PulseOutputDevice* dev) { - #ifdef PULSE_DEBUG - debug_printf("Freeing PulseOutputDevice with app_name: %s, stream_name: %s, device: %s\n", dev->app_name, dev->stream_name, dev->device); - #endif - - if (dev->dev && dev->initialized) pa_simple_free(dev->dev); - free(dev->app_name); - free(dev->stream_name); - free(dev->device); - dev->initialized = 0; -} \ No newline at end of file diff --git a/io/audio.h b/io/audio.h index 190030d..05cc76a 100644 --- a/io/audio.h +++ b/io/audio.h @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef DEBUG #define PULSE_DEBUG @@ -20,15 +21,15 @@ typedef struct char* app_name; char* stream_name; char* device; - sig_atomic_t initialized; + bool initialized; + bool direction; // 0 = output, 1 - input } PulseDevice; typedef PulseDevice PulseInputDevice; int init_PulseInputDevice(PulseInputDevice* dev, const int sample_rate, const int channels, const char* app_name, const char *stream_name, const char* device, pa_buffer_attr* buffer_attr, enum pa_sample_format format); int read_PulseInputDevice(PulseInputDevice *dev, void *buffer, size_t size); -void free_PulseInputDevice(PulseInputDevice *dev); +void free_PulseDevice(PulseDevice *dev); typedef PulseDevice PulseOutputDevice; int init_PulseOutputDevice(PulseOutputDevice* dev, const int sample_rate, const int channels, const char* app_name, const char *stream_name, const char* device, pa_buffer_attr* buffer_attr, enum pa_sample_format format); int write_PulseOutputDevice(PulseOutputDevice *dev, void *buffer, size_t size); -void free_PulseOutputDevice(PulseOutputDevice *dev); \ No newline at end of file diff --git a/src/chimer95.c b/src/chimer95.c index fb57518..3d5d132 100644 --- a/src/chimer95.c +++ b/src/chimer95.c @@ -311,6 +311,6 @@ int main(int argc, char **argv) { int ret = run_chimer95(config, &runtime); printf("Cleaning up...\n"); - free_PulseOutputDevice(&runtime.output_device); + free_PulseDevice(&runtime.output_device); return ret; } \ No newline at end of file diff --git a/src/fm95.c b/src/fm95.c index f5533a4..3a9a700 100644 --- a/src/fm95.c +++ b/src/fm95.c @@ -7,7 +7,6 @@ #define buffer_maxlength 12288 #define buffer_tlength_fragsize 12288 -#define buffer_prebuf 8 #include "../dsp/oscillator.h" #include "../filter/iir.h" @@ -129,13 +128,13 @@ void cleanup_runtime(FM95_Runtime* runtime, FM95_Config config) { } void cleanup_audio_runtime(FM95_Runtime *rt, bool mpx_on, bool rds_on) { - free_PulseInputDevice(&rt->input_device); - if (mpx_on) free_PulseInputDevice(&rt->mpx_device); + free_PulseDevice(&rt->input_device); + if (mpx_on) free_PulseDevice(&rt->mpx_device); if (rds_on) { - free_PulseInputDevice(&rt->rds_device); + free_PulseDevice(&rt->rds_device); free(rt->rds_in); } - free_PulseOutputDevice(&rt->output_device); + free_PulseDevice(&rt->output_device); } int run_fm95(const FM95_Config config, FM95_Runtime* runtime) { @@ -379,7 +378,7 @@ int setup_audio(FM95_Runtime* runtime, const FM95_DeviceNames dv_names, const FM pa_buffer_attr output_buffer_atr = { .maxlength = buffer_maxlength, .tlength = buffer_tlength_fragsize, - .prebuf = buffer_prebuf + .prebuf = 16 }; int opentime_pulse_error; @@ -397,7 +396,7 @@ int setup_audio(FM95_Runtime* runtime, const FM95_DeviceNames dv_names, const FM opentime_pulse_error = init_PulseInputDevice(&runtime->mpx_device, config.sample_rate, 1, "fm95", "MPX Input", dv_names.mpx, &input_buffer_atr, PA_SAMPLE_FLOAT32NE); if (opentime_pulse_error) { fprintf(stderr, "Error: cannot open MPX device: %s\n", pa_strerror(opentime_pulse_error)); - free_PulseInputDevice(&runtime->input_device); + free_PulseDevice(&runtime->input_device); return 1; } } @@ -407,8 +406,8 @@ int setup_audio(FM95_Runtime* runtime, const FM95_DeviceNames dv_names, const FM opentime_pulse_error = init_PulseInputDevice(&runtime->rds_device, config.sample_rate, config.rds_streams, "fm95", "RDS95 Input", dv_names.rds, &input_buffer_atr, PA_SAMPLE_FLOAT32NE); if (opentime_pulse_error) { fprintf(stderr, "Error: cannot open RDS device: %s\n", pa_strerror(opentime_pulse_error)); - free_PulseInputDevice(&runtime->input_device); - if(mpx_on) free_PulseInputDevice(&runtime->mpx_device); + free_PulseDevice(&runtime->input_device); + if(mpx_on) free_PulseDevice(&runtime->mpx_device); return 1; } runtime->rds_in = malloc(sizeof(float) * BUFFER_SIZE * config.rds_streams); @@ -419,9 +418,9 @@ int setup_audio(FM95_Runtime* runtime, const FM95_DeviceNames dv_names, const FM opentime_pulse_error = init_PulseOutputDevice(&runtime->output_device, config.sample_rate, 1, "fm95", "Main Audio Output", dv_names.output, &output_buffer_atr, PA_SAMPLE_FLOAT32NE); if (opentime_pulse_error) { fprintf(stderr, "Error: cannot open output device: %s\n", pa_strerror(opentime_pulse_error)); - free_PulseInputDevice(&runtime->input_device); - if(mpx_on) free_PulseInputDevice(&runtime->mpx_device); - if(rds_on) free_PulseInputDevice(&runtime->rds_device); + free_PulseDevice(&runtime->input_device); + if(mpx_on) free_PulseDevice(&runtime->mpx_device); + if(rds_on) free_PulseDevice(&runtime->rds_device); return 1; } return 0; diff --git a/src/sca95.c b/src/sca95.c index 10999e1..fd59492 100644 --- a/src/sca95.c +++ b/src/sca95.c @@ -187,7 +187,7 @@ int main(int argc, char **argv) { opentime_pulse_error = init_PulseOutputDevice(&runtime.output, config.sample_rate, 1, "sca95", "Signal Output", audio_output_device, &output_buffer_atr, PA_SAMPLE_FLOAT32NE); if (opentime_pulse_error) { fprintf(stderr, "Error: cannot open output device: %s\n", pa_strerror(opentime_pulse_error)); - free_PulseInputDevice(&runtime.input); + free_PulseDevice(&runtime.input); return 1; } @@ -196,7 +196,7 @@ int main(int argc, char **argv) { int ret = run_sca95(config, &runtime); printf("Cleaning up...\n"); - free_PulseInputDevice(&runtime.input); - free_PulseOutputDevice(&runtime.output); + free_PulseDevice(&runtime.input); + free_PulseDevice(&runtime.output); return ret; } \ No newline at end of file diff --git a/src/vban95.c b/src/vban95.c index d953fa1..3099135 100644 --- a/src/vban95.c +++ b/src/vban95.c @@ -385,7 +385,7 @@ int main(int argc, char *argv[]) { continue; } - if (output.initialized) free_PulseOutputDevice(&output); + if (output.initialized) free_PulseDevice(&output); int result = init_PulseOutputDevice( &output, @@ -410,7 +410,7 @@ int main(int argc, char *argv[]) { // Clean up printf("Cleaning up...\n"); - if (output.initialized) free_PulseOutputDevice(&output); + if (output.initialized) free_PulseDevice(&output); destroy_audio_buffer(audio_buffer); close(sockfd);