From: philippewarren Date: Thu, 3 Feb 2022 16:36:32 +0000 (-0500) Subject: Added pulseaudio mapping X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=928fe1fd0ba217729016661adc354f5c57aaddfb;p=yn%2Fyn-odas.git Added pulseaudio mapping --- diff --git a/demo/odaslive/parameters.c b/demo/odaslive/parameters.c index bc4ad32..e78eee8 100644 --- a/demo/odaslive/parameters.c +++ b/demo/odaslive/parameters.c @@ -141,6 +141,37 @@ } + 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; @@ -150,6 +181,7 @@ unsigned int tmpInt2; cfg = src_hops_cfg_construct(); + cfg->channel_map = NULL; // Only used in pulseaudio mode // +----------------------------------------------------------+ // | Format | @@ -204,12 +236,17 @@ 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 { @@ -251,6 +288,89 @@ } + 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) { @@ -2080,4 +2200,4 @@ return cfg; - } \ No newline at end of file + } diff --git a/demo/odaslive/parameters.h b/demo/odaslive/parameters.h index 10daacd..514d82a 100644 --- a/demo/odaslive/parameters.h +++ b/demo/odaslive/parameters.h @@ -3,6 +3,7 @@ #include #include + #include int parameters_lookup_int(const char * file, const char * path); @@ -12,11 +13,15 @@ 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); diff --git a/include/odas/source/src_hops.h b/include/odas/source/src_hops.h index 8856bb9..61e5bde 100644 --- a/include/odas/source/src_hops.h +++ b/include/odas/source/src_hops.h @@ -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; diff --git a/src/general/interface.c b/src/general/interface.c index 02e07a5..89ad927 100644 --- a/src/general/interface.c +++ b/src/general/interface.c @@ -306,7 +306,8 @@ // | Soundcard | // +----------------------------------------------------------+ - obj->deviceName = deviceName; + obj->deviceName = (char *) malloc(sizeof(char) * (strlen(deviceName)+1)); + strcpy(obj->deviceName, deviceName); // +----------------------------------------------------------+ // | Terminal | diff --git a/src/source/src_hops.c b/src/source/src_hops.c index 96ec097..5fa51e8 100644 --- a/src/source/src_hops.c +++ b/src/source/src_hops.c @@ -38,6 +38,20 @@ 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)); @@ -187,7 +201,7 @@ 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); } @@ -401,7 +415,7 @@ printf("Source hops: Invalid interface type.\n"); exit(EXIT_FAILURE); - break; + break; } @@ -604,6 +618,9 @@ 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);