mirror of
https://github.com/radio95-rnt/fm95.git
synced 2026-02-26 19:23:51 +01:00
remove darc, shorten audio.c and add some error checking for pulse api
This commit is contained in:
102
io/audio.c
102
io/audio.c
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
11
io/audio.h
11
io/audio.h
@@ -20,16 +20,17 @@ typedef struct
|
||||
char* app_name;
|
||||
char* stream_name;
|
||||
char* device;
|
||||
int initialized;
|
||||
} PulseInputDevice;
|
||||
sig_atomic_t initialized;
|
||||
} PulseDevice;
|
||||
|
||||
int init_PulseInputDevice(PulseInputDevice *dev, int sample_rate, int channels, char *app_name, char *stream_name, char *device, pa_buffer_attr *buffer_attr);
|
||||
typedef PulseDevice PulseInputDevice;
|
||||
int init_PulseInputDevicef(PulseInputDevice *dev, int sample_rate, int channels, char *app_name, char *stream_name, char *device, pa_buffer_attr *buffer_attr, pa_sample_format_t format);
|
||||
int init_PulseInputDevice(PulseInputDevice *dev, int sample_rate, int channels, char *app_name, char *stream_name, char *device, pa_buffer_attr *buffer_attr);
|
||||
int read_PulseInputDevice(PulseInputDevice *dev, void *buffer, size_t size);
|
||||
void free_PulseInputDevice(PulseInputDevice *dev);
|
||||
|
||||
typedef PulseInputDevice PulseOutputDevice;
|
||||
int init_PulseOutputDevice(PulseOutputDevice *dev, int sample_rate, int channels, char *app_name, char *stream_name, char *device, pa_buffer_attr *buffer_attr);
|
||||
typedef PulseDevice PulseOutputDevice;
|
||||
int init_PulseOutputDevicef(PulseOutputDevice *dev, int sample_rate, int channels, char *app_name, char *stream_name, char *device, pa_buffer_attr *buffer_attr, pa_sample_format_t format);
|
||||
int init_PulseOutputDevice(PulseOutputDevice *dev, int sample_rate, int channels, char *app_name, char *stream_name, char *device, pa_buffer_attr *buffer_attr);
|
||||
int write_PulseOutputDevice(PulseOutputDevice *dev, void *buffer, size_t size);
|
||||
void free_PulseOutputDevice(PulseOutputDevice *dev);
|
||||
Reference in New Issue
Block a user