From 5c9bd337a0c64470a411b0863d28439eaf1c3fc3 Mon Sep 17 00:00:00 2001 From: Patrick Beaulieu Date: Tue, 22 Oct 2019 15:03:35 -0400 Subject: [PATCH] added pulseaudio source support --- CMakeLists.txt | 2 + include/odas/general/interface.h | 3 + include/odas/source/src_hops.h | 10 +++ src/general/interface.c | 63 +++++++++++++++++++ src/source/src_hops.c | 102 ++++++++++++++++++++++++++++++- 5 files changed, 179 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a2ac3c..effc1f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,6 +193,8 @@ target_link_libraries(odas ${PC_FFTW3_LIBRARIES} ${PC_ALSA_LIBRARIES} ${PC_LIBCONFIG_LIBRARIES} + pulse + pulse-simple m pthread ) diff --git a/include/odas/general/interface.h b/include/odas/general/interface.h index 349ca1e..58c71fe 100644 --- a/include/odas/general/interface.h +++ b/include/odas/general/interface.h @@ -35,6 +35,7 @@ interface_socket = 3, interface_soundcard = 4, interface_terminal = 5, + interface_pulseaudio = 6 } interface_type; @@ -59,6 +60,8 @@ interface_obj * interface_construct_socket(const char * ip, const unsigned int port); + interface_obj * interface_construct_pulseaudio(const char * sourceName); + interface_obj * interface_construct_soundcard(const unsigned int card, const unsigned int device); interface_obj * interface_construct_soundcard_by_name(char * deviceName); diff --git a/include/odas/source/src_hops.h b/include/odas/source/src_hops.h index b0a32b9..8856bb9 100644 --- a/include/odas/source/src_hops.h +++ b/include/odas/source/src_hops.h @@ -27,6 +27,8 @@ #include #include #include + #include + #include #include "../general/format.h" #include "../general/interface.h" @@ -47,6 +49,8 @@ FILE * fp; snd_pcm_t * ch; + pa_simple * pa; + pa_sample_spec ss; char * buffer; unsigned int bufferSize; @@ -78,18 +82,24 @@ void src_hops_open_interface_soundcard(src_hops_obj * obj); + void src_hops_open_interface_pulseaudio(src_hops_obj * obj); + void src_hops_close(src_hops_obj * obj); void src_hops_close_interface_file(src_hops_obj * obj); void src_hops_close_interface_soundcard(src_hops_obj * obj); + void src_hops_close_interface_pulseaudio(src_hops_obj * obj); + int src_hops_process(src_hops_obj * obj); int src_hops_process_interface_file(src_hops_obj * obj); int src_hops_process_interface_soundcard(src_hops_obj * obj); + int src_hops_process_interface_pulseaudio(src_hops_obj * obj); + void src_hops_process_format_binary_int08(src_hops_obj * obj); void src_hops_process_format_binary_int16(src_hops_obj * obj); diff --git a/src/general/interface.c b/src/general/interface.c index c01a628..02e07a5 100644 --- a/src/general/interface.c +++ b/src/general/interface.c @@ -213,6 +213,54 @@ } + interface_obj * interface_construct_pulseaudio(const char * sourceName) { + + interface_obj * obj; + + obj = (interface_obj *) malloc(sizeof(interface_obj)); + + // +----------------------------------------------------------+ + // | Type | + // +----------------------------------------------------------+ + + obj->type = interface_pulseaudio; + + // +----------------------------------------------------------+ + // | Blackhole | + // +----------------------------------------------------------+ + + // (Empty) + + // +----------------------------------------------------------+ + // | File | + // +----------------------------------------------------------+ + + obj->fileName = (char *) NULL; + + // +----------------------------------------------------------+ + // | Socket | + // +----------------------------------------------------------+ + + obj->ip = (char *) NULL; + obj->port = 0; + + // +----------------------------------------------------------+ + // | Soundcard | + // +----------------------------------------------------------+ + + obj->deviceName = (char *) malloc(sizeof(char) * (strlen(sourceName)+1)); + strcpy(obj->deviceName, sourceName); + + // +----------------------------------------------------------+ + // | Terminal | + // +----------------------------------------------------------+ + + // (Empty) + + return obj; + + } + interface_obj * interface_construct_soundcard(const unsigned int card, const unsigned int device) { char * deviceName = (char *) malloc(sizeof(char) * 1024); @@ -366,6 +414,15 @@ clone->deviceName = obj->deviceName; } + // +----------------------------------------------------------+ + // | Pulseaudio | + // +----------------------------------------------------------+ + + if (obj->type == interface_pulseaudio) { + clone->deviceName = obj->deviceName; + } + + // +----------------------------------------------------------+ // | Terminal | // +----------------------------------------------------------+ @@ -432,6 +489,12 @@ break; + case interface_pulseaudio: + + printf("type = pulseaudio, devicename = %s\n", obj->deviceName); + + break; + case interface_terminal: printf("type = terminal\n"); diff --git a/src/source/src_hops.c b/src/source/src_hops.c index 8c2081f..96ec097 100644 --- a/src/source/src_hops.c +++ b/src/source/src_hops.c @@ -48,7 +48,11 @@ ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int08)) || ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int16)) || ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int24)) || - ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int32)))) { + ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int32)) || + ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int08)) || + ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int16)) || + ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int24)) || + ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int32)))) { printf("Source hops: Invalid interface and/or format.\n"); exit(EXIT_FAILURE); @@ -111,6 +115,12 @@ break; + case interface_pulseaudio: + + src_hops_open_interface_pulseaudio(obj); + + break; + default: printf("Source hops: Invalid interface type.\n"); @@ -133,6 +143,57 @@ } + void src_hops_open_interface_pulseaudio(src_hops_obj * obj) { + + pa_sample_format_t format; + + switch (obj->format->type) { + + case format_binary_int08: + + format = PA_SAMPLE_U8; + + break; + + case format_binary_int16: + + format = PA_SAMPLE_S16LE; + + break; + + case format_binary_int24: + + format = PA_SAMPLE_S24LE; + + break; + + case format_binary_int32: + + format = PA_SAMPLE_S32LE; + + break; + + default: + + printf("Source hops: Invalid format.\n"); + exit(EXIT_FAILURE); + + break; + + } + + obj->ss.format = format; + obj->ss.rate = obj->fS; + obj->ss.channels = obj->nChannels; + + int err; + if (!(obj->pa = pa_simple_new(NULL, "Odas", PA_STREAM_RECORD, obj->interface->deviceName, "record", &obj->ss, NULL, NULL, &err))) { + printf("Source hops: Cannot open pulseaudio device %s: %s\n", obj->interface->deviceName, pa_strerror(err)); + exit(EXIT_FAILURE); + } + + } + void src_hops_open_interface_soundcard(src_hops_obj * obj) { snd_pcm_hw_params_t * hw_params; @@ -240,6 +301,12 @@ break; + case interface_pulseaudio: + + src_hops_close_interface_pulseaudio(obj); + + break; + default: printf("Source hops: Invalid interface type.\n"); @@ -263,6 +330,13 @@ } + void src_hops_close_interface_pulseaudio(src_hops_obj * obj) { + + if (obj->pa != NULL) + pa_simple_free(obj->pa); + + } + int src_hops_process(src_hops_obj * obj) { int rtnValue; @@ -316,6 +390,12 @@ break; + case interface_pulseaudio: + + rtnValue = src_hops_process_interface_pulseaudio(obj); + + break; + default: printf("Source hops: Invalid interface type.\n"); @@ -375,6 +455,26 @@ } + int src_hops_process_interface_pulseaudio(src_hops_obj * obj) { + + int rtnValue; + int err; + + if (pa_simple_read(obj->pa, obj->buffer, obj->bufferSize, &err) < 0) { + + rtnValue = -1; + + } + else { + + rtnValue = 0; + + } + + return rtnValue; + + } + void src_hops_process_format_binary_int08(src_hops_obj * obj) { unsigned int iSample; -- 2.34.1