Statistics
| Branch: | Revision:

root / audio / mixeng.c @ cf2c1839

History | View | Annotate | Download (8.5 kB)

1 85571bc7 bellard
/*
2 85571bc7 bellard
 * QEMU Mixing engine
3 85571bc7 bellard
 *
4 1d14ffa9 bellard
 * Copyright (c) 2004-2005 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 87ecb68b pbrook
#include "qemu-common.h"
26 87ecb68b pbrook
#include "audio.h"
27 85571bc7 bellard
28 1d14ffa9 bellard
#define AUDIO_CAP "mixeng"
29 1d14ffa9 bellard
#include "audio_int.h"
30 1d14ffa9 bellard
31 1d14ffa9 bellard
/* 8 bit */
32 1d14ffa9 bellard
#define ENDIAN_CONVERSION natural
33 1d14ffa9 bellard
#define ENDIAN_CONVERT(v) (v)
34 1d14ffa9 bellard
35 1d14ffa9 bellard
/* Signed 8 bit */
36 85571bc7 bellard
#define IN_T int8_t
37 1d14ffa9 bellard
#define IN_MIN SCHAR_MIN
38 1d14ffa9 bellard
#define IN_MAX SCHAR_MAX
39 85571bc7 bellard
#define SIGNED
40 1d14ffa9 bellard
#define SHIFT 8
41 85571bc7 bellard
#include "mixeng_template.h"
42 85571bc7 bellard
#undef SIGNED
43 85571bc7 bellard
#undef IN_MAX
44 85571bc7 bellard
#undef IN_MIN
45 85571bc7 bellard
#undef IN_T
46 1d14ffa9 bellard
#undef SHIFT
47 85571bc7 bellard
48 1d14ffa9 bellard
/* Unsigned 8 bit */
49 85571bc7 bellard
#define IN_T uint8_t
50 85571bc7 bellard
#define IN_MIN 0
51 85571bc7 bellard
#define IN_MAX UCHAR_MAX
52 1d14ffa9 bellard
#define SHIFT 8
53 85571bc7 bellard
#include "mixeng_template.h"
54 85571bc7 bellard
#undef IN_MAX
55 85571bc7 bellard
#undef IN_MIN
56 85571bc7 bellard
#undef IN_T
57 1d14ffa9 bellard
#undef SHIFT
58 1d14ffa9 bellard
59 1d14ffa9 bellard
#undef ENDIAN_CONVERT
60 1d14ffa9 bellard
#undef ENDIAN_CONVERSION
61 85571bc7 bellard
62 1d14ffa9 bellard
/* Signed 16 bit */
63 85571bc7 bellard
#define IN_T int16_t
64 85571bc7 bellard
#define IN_MIN SHRT_MIN
65 85571bc7 bellard
#define IN_MAX SHRT_MAX
66 85571bc7 bellard
#define SIGNED
67 1d14ffa9 bellard
#define SHIFT 16
68 1d14ffa9 bellard
#define ENDIAN_CONVERSION natural
69 1d14ffa9 bellard
#define ENDIAN_CONVERT(v) (v)
70 85571bc7 bellard
#include "mixeng_template.h"
71 1d14ffa9 bellard
#undef ENDIAN_CONVERT
72 1d14ffa9 bellard
#undef ENDIAN_CONVERSION
73 1d14ffa9 bellard
#define ENDIAN_CONVERSION swap
74 1d14ffa9 bellard
#define ENDIAN_CONVERT(v) bswap16 (v)
75 1d14ffa9 bellard
#include "mixeng_template.h"
76 1d14ffa9 bellard
#undef ENDIAN_CONVERT
77 1d14ffa9 bellard
#undef ENDIAN_CONVERSION
78 85571bc7 bellard
#undef SIGNED
79 85571bc7 bellard
#undef IN_MAX
80 85571bc7 bellard
#undef IN_MIN
81 85571bc7 bellard
#undef IN_T
82 1d14ffa9 bellard
#undef SHIFT
83 85571bc7 bellard
84 f941aa25 ths
/* Unsigned 16 bit */
85 85571bc7 bellard
#define IN_T uint16_t
86 85571bc7 bellard
#define IN_MIN 0
87 85571bc7 bellard
#define IN_MAX USHRT_MAX
88 1d14ffa9 bellard
#define SHIFT 16
89 1d14ffa9 bellard
#define ENDIAN_CONVERSION natural
90 1d14ffa9 bellard
#define ENDIAN_CONVERT(v) (v)
91 1d14ffa9 bellard
#include "mixeng_template.h"
92 1d14ffa9 bellard
#undef ENDIAN_CONVERT
93 1d14ffa9 bellard
#undef ENDIAN_CONVERSION
94 1d14ffa9 bellard
#define ENDIAN_CONVERSION swap
95 1d14ffa9 bellard
#define ENDIAN_CONVERT(v) bswap16 (v)
96 85571bc7 bellard
#include "mixeng_template.h"
97 1d14ffa9 bellard
#undef ENDIAN_CONVERT
98 1d14ffa9 bellard
#undef ENDIAN_CONVERSION
99 85571bc7 bellard
#undef IN_MAX
100 85571bc7 bellard
#undef IN_MIN
101 85571bc7 bellard
#undef IN_T
102 1d14ffa9 bellard
#undef SHIFT
103 85571bc7 bellard
104 f941aa25 ths
/* Signed 32 bit */
105 f941aa25 ths
#define IN_T int32_t
106 f941aa25 ths
#define IN_MIN INT32_MIN
107 f941aa25 ths
#define IN_MAX INT32_MAX
108 f941aa25 ths
#define SIGNED
109 f941aa25 ths
#define SHIFT 32
110 f941aa25 ths
#define ENDIAN_CONVERSION natural
111 f941aa25 ths
#define ENDIAN_CONVERT(v) (v)
112 f941aa25 ths
#include "mixeng_template.h"
113 f941aa25 ths
#undef ENDIAN_CONVERT
114 f941aa25 ths
#undef ENDIAN_CONVERSION
115 f941aa25 ths
#define ENDIAN_CONVERSION swap
116 f941aa25 ths
#define ENDIAN_CONVERT(v) bswap32 (v)
117 f941aa25 ths
#include "mixeng_template.h"
118 f941aa25 ths
#undef ENDIAN_CONVERT
119 f941aa25 ths
#undef ENDIAN_CONVERSION
120 f941aa25 ths
#undef SIGNED
121 f941aa25 ths
#undef IN_MAX
122 f941aa25 ths
#undef IN_MIN
123 f941aa25 ths
#undef IN_T
124 f941aa25 ths
#undef SHIFT
125 f941aa25 ths
126 ad483a51 malc
/* Unsigned 32 bit */
127 f941aa25 ths
#define IN_T uint32_t
128 f941aa25 ths
#define IN_MIN 0
129 f941aa25 ths
#define IN_MAX UINT32_MAX
130 f941aa25 ths
#define SHIFT 32
131 f941aa25 ths
#define ENDIAN_CONVERSION natural
132 f941aa25 ths
#define ENDIAN_CONVERT(v) (v)
133 f941aa25 ths
#include "mixeng_template.h"
134 f941aa25 ths
#undef ENDIAN_CONVERT
135 f941aa25 ths
#undef ENDIAN_CONVERSION
136 f941aa25 ths
#define ENDIAN_CONVERSION swap
137 f941aa25 ths
#define ENDIAN_CONVERT(v) bswap32 (v)
138 f941aa25 ths
#include "mixeng_template.h"
139 f941aa25 ths
#undef ENDIAN_CONVERT
140 f941aa25 ths
#undef ENDIAN_CONVERSION
141 f941aa25 ths
#undef IN_MAX
142 f941aa25 ths
#undef IN_MIN
143 f941aa25 ths
#undef IN_T
144 f941aa25 ths
#undef SHIFT
145 f941aa25 ths
146 f941aa25 ths
t_sample *mixeng_conv[2][2][2][3] = {
147 85571bc7 bellard
    {
148 85571bc7 bellard
        {
149 1d14ffa9 bellard
            {
150 1d14ffa9 bellard
                conv_natural_uint8_t_to_mono,
151 f941aa25 ths
                conv_natural_uint16_t_to_mono,
152 f941aa25 ths
                conv_natural_uint32_t_to_mono
153 1d14ffa9 bellard
            },
154 1d14ffa9 bellard
            {
155 1d14ffa9 bellard
                conv_natural_uint8_t_to_mono,
156 f941aa25 ths
                conv_swap_uint16_t_to_mono,
157 f941aa25 ths
                conv_swap_uint32_t_to_mono,
158 1d14ffa9 bellard
            }
159 85571bc7 bellard
        },
160 85571bc7 bellard
        {
161 1d14ffa9 bellard
            {
162 1d14ffa9 bellard
                conv_natural_int8_t_to_mono,
163 f941aa25 ths
                conv_natural_int16_t_to_mono,
164 f941aa25 ths
                conv_natural_int32_t_to_mono
165 1d14ffa9 bellard
            },
166 1d14ffa9 bellard
            {
167 1d14ffa9 bellard
                conv_natural_int8_t_to_mono,
168 f941aa25 ths
                conv_swap_int16_t_to_mono,
169 f941aa25 ths
                conv_swap_int32_t_to_mono
170 1d14ffa9 bellard
            }
171 85571bc7 bellard
        }
172 85571bc7 bellard
    },
173 85571bc7 bellard
    {
174 85571bc7 bellard
        {
175 1d14ffa9 bellard
            {
176 1d14ffa9 bellard
                conv_natural_uint8_t_to_stereo,
177 f941aa25 ths
                conv_natural_uint16_t_to_stereo,
178 f941aa25 ths
                conv_natural_uint32_t_to_stereo
179 1d14ffa9 bellard
            },
180 1d14ffa9 bellard
            {
181 1d14ffa9 bellard
                conv_natural_uint8_t_to_stereo,
182 f941aa25 ths
                conv_swap_uint16_t_to_stereo,
183 f941aa25 ths
                conv_swap_uint32_t_to_stereo
184 1d14ffa9 bellard
            }
185 85571bc7 bellard
        },
186 85571bc7 bellard
        {
187 1d14ffa9 bellard
            {
188 1d14ffa9 bellard
                conv_natural_int8_t_to_stereo,
189 f941aa25 ths
                conv_natural_int16_t_to_stereo,
190 f941aa25 ths
                conv_natural_int32_t_to_stereo
191 1d14ffa9 bellard
            },
192 1d14ffa9 bellard
            {
193 1d14ffa9 bellard
                conv_natural_int8_t_to_stereo,
194 f941aa25 ths
                conv_swap_int16_t_to_stereo,
195 f941aa25 ths
                conv_swap_int32_t_to_stereo,
196 1d14ffa9 bellard
            }
197 85571bc7 bellard
        }
198 85571bc7 bellard
    }
199 85571bc7 bellard
};
200 85571bc7 bellard
201 f941aa25 ths
f_sample *mixeng_clip[2][2][2][3] = {
202 85571bc7 bellard
    {
203 85571bc7 bellard
        {
204 1d14ffa9 bellard
            {
205 1d14ffa9 bellard
                clip_natural_uint8_t_from_mono,
206 f941aa25 ths
                clip_natural_uint16_t_from_mono,
207 f941aa25 ths
                clip_natural_uint32_t_from_mono
208 1d14ffa9 bellard
            },
209 1d14ffa9 bellard
            {
210 1d14ffa9 bellard
                clip_natural_uint8_t_from_mono,
211 f941aa25 ths
                clip_swap_uint16_t_from_mono,
212 f941aa25 ths
                clip_swap_uint32_t_from_mono
213 1d14ffa9 bellard
            }
214 85571bc7 bellard
        },
215 85571bc7 bellard
        {
216 1d14ffa9 bellard
            {
217 1d14ffa9 bellard
                clip_natural_int8_t_from_mono,
218 f941aa25 ths
                clip_natural_int16_t_from_mono,
219 f941aa25 ths
                clip_natural_int32_t_from_mono
220 1d14ffa9 bellard
            },
221 1d14ffa9 bellard
            {
222 1d14ffa9 bellard
                clip_natural_int8_t_from_mono,
223 f941aa25 ths
                clip_swap_int16_t_from_mono,
224 f941aa25 ths
                clip_swap_int32_t_from_mono
225 1d14ffa9 bellard
            }
226 85571bc7 bellard
        }
227 85571bc7 bellard
    },
228 85571bc7 bellard
    {
229 85571bc7 bellard
        {
230 1d14ffa9 bellard
            {
231 1d14ffa9 bellard
                clip_natural_uint8_t_from_stereo,
232 f941aa25 ths
                clip_natural_uint16_t_from_stereo,
233 f941aa25 ths
                clip_natural_uint32_t_from_stereo
234 1d14ffa9 bellard
            },
235 1d14ffa9 bellard
            {
236 1d14ffa9 bellard
                clip_natural_uint8_t_from_stereo,
237 f941aa25 ths
                clip_swap_uint16_t_from_stereo,
238 f941aa25 ths
                clip_swap_uint32_t_from_stereo
239 1d14ffa9 bellard
            }
240 85571bc7 bellard
        },
241 85571bc7 bellard
        {
242 1d14ffa9 bellard
            {
243 1d14ffa9 bellard
                clip_natural_int8_t_from_stereo,
244 f941aa25 ths
                clip_natural_int16_t_from_stereo,
245 f941aa25 ths
                clip_natural_int32_t_from_stereo
246 1d14ffa9 bellard
            },
247 1d14ffa9 bellard
            {
248 1d14ffa9 bellard
                clip_natural_int8_t_from_stereo,
249 f941aa25 ths
                clip_swap_int16_t_from_stereo,
250 f941aa25 ths
                clip_swap_int32_t_from_stereo
251 1d14ffa9 bellard
            }
252 85571bc7 bellard
        }
253 85571bc7 bellard
    }
254 85571bc7 bellard
};
255 85571bc7 bellard
256 85571bc7 bellard
/*
257 85571bc7 bellard
 * August 21, 1998
258 85571bc7 bellard
 * Copyright 1998 Fabrice Bellard.
259 85571bc7 bellard
 *
260 85571bc7 bellard
 * [Rewrote completly the code of Lance Norskog And Sundry
261 85571bc7 bellard
 * Contributors with a more efficient algorithm.]
262 85571bc7 bellard
 *
263 85571bc7 bellard
 * This source code is freely redistributable and may be used for
264 1d14ffa9 bellard
 * any purpose.  This copyright notice must be maintained.
265 1d14ffa9 bellard
 * Lance Norskog And Sundry Contributors are not responsible for
266 1d14ffa9 bellard
 * the consequences of using this software.
267 85571bc7 bellard
 */
268 85571bc7 bellard
269 85571bc7 bellard
/*
270 85571bc7 bellard
 * Sound Tools rate change effect file.
271 85571bc7 bellard
 */
272 85571bc7 bellard
/*
273 85571bc7 bellard
 * Linear Interpolation.
274 85571bc7 bellard
 *
275 85571bc7 bellard
 * The use of fractional increment allows us to use no buffer. It
276 85571bc7 bellard
 * avoid the problems at the end of the buffer we had with the old
277 85571bc7 bellard
 * method which stored a possibly big buffer of size
278 85571bc7 bellard
 * lcm(in_rate,out_rate).
279 85571bc7 bellard
 *
280 85571bc7 bellard
 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
281 85571bc7 bellard
 * the input & output frequencies are equal, a delay of one sample is
282 85571bc7 bellard
 * introduced.  Limited to processing 32-bit count worth of samples.
283 85571bc7 bellard
 *
284 85571bc7 bellard
 * 1 << FRAC_BITS evaluating to zero in several places.  Changed with
285 85571bc7 bellard
 * an (unsigned long) cast to make it safe.  MarkMLl 2/1/99
286 85571bc7 bellard
 */
287 85571bc7 bellard
288 85571bc7 bellard
/* Private data */
289 c0fe3827 bellard
struct rate {
290 85571bc7 bellard
    uint64_t opos;
291 85571bc7 bellard
    uint64_t opos_inc;
292 85571bc7 bellard
    uint32_t ipos;              /* position in the input stream (integer) */
293 1ea879e5 malc
    struct st_sample ilast;          /* last sample in the input stream */
294 c0fe3827 bellard
};
295 85571bc7 bellard
296 85571bc7 bellard
/*
297 85571bc7 bellard
 * Prepare processing.
298 85571bc7 bellard
 */
299 85571bc7 bellard
void *st_rate_start (int inrate, int outrate)
300 85571bc7 bellard
{
301 c0fe3827 bellard
    struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate));
302 85571bc7 bellard
303 85571bc7 bellard
    if (!rate) {
304 e7cad338 bellard
        dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate));
305 1d14ffa9 bellard
        return NULL;
306 85571bc7 bellard
    }
307 85571bc7 bellard
308 85571bc7 bellard
    rate->opos = 0;
309 85571bc7 bellard
310 85571bc7 bellard
    /* increment */
311 1d14ffa9 bellard
    rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
312 85571bc7 bellard
313 85571bc7 bellard
    rate->ipos = 0;
314 85571bc7 bellard
    rate->ilast.l = 0;
315 85571bc7 bellard
    rate->ilast.r = 0;
316 85571bc7 bellard
    return rate;
317 85571bc7 bellard
}
318 85571bc7 bellard
319 1d14ffa9 bellard
#define NAME st_rate_flow_mix
320 1d14ffa9 bellard
#define OP(a, b) a += b
321 1d14ffa9 bellard
#include "rate_template.h"
322 85571bc7 bellard
323 1d14ffa9 bellard
#define NAME st_rate_flow
324 1d14ffa9 bellard
#define OP(a, b) a = b
325 1d14ffa9 bellard
#include "rate_template.h"
326 85571bc7 bellard
327 85571bc7 bellard
void st_rate_stop (void *opaque)
328 85571bc7 bellard
{
329 85571bc7 bellard
    qemu_free (opaque);
330 85571bc7 bellard
}
331 1d14ffa9 bellard
332 1ea879e5 malc
void mixeng_clear (struct st_sample *buf, int len)
333 1d14ffa9 bellard
{
334 1ea879e5 malc
    memset (buf, 0, len * sizeof (struct st_sample));
335 1d14ffa9 bellard
}