[pulseaudio-commits] r2041 - in /trunk/src: ./ Makefile.am pulsecore/sample-util.c pulsecore/sample-util.h tests/mix-test.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Fri Nov 9 09:11:47 PST 2007


Author: lennart
Date: Fri Nov  9 18:11:45 2007
New Revision: 2041

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=2041&root=pulseaudio&view=rev
Log:
optimize mixing code a bit. Add mixers for S32LE, S32BE, ULAW, ALAW and FLOAT32BE. Add volume adjusters for FLOAT32BE, ALAW, ULAW.

Added:
    trunk/src/tests/mix-test.c
Modified:
    trunk/src/   (props changed)
    trunk/src/Makefile.am
    trunk/src/pulsecore/sample-util.c
    trunk/src/pulsecore/sample-util.h

Propchange: trunk/src/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Fri Nov  9 18:11:45 2007
@@ -1,3 +1,4 @@
+mix-test
 smoother-test
 resampler-test
 sig2str-test

Modified: trunk/src/Makefile.am
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/Makefile.am?rev=2041&root=pulseaudio&r1=2040&r2=2041&view=diff
==============================================================================
--- trunk/src/Makefile.am (original)
+++ trunk/src/Makefile.am Fri Nov  9 18:11:45 2007
@@ -239,7 +239,8 @@
 		rtpoll-test \
 		sig2str-test \
 		resampler-test \
-		smoother-test
+		smoother-test \
+		mix-test
 
 if HAVE_SIGXCPU
 noinst_PROGRAMS += \
@@ -388,6 +389,11 @@
 resampler_test_LDADD = $(AM_LDADD) libpulsecore.la
 resampler_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS)
 resampler_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS)
+
+mix_test_SOURCES = tests/mix-test.c
+mix_test_LDADD = $(AM_LDADD) libpulsecore.la
+mix_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS)
+mix_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS)
 
 smoother_test_SOURCES = tests/smoother-test.c
 smoother_test_LDADD = $(AM_LDADD) libpulsecore.la

Modified: trunk/src/pulsecore/sample-util.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/sample-util.c?rev=2041&root=pulseaudio&r1=2040&r2=2041&view=diff
==============================================================================
--- trunk/src/pulsecore/sample-util.c (original)
+++ trunk/src/pulsecore/sample-util.c Fri Nov  9 18:11:45 2007
@@ -35,6 +35,7 @@
 
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/g711.h>
 
 #include "sample-util.h"
 #include "endianmacros.h"
@@ -119,6 +120,58 @@
     memset(p, c, length);
 }
 
+static void calc_linear_integer_stream_volumes(pa_mix_info streams[], unsigned nstreams, const pa_sample_spec *spec) {
+    unsigned k;
+
+    pa_assert(streams);
+    pa_assert(spec);
+
+    for (k = 0; k < nstreams; k++) {
+        unsigned channel;
+
+        for (channel = 0; channel < spec->channels; channel++) {
+            pa_mix_info *m = streams + k;
+            m->linear[channel].i = (int32_t) (pa_sw_volume_to_linear(m->volume.values[channel]) * 0x10000);
+        }
+    }
+}
+
+static void calc_linear_integer_volume(int32_t linear[], const pa_cvolume *volume) {
+    unsigned channel;
+
+    pa_assert(linear);
+    pa_assert(volume);
+
+    for (channel = 0; channel < volume->channels; channel++)
+        linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
+}
+
+static void calc_linear_float_stream_volumes(pa_mix_info streams[], unsigned nstreams, const pa_sample_spec *spec) {
+    unsigned k;
+
+    pa_assert(streams);
+    pa_assert(spec);
+
+    for (k = 0; k < nstreams; k++) {
+        unsigned channel;
+
+        for (channel = 0; channel < spec->channels; channel++) {
+            pa_mix_info *m = streams + k;
+            m->linear[channel].f = pa_sw_volume_to_linear(m->volume.values[channel]);
+        }
+    }
+}
+
+static void calc_linear_float_volume(float linear[], const pa_cvolume *volume) {
+    unsigned channel;
+
+    pa_assert(linear);
+    pa_assert(volume);
+
+    for (channel = 0; channel < volume->channels; channel++)
+        linear[channel] = pa_sw_volume_to_linear(volume->values[channel]);
+}
+
 size_t pa_mix(
         pa_mix_info streams[],
         unsigned nstreams,
@@ -126,11 +179,11 @@
         size_t length,
         const pa_sample_spec *spec,
         const pa_cvolume *volume,
-        int mute) {
+        pa_bool_t mute) {
 
     pa_cvolume full_volume;
+    unsigned k;
     size_t d = 0;
-    unsigned k;
 
     pa_assert(streams);
     pa_assert(data);
@@ -141,50 +194,49 @@
         volume = pa_cvolume_reset(&full_volume, spec->channels);
 
     for (k = 0; k < nstreams; k++)
-        streams[k].internal = pa_memblock_acquire(streams[k].chunk.memblock);
+        streams[k].ptr = (uint8_t*) pa_memblock_acquire(streams[k].chunk.memblock) + streams[k].chunk.index;
 
     switch (spec->format) {
+
         case PA_SAMPLE_S16NE:{
             unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
 
             for (d = 0;; d += sizeof(int16_t)) {
                 int32_t sum = 0;
-
-                if (d >= length)
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
                     goto finish;
 
-                if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
-                    unsigned i;
-
-                    for (i = 0; i < nstreams; i++) {
-                        int32_t v;
-                        pa_volume_t cvolume = streams[i].volume.values[channel];
-
-                        if (d >= streams[i].chunk.length)
-                            goto finish;
-
-                        if (cvolume == PA_VOLUME_MUTED)
-                            v = 0;
-                        else {
-                            v = *((int16_t*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d));
-
-                            if (cvolume != PA_VOLUME_NORM)
-                                v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
-                        }
-
-                        sum += v;
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int32_t v, cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = *((int16_t*) m->ptr);
+                        v = (v * cv) / 0x10000;
                     }
 
-                    if (volume->values[channel] != PA_VOLUME_NORM)
-                        sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
-
-                    sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + sizeof(int16_t);
                 }
 
+                sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
+                sum = (sum * linear[channel]) / 0x10000;
                 *((int16_t*) data) = (int16_t) sum;
+
                 data = (uint8_t*) data + sizeof(int16_t);
 
-                if (++channel >= spec->channels)
+                if (PA_UNLIKELY(++channel >= spec->channels))
                     channel = 0;
             }
 
@@ -193,45 +245,135 @@
 
         case PA_SAMPLE_S16RE:{
             unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
 
             for (d = 0;; d += sizeof(int16_t)) {
                 int32_t sum = 0;
-
-                if (d >= length)
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
                     goto finish;
 
-                if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
-                    unsigned i;
-
-                    for (i = 0; i < nstreams; i++) {
-                        int32_t v;
-                        pa_volume_t cvolume = streams[i].volume.values[channel];
-
-                        if (d >= streams[i].chunk.length)
-                            goto finish;
-
-                        if (cvolume == PA_VOLUME_MUTED)
-                            v = 0;
-                        else {
-                            v = PA_INT16_SWAP(*((int16_t*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d)));
-
-                            if (cvolume != PA_VOLUME_NORM)
-                                v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
-                        }
-
-                        sum += v;
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int32_t v, cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = PA_INT16_SWAP(*((int16_t*) m->ptr));
+                        v = (v * cv) / 0x10000;
                     }
 
-                    if (volume->values[channel] != PA_VOLUME_NORM)
-                        sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
-
-                    sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + sizeof(int16_t);
                 }
 
+                sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
+                sum = (sum * linear[channel]) / 0x10000;
                 *((int16_t*) data) = PA_INT16_SWAP((int16_t) sum);
+
                 data = (uint8_t*) data + sizeof(int16_t);
 
-                if (++channel >= spec->channels)
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+
+            break;
+        }
+
+        case PA_SAMPLE_S32NE:{
+            unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
+
+            for (d = 0;; d += sizeof(int32_t)) {
+                int64_t sum = 0;
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
+                    goto finish;
+
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int64_t v;
+                    int32_t cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = *((int32_t*) m->ptr);
+                        v = (v * cv) / 0x10000;
+                    }
+
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
+                }
+
+                sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL);
+                sum = (sum * linear[channel]) / 0x10000;
+                *((int32_t*) data) = (int32_t) sum;
+
+                data = (uint8_t*) data + sizeof(int32_t);
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+
+            break;
+        }
+
+        case PA_SAMPLE_S32RE:{
+            unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
+
+            for (d = 0;; d += sizeof(int32_t)) {
+                int64_t sum = 0;
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
+                    goto finish;
+
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int64_t v;
+                    int32_t cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = PA_INT32_SWAP(*((int32_t*) m->ptr));
+                        v = (v * cv) / 0x10000;
+                    }
+
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + sizeof(int32_t);
+                }
+
+                sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL);
+                sum = (sum * linear[channel]) / 0x10000;
+                *((int32_t*) data) = PA_INT32_SWAP((int32_t) sum);
+
+                data = (uint8_t*) data + sizeof(int32_t);
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
                     channel = 0;
             }
 
@@ -240,45 +382,133 @@
 
         case PA_SAMPLE_U8: {
             unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
 
             for (d = 0;; d ++) {
                 int32_t sum = 0;
-
-                if (d >= length)
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
                     goto finish;
 
-                if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
-                    unsigned i;
-
-                    for (i = 0; i < nstreams; i++) {
-                        int32_t v;
-                        pa_volume_t cvolume = streams[i].volume.values[channel];
-
-                        if (d >= streams[i].chunk.length)
-                            goto finish;
-
-                        if (cvolume == PA_VOLUME_MUTED)
-                            v = 0;
-                        else {
-                            v = (int32_t) *((uint8_t*) streams[i].internal + streams[i].chunk.index + d) - 0x80;
-
-                            if (cvolume != PA_VOLUME_NORM)
-                                v = (int32_t) (v * pa_sw_volume_to_linear(cvolume));
-                        }
-
-                        sum += v;
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int32_t v, cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = (int32_t) *((uint8_t*) m->ptr) - 0x80;
+                        v = (v * cv) / 0x10000;
                     }
 
-                    if (volume->values[channel] != PA_VOLUME_NORM)
-                        sum = (int32_t) (sum * pa_sw_volume_to_linear(volume->values[channel]));
-
-                    sum = PA_CLAMP_UNLIKELY(sum, -0x80, 0x7F);
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + 1;
                 }
 
+                sum = (sum * linear[channel]) / 0x10000;
+                sum = PA_CLAMP_UNLIKELY(sum, -0x80, 0x7F);
                 *((uint8_t*) data) = (uint8_t) (sum + 0x80);
+
                 data = (uint8_t*) data + 1;
 
-                if (++channel >= spec->channels)
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+
+            break;
+        }
+
+        case PA_SAMPLE_ULAW: {
+            unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
+
+            for (d = 0;; d ++) {
+                int32_t sum = 0;
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
+                    goto finish;
+
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int32_t v, cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = (int32_t) st_ulaw2linear16(*((uint8_t*) m->ptr));
+                        v = (v * cv) / 0x10000;
+                    }
+
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + 1;
+                }
+
+                sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
+                sum = (sum * linear[channel]) / 0x10000;
+                *((uint8_t*) data) = (uint8_t) st_14linear2ulaw(sum >> 2);
+
+                data = (uint8_t*) data + 1;
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+
+            break;
+        }
+
+        case PA_SAMPLE_ALAW: {
+            unsigned channel = 0;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_stream_volumes(streams, nstreams, spec);
+            calc_linear_integer_volume(linear, volume);
+
+            for (d = 0;; d ++) {
+                int32_t sum = 0;
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
+                    goto finish;
+
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    int32_t v, cv = m->linear[channel].i;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = (int32_t) st_alaw2linear16(*((uint8_t*) m->ptr));
+                        v = (v * cv) / 0x10000;
+                    }
+
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + 1;
+                }
+
+                sum = PA_CLAMP_UNLIKELY(sum, -0x8000, 0x7FFF);
+                sum = (sum * linear[channel]) / 0x10000;
+                *((uint8_t*) data) = (uint8_t) st_13linear2alaw(sum >> 3);
+
+                data = (uint8_t*) data + 1;
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
                     channel = 0;
             }
 
@@ -287,43 +517,88 @@
 
         case PA_SAMPLE_FLOAT32NE: {
             unsigned channel = 0;
+            float linear[PA_CHANNELS_MAX];
+
+            calc_linear_float_stream_volumes(streams, nstreams, spec);
+            calc_linear_float_volume(linear, volume);
 
             for (d = 0;; d += sizeof(float)) {
                 float sum = 0;
-
-                if (d >= length)
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
                     goto finish;
 
-                if (!mute && volume->values[channel] != PA_VOLUME_MUTED) {
-                    unsigned i;
-
-                    for (i = 0; i < nstreams; i++) {
-                        float v;
-                        pa_volume_t cvolume = streams[i].volume.values[channel];
-
-                        if (d >= streams[i].chunk.length)
-                            goto finish;
-
-                        if (cvolume == PA_VOLUME_MUTED)
-                            v = 0;
-                        else {
-                            v = *((float*) ((uint8_t*) streams[i].internal + streams[i].chunk.index + d));
-
-                            if (cvolume != PA_VOLUME_NORM)
-                                v *= pa_sw_volume_to_linear(cvolume);
-                        }
-
-                        sum += v;
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    float v, cv = m->linear[channel].f;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        v = *((float*) m->ptr);
+                        v *= cv;
                     }
 
-                    if (volume->values[channel] != PA_VOLUME_NORM)
-                        sum *= pa_sw_volume_to_linear(volume->values[channel]);
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + sizeof(float);
                 }
 
+                sum *= linear[channel];
                 *((float*) data) = sum;
+
                 data = (uint8_t*) data + sizeof(float);
 
-                if (++channel >= spec->channels)
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+
+            break;
+        }
+
+        case PA_SAMPLE_FLOAT32RE: {
+            unsigned channel = 0;
+            float linear[PA_CHANNELS_MAX];
+
+            calc_linear_float_stream_volumes(streams, nstreams, spec);
+            calc_linear_float_volume(linear, volume);
+
+            for (d = 0;; d += sizeof(float)) {
+                float sum = 0;
+                unsigned i;
+
+                if (PA_UNLIKELY(d >= length))
+                    goto finish;
+
+                for (i = 0; i < nstreams; i++) {
+                    pa_mix_info *m = streams + i;
+                    float v, cv = m->linear[channel].f;
+
+                    if (PA_UNLIKELY(d >= m->chunk.length))
+                        goto finish;
+
+                    if (PA_UNLIKELY(cv <= 0) || PA_UNLIKELY(!!mute) || PA_UNLIKELY(linear[channel] <= 0))
+                        v = 0;
+                    else {
+                        uint32_t z = *(uint32_t*) m->ptr;
+                        z = PA_UINT32_SWAP(z);
+                        v = *((float*) &z);
+                        v *= cv;
+                    }
+
+                    sum += v;
+                    m->ptr = (uint8_t*) m->ptr + sizeof(float);
+                }
+
+                sum *= linear[channel];
+                *((uint32_t*) data) = PA_UINT32_SWAP(*(uint32_t*) &sum);
+
+                data = (uint8_t*) data + sizeof(float);
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
                     channel = 0;
             }
 
@@ -332,7 +607,7 @@
 
         default:
             pa_log_error("ERROR: Unable to mix audio data of format %s.", pa_sample_format_to_string(spec->format));
-            abort();
+            pa_assert_not_reached();
     }
 
 finish:
@@ -364,7 +639,7 @@
         return;
     }
 
-    ptr = pa_memblock_acquire(c->memblock);
+    ptr = (uint8_t*) pa_memblock_acquire(c->memblock) + c->index;
 
     switch (spec->format) {
 
@@ -374,10 +649,9 @@
             unsigned channel;
             int32_t linear[PA_CHANNELS_MAX];
 
-            for (channel = 0; channel < spec->channels; channel++)
-                linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
-
-            for (channel = 0, d = (int16_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length/sizeof(int16_t); n > 0; d++, n--) {
                 int32_t t;
 
                 t = (int32_t)(*d);
@@ -397,10 +671,9 @@
             unsigned channel;
             int32_t linear[PA_CHANNELS_MAX];
 
-            for (channel = 0; channel < spec->channels; channel++)
-                linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
-
-            for (channel = 0, d = (int16_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length/sizeof(int16_t); n > 0; d++, n--) {
                 int32_t t;
 
                 t = (int32_t)(PA_INT16_SWAP(*d));
@@ -421,10 +694,9 @@
             unsigned channel;
             int32_t linear[PA_CHANNELS_MAX];
 
-            for (channel = 0; channel < spec->channels; channel++)
-                linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
-
-            for (channel = 0, d = (int32_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int32_t); n > 0; d++, n--) {
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length/sizeof(int32_t); n > 0; d++, n--) {
                 int64_t t;
 
                 t = (int64_t)(*d);
@@ -444,10 +716,9 @@
             unsigned channel;
             int32_t linear[PA_CHANNELS_MAX];
 
-            for (channel = 0; channel < spec->channels; channel++)
-                linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
-
-            for (channel = 0, d = (int32_t*) ((uint8_t*) ptr + c->index), n = c->length/sizeof(int32_t); n > 0; d++, n--) {
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length/sizeof(int32_t); n > 0; d++, n--) {
                 int64_t t;
 
                 t = (int64_t)(PA_INT32_SWAP(*d));
@@ -468,16 +739,59 @@
             unsigned channel;
             int32_t linear[PA_CHANNELS_MAX];
 
-            for (channel = 0; channel < spec->channels; channel++)
-                linear[channel] = (int32_t) (pa_sw_volume_to_linear(volume->values[channel]) * 0x10000);
-
-            for (channel = 0, d = (uint8_t*) ptr + c->index, n = c->length; n > 0; d++, n--) {
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length; n > 0; d++, n--) {
                 int32_t t;
 
                 t = (int32_t) *d - 0x80;
                 t = (t * linear[channel]) / 0x10000;
                 t = PA_CLAMP_UNLIKELY(t, -0x80, 0x7F);
                 *d = (uint8_t) (t + 0x80);
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+            break;
+        }
+
+        case PA_SAMPLE_ULAW: {
+            uint8_t *d;
+            size_t n;
+            unsigned channel;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length; n > 0; d++, n--) {
+                int32_t t;
+
+                t = (int32_t) st_ulaw2linear16(*d);
+                t = (t * linear[channel]) / 0x10000;
+                t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+                *d = (uint8_t) st_14linear2ulaw(t >> 2);
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+            break;
+        }
+
+        case PA_SAMPLE_ALAW: {
+            uint8_t *d;
+            size_t n;
+            unsigned channel;
+            int32_t linear[PA_CHANNELS_MAX];
+
+            calc_linear_integer_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length; n > 0; d++, n--) {
+                int32_t t;
+
+                t = (int32_t) st_alaw2linear16(*d);
+                t = (t * linear[channel]) / 0x10000;
+                t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+                *d = (uint8_t) st_13linear2alaw(t >> 3);
 
                 if (PA_UNLIKELY(++channel >= spec->channels))
                     channel = 0;
@@ -491,14 +805,14 @@
             unsigned n;
             unsigned channel;
 
-            d = (float*) ((uint8_t*) ptr + c->index);
+            d = ptr;
             skip = spec->channels * sizeof(float);
             n = c->length/sizeof(float)/spec->channels;
 
-            for (channel = 0; channel < spec->channels ; channel ++) {
+            for (channel = 0; channel < spec->channels; channel ++) {
                 float v, *t;
 
-                if (volume->values[channel] == PA_VOLUME_NORM)
+                if (PA_UNLIKELY(volume->values[channel] == PA_VOLUME_NORM))
                     continue;
 
                 v = (float) pa_sw_volume_to_linear(volume->values[channel]);
@@ -507,6 +821,32 @@
             }
             break;
         }
+
+        case PA_SAMPLE_FLOAT32RE: {
+            uint32_t *d;
+            size_t n;
+            unsigned channel;
+            float linear[PA_CHANNELS_MAX];
+
+            calc_linear_float_volume(linear, volume);
+
+            for (channel = 0, d = ptr, n = c->length/sizeof(float); n > 0; d++, n--) {
+                float t;
+                uint32_t z;
+
+                z = PA_UINT32_SWAP(*d);
+                t = *(float*) &z;
+                t *= linear[channel];
+                z = *(uint32_t*) &t;
+                *d = PA_UINT32_SWAP(z);
+
+                if (PA_UNLIKELY(++channel >= spec->channels))
+                    channel = 0;
+            }
+
+            break;
+        }
+
 
         default:
             pa_log_warn(" Unable to change volume of format %s.", pa_sample_format_to_string(spec->format));

Modified: trunk/src/pulsecore/sample-util.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/sample-util.h?rev=2041&root=pulseaudio&r1=2040&r2=2041&view=diff
==============================================================================
--- trunk/src/pulsecore/sample-util.h (original)
+++ trunk/src/pulsecore/sample-util.h Fri Nov  9 18:11:45 2007
@@ -39,7 +39,14 @@
     pa_memchunk chunk;
     pa_cvolume volume;
     void *userdata;
-    void *internal; /* Used internally by pa_mix(), should not be initialised when calling pa_mix() */
+
+    /* The following fields are used internally by pa_mix(), should
+     * not be initialised by the caller of pa_mix(). */
+    void *ptr;
+    union {
+        int32_t i;
+        float f;
+    } linear[PA_CHANNELS_MAX];
 } pa_mix_info;
 
 size_t pa_mix(
@@ -49,7 +56,7 @@
     size_t length,
     const pa_sample_spec *spec,
     const pa_cvolume *volume,
-    int mute);
+    pa_bool_t mute);
 
 void pa_volume_memchunk(
     pa_memchunk*c,

Added: trunk/src/tests/mix-test.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/tests/mix-test.c?rev=2041&root=pulseaudio&view=auto
==============================================================================
--- trunk/src/tests/mix-test.c (added)
+++ trunk/src/tests/mix-test.c Fri Nov  9 18:11:45 2007
@@ -1,0 +1,261 @@
+/* $Id: resampler-test.c 2037 2007-11-09 02:45:07Z lennart $ */
+
+/***
+  This file is part of PulseAudio.
+
+  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 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.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+
+#include <pulse/sample.h>
+#include <pulse/volume.h>
+
+#include <pulsecore/resampler.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/endianmacros.h>
+#include <pulsecore/memblock.h>
+#include <pulsecore/sample-util.h>
+
+#include <liboil/liboil.h>
+
+static float swap_float(float a) {
+    uint32_t *b = (uint32_t*) &a;
+    *b = PA_UINT32_SWAP(*b);
+    return a;
+}
+
+static void dump_block(const pa_sample_spec *ss, const pa_memchunk *chunk) {
+    void *d;
+    unsigned i;
+
+    d = pa_memblock_acquire(chunk->memblock);
+
+    switch (ss->format) {
+
+        case PA_SAMPLE_U8:
+        case PA_SAMPLE_ULAW:
+        case PA_SAMPLE_ALAW: {
+            uint8_t *u = d;
+
+            for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
+                printf("0x%02x ", *(u++));
+
+            break;
+        }
+
+        case PA_SAMPLE_S16NE:
+        case PA_SAMPLE_S16RE: {
+            uint16_t *u = d;
+
+            for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
+                printf("0x%04x ", *(u++));
+
+            break;
+        }
+
+        case PA_SAMPLE_S32NE:
+        case PA_SAMPLE_S32RE: {
+            uint32_t *u = d;
+
+            for (i = 0; i < chunk->length / pa_frame_size(ss); i++)
+                printf("0x%08x ", *(u++));
+
+            break;
+        }
+
+        case PA_SAMPLE_FLOAT32NE:
+        case PA_SAMPLE_FLOAT32RE: {
+            float *u = d;
+
+            for (i = 0; i < chunk->length / pa_frame_size(ss); i++) {
+                printf("%1.5f ",  ss->format == PA_SAMPLE_FLOAT32NE ? *u : swap_float(*u));
+                u++;
+            }
+
+            break;
+        }
+
+        default:
+            pa_assert_not_reached();
+    }
+
+    printf("\n");
+
+    pa_memblock_release(chunk->memblock);
+}
+
+static pa_memblock* generate_block(pa_mempool *pool, const pa_sample_spec *ss) {
+    pa_memblock *r;
+    void *d;
+    unsigned i;
+
+    pa_assert_se(r = pa_memblock_new(pool, pa_frame_size(ss) * 10));
+    d = pa_memblock_acquire(r);
+
+    switch (ss->format) {
+
+        case PA_SAMPLE_U8:
+        case PA_SAMPLE_ULAW:
+        case PA_SAMPLE_ALAW: {
+            uint8_t *u = d;
+
+            u[0] = 0x00;
+            u[1] = 0xFF;
+            u[2] = 0x7F;
+            u[3] = 0x80;
+            u[4] = 0x9f;
+            u[5] = 0x3f;
+            u[6] = 0x1;
+            u[7] = 0xF0;
+            u[8] = 0x20;
+            u[9] = 0x21;
+            break;
+        }
+
+        case PA_SAMPLE_S16NE:
+        case PA_SAMPLE_S16RE: {
+            uint16_t *u = d;
+
+            u[0] = 0x0000;
+            u[1] = 0xFFFF;
+            u[2] = 0x7FFF;
+            u[3] = 0x8000;
+            u[4] = 0x9fff;
+            u[5] = 0x3fff;
+            u[6] = 0x1;
+            u[7] = 0xF000;
+            u[8] = 0x20;
+            u[9] = 0x21;
+            break;
+        }
+
+        case PA_SAMPLE_S32NE:
+        case PA_SAMPLE_S32RE: {
+            uint32_t *u = d;
+
+            u[0] = 0x00000001;
+            u[1] = 0xFFFF0002;
+            u[2] = 0x7FFF0003;
+            u[3] = 0x80000004;
+            u[4] = 0x9fff0005;
+            u[5] = 0x3fff0006;
+            u[6] =    0x10007;
+            u[7] = 0xF0000008;
+            u[8] =   0x200009;
+            u[9] =   0x21000A;
+            break;
+        }
+
+        case PA_SAMPLE_FLOAT32NE:
+        case PA_SAMPLE_FLOAT32RE: {
+            float *u = d;
+
+            u[0] = 0.0;
+            u[1] = -1.0;
+            u[2] = 1.0;
+            u[3] = 4711;
+            u[4] = 0.222;
+            u[5] = 0.33;
+            u[6] = -.3;
+            u[7] = 99;
+            u[8] = -0.555;
+            u[9] = -.123;
+
+            if (ss->format == PA_SAMPLE_FLOAT32RE)
+                for (i = 0; i < 10; i++)
+                    u[i] = swap_float(u[i]);
+
+            break;
+        }
+
+        default:
+            pa_assert_not_reached();
+    }
+
+    pa_memblock_release(r);
+
+    return r;
+}
+
+int main(int argc, char *argv[]) {
+    pa_mempool *pool;
+    pa_sample_spec a;
+    pa_cvolume v;
+
+    oil_init();
+    pa_log_set_maximal_level(PA_LOG_DEBUG);
+
+    pa_assert_se(pool = pa_mempool_new(FALSE));
+
+    a.channels = 1;
+    a.rate = 44100;
+
+    v.channels = a.channels;
+    v.values[0] = pa_sw_volume_from_linear(0.9);
+
+    for (a.format = 0; a.format < PA_SAMPLE_MAX; a.format ++) {
+        pa_memchunk i, j, k;
+        pa_mix_info m[2];
+        void *ptr;
+
+        printf("=== mixing: %s\n", pa_sample_format_to_string(a.format));
+
+        /* Generate block */
+        i.memblock = generate_block(pool, &a);
+        i.length = pa_memblock_get_length(i.memblock);
+        i.index = 0;
+
+        /* Make a copy */
+        j = i;
+        pa_memblock_ref(j.memblock);
+        pa_memchunk_make_writable(&j, 0);
+
+        /* Adjust volume of the copy */
+        pa_volume_memchunk(&j, &a, &v);
+
+        m[0].chunk = i;
+        m[0].volume.values[0] = PA_VOLUME_NORM;
+        m[0].volume.channels = a.channels;
+        m[1].chunk = j;
+        m[1].volume.values[0] = PA_VOLUME_NORM;
+        m[1].volume.channels = a.channels;
+
+        k.memblock = pa_memblock_new(pool, i.length);
+        k.length = i.length;
+        k.index = 0;
+
+        ptr = (uint8_t*) pa_memblock_acquire(k.memblock) + k.index;
+        pa_mix(m, 2, ptr, k.length, &a, NULL, FALSE);
+        pa_memblock_release(k.memblock);
+
+        dump_block(&a, &i);
+        dump_block(&a, &j);
+        dump_block(&a, &k);
+
+        pa_memblock_unref(i.memblock);
+        pa_memblock_unref(j.memblock);
+        pa_memblock_unref(k.memblock);
+    }
+
+    pa_mempool_free(pool);
+
+    return 0;
+}




More information about the pulseaudio-commits mailing list