Statistics
| Branch: | Revision:

root / audio / rate_template.h @ cf2c1839

History | View | Annotate | Download (3.2 kB)

1 1d14ffa9 bellard
/*
2 1d14ffa9 bellard
 * QEMU Mixing engine
3 1d14ffa9 bellard
 *
4 1d14ffa9 bellard
 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 1d14ffa9 bellard
 * Copyright (c) 1998 Fabrice Bellard
6 1d14ffa9 bellard
 *
7 1d14ffa9 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 1d14ffa9 bellard
 * of this software and associated documentation files (the "Software"), to deal
9 1d14ffa9 bellard
 * in the Software without restriction, including without limitation the rights
10 1d14ffa9 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 1d14ffa9 bellard
 * copies of the Software, and to permit persons to whom the Software is
12 1d14ffa9 bellard
 * furnished to do so, subject to the following conditions:
13 1d14ffa9 bellard
 *
14 1d14ffa9 bellard
 * The above copyright notice and this permission notice shall be included in
15 1d14ffa9 bellard
 * all copies or substantial portions of the Software.
16 1d14ffa9 bellard
 *
17 1d14ffa9 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 1d14ffa9 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 1d14ffa9 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 1d14ffa9 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 1d14ffa9 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 1d14ffa9 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 1d14ffa9 bellard
 * THE SOFTWARE.
24 1d14ffa9 bellard
 */
25 1d14ffa9 bellard
26 1d14ffa9 bellard
/*
27 1d14ffa9 bellard
 * Processed signed long samples from ibuf to obuf.
28 1d14ffa9 bellard
 * Return number of samples processed.
29 1d14ffa9 bellard
 */
30 1ea879e5 malc
void NAME (void *opaque, struct st_sample *ibuf, struct st_sample *obuf,
31 1d14ffa9 bellard
           int *isamp, int *osamp)
32 1d14ffa9 bellard
{
33 c0fe3827 bellard
    struct rate *rate = opaque;
34 1ea879e5 malc
    struct st_sample *istart, *iend;
35 1ea879e5 malc
    struct st_sample *ostart, *oend;
36 1ea879e5 malc
    struct st_sample ilast, icur, out;
37 1d14ffa9 bellard
#ifdef FLOAT_MIXENG
38 1ea879e5 malc
    mixeng_real t;
39 1d14ffa9 bellard
#else
40 1d14ffa9 bellard
    int64_t t;
41 1d14ffa9 bellard
#endif
42 1d14ffa9 bellard
43 1d14ffa9 bellard
    ilast = rate->ilast;
44 1d14ffa9 bellard
45 1d14ffa9 bellard
    istart = ibuf;
46 1d14ffa9 bellard
    iend = ibuf + *isamp;
47 1d14ffa9 bellard
48 1d14ffa9 bellard
    ostart = obuf;
49 1d14ffa9 bellard
    oend = obuf + *osamp;
50 1d14ffa9 bellard
51 1d14ffa9 bellard
    if (rate->opos_inc == (1ULL + UINT_MAX)) {
52 1d14ffa9 bellard
        int i, n = *isamp > *osamp ? *osamp : *isamp;
53 1d14ffa9 bellard
        for (i = 0; i < n; i++) {
54 6c270db7 bellard
            OP (obuf[i].l, ibuf[i].l);
55 1d14ffa9 bellard
            OP (obuf[i].r, ibuf[i].r);
56 1d14ffa9 bellard
        }
57 1d14ffa9 bellard
        *isamp = n;
58 1d14ffa9 bellard
        *osamp = n;
59 1d14ffa9 bellard
        return;
60 1d14ffa9 bellard
    }
61 1d14ffa9 bellard
62 1d14ffa9 bellard
    while (obuf < oend) {
63 1d14ffa9 bellard
64 1d14ffa9 bellard
        /* Safety catch to make sure we have input samples.  */
65 1d14ffa9 bellard
        if (ibuf >= iend) {
66 1d14ffa9 bellard
            break;
67 1d14ffa9 bellard
        }
68 1d14ffa9 bellard
69 1d14ffa9 bellard
        /* read as many input samples so that ipos > opos */
70 1d14ffa9 bellard
71 1d14ffa9 bellard
        while (rate->ipos <= (rate->opos >> 32)) {
72 1d14ffa9 bellard
            ilast = *ibuf++;
73 1d14ffa9 bellard
            rate->ipos++;
74 1d14ffa9 bellard
            /* See if we finished the input buffer yet */
75 1d14ffa9 bellard
            if (ibuf >= iend) {
76 1d14ffa9 bellard
                goto the_end;
77 1d14ffa9 bellard
            }
78 1d14ffa9 bellard
        }
79 1d14ffa9 bellard
80 1d14ffa9 bellard
        icur = *ibuf;
81 1d14ffa9 bellard
82 1d14ffa9 bellard
        /* interpolate */
83 1d14ffa9 bellard
#ifdef FLOAT_MIXENG
84 1d14ffa9 bellard
#ifdef RECIPROCAL
85 1d14ffa9 bellard
        t = (rate->opos & UINT_MAX) * (1.f / UINT_MAX);
86 1d14ffa9 bellard
#else
87 1ea879e5 malc
        t = (rate->opos & UINT_MAX) / (mixeng_real) UINT_MAX;
88 1d14ffa9 bellard
#endif
89 1d14ffa9 bellard
        out.l = (ilast.l * (1.0 - t)) + icur.l * t;
90 1d14ffa9 bellard
        out.r = (ilast.r * (1.0 - t)) + icur.r * t;
91 1d14ffa9 bellard
#else
92 1d14ffa9 bellard
        t = rate->opos & 0xffffffff;
93 1d14ffa9 bellard
        out.l = (ilast.l * ((int64_t) UINT_MAX - t) + icur.l * t) >> 32;
94 1d14ffa9 bellard
        out.r = (ilast.r * ((int64_t) UINT_MAX - t) + icur.r * t) >> 32;
95 1d14ffa9 bellard
#endif
96 1d14ffa9 bellard
97 1d14ffa9 bellard
        /* output sample & increment position */
98 1d14ffa9 bellard
        OP (obuf->l, out.l);
99 1d14ffa9 bellard
        OP (obuf->r, out.r);
100 1d14ffa9 bellard
        obuf += 1;
101 1d14ffa9 bellard
        rate->opos += rate->opos_inc;
102 1d14ffa9 bellard
    }
103 1d14ffa9 bellard
104 1d14ffa9 bellard
the_end:
105 1d14ffa9 bellard
    *isamp = ibuf - istart;
106 1d14ffa9 bellard
    *osamp = obuf - ostart;
107 1d14ffa9 bellard
    rate->ilast = ilast;
108 1d14ffa9 bellard
}
109 1d14ffa9 bellard
110 1d14ffa9 bellard
#undef NAME
111 1d14ffa9 bellard
#undef OP