[pulseaudio-commits] src/pulsecore

Tanu Kaskinen tanuk at kemper.freedesktop.org
Thu Oct 25 02:42:36 PDT 2012


 src/pulsecore/core-util.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 src/pulsecore/core-util.h |    3 +++
 2 files changed, 45 insertions(+)

New commits:
commit dd31d6521758c2d39457aab4e91d5c7644732052
Author: Flavio Ceolin <flavio.ceolin at profusion.mobi>
Date:   Wed Oct 24 17:29:45 2012 -0200

    utils: Adding a function to get volume from string
    
    The allowed volume formats are dB, % or integer.
    For example: 10% or 10db or 10.

diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index 710c9dc..d8d44a7 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -960,6 +960,48 @@ int pa_parse_boolean(const char *v) {
     return -1;
 }
 
+/* Try to parse a volume string to pa_volume_t. The allowed formats are:
+ * db, % and unsigned integer */
+int pa_parse_volume(const char *v, pa_volume_t *volume) {
+    int len, ret = -1;
+    uint32_t i;
+    double d;
+    char str[64];
+
+    pa_assert(v);
+    pa_assert(volume);
+
+    len = strlen(v);
+
+    if (len >= 64)
+        return -1;
+
+    memcpy(str, v, len + 1);
+
+    if (str[len - 1] == '%') {
+        str[len - 1] = '\0';
+        if (pa_atou(str, &i) == 0) {
+            *volume = PA_CLAMP_VOLUME((uint64_t) PA_VOLUME_NORM * i / 100);
+            ret = 0;
+        }
+    } else if (len > 2 && (str[len - 1] == 'b' || str[len - 1] == 'B') &&
+               (str[len - 2] == 'd' || str[len - 2] == 'D')) {
+        str[len - 2] = '\0';
+        if (pa_atod(str, &d) == 0) {
+            *volume = pa_sw_volume_from_dB(d);
+            ret = 0;
+        }
+    } else {
+        if (pa_atou(v, &i) == 0) {
+            *volume= PA_CLAMP_VOLUME(i);
+            ret = 0;
+        }
+
+    }
+
+    return ret;
+}
+
 /* 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 b181266..9d59383 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -34,6 +34,7 @@
 #endif
 
 #include <pulse/gccmacro.h>
+#include <pulse/volume.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/socket.h>
 
@@ -83,6 +84,8 @@ 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);
+
 static inline const char *pa_yes_no(pa_bool_t b) {
     return b ? "yes" : "no";
 }



More information about the pulseaudio-commits mailing list