added pulseaudio source support
authorPatrick Beaulieu <patrick.beaulieu3@usherbrooke.ca>
Tue, 22 Oct 2019 19:03:35 +0000 (15:03 -0400)
committerPatrick Beaulieu <patrick.beaulieu3@usherbrooke.ca>
Tue, 22 Oct 2019 19:03:35 +0000 (15:03 -0400)
CMakeLists.txt
include/odas/general/interface.h
include/odas/source/src_hops.h
src/general/interface.c
src/source/src_hops.c

index 9a2ac3ca96ff48f1746a3f60f4eee440e05513ed..effc1f6bb0f5b1914edbf52a9f8d484434e59016 100644 (file)
@@ -193,6 +193,8 @@ target_link_libraries(odas
        ${PC_FFTW3_LIBRARIES}
        ${PC_ALSA_LIBRARIES}
     ${PC_LIBCONFIG_LIBRARIES}
+    pulse
+    pulse-simple
        m
     pthread
 )
index 349ca1e5eb0b4501f8c41a3c8092de9d9f3a5898..58c71fe3265788b64870116b6f4738e01ef59634 100644 (file)
@@ -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);
index b0a32b9ee00f342745b25c395f426cc5a3d68c69..8856bb9667d327db4f65c15a6a51a64023282d9b 100644 (file)
@@ -27,6 +27,8 @@
     #include <stdio.h>
     #include <string.h>
     #include <alsa/asoundlib.h>
+    #include <pulse/simple.h>
+    #include <pulse/error.h>
 
     #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;
 
     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);
index c01a628c7d3b651fdd1fdfc1ed429943e43f1eef..02e07a5cfd9ed88f20a8c420346636758c1ef76a 100644 (file)
 
     }
 
+    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);
                 clone->deviceName = obj->deviceName;
             }
 
+        // +----------------------------------------------------------+
+        // | Pulseaudio                                               |
+        // +----------------------------------------------------------+
+
+            if (obj->type == interface_pulseaudio) {
+                clone->deviceName = obj->deviceName;
+            }
+
+
         // +----------------------------------------------------------+
         // | Terminal                                                 |
         // +----------------------------------------------------------+
 
                     break;
 
+                case interface_pulseaudio:
+
+                    printf("type = pulseaudio, devicename = %s\n", obj->deviceName);
+
+                    break;
+
                 case interface_terminal:
 
                     printf("type = terminal\n");
index 8c2081f21ea6c9059132d3692b8d67548ab25958..96ec0973f36d55d880bec67e2c26facb1d771a7d 100644 (file)
               ((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);
 
             break;
 
+            case interface_pulseaudio:
+
+                src_hops_open_interface_pulseaudio(obj);
+
+            break;
+
             default:
 
                 printf("Source hops: Invalid interface type.\n");
 
     }
 
+    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;
 
             break;
 
+            case interface_pulseaudio:
+
+                src_hops_close_interface_pulseaudio(obj);
+
+            break;
+
             default:
 
                 printf("Source hops: Invalid interface type.\n");
 
     }
 
+    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;
 
             break;
 
+            case interface_pulseaudio:
+
+                rtnValue = src_hops_process_interface_pulseaudio(obj);
+
+            break;
+
             default:
 
                 printf("Source hops: Invalid interface type.\n");
 
     }
 
+    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;