Added pulseaudio mapping
authorphilippewarren <philippewarren31@gmail.com>
Thu, 3 Feb 2022 16:36:32 +0000 (11:36 -0500)
committerphilippewarren <philippewarren31@gmail.com>
Thu, 3 Feb 2022 16:53:39 +0000 (11:53 -0500)
demo/odaslive/parameters.c
demo/odaslive/parameters.h
include/odas/source/src_hops.h
src/general/interface.c
src/source/src_hops.c

index bc4ad327450c2fd78b4ee48bdab9e51b37f60cec..e78eee8a498e5a7e909d747a7fa7353a465704fc 100644 (file)
 
     }
 
+    bool parameters_exists(const char * file, const char * path)
+    {
+        config_t cfg;
+        unsigned int rtnValue;
+
+        config_init(&cfg); 
+
+        if(!config_read_file(&cfg, file))
+        {
+            
+            printf("%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
+            config_destroy(&cfg);
+          
+            exit(EXIT_FAILURE);
+
+        }
+
+        if (config_lookup(&cfg, path) == NULL)
+        {
+            rtnValue = false;
+        }
+        else
+        {
+            rtnValue = true;
+        }
+
+        config_destroy(&cfg);   
+
+        return rtnValue;
+    }
+
     src_hops_cfg * parameters_src_hops_mics_config(const char * fileConfig) {
 
         src_hops_cfg * cfg;
         unsigned int tmpInt2;
 
         cfg = src_hops_cfg_construct();
+        cfg->channel_map = NULL; // Only used in pulseaudio mode
 
         // +----------------------------------------------------------+
         // | Format                                                   |
 
                 cfg->interface = interface_construct_soundcard_by_name(tmpStr2);
 
+                free((void *) tmpStr2);
+
             }
             else if (strcmp(tmpStr1, "pulseaudio") == 0) {
 
                 tmpStr2 = parameters_lookup_string(fileConfig, "raw.interface.source");
 
                 cfg->interface = interface_construct_pulseaudio(tmpStr2);
+                cfg->channel_map = parameters_pa_channel_map_config(fileConfig);
+
+                free((void *) tmpStr2);
 
             }
             else {
 
     }
 
+    pa_channel_map* parameters_pa_channel_map_config(const char* fileConfig)
+    {
+        bool channelNamesExists = parameters_exists(fileConfig, "raw.interface.channelnames");
+        bool channelMapExists = parameters_exists(fileConfig, "raw.interface.channelmap");
+        if (channelNamesExists && channelMapExists)
+        {
+            printf("Configuration error: raw.interface.channelnames and raw.interface.channelmap cannot be used at the same time\n");
+            exit(EXIT_FAILURE);
+        }
+        else if (!(channelNamesExists || channelMapExists))
+        {
+            printf("Configuration error: either raw.interface.channelnames or raw.interface.channelmap must be used with pulseaudio\n");
+            exit(EXIT_FAILURE);
+        }
+
+        if (channelNamesExists)
+        {
+            char * tmpStr;
+            pa_channel_map* map = (pa_channel_map*) malloc(sizeof(pa_channel_map));
+            pa_channel_map_init(map);
+            map->channels = parameters_lookup_int(fileConfig, "raw.nChannels");
+
+            tmpStr = parameters_lookup_string(fileConfig, "raw.interface.channelnames");
+
+            if (!pa_channel_map_parse(map, tmpStr))
+            {
+                printf("Invalid channel map string: %s\n", tmpStr);
+                exit(EXIT_FAILURE);
+            }
+
+            if (map->channels != parameters_lookup_int(fileConfig, "raw.nChannels"))
+            {
+                printf("Configuration error: raw.interface.channelnames length does not match raw.nChannels\n");
+                exit(EXIT_FAILURE);
+            }
+
+            if (!pa_channel_map_valid(map))
+            {
+                printf("Invalid channel map\n");
+                exit(EXIT_FAILURE);
+            }
+
+            free((void *) tmpStr);
+
+            return map;
+
+        }
+        else
+        {
+            unsigned int nChannels = parameters_count(fileConfig, "raw.interface.channelmap");
+            char tmpStr[1024];
+            pa_channel_map* map = (pa_channel_map*) malloc(sizeof(pa_channel_map));
+            pa_channel_map_init(map);
+            map->channels = 0;
+
+            for (unsigned int iChannel = 0; iChannel < nChannels; ++iChannel)
+            {
+                sprintf(tmpStr, "raw.interface.channelmap.[%u]", iChannel);
+                char* channel = parameters_lookup_string(fileConfig, tmpStr);
+                if (((map->map[iChannel] = pa_channel_position_from_string(channel)) == PA_CHANNEL_POSITION_INVALID))
+                {
+                    printf("Invalid channel position: %s\n", channel);
+                    exit(EXIT_FAILURE);
+                }
+                ++(map->channels);
+                free(channel);
+            }
+
+            if (map->channels != parameters_lookup_int(fileConfig, "raw.nChannels"))
+            {
+                printf("Configuration error: raw.interface.channelmap length does not match raw.nChannels\n");
+                exit(EXIT_FAILURE);
+            }
+
+            if (!pa_channel_map_valid(map))
+            {
+                printf("Invalid channel map\n");
+                exit(EXIT_FAILURE);
+            }
+            
+            return map;
+        }
+    }
 
     mod_mapping_cfg * parameters_mod_mapping_mics_config(const char * fileConfig) {
 
 
         return cfg;
 
-    }
\ No newline at end of file
+    }
index 10daacd2e12ca831fccfa0e6f4ce60817054f6be..514d82a727346d3b41659e9b5439c3f066ab9f18 100644 (file)
@@ -3,6 +3,7 @@
 
     #include <odas/odas.h>
     #include <libconfig.h>
+    #include <stdbool.h>
     
     int parameters_lookup_int(const char * file, const char * path);
 
 
     unsigned int parameters_count(const char * file, const char * path);
 
+    bool parameters_exists(const char * file, const char * path);
+
 
     src_hops_cfg * parameters_src_hops_mics_config(const char * fileConfig);
 
     msg_hops_cfg * parameters_msg_hops_mics_raw_config(const char * fileConfig);
 
+    pa_channel_map* parameters_pa_channel_map_config(const char* fileConfig);
+
 
     mod_mapping_cfg * parameters_mod_mapping_mics_config(const char * fileConfig);
 
index 8856bb9667d327db4f65c15a6a51a64023282d9b..61e5bde80ac0b35f16448f6afe33231a3f3802f9 100644 (file)
@@ -51,6 +51,7 @@
         snd_pcm_t * ch;
         pa_simple * pa;
         pa_sample_spec ss;
+        pa_channel_map cm;
 
         char * buffer;
         unsigned int bufferSize;
@@ -65,6 +66,7 @@
 
         format_obj * format;
         interface_obj * interface;
+        pa_channel_map * channel_map;
 
     } src_hops_cfg;
 
index 02e07a5cfd9ed88f20a8c420346636758c1ef76a..89ad927b62c38ebe0961ec08775b5fb4ff4b2e92 100644 (file)
        // | Soundcard                                                |
        // +----------------------------------------------------------+
 
-       obj->deviceName = deviceName;
+        obj->deviceName = (char *) malloc(sizeof(char) * (strlen(deviceName)+1));
+        strcpy(obj->deviceName, deviceName);
 
        // +----------------------------------------------------------+
        // | Terminal                                                 |
index 96ec0973f36d55d880bec67e2c26facb1d771a7d..5fa51e8555334f5653c1833120f7918d21ae6f9b 100644 (file)
         
         obj->format = format_clone(src_hops_config->format);
         obj->interface = interface_clone(src_hops_config->interface);
+        if (src_hops_config->channel_map != NULL)
+        {
+            // Will not be null if in pulseaudio mode
+            memcpy(&obj->cm, src_hops_config->channel_map, sizeof(pa_channel_map));
+        }
+        else
+        {
+            // Can be null if not in pulseaudio mode ONLY
+            if (obj->interface->type == interface_pulseaudio)
+            {
+                printf("Error: Pulseaudio interface requires channel map.\n");
+                exit(EXIT_FAILURE);
+            }
+        }
 
         memset(obj->bytes, 0x00, 4 * sizeof(char));
 
         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))) {
+        if (!(obj->pa = pa_simple_new(NULL, "Odas", PA_STREAM_RECORD, obj->interface->deviceName, "record", &obj->ss, &obj->cm, NULL, &err))) {
             printf("Source hops: Cannot open pulseaudio device %s: %s\n", obj->interface->deviceName, pa_strerror(err));
             exit(EXIT_FAILURE);
         }
                 printf("Source hops: Invalid interface type.\n");
                 exit(EXIT_FAILURE);
 
-            break;           
+            break;
 
         }
 
         if (src_hops_config->interface != NULL) {
             interface_destroy(src_hops_config->interface);
         }
+        if (src_hops_config->channel_map != NULL) {
+            free((void *) src_hops_config->channel_map);
+        }
 
         free((void *) src_hops_config);