Statistics
| Branch: | Revision:

root / audio / mixeng.c @ 1d6e34fd

History | View | Annotate | Download (6.2 kB)

1 85571bc7 bellard
/*
2 85571bc7 bellard
 * QEMU Mixing engine
3 85571bc7 bellard
 *
4 85571bc7 bellard
 * Copyright (c) 2004 Vassili Karpov (malc)
5 85571bc7 bellard
 * Copyright (c) 1998 Fabrice Bellard
6 85571bc7 bellard
 *
7 85571bc7 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 85571bc7 bellard
 * of this software and associated documentation files (the "Software"), to deal
9 85571bc7 bellard
 * in the Software without restriction, including without limitation the rights
10 85571bc7 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 85571bc7 bellard
 * copies of the Software, and to permit persons to whom the Software is
12 85571bc7 bellard
 * furnished to do so, subject to the following conditions:
13 85571bc7 bellard
 *
14 85571bc7 bellard
 * The above copyright notice and this permission notice shall be included in
15 85571bc7 bellard
 * all copies or substantial portions of the Software.
16 85571bc7 bellard
 *
17 85571bc7 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 85571bc7 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 85571bc7 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 85571bc7 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 85571bc7 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 85571bc7 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 85571bc7 bellard
 * THE SOFTWARE.
24 85571bc7 bellard
 */
25 85571bc7 bellard
#include "vl.h"
26 85571bc7 bellard
//#define DEBUG_FP
27 85571bc7 bellard
#include "audio/mixeng.h"
28 85571bc7 bellard
29 85571bc7 bellard
#define IN_T int8_t
30 85571bc7 bellard
#define IN_MIN CHAR_MIN
31 85571bc7 bellard
#define IN_MAX CHAR_MAX
32 85571bc7 bellard
#define SIGNED
33 85571bc7 bellard
#include "mixeng_template.h"
34 85571bc7 bellard
#undef SIGNED
35 85571bc7 bellard
#undef IN_MAX
36 85571bc7 bellard
#undef IN_MIN
37 85571bc7 bellard
#undef IN_T
38 85571bc7 bellard
39 85571bc7 bellard
#define IN_T uint8_t
40 85571bc7 bellard
#define IN_MIN 0
41 85571bc7 bellard
#define IN_MAX UCHAR_MAX
42 85571bc7 bellard
#include "mixeng_template.h"
43 85571bc7 bellard
#undef IN_MAX
44 85571bc7 bellard
#undef IN_MIN
45 85571bc7 bellard
#undef IN_T
46 85571bc7 bellard
47 85571bc7 bellard
#define IN_T int16_t
48 85571bc7 bellard
#define IN_MIN SHRT_MIN
49 85571bc7 bellard
#define IN_MAX SHRT_MAX
50 85571bc7 bellard
#define SIGNED
51 85571bc7 bellard
#include "mixeng_template.h"
52 85571bc7 bellard
#undef SIGNED
53 85571bc7 bellard
#undef IN_MAX
54 85571bc7 bellard
#undef IN_MIN
55 85571bc7 bellard
#undef IN_T
56 85571bc7 bellard
57 85571bc7 bellard
#define IN_T uint16_t
58 85571bc7 bellard
#define IN_MIN 0
59 85571bc7 bellard
#define IN_MAX USHRT_MAX
60 85571bc7 bellard
#include "mixeng_template.h"
61 85571bc7 bellard
#undef IN_MAX
62 85571bc7 bellard
#undef IN_MIN
63 85571bc7 bellard
#undef IN_T
64 85571bc7 bellard
65 85571bc7 bellard
t_sample *mixeng_conv[2][2][2] = {
66 85571bc7 bellard
    {
67 85571bc7 bellard
        {
68 85571bc7 bellard
            conv_uint8_t_to_mono,
69 85571bc7 bellard
            conv_uint16_t_to_mono
70 85571bc7 bellard
        },
71 85571bc7 bellard
        {
72 85571bc7 bellard
            conv_int8_t_to_mono,
73 85571bc7 bellard
            conv_int16_t_to_mono
74 85571bc7 bellard
        }
75 85571bc7 bellard
    },
76 85571bc7 bellard
    {
77 85571bc7 bellard
        {
78 85571bc7 bellard
            conv_uint8_t_to_stereo,
79 85571bc7 bellard
            conv_uint16_t_to_stereo
80 85571bc7 bellard
        },
81 85571bc7 bellard
        {
82 85571bc7 bellard
            conv_int8_t_to_stereo,
83 85571bc7 bellard
            conv_int16_t_to_stereo
84 85571bc7 bellard
        }
85 85571bc7 bellard
    }
86 85571bc7 bellard
};
87 85571bc7 bellard
88 85571bc7 bellard
f_sample *mixeng_clip[2][2][2] = {
89 85571bc7 bellard
    {
90 85571bc7 bellard
        {
91 85571bc7 bellard
            clip_uint8_t_from_mono,
92 85571bc7 bellard
            clip_uint16_t_from_mono
93 85571bc7 bellard
        },
94 85571bc7 bellard
        {
95 85571bc7 bellard
            clip_int8_t_from_mono,
96 85571bc7 bellard
            clip_int16_t_from_mono
97 85571bc7 bellard
        }
98 85571bc7 bellard
    },
99 85571bc7 bellard
    {
100 85571bc7 bellard
        {
101 85571bc7 bellard
            clip_uint8_t_from_stereo,
102 85571bc7 bellard
            clip_uint16_t_from_stereo
103 85571bc7 bellard
        },
104 85571bc7 bellard
        {
105 85571bc7 bellard
            clip_int8_t_from_stereo,
106 85571bc7 bellard
            clip_int16_t_from_stereo
107 85571bc7 bellard
        }
108 85571bc7 bellard
    }
109 85571bc7 bellard
};
110 85571bc7 bellard
111 85571bc7 bellard
/*
112 85571bc7 bellard
 * August 21, 1998
113 85571bc7 bellard
 * Copyright 1998 Fabrice Bellard.
114 85571bc7 bellard
 *
115 85571bc7 bellard
 * [Rewrote completly the code of Lance Norskog And Sundry
116 85571bc7 bellard
 * Contributors with a more efficient algorithm.]
117 85571bc7 bellard
 *
118 85571bc7 bellard
 * This source code is freely redistributable and may be used for
119 85571bc7 bellard
 * any purpose.  This copyright notice must be maintained. 
120 85571bc7 bellard
 * Lance Norskog And Sundry Contributors are not responsible for 
121 85571bc7 bellard
 * the consequences of using this software.  
122 85571bc7 bellard
 */
123 85571bc7 bellard
124 85571bc7 bellard
/*
125 85571bc7 bellard
 * Sound Tools rate change effect file.
126 85571bc7 bellard
 */
127 85571bc7 bellard
/*
128 85571bc7 bellard
 * Linear Interpolation.
129 85571bc7 bellard
 *
130 85571bc7 bellard
 * The use of fractional increment allows us to use no buffer. It
131 85571bc7 bellard
 * avoid the problems at the end of the buffer we had with the old
132 85571bc7 bellard
 * method which stored a possibly big buffer of size
133 85571bc7 bellard
 * lcm(in_rate,out_rate).
134 85571bc7 bellard
 *
135 85571bc7 bellard
 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
136 85571bc7 bellard
 * the input & output frequencies are equal, a delay of one sample is
137 85571bc7 bellard
 * introduced.  Limited to processing 32-bit count worth of samples.
138 85571bc7 bellard
 *
139 85571bc7 bellard
 * 1 << FRAC_BITS evaluating to zero in several places.  Changed with
140 85571bc7 bellard
 * an (unsigned long) cast to make it safe.  MarkMLl 2/1/99
141 85571bc7 bellard
 */
142 85571bc7 bellard
143 85571bc7 bellard
/* Private data */
144 85571bc7 bellard
typedef struct ratestuff {
145 85571bc7 bellard
    uint64_t opos;
146 85571bc7 bellard
    uint64_t opos_inc;
147 85571bc7 bellard
    uint32_t ipos;              /* position in the input stream (integer) */
148 85571bc7 bellard
    st_sample_t ilast;          /* last sample in the input stream */
149 85571bc7 bellard
} *rate_t;
150 85571bc7 bellard
151 85571bc7 bellard
/*
152 85571bc7 bellard
 * Prepare processing.
153 85571bc7 bellard
 */
154 85571bc7 bellard
void *st_rate_start (int inrate, int outrate)
155 85571bc7 bellard
{
156 85571bc7 bellard
    rate_t rate = (rate_t) qemu_mallocz (sizeof (struct ratestuff));
157 85571bc7 bellard
158 85571bc7 bellard
    if (!rate) {
159 85571bc7 bellard
        exit (EXIT_FAILURE);
160 85571bc7 bellard
    }
161 85571bc7 bellard
162 85571bc7 bellard
    if (inrate == outrate) {
163 85571bc7 bellard
        // exit (EXIT_FAILURE);
164 85571bc7 bellard
    }
165 85571bc7 bellard
166 85571bc7 bellard
    if (inrate >= 65535 || outrate >= 65535) {
167 85571bc7 bellard
        // exit (EXIT_FAILURE);
168 85571bc7 bellard
    }
169 85571bc7 bellard
170 85571bc7 bellard
    rate->opos = 0;
171 85571bc7 bellard
172 85571bc7 bellard
    /* increment */
173 85571bc7 bellard
    rate->opos_inc = (inrate * ((int64_t) UINT_MAX)) / outrate;
174 85571bc7 bellard
175 85571bc7 bellard
    rate->ipos = 0;
176 85571bc7 bellard
    rate->ilast.l = 0;
177 85571bc7 bellard
    rate->ilast.r = 0;
178 85571bc7 bellard
    return rate;
179 85571bc7 bellard
}
180 85571bc7 bellard
181 85571bc7 bellard
/*
182 85571bc7 bellard
 * Processed signed long samples from ibuf to obuf.
183 85571bc7 bellard
 * Return number of samples processed.
184 85571bc7 bellard
 */
185 85571bc7 bellard
void st_rate_flow (void *opaque, st_sample_t *ibuf, st_sample_t *obuf,
186 85571bc7 bellard
                   int *isamp, int *osamp)
187 85571bc7 bellard
{
188 85571bc7 bellard
    rate_t rate = (rate_t) opaque;
189 85571bc7 bellard
    st_sample_t *istart, *iend;
190 85571bc7 bellard
    st_sample_t *ostart, *oend;
191 85571bc7 bellard
    st_sample_t ilast, icur, out;
192 85571bc7 bellard
    int64_t t;
193 85571bc7 bellard
194 85571bc7 bellard
    ilast = rate->ilast;
195 85571bc7 bellard
196 85571bc7 bellard
    istart = ibuf;
197 85571bc7 bellard
    iend = ibuf + *isamp;
198 85571bc7 bellard
199 85571bc7 bellard
    ostart = obuf;
200 85571bc7 bellard
    oend = obuf + *osamp;
201 85571bc7 bellard
202 85571bc7 bellard
    if (rate->opos_inc == 1ULL << 32) {
203 85571bc7 bellard
        int i, n = *isamp > *osamp ? *osamp : *isamp;
204 85571bc7 bellard
        for (i = 0; i < n; i++) {
205 85571bc7 bellard
            obuf[i].l += ibuf[i].r;
206 85571bc7 bellard
            obuf[i].r += ibuf[i].r;
207 85571bc7 bellard
        }
208 85571bc7 bellard
        *isamp = n;
209 85571bc7 bellard
        *osamp = n;
210 85571bc7 bellard
        return;
211 85571bc7 bellard
    }
212 85571bc7 bellard
213 85571bc7 bellard
    while (obuf < oend) {
214 85571bc7 bellard
215 85571bc7 bellard
        /* Safety catch to make sure we have input samples.  */
216 85571bc7 bellard
        if (ibuf >= iend)
217 85571bc7 bellard
            break;
218 85571bc7 bellard
219 85571bc7 bellard
        /* read as many input samples so that ipos > opos */
220 85571bc7 bellard
221 85571bc7 bellard
        while (rate->ipos <= (rate->opos >> 32)) {
222 85571bc7 bellard
            ilast = *ibuf++;
223 85571bc7 bellard
            rate->ipos++;
224 85571bc7 bellard
            /* See if we finished the input buffer yet */
225 85571bc7 bellard
            if (ibuf >= iend) goto the_end;
226 85571bc7 bellard
        }
227 85571bc7 bellard
228 85571bc7 bellard
        icur = *ibuf;
229 85571bc7 bellard
230 85571bc7 bellard
        /* interpolate */
231 85571bc7 bellard
        t = rate->opos & 0xffffffff;
232 85571bc7 bellard
        out.l = (ilast.l * (INT_MAX - t) + icur.l * t) / INT_MAX;
233 85571bc7 bellard
        out.r = (ilast.r * (INT_MAX - t) + icur.r * t) / INT_MAX;
234 85571bc7 bellard
235 85571bc7 bellard
        /* output sample & increment position */
236 85571bc7 bellard
#if 0
237 85571bc7 bellard
        *obuf++ = out;
238 85571bc7 bellard
#else
239 85571bc7 bellard
        obuf->l += out.l;
240 85571bc7 bellard
        obuf->r += out.r;
241 85571bc7 bellard
        obuf += 1;
242 85571bc7 bellard
#endif
243 85571bc7 bellard
        rate->opos += rate->opos_inc;
244 85571bc7 bellard
    }
245 85571bc7 bellard
246 85571bc7 bellard
the_end:
247 85571bc7 bellard
    *isamp = ibuf - istart;
248 85571bc7 bellard
    *osamp = obuf - ostart;
249 85571bc7 bellard
    rate->ilast = ilast;
250 85571bc7 bellard
}
251 85571bc7 bellard
252 85571bc7 bellard
void st_rate_stop (void *opaque)
253 85571bc7 bellard
{
254 85571bc7 bellard
    qemu_free (opaque);
255 85571bc7 bellard
}