0
1
mirror of https://github.com/radio95-rnt/fm95.git synced 2026-02-27 03:23:54 +01:00

remove darc, shorten audio.c and add some error checking for pulse api

This commit is contained in:
2025-06-08 10:00:03 +02:00
parent 77489752f9
commit 256f91fbdf
4 changed files with 48 additions and 167 deletions

View File

@@ -1,52 +1,12 @@
#include "audio.h"
int init_PulseInputDevice(PulseInputDevice* dev, int sample_rate, int channels, char* app_name, char *stream_name, char* device, pa_buffer_attr* buffer_attr) {
#ifdef PULSE_DEBUG
debug_printf("Initializing PulseInputDevice with app_name: %s, stream_name: %s, device: %s, sample_rate: %d, channels: %d\n", app_name, stream_name, device, sample_rate, channels);
#endif
if (dev->initialized) return -1;
pa_sample_spec sample_spec = {
.format = PA_SAMPLE_FLOAT32NE,
.channels = channels,
.rate = sample_rate
};
pa_buffer_attr new_buffer_attr = *buffer_attr;
dev->sample_spec = sample_spec;
dev->buffer_attr = new_buffer_attr;
dev->app_name = strdup(app_name);
dev->stream_name = strdup(stream_name);
dev->device = strdup(device);
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;
dev->initialized = 1;
return 0;
}
int init_PulseInputDevicef(PulseInputDevice* dev, int sample_rate, int channels, char* app_name, char *stream_name, char* device, pa_buffer_attr* buffer_attr, enum pa_sample_format format) {
#ifdef PULSE_DEBUG
debug_printf("Initializing PulseInputDevice 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);
#endif
if (dev->initialized) return -1;
pa_sample_spec sample_spec = {
.format = format,
.channels = channels,
.rate = sample_rate
};
pa_sample_spec sample_spec = {.format = format, .channels = channels, .rate = sample_rate};
pa_buffer_attr new_buffer_attr = *buffer_attr;
dev->sample_spec = sample_spec;
dev->buffer_attr = new_buffer_attr;
@@ -56,22 +16,16 @@ int init_PulseInputDevicef(PulseInputDevice* dev, int sample_rate, int channels,
dev->device = strdup(device);
int error;
dev->dev = pa_simple_new(
NULL,
app_name,
PA_STREAM_RECORD,
device,
stream_name,
&sample_spec,
NULL,
&new_buffer_attr,
&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;
dev->initialized = 1;
return 0;
}
int init_PulseInputDevice(PulseInputDevice* dev, int sample_rate, int channels, char* app_name, char *stream_name, char* device, pa_buffer_attr* buffer_attr) {
return init_PulseInputDevicef(dev, sample_rate, channels, app_name, stream_name, device, buffer_attr, PA_SAMPLE_FLOAT32NE);
}
int read_PulseInputDevice(PulseInputDevice* dev, void* buffer, size_t size) {
if (!dev->initialized) return -1;
int error = 0;
@@ -82,7 +36,7 @@ int read_PulseInputDevice(PulseInputDevice* dev, void* buffer, size_t size) {
int read_PulseInputDevicef(PulseInputDevice* dev, void* buffer, size_t size) {
if (!dev->initialized) return -1;
int error = 0;
pa_simple_read(dev->dev, buffer, size, &error);
if(pa_simple_read(dev->dev, buffer, size, &error) == 0) return 0;
return error;
}
@@ -98,39 +52,11 @@ void free_PulseInputDevice(PulseInputDevice* dev) {
dev->initialized = 0;
}
int init_PulseOutputDevice(PulseOutputDevice* dev, int sample_rate, int channels, char* app_name, char *stream_name, char* device, pa_buffer_attr* buffer_attr) {
if (dev->initialized) return -1;
pa_sample_spec sample_spec = {
.format = PA_SAMPLE_FLOAT32NE,
.channels = channels,
.rate = sample_rate
};
pa_buffer_attr new_buffer_attr = *buffer_attr;
dev->sample_spec = sample_spec;
dev->buffer_attr = new_buffer_attr;
dev->app_name = strdup(app_name);
dev->stream_name = strdup(stream_name);
dev->device = strdup(device);
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;
dev->initialized = 1;
return 0;
}
int init_PulseOutputDevicef(PulseOutputDevice* dev, int sample_rate, int channels, char* app_name, char *stream_name, 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);
#endif
if (dev->initialized) return -1;
pa_sample_spec sample_spec = {
.format = format,
@@ -162,10 +88,14 @@ int init_PulseOutputDevicef(PulseOutputDevice* dev, int sample_rate, int channel
return 0;
}
int init_PulseOutputDevice(PulseOutputDevice* dev, int sample_rate, int channels, char* app_name, char *stream_name, char* device, pa_buffer_attr* buffer_attr) {
return init_PulseOutputDevicef(dev, sample_rate, channels, app_name, stream_name, device, buffer_attr, PA_SAMPLE_FLOAT32NE);
}
int write_PulseOutputDevice(PulseOutputDevice* dev, void* buffer, size_t size) {
if (!dev->initialized) return -1;
int error = 0;
pa_simple_write(dev->dev, buffer, size, &error);
if(pa_simple_write(dev->dev, buffer, size, &error) == 0) return 0;
return error;
}