[pulseaudio-discuss] [PATCH 3/6] core-util: Add pa_parse_cvolume().
Tanu Kaskinen
tanu.kaskinen at jollamobile.com
Thu Oct 25 10:37:23 PDT 2012
---
src/pulsecore/core-util.c | 57 +++++++++++++++++++++++++++++++++++++++++++++
src/pulsecore/core-util.h | 1 +
2 files changed, 58 insertions(+)
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index d8d44a7..b09d5d2 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -1002,6 +1002,63 @@ int pa_parse_volume(const char *v, pa_volume_t *volume) {
return ret;
}
+/* Parses a comma-separated list of "channel:volume" pairs. */
+int pa_parse_cvolume(const char *s, pa_cvolume *vol, pa_channel_map *map) {
+ char *channel_and_volume;
+ const char *state = NULL;
+ pa_cvolume vol_tmp;
+ pa_channel_map map_tmp;
+
+ pa_cvolume_init(&vol_tmp);
+ pa_channel_map_init(&map_tmp);
+
+ while ((channel_and_volume = pa_split(s, ",", &state))) {
+ char *channel_str;
+ char *volume_str;
+ pa_channel_position_t channel;
+ pa_volume_t volume;
+
+ if (map_tmp.channels >= PA_CHANNELS_MAX)
+ goto fail;
+
+ channel_str = channel_and_volume;
+ volume_str = channel_and_volume + strcspn(channel_and_volume, ":");
+
+ if (!*volume_str)
+ goto fail; /* No colon found. */
+
+ if (volume_str == channel_and_volume)
+ goto fail; /* No channel found. */
+
+ *volume_str = '\0'; /* Replace the colon with null. */
+ volume_str++; /* Move to the real beginning of the volume string. */
+
+ if (!*volume_str)
+ goto fail; /* No volume found. */
+
+ if ((channel = pa_channel_position_from_string(channel_str)) == PA_CHANNEL_POSITION_INVALID)
+ goto fail;
+
+ if (pa_parse_volume(volume_str, &volume) < 0)
+ goto fail;
+
+ map_tmp.map[map_tmp.channels++] = channel;
+ vol_tmp.values[vol_tmp.channels++] = volume;
+
+ pa_xfree(channel_and_volume);
+ }
+
+ *vol = vol_tmp;
+ *map = map_tmp;
+
+ return 0;
+
+fail:
+ pa_xfree(channel_and_volume);
+
+ return -1;
+}
+
/* Split the specified string wherever one of the strings in delimiter
* occurs. Each time it is called returns a newly allocated string
* with pa_xmalloc(). The variable state points to, should be
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index 9d59383..c15b652 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -85,6 +85,7 @@ void pa_reset_priority(void);
int pa_parse_boolean(const char *s) PA_GCC_PURE;
int pa_parse_volume(const char *s, pa_volume_t *volume);
+int pa_parse_cvolume(const char *s, pa_cvolume *vol, pa_channel_map *map);
static inline const char *pa_yes_no(pa_bool_t b) {
return b ? "yes" : "no";
--
1.7.10.4
More information about the pulseaudio-discuss
mailing list