From 2ce7d38005a82c56cbc1ad2af70afabe9def9e0d Mon Sep 17 00:00:00 2001
From: David Henningsson <david.henningsson@canonical.com>
Date: Thu, 9 Feb 2012 15:59:20 +0100
Subject: [PATCH] sample-util: Fix "Darth Vader" panning bug

For muted channels, we forgot to increment a pointer, so if one
channel was muted but not the other, sound became distorted in a
Darth Vader like way. To test the difference, start two input
streams and pan one of them hard left (or right).

And hey, if you didn't think it sounded like Darth Vader, it's
your imagination that's broken, not mine! ;-)

BugLink: https://bugs.launchpad.net/bugs/928757
Signed-off-by: David Henningsson <david.henningsson@canonical.com>
---
 src/pulsecore/sample-util.c |  173 ++++++++++++++++++++------------------------
 1 file changed, 80 insertions(+), 93 deletions(-)

--- a/src/pulsecore/sample-util.c
+++ b/src/pulsecore/sample-util.c
@@ -224,22 +224,21 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     int32_t v, lo, hi, cv = m->linear[channel].i;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    /* Multiplying the 32bit volume factor with the
-                     * 16bit sample might result in an 48bit value. We
-                     * want to do without 64 bit integers and hence do
-                     * the multiplication independantly for the HI and
-                     * LO part of the volume. */
-
-                    hi = cv >> 16;
-                    lo = cv & 0xFFFF;
-
-                    v = *((int16_t*) m->ptr);
-                    v = ((v * lo) >> 16) + (v * hi);
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        /* Multiplying the 32bit volume factor with the
+                         * 16bit sample might result in an 48bit value. We
+                         * want to do without 64 bit integers and hence do
+                         * the multiplication independently for the HI and
+                         * LO part of the volume. */
+
+                        hi = cv >> 16;
+                        lo = cv & 0xFFFF;
+
+                        v = *((int16_t*) m->ptr);
+                        v = ((v * lo) >> 16) + (v * hi);
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(int16_t);
                 }
 
@@ -268,16 +267,15 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     int32_t v, lo, hi, cv = m->linear[channel].i;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    hi = cv >> 16;
-                    lo = cv & 0xFFFF;
+                    if (PA_LIKELY(cv > 0)) {
 
-                    v = PA_INT16_SWAP(*((int16_t*) m->ptr));
-                    v = ((v * lo) >> 16) + (v * hi);
-                    sum += v;
+                        hi = cv >> 16;
+                        lo = cv & 0xFFFF;
 
+                        v = PA_INT16_SWAP(*((int16_t*) m->ptr));
+                        v = ((v * lo) >> 16) + (v * hi);
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(int16_t);
                 }
 
@@ -307,13 +305,12 @@ size_t pa_mix(
                     int32_t cv = m->linear[channel].i;
                     int64_t v;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = *((int32_t*) m->ptr);
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = *((int32_t*) m->ptr);
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
                 }
 
@@ -343,13 +340,12 @@ size_t pa_mix(
                     int32_t cv = m->linear[channel].i;
                     int64_t v;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = PA_INT32_SWAP(*((int32_t*) m->ptr));
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = PA_INT32_SWAP(*((int32_t*) m->ptr));
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
                 }
 
@@ -379,13 +375,12 @@ size_t pa_mix(
                     int32_t cv = m->linear[channel].i;
                     int64_t v;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = (int32_t) (PA_READ24NE(m->ptr) << 8);
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = (int32_t) (PA_READ24NE(m->ptr) << 8);
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + 3;
                 }
 
@@ -415,13 +410,12 @@ size_t pa_mix(
                     int32_t cv = m->linear[channel].i;
                     int64_t v;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = (int32_t) (PA_READ24RE(m->ptr) << 8);
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = (int32_t) (PA_READ24RE(m->ptr) << 8);
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + 3;
                 }
 
@@ -451,13 +445,12 @@ size_t pa_mix(
                     int32_t cv = m->linear[channel].i;
                     int64_t v;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = (int32_t) (*((uint32_t*)m->ptr) << 8);
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = (int32_t) (*((uint32_t*)m->ptr) << 8);
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
                 }
 
@@ -487,13 +480,12 @@ size_t pa_mix(
                     int32_t cv = m->linear[channel].i;
                     int64_t v;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = (int32_t) (PA_UINT32_SWAP(*((uint32_t*) m->ptr)) << 8);
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = (int32_t) (PA_UINT32_SWAP(*((uint32_t*) m->ptr)) << 8);
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + 3;
                 }
 
@@ -522,13 +514,12 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     int32_t v, cv = m->linear[channel].i;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = (int32_t) *((uint8_t*) m->ptr) - 0x80;
-                    v = (v * cv) >> 16;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = (int32_t) *((uint8_t*) m->ptr) - 0x80;
+                        v = (v * cv) >> 16;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + 1;
                 }
 
@@ -557,16 +548,15 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     int32_t v, hi, lo, cv = m->linear[channel].i;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    hi = cv >> 16;
-                    lo = cv & 0xFFFF;
+                    if (PA_LIKELY(cv > 0)) {
 
-                    v = (int32_t) st_ulaw2linear16(*((uint8_t*) m->ptr));
-                    v = ((v * lo) >> 16) + (v * hi);
-                    sum += v;
+                        hi = cv >> 16;
+                        lo = cv & 0xFFFF;
 
+                        v = (int32_t) st_ulaw2linear16(*((uint8_t*) m->ptr));
+                        v = ((v * lo) >> 16) + (v * hi);
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + 1;
                 }
 
@@ -595,16 +585,15 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     int32_t v, hi, lo, cv = m->linear[channel].i;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
+                    if (PA_LIKELY(cv > 0)) {
 
-                    hi = cv >> 16;
-                    lo = cv & 0xFFFF;
-
-                    v = (int32_t) st_alaw2linear16(*((uint8_t*) m->ptr));
-                    v = ((v * lo) >> 16) + (v * hi);
-                    sum += v;
+                        hi = cv >> 16;
+                        lo = cv & 0xFFFF;
 
+                        v = (int32_t) st_alaw2linear16(*((uint8_t*) m->ptr));
+                        v = ((v * lo) >> 16) + (v * hi);
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + 1;
                 }
 
@@ -633,13 +622,12 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     float v, cv = m->linear[channel].f;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = *((float*) m->ptr);
-                    v *= cv;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = *((float*) m->ptr);
+                        v *= cv;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(float);
                 }
 
@@ -667,13 +655,12 @@ size_t pa_mix(
                     pa_mix_info *m = streams + i;
                     float v, cv = m->linear[channel].f;
 
-                    if (PA_UNLIKELY(cv <= 0))
-                        continue;
-
-                    v = PA_FLOAT32_SWAP(*(float*) m->ptr);
-                    v *= cv;
-                    sum += v;
+                    if (PA_LIKELY(cv > 0)) {
 
+                        v = PA_FLOAT32_SWAP(*(float*) m->ptr);
+                        v *= cv;
+                        sum += v;
+                    }
                     m->ptr = (uint8_t*) m->ptr + sizeof(float);
                 }
 
