summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/envelope.c
blob: 7f2252e99578a566007a4d5d6ebb33ba633e00e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/***
  This file is part of PulseAudio.

  Copyright 2007 Lennart Poettering

  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
  Lesser 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/xmalloc.h>

#include <pulsecore/endianmacros.h>
#include <pulsecore/memchunk.h>
#include <pulsecore/macro.h>
#include <pulsecore/flist.h>
#include <pulsecore/semaphore.h>
#include <pulsecore/g711.h>

#include "envelope.h"

/*
    Envelope subsystem for applying linear interpolated volume
    envelopes on audio data. If multiple enevelopes shall be applied
    at the same time, the "minimum" envelope is determined and
    applied.

    Envelopes are defined in a statically allocated constant structure
    pa_envelope_def. It may be activated using pa_envelope_add(). And
    already active envelope may be replaced with pa_envelope_replace()
    and removed with pa_envelope_remove().The combined "minimum"
    envelope can be applied to audio data with pa_envelope_apply().

    _apply() on one hand and _add()/_replace()/_remove() on the other
    can be executed in seperate threads, in which case no locking is
    used.
*/

PA_STATIC_FLIST_DECLARE(items, 0, pa_xfree);

struct pa_envelope_item {
    PA_LLIST_FIELDS(pa_envelope_item);
    const pa_envelope_def *def;
    pa_usec_t start_x;
    union {
        int32_t i;
        float f;
    } start_y;
    unsigned j;
};

enum envelope_state {
    STATE_VALID0,
    STATE_VALID1,
    STATE_READ0,
    STATE_READ1,
    STATE_WAIT0,
    STATE_WAIT1,
    STATE_WRITE0,
    STATE_WRITE1
};

struct pa_envelope {
    pa_sample_spec sample_spec;

    PA_LLIST_HEAD(pa_envelope_item, items);

    pa_atomic_t state;

    size_t x;

    struct {
        unsigned n_points, n_allocated, n_current;

        size_t *x;
        union {
            int32_t *i;
            float *f;
        } y;

        size_t cached_dx;
        int32_t cached_dy_i;
        float cached_dy_dx;
        pa_bool_t cached_valid;
    } points[2];

    pa_bool_t is_float;

    pa_semaphore *semaphore;
};

pa_envelope *pa_envelope_new(const pa_sample_spec *ss) {
    pa_envelope *e;
    pa_assert(ss);

    e = pa_xnew(pa_envelope, 1);

    e->sample_spec = *ss;
    PA_LLIST_HEAD_INIT(pa_envelope_item, e->items);

    e->x = 0;

    e->points[0].n_points = e->points[1].n_points = 0;
    e->points[0].n_allocated = e->points[1].n_allocated = 0;
    e->points[0].n_current = e->points[1].n_current = 0;
    e->points[0].x = e->points[1].x = NULL;
    e->points[0].y.i = e->points[1].y.i = NULL;
    e->points[0].cached_valid = e->points[1].cached_valid = FALSE;

    pa_atomic_store(&e->state, STATE_VALID0);

    e->is_float =
        ss->format == PA_SAMPLE_FLOAT32LE ||
        ss->format == PA_SAMPLE_FLOAT32BE;

    e->semaphore = pa_semaphore_new(0);

    return e;
}

void pa_envelope_free(pa_envelope *e) {
    pa_assert(e);

    while (e->items)
        pa_envelope_remove(e, e->items);

    pa_xfree(e->points[0].x);
    pa_xfree(e->points[1].x);
    pa_xfree(e->points[0].y.i);
    pa_xfree(e->points[1].y.i);

    pa_semaphore_free(e->semaphore);

    pa_xfree(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) ((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 + ((float) x3 - (float) x1) * (y2 - _y1) / ((float) x2 - (float) x1);
}

static int32_t item_get_int(pa_envelope_item *i, pa_usec_t x) {
    pa_assert(i);

    if (x <= i->start_x)
        return i->start_y.i;

    x -= i->start_x;

    if (x <= i->def->points_x[0])
        return linear_interpolate_int(0, i->start_y.i,
                                      i->def->points_x[0], i->def->points_y.i[0], x);

    if (x >= i->def->points_x[i->def->n_points-1])
        return i->def->points_y.i[i->def->n_points-1];

    pa_assert(i->j > 0);
    pa_assert(i->def->points_x[i->j-1] <= x);
    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);
}

static float item_get_float(pa_envelope_item *i, pa_usec_t x) {
    pa_assert(i);

    if (x <= i->start_x)
        return i->start_y.f;

    x -= i->start_x;

    if (x <= i->def->points_x[0])
        return linear_interpolate_float(0, i->start_y.f,
                                        i->def->points_x[0], i->def->points_y.f[0], x);

    if (x >= i->def->points_x[i->def->n_points-1])
        return i->def->points_y.f[i->def->n_points-1];

    pa_assert(i->j > 0);
    pa_assert(i->def->points_x[i->j-1] <= x);
    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);
}

static void envelope_begin_write(pa_envelope *e, int *v) {
    enum envelope_state new_state, old_state;
    pa_bool_t wait_sem;

    pa_assert(e);
    pa_assert(v);

    for (;;) {
        do {
            wait_sem = FALSE;
            old_state = pa_atomic_load(&e->state);

            switch (old_state) {
                case STATE_VALID0:
                    *v = 1;
                    new_state = STATE_WRITE0;
                    break;
                case STATE_VALID1:
                    *v = 0;
                    new_state = STATE_WRITE1;
                    break;
                case STATE_READ0:
                    new_state = STATE_WAIT0;
                    wait_sem = TRUE;
                    break;
                case STATE_READ1:
                    new_state = STATE_WAIT1;
                    wait_sem = TRUE;
                    break;
                default:
                    pa_assert_not_reached();
            }
        } while (!pa_atomic_cmpxchg(&e->state, old_state, new_state));

        if (!wait_sem)
            break;

        pa_semaphore_wait(e->semaphore);
    }
}

static pa_bool_t envelope_commit_write(pa_envelope *e, int v) {
    enum envelope_state new_state, old_state;

    pa_assert(e);

    do {
        old_state = pa_atomic_load(&e->state);

        switch (old_state) {
            case STATE_WRITE0:
                pa_assert(v == 1);
                new_state = STATE_VALID1;
                break;
            case STATE_WRITE1:
                pa_assert(v == 0);
                new_state = STATE_VALID0;
                break;
            case STATE_VALID0:
            case STATE_VALID1:
            case STATE_READ0:
            case STATE_READ1:
                return FALSE;
            default:
                pa_assert_not_reached();
        }
    } while (!pa_atomic_cmpxchg(&e->state, old_state, new_state));

    return TRUE;
}

static void envelope_begin_read(pa_envelope *e, int *v) {
    enum envelope_state new_state, old_state;
    pa_assert(e);
    pa_assert(v);

    do {
        old_state = pa_atomic_load(&e->state);

        switch (old_state) {
            case STATE_VALID0:
            case STATE_WRITE0:
                *v = 0;
                new_state = STATE_READ0;
                break;
            case STATE_VALID1:
            case STATE_WRITE1:
                *v = 1;
                new_state = STATE_READ1;
                break;
            default:
                pa_assert_not_reached();
        }
    } while (!pa_atomic_cmpxchg(&e->state, old_state, new_state));
}

static void envelope_commit_read(pa_envelope *e, int v) {
    enum envelope_state new_state, old_state;
    pa_bool_t post_sem;

    pa_assert(e);

    do {
        post_sem = FALSE;
        old_state = pa_atomic_load(&e->state);

        switch (old_state) {
            case STATE_READ0:
                pa_assert(v == 0);
                new_state = STATE_VALID0;
                break;
            case STATE_READ1:
                pa_assert(v == 1);
                new_state = STATE_VALID1;
                break;
            case STATE_WAIT0:
                pa_assert(v == 0);
                new_state = STATE_VALID0;
                post_sem = TRUE;
                break;
            case STATE_WAIT1:
                pa_assert(v == 1);
                new_state = STATE_VALID1;
                post_sem = TRUE;
                break;
            default:
                pa_assert_not_reached();
        }
    } while (!pa_atomic_cmpxchg(&e->state, old_state, new_state));

    if (post_sem)
        pa_semaphore_post(e->semaphore);
}

static void envelope_merge(pa_envelope *e, int v) {

    e->points[v].n_points = 0;

    if (e->items) {
        pa_envelope_item *i;
        pa_usec_t x = (pa_usec_t) -1;

        for (i = e->items; i; i = i->next)
            i->j = 0;

        for (;;) {
            pa_bool_t min_is_set;
            pa_envelope_item *s = NULL;

            /* Let's find the next spot on the X axis to analyze */
            for (i = e->items; i; i = i->next) {

                for (;;) {

                    if (i->j >= i->def->n_points)
                        break;

                    if ((x != (pa_usec_t) -1) && i->start_x + i->def->points_x[i->j] <= x) {
                        i->j++;
                        continue;
                    }

                    if (!s || (i->start_x + i->def->points_x[i->j] < s->start_x + s->def->points_x[s->j]))
                        s = i;

                    break;
                }
            }

            if (!s)
                break;

            if (e->points[v].n_points >= e->points[v].n_allocated) {
                e->points[v].n_allocated = PA_MAX(e->points[v].n_points*2, PA_ENVELOPE_POINTS_MAX);

                e->points[v].x = pa_xrealloc(e->points[v].x, sizeof(size_t) * e->points[v].n_allocated);
                e->points[v].y.i = pa_xrealloc(e->points[v].y.i, sizeof(int32_t) * e->points[v].n_allocated);
            }

            x = s->start_x + s->def->points_x[s->j];
            e->points[v].x[e->points[v].n_points] = pa_usec_to_bytes(x, &e->sample_spec);

            min_is_set = FALSE;

            /* Now let's find the lowest value */
            if (e->is_float) {
                float min_f;

                for (i = e->items; i; i = i->next) {
                    float f = item_get_float(i, x);
                    if (!min_is_set || f < min_f) {
                        min_f = f;
                        min_is_set = TRUE;
                    }
                }

                e->points[v].y.f[e->points[v].n_points] = min_f;
            } else {
                int32_t min_k;

                for (i = e->items; i; i = i->next) {
                    int32_t k = item_get_int(i, x);
                    if (!min_is_set || k < min_k) {
                        min_k = k;
                        min_is_set = TRUE;
                    }
                }

                e->points[v].y.i[e->points[v].n_points] = min_k;
            }

            pa_assert_se(min_is_set);
            e->points[v].n_points++;
        }
    }

    e->points[v].n_current = 0;
    e->points[v].cached_valid = FALSE;
}

pa_envelope_item *pa_envelope_add(pa_envelope *e, const pa_envelope_def *def) {
    pa_envelope_item *i;
    int v;

    pa_assert(e);
    pa_assert(def);
    pa_assert(def->n_points > 0);

    if (!(i = pa_flist_pop(PA_STATIC_FLIST_GET(items))))
        i = pa_xnew(pa_envelope_item, 1);

    i->def = def;

    if (e->is_float)
        i->start_y.f = def->points_y.f[0];
    else
        i->start_y.i = def->points_y.i[0];

    PA_LLIST_PREPEND(pa_envelope_item, e->items, i);

    envelope_begin_write(e, &v);

    do {

        i->start_x = pa_bytes_to_usec(e->x, &e->sample_spec);
        envelope_merge(e, v);

    } while (!envelope_commit_write(e, v));

    return i;
}

pa_envelope_item *pa_envelope_replace(pa_envelope *e, pa_envelope_item *i, const pa_envelope_def *def) {
    pa_usec_t x;
    int v;

    pa_assert(e);
    pa_assert(i);
    pa_assert(def->n_points > 0);

    envelope_begin_write(e, &v);

    for (;;) {
        float saved_f;
        int32_t saved_i;
        uint64_t saved_start_x;
        const pa_envelope_def *saved_def;

        x = pa_bytes_to_usec(e->x, &e->sample_spec);

        if (e->is_float) {
            saved_f = i->start_y.f;
            i->start_y.f = item_get_float(i, x);
        } else {
            saved_i = i->start_y.i;
            i->start_y.i = item_get_int(i, x);
        }

        saved_start_x = i->start_x;
        saved_def = i->def;

        i->start_x = x;
        i->def = def;

        envelope_merge(e, v);

        if (envelope_commit_write(e, v))
            break;

        i->start_x = saved_start_x;
        i->def = saved_def;

        if (e->is_float)
            i->start_y.f = saved_f;
        else
            i->start_y.i = saved_i;
    }

    return i;
}

void pa_envelope_remove(pa_envelope *e, pa_envelope_item *i) {
    int v;

    pa_assert(e);
    pa_assert(i);

    PA_LLIST_REMOVE(pa_envelope_item, e->items, i);

    if (pa_flist_push(PA_STATIC_FLIST_GET(items), i) < 0)
        pa_xfree(i);

    envelope_begin_write(e, &v);
    do {
        envelope_merge(e, v);
    } while (!envelope_commit_write(e, v));
}

static int32_t linear_get_int(pa_envelope *e, int v) {
    pa_assert(e);

    /* The repeated division could be replaced by Bresenham, as an
     * optimization */

    if (e->x < e->points[v].x[0])
        return e->points[v].y.i[0];

    for (;;) {
        if (e->points[v].n_current+1 >= e->points[v].n_points)
            return e->points[v].y.i[e->points[v].n_points-1];

        if (e->x < e->points[v].x[e->points[v].n_current+1])
            break;

        e->points[v].n_current++;
        e->points[v].cached_valid = FALSE;
    }

    if (!e->points[v].cached_valid) {
        e->points[v].cached_dx = e->points[v].x[e->points[v].n_current+1] - e->points[v].x[e->points[v].n_current];
        e->points[v].cached_dy_i = e->points[v].y.i[e->points[v].n_current+1] - e->points[v].y.i[e->points[v].n_current];
        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;
}

static float linear_get_float(pa_envelope *e, int v) {
    pa_assert(e);

    if (e->x < e->points[v].x[0])
        return e->points[v].y.f[0];

    for (;;) {
        if (e->points[v].n_current+1 >= e->points[v].n_points)
            return e->points[v].y.f[e->points[v].n_points-1];

        if (e->x < e->points[v].x[e->points[v].n_current+1])
            break;

        e->points[v].n_current++;
        e->points[v].cached_valid = FALSE;
    }

    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]) /
            ((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] + (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) {
    int v;

    pa_assert(e);
    pa_assert(chunk);

    envelope_begin_read(e, &v);

    if (e->points[v].n_points > 0) {
        void *p;
        size_t fs, n;

        pa_memchunk_make_writable(chunk, 0);
        p = (uint8_t*) pa_memblock_acquire(chunk->memblock) + chunk->index;
        fs = pa_frame_size(&e->sample_spec);
        n = chunk->length;

        switch (e->sample_spec.format) {



            case PA_SAMPLE_U8: {
                uint8_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++)
                        *t = (uint8_t) (((factor * ((int16_t) *t - 0x80)) / 0x10000) + 0x80);
                }

                break;
            }

            case PA_SAMPLE_ULAW: {
                uint8_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
                        int16_t k = st_ulaw2linear16(*t);
                        *t = (uint8_t) st_14linear2ulaw((int16_t) (((factor * k) / 0x10000) >> 2));
                    }
                }

                break;
            }

            case PA_SAMPLE_ALAW: {
                uint8_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
                        int16_t k = st_alaw2linear16(*t);
                        *t = (uint8_t) st_13linear2alaw((int16_t) (((factor * k) / 0x10000) >> 3));
                    }
                }

                break;
            }

            case PA_SAMPLE_S16NE: {
                int16_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++)
                        *t = (int16_t) ((factor * *t) / 0x10000);
                }

                break;
            }

            case PA_SAMPLE_S16RE: {
                int16_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
                        int16_t r = (int16_t) ((factor * PA_INT16_SWAP(*t)) / 0x10000);
                        *t = PA_INT16_SWAP(r);
                    }
                }

                break;
            }

            case PA_SAMPLE_S32NE: {
                int32_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++)
                        *t = (int32_t) (((int64_t) factor * (int64_t) *t) / 0x10000);
                }

                break;
            }

            case PA_SAMPLE_S32RE: {
                int32_t *t;

                for (t = p; n > 0; n -= fs) {
                    int32_t factor = linear_get_int(e, v);
                    unsigned c;
                    e->x += fs;

                    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);
                    }
                }

                break;
            }

            case PA_SAMPLE_FLOAT32NE: {
                float *t;

                for (t = p; n > 0; n -= fs) {
                    float factor = linear_get_float(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++)
                        *t = *t * factor;
                }

                break;
            }

            case PA_SAMPLE_FLOAT32RE: {
                float *t;

                for (t = p; n > 0; n -= fs) {
                    float factor = linear_get_float(e, v);
                    unsigned c;
                    e->x += fs;

                    for (c = 0; c < e->sample_spec.channels; c++, t++) {
                        float r = PA_FLOAT32_SWAP(*t) * factor;
                        *t = PA_FLOAT32_SWAP(r);
                    }
                }

                break;
            }

            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;
    }

    envelope_commit_read(e, v);
}

void pa_envelope_rewind(pa_envelope *e, size_t n_bytes) {
    int v;

    pa_assert(e);

    envelope_begin_read(e, &v);

    if (n_bytes < e->x)
        e->x -= n_bytes;
    else
        e->x = 0;

    e->points[v].n_current = 0;
    e->points[v].cached_valid = FALSE;

    envelope_commit_read(e, v);
}