add new API call pa_cvolume_snprint_dB()
authorLennart Poettering <lennart@poettering.net>
Fri, 3 Oct 2008 15:42:38 +0000 (17:42 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 3 Oct 2008 15:42:38 +0000 (17:42 +0200)
src/map-file
src/pulse/volume.c
src/pulse/volume.h
src/tests/voltest.c

index 67a5ee3..2ef2ad6 100644 (file)
@@ -103,6 +103,7 @@ pa_cvolume_max;
 pa_cvolume_remap;
 pa_cvolume_set;
 pa_cvolume_snprint;
+pa_cvolume_snprint_dB;
 pa_cvolume_valid;
 pa_ext_stream_restore_delete;
 pa_ext_stream_restore_read;
index 7c0522e..4d5fcf0 100644 (file)
@@ -141,7 +141,7 @@ double pa_sw_volume_to_linear(pa_volume_t v) {
 
 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
     unsigned channel;
-    int first = 1;
+    pa_bool_t first = TRUE;
     char *e;
 
     pa_assert(s);
@@ -164,7 +164,38 @@ char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
                       (c->values[channel]*100)/PA_VOLUME_NORM);
 
         e = strchr(e, 0);
-        first = 0;
+        first = FALSE;
+    }
+
+    return s;
+}
+
+char *pa_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c) {
+    unsigned channel;
+    pa_bool_t first = TRUE;
+    char *e;
+
+    pa_assert(s);
+    pa_assert(l > 0);
+    pa_assert(c);
+
+    pa_init_i18n();
+
+    if (!pa_cvolume_valid(c)) {
+        pa_snprintf(s, l, _("(invalid)"));
+        return s;
+    }
+
+    *(e = s) = 0;
+
+    for (channel = 0; channel < c->channels && l > 1; channel++) {
+        l -= pa_snprintf(e, l, "%s%u: %0.2f dB",
+                      first ? "" : " ",
+                      channel,
+                      pa_sw_volume_to_dB(c->values[channel]));
+
+        e = strchr(e, 0);
+        first = FALSE;
     }
 
     return s;
index 4b2f3a7..d6eb606 100644 (file)
@@ -136,6 +136,16 @@ pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v);
 /** Pretty print a volume structure */
 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c);
 
+/** Maximum length of the strings returned by
+ * pa_cvolume_snprint_dB(). Please note that this value can change with
+ * any release without warning and without being considered API or ABI
+ * breakage. You should not use this definition anywhere where it
+ * might become part of an ABI. \since 0.9.13 */
+#define PA_CVOLUME_SNPRINT_DB_MAX 448
+
+/** Pretty print a volume structure but show dB values. \since 0.9.13 */
+char *pa_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c);
+
 /** Return the average volume of all channels */
 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) PA_GCC_PURE;
 
index 5b26c0f..bbf3ea1 100644 (file)
@@ -5,6 +5,7 @@
 
 int main(int argc, char *argv[]) {
     pa_volume_t v;
+    pa_cvolume cv;
 
     for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 256) {
 
@@ -13,6 +14,17 @@ int main(int argc, char *argv[]) {
 
         printf("Volume: %3i; percent: %i%%; decibel %0.2f; linear = %0.2f; volume(decibel): %3i; volume(linear): %3i\n",
                v, (v*100)/PA_VOLUME_NORM, dB, f, pa_sw_volume_from_dB(dB), pa_sw_volume_from_linear(f));
+    }
+
+    for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 256) {
+        char s[PA_CVOLUME_SNPRINT_MAX], t[PA_CVOLUME_SNPRINT_DB_MAX];
+
+        pa_cvolume_set(&cv, 2, v);
+
+        printf("Volume: %3i [%s] [%s]\n",
+               v,
+               pa_cvolume_snprint(s, sizeof(s), &cv),
+               pa_cvolume_snprint_dB(t, sizeof(t), &cv));
 
     }