]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/envelope.c
fix a number of warnings
[pulseaudio] / src / pulsecore / envelope.c
index e2691611477700cea13fa4c737a27b5fd8891b7c..75e189cb5915450d63894755ae0a57c3e308c851 100644 (file)
@@ -153,11 +153,11 @@ void pa_envelope_free(pa_envelope *e) {
 }
 
 static int32_t linear_interpolate_int(pa_usec_t x1, int32_t _y1, pa_usec_t x2, int32_t y2, pa_usec_t x3) {
-    return (int32_t) (_y1 + (x3 - x1) * (float) (y2 - _y1) / (float) (x2 - x1));
+    return (int32_t) ((double) _y1 + (double) (x3 - x1) * (double) (y2 - _y1) / (double) (x2 - x1));
 }
 
 static float linear_interpolate_float(pa_usec_t x1, float _y1, pa_usec_t x2, float y2, pa_usec_t x3) {
-    return _y1 + (x3 - x1) * (y2 - _y1) / (x2 - x1);
+    return _y1 + ((float) x3 - (float) x1) * (y2 - _y1) / ((float) x2 - (float) x1);
 }
 
 static int32_t item_get_int(pa_envelope_item *i, pa_usec_t x) {
@@ -177,7 +177,7 @@ static int32_t item_get_int(pa_envelope_item *i, pa_usec_t x) {
 
     pa_assert(i->j > 0);
     pa_assert(i->def->points_x[i->j-1] <= x);
-    pa_assert(x < i->def->points_x[i->j]);
+    pa_assert(x <= i->def->points_x[i->j]);
 
     return linear_interpolate_int(i->def->points_x[i->j-1], i->def->points_y.i[i->j-1],
                                   i->def->points_x[i->j], i->def->points_y.i[i->j], x);
@@ -200,7 +200,7 @@ static float item_get_float(pa_envelope_item *i, pa_usec_t x) {
 
     pa_assert(i->j > 0);
     pa_assert(i->def->points_x[i->j-1] <= x);
-    pa_assert(x < i->def->points_x[i->j]);
+    pa_assert(x <= i->def->points_x[i->j]);
 
     return linear_interpolate_float(i->def->points_x[i->j-1], i->def->points_y.f[i->j-1],
                                     i->def->points_x[i->j], i->def->points_y.f[i->j], x);
@@ -550,7 +550,7 @@ static int32_t linear_get_int(pa_envelope *e, int v) {
         e->points[v].cached_valid = TRUE;
     }
 
-    return e->points[v].y.i[e->points[v].n_current] + (e->points[v].cached_dy_i * (int32_t) (e->x - e->points[v].x[e->points[v].n_current])) / (int32_t) e->points[v].cached_dx;
+       return e->points[v].y.i[e->points[v].n_current] + ((float)e->points[v].cached_dy_i * (int32_t) (e->x - e->points[v].x[e->points[v].n_current])) / (int32_t) e->points[v].cached_dx;
 }
 
 static float linear_get_float(pa_envelope *e, int v) {
@@ -573,11 +573,11 @@ static float linear_get_float(pa_envelope *e, int v) {
     if (!e->points[v].cached_valid) {
         e->points[v].cached_dy_dx =
             (e->points[v].y.f[e->points[v].n_current+1] - e->points[v].y.f[e->points[v].n_current]) /
-            (e->points[v].x[e->points[v].n_current+1] - e->points[v].x[e->points[v].n_current]);
+            ((float) e->points[v].x[e->points[v].n_current+1] - (float) e->points[v].x[e->points[v].n_current]);
         e->points[v].cached_valid = TRUE;
     }
 
-    return e->points[v].y.f[e->points[v].n_current] + (e->x - e->points[v].x[e->points[v].n_current]) * e->points[v].cached_dy_dx;
+    return e->points[v].y.f[e->points[v].n_current] + (float) (e->x - e->points[v].x[e->points[v].n_current]) * e->points[v].cached_dy_dx;
 }
 
 void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
@@ -597,36 +597,60 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
         fs = pa_frame_size(&e->sample_spec);
         n = chunk->length;
 
+        pa_log_debug("Envelop position %zu applying factor %d=%f, sample spec is %d, chunk's length is %zu, fs is %zu\n", e->x, linear_get_int(e, v), ((float) linear_get_int(e,v))/0x10000, e->sample_spec.format, n, fs);
+
         switch (e->sample_spec.format) {
 
+            case PA_SAMPLE_U8: {
+                uint8_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
+                s = (uint8_t*) p + n;
 
-            case PA_SAMPLE_U8: {
-                uint8_t *t;
+                for (channel = 0, d = p; d < s; d++) {
+                    int32_t t, hi, lo;
 
-                for (t = p; n > 0; n -= fs) {
-                    int16_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                    hi = factor >> 16;
+                    lo = factor & 0xFFFF;
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++)
-                        *t = (uint8_t) (((factor * ((int16_t) *t - 0x80)) / 0x10000) + 0x80);
+                    t = (int32_t) *d - 0x80;
+                    t = ((t * lo) >> 16) + (t * hi);
+                    t = PA_CLAMP_UNLIKELY(t, -0x80, 0x7F);
+                    *d = (uint8_t) (t + 0x80);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
+                    }
                 }
 
                 break;
             }
 
             case PA_SAMPLE_ULAW: {
-                uint8_t *t;
+                uint8_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
-                for (t = p; n > 0; n -= fs) {
-                    int16_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                s = (uint8_t*) p + n;
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
-                        int16_t k = st_ulaw2linear16(*t);
-                        *t = (uint8_t) st_14linear2ulaw(((factor * k) / 0x10000) >> 2);
+                for (channel = 0, d = p; d < s; d++) {
+                    int32_t t, hi, lo;
+
+                    hi = factor >> 16;
+                    lo = factor & 0xFFFF;
+
+                    t = (int32_t) st_ulaw2linear16(*d);
+                    t = ((t * lo) >> 16) + (t * hi);
+                    t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+                    *d = (uint8_t) st_14linear2ulaw((int16_t) t >> 2);
+
+                     if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
                     }
                 }
 
@@ -634,16 +658,27 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
             }
 
             case PA_SAMPLE_ALAW: {
-                uint8_t *t;
+                uint8_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
-                for (t = p; n > 0; n -= fs) {
-                    int16_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                s = (uint8_t*) p + n;
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
-                        int16_t k = st_alaw2linear16(*t);
-                        *t = (uint8_t) st_13linear2alaw(((factor * k) / 0x10000) >> 3);
+                for (channel = 0, d = p; d < s; d++) {
+                    int32_t t, hi, lo;
+
+                    hi = factor >> 16;
+                    lo = factor & 0xFFFF;
+
+                    t = (int32_t) st_alaw2linear16(*d);
+                    t = ((t * lo) >> 16) + (t * hi);
+                    t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+                    *d = (uint8_t) st_13linear2alaw((int16_t) t >> 3);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
                     }
                 }
 
@@ -651,31 +686,55 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
             }
 
             case PA_SAMPLE_S16NE: {
-                int16_t *t;
+                int16_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
-                for (t = p; n > 0; n -= fs) {
-                    int32_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                s = (int16_t*) p + n/sizeof(int16_t);
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++)
-                        *t = (factor * *t) / 0x10000;
+                for (channel = 0, d = p; d < s; d++) {
+                    int32_t t, hi, lo;
+
+                    hi = factor >> 16;
+                    lo = factor & 0xFFFF;
+
+                    t = (int32_t)(*d);
+                    t = ((t * lo) >> 16) + (t * hi);
+                    t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+                    *d = (int16_t) t;
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
+                    }
                 }
 
                 break;
             }
 
             case PA_SAMPLE_S16RE: {
-                int16_t *t;
+                int16_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
-                for (t = p; n > 0; n -= fs) {
-                    int32_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                s = (int16_t*) p + n/sizeof(int16_t);
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
-                        int16_t r = (factor * PA_INT16_SWAP(*t)) / 0x10000;
-                        *t = PA_INT16_SWAP(r);
+                for (channel = 0, d = p; d < s; d++) {
+                    int32_t t, hi, lo;
+
+                    hi = factor >> 16;
+                    lo = factor & 0xFFFF;
+
+                    t = (int32_t) PA_INT16_SWAP(*d);
+                    t = ((t * lo) >> 16) + (t * hi);
+                    t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+                    *d = PA_INT16_SWAP((int16_t) t);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
                     }
                 }
 
@@ -683,31 +742,49 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
             }
 
             case PA_SAMPLE_S32NE: {
-                int32_t *t;
+                int32_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
-                for (t = p; n > 0; n -= fs) {
-                    int32_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                s = (int32_t*) p + n/sizeof(int32_t);
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++)
-                        *t = (int32_t) (((int64_t) factor * (int64_t) *t) / 0x10000);
+                for (channel = 0, d = p; d < s; d++) {
+                    int64_t t;
+
+                    t = (int64_t)(*d);
+                    t = (t * factor) >> 16;
+                    t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+                    *d = (int32_t) t;
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
+                    }
                 }
 
                 break;
             }
 
             case PA_SAMPLE_S32RE: {
-                int32_t *t;
+                int32_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
 
-                for (t = p; n > 0; n -= fs) {
-                    int32_t factor = linear_get_int(e, v);
-                    unsigned c;
-                    e->x += fs;
+                s = (int32_t*) p + n/sizeof(int32_t);
 
-                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
-                        int32_t r = (int32_t) (((int64_t) factor * (int64_t) PA_INT32_SWAP(*t)) / 0x10000);
-                        *t = PA_INT32_SWAP(r);
+                for (channel = 0, d = p; d < s; d++) {
+                    int64_t t;
+
+                    t = (int64_t) PA_INT32_SWAP(*d);
+                    t = (t * factor) >> 16;
+                    t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+                    *d = PA_INT32_SWAP((int32_t) t);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                        channel = 0;
+                        e->x += fs;
+                        factor = linear_get_int(e, v);
                     }
                 }
 
@@ -715,6 +792,7 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
             }
 
             case PA_SAMPLE_FLOAT32NE: {
+            /*Seems the FLOAT32NE part of pa_volume_memchunk not right, do not reuse here*/
                 float *t;
 
                 for (t = p; n > 0; n -= fs) {
@@ -730,6 +808,7 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
             }
 
             case PA_SAMPLE_FLOAT32RE: {
+            /*Seems the FLOAT32RE part of pa_volume_memchunk not right, do not reuse here*/
                 float *t;
 
                 for (t = p; n > 0; n -= fs) {
@@ -746,14 +825,110 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
                 break;
             }
 
+            case PA_SAMPLE_S24NE: {
+                uint8_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
+
+                s = (uint8_t*) p + n/3;
+
+                for (channel = 0, d = p; d < s; d++) {
+                    int64_t t;
+
+                    t = (int64_t)((int32_t) (PA_READ24NE(d) << 8));
+                    t = (t * factor) >> 16;
+                    t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+                    PA_WRITE24NE(d, ((uint32_t) (int32_t) t) >> 8);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                            channel = 0;
+                            e->x += fs;
+                            factor = linear_get_int(e, v);
+                    }
+                }
+
+                break;
+            }
+            case PA_SAMPLE_S24RE: {
+                uint8_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
+
+                s = (uint8_t*) p + n/3;
+
+                for (channel = 0, d = p; d < s; d++) {
+                    int64_t t;
+
+                    t = (int64_t)((int32_t) (PA_READ24RE(d) << 8));
+                    t = (t * factor) >> 16;
+                    t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+                    PA_WRITE24RE(d, ((uint32_t) (int32_t) t) >> 8);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                            channel = 0;
+                            e->x += fs;
+                            factor = linear_get_int(e, v);
+                    }
+                }
+
+                break;
+            }
+            case PA_SAMPLE_S24_32NE: {
+                uint32_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
+
+                s = (uint32_t*) p + n/sizeof(uint32_t);
+
+                for (channel = 0, d = p; d < s; d++) {
+                    int64_t t;
+
+                    t = (int64_t) ((int32_t) (*d << 8));
+                    t = (t * factor) >> 16;
+                    t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+                    *d = ((uint32_t) ((int32_t) t)) >> 8;
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                            channel = 0;
+                            e->x += fs;
+                            factor = linear_get_int(e, v);
+                    }
+                }
+
+                break;
+            }
+            case PA_SAMPLE_S24_32RE: {
+                uint32_t *d, *s;
+                unsigned channel;
+                int32_t factor = linear_get_int(e, v);
+
+                s = (uint32_t*) p + n/sizeof(uint32_t);
+
+                for (channel = 0, d = p; d < s; d++) {
+                    int64_t t;
+
+                    t = (int64_t) ((int32_t) (PA_UINT32_SWAP(*d) << 8));
+                    t = (t * factor) >> 16;
+                    t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+                    *d = PA_UINT32_SWAP(((uint32_t) ((int32_t) t)) >> 8);
+
+                    if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+                            channel = 0;
+                            e->x += fs;
+                            factor = linear_get_int(e, v);
+                    }
+                }
+                break;
+            }
+                /* FIXME */
+                pa_assert_not_reached();
+
             case PA_SAMPLE_MAX:
             case PA_SAMPLE_INVALID:
                 pa_assert_not_reached();
         }
 
         pa_memblock_release(chunk->memblock);
-
-        e->x += chunk->length;
     } else {
         /* When we have no envelope to apply we reset our origin */
         e->x = 0;
@@ -769,13 +944,46 @@ void pa_envelope_rewind(pa_envelope *e, size_t n_bytes) {
 
     envelope_begin_read(e, &v);
 
-    if (n_bytes < e->x)
-        e->x -= n_bytes;
+    if (e->x - n_bytes <= e->points[v].x[0])
+        e->x = e->points[v].x[0];
     else
-        e->x = 0;
+        e->x -= n_bytes;
 
     e->points[v].n_current = 0;
     e->points[v].cached_valid = FALSE;
 
     envelope_commit_read(e, v);
 }
+
+void pa_envelope_restart(pa_envelope* e) {
+    int v;
+    pa_assert(e);
+
+    envelope_begin_read(e, &v);
+    e->x = e->points[v].x[0];
+    envelope_commit_read(e, v);
+}
+
+pa_bool_t pa_envelope_is_finished(pa_envelope* e) {
+    int v;
+    pa_bool_t finished;
+
+    pa_assert(e);
+    envelope_begin_read(e, &v);
+    finished = (e->x >=  e->points[v].x[e->points[v].n_points-1]);
+    envelope_commit_read(e, v);
+
+    return finished;
+}
+
+int32_t pa_envelope_length(pa_envelope *e) {
+    int v;
+    size_t size;
+
+    pa_assert(e);
+    envelope_begin_read(e, &v);
+    size = e->points[v].x[e->points[v].n_points-1] - e->points[v].x[0];
+    envelope_commit_read(e, v);
+
+    return size;
+}