[pulseaudio-discuss] [PATCH RFCv3 43/51] core: Add volume-util.h

Peter Meerwald pmeerw at pmeerw.net
Tue Nov 4 15:26:38 PST 2014


implement inlineable functions PA_CVOLUME_VALID(),
PA_CVOLUME_CHANNELS_EQUALS_TO(), PA_CVOLUME_IS_MUTED(),
PA_CVOLUME_IS_NORM() that assume data is valid

Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
 src/pulsecore/volume-util.h | 92 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 src/pulsecore/volume-util.h

diff --git a/src/pulsecore/volume-util.h b/src/pulsecore/volume-util.h
new file mode 100644
index 0000000..d96c472
--- /dev/null
+++ b/src/pulsecore/volume-util.h
@@ -0,0 +1,92 @@
+#ifndef foovolumeutilhfoo
+#define foovolumeutilhfoo
+
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2014 Peter Meerwald <pmeerw at pmeerw.net>
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#include <pulse/gccmacro.h>
+#include <pulse/sample.h>
+#include <pulse/volume.h>
+
+static inline int PA_CVOLUME_VALID(const pa_cvolume *a) {
+    unsigned c;
+
+    pa_assert_fp(a);
+    pa_assert_fp(pa_channels_valid(a->channels));
+
+    for (c = 0; c < a->channels; c++)
+        if (!PA_VOLUME_IS_VALID(a->values[c]))
+            return 0;
+
+    return 1;
+}
+
+static inline int PA_CVOLUME_CHANNELS_EQUALS_TO(const pa_cvolume *a, pa_volume_t v) {
+    unsigned c;
+    pa_assert_fp(a);
+
+    pa_assert_fp(PA_CVOLUME_VALID(a));
+    pa_assert_fp(PA_VOLUME_IS_VALID(v));
+
+    for (c = 0; c < a->channels; c++)
+        if (a->values[c] != v)
+            return 0;
+
+    return 1;
+}
+
+static inline int PA_CVOLUME_IS_MUTED(const pa_cvolume *a) {
+    return PA_CVOLUME_CHANNELS_EQUALS_TO(a, PA_VOLUME_MUTED);
+}
+
+/** Return 1 if the specified volume has all channels on normal level */
+static inline int PA_CVOLUME_IS_NORM(const pa_cvolume *a) {
+    return PA_CVOLUME_CHANNELS_EQUALS_TO(a, PA_VOLUME_NORM);
+}
+
+static inline pa_volume_t PA_SW_VOLUME_MULTIPLY(pa_volume_t a, pa_volume_t b) {
+    pa_assert_fp(PA_VOLUME_IS_VALID(a));
+    pa_assert_fp(PA_VOLUME_IS_VALID(b));
+
+    /* cbrt((a/PA_VOLUME_NORM)^3*(b/PA_VOLUME_NORM)^3)*PA_VOLUME_NORM = a*b/PA_VOLUME_NORM */
+
+    return (pa_volume_t) PA_CLAMP_VOLUME((((uint64_t) a * (uint64_t) b + (uint64_t) PA_VOLUME_NORM / 2ULL) / (uint64_t) PA_VOLUME_NORM));
+}
+
+static inline pa_cvolume *PA_SW_CVOLUME_MULTIPLY(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
+    unsigned i;
+
+    pa_assert(dest);
+    pa_assert(a);
+    pa_assert(b);
+
+    pa_assert_fp(pa_cvolume_valid(a));
+    pa_assert_fp(pa_cvolume_valid(b));
+
+    for (i = 0; i < a->channels && i < b->channels; i++)
+        dest->values[i] = PA_SW_VOLUME_MULTIPLY(a->values[i], b->values[i]);
+
+    dest->channels = (uint8_t) i;
+
+    return dest;
+}
+
+#endif
-- 
1.9.1



More information about the pulseaudio-discuss mailing list