root / audio / mixeng.c @ d796321b
History | View | Annotate | Download (6.8 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 | 85571bc7 | bellard | #include "vl.h" |
26 | 85571bc7 | bellard | |
27 | 1d14ffa9 | bellard | #define AUDIO_CAP "mixeng" |
28 | 1d14ffa9 | bellard | #include "audio_int.h" |
29 | 1d14ffa9 | bellard | |
30 | 1d14ffa9 | bellard | #define NOVOL
|
31 | 1d14ffa9 | bellard | |
32 | 1d14ffa9 | bellard | /* 8 bit */
|
33 | 1d14ffa9 | bellard | #define ENDIAN_CONVERSION natural
|
34 | 1d14ffa9 | bellard | #define ENDIAN_CONVERT(v) (v)
|
35 | 1d14ffa9 | bellard | |
36 | 1d14ffa9 | bellard | /* Signed 8 bit */
|
37 | 85571bc7 | bellard | #define IN_T int8_t
|
38 | 1d14ffa9 | bellard | #define IN_MIN SCHAR_MIN
|
39 | 1d14ffa9 | bellard | #define IN_MAX SCHAR_MAX
|
40 | 85571bc7 | bellard | #define SIGNED
|
41 | 1d14ffa9 | bellard | #define SHIFT 8 |
42 | 85571bc7 | bellard | #include "mixeng_template.h" |
43 | 85571bc7 | bellard | #undef SIGNED
|
44 | 85571bc7 | bellard | #undef IN_MAX
|
45 | 85571bc7 | bellard | #undef IN_MIN
|
46 | 85571bc7 | bellard | #undef IN_T
|
47 | 1d14ffa9 | bellard | #undef SHIFT
|
48 | 85571bc7 | bellard | |
49 | 1d14ffa9 | bellard | /* Unsigned 8 bit */
|
50 | 85571bc7 | bellard | #define IN_T uint8_t
|
51 | 85571bc7 | bellard | #define IN_MIN 0 |
52 | 85571bc7 | bellard | #define IN_MAX UCHAR_MAX
|
53 | 1d14ffa9 | bellard | #define SHIFT 8 |
54 | 85571bc7 | bellard | #include "mixeng_template.h" |
55 | 85571bc7 | bellard | #undef IN_MAX
|
56 | 85571bc7 | bellard | #undef IN_MIN
|
57 | 85571bc7 | bellard | #undef IN_T
|
58 | 1d14ffa9 | bellard | #undef SHIFT
|
59 | 1d14ffa9 | bellard | |
60 | 1d14ffa9 | bellard | #undef ENDIAN_CONVERT
|
61 | 1d14ffa9 | bellard | #undef ENDIAN_CONVERSION
|
62 | 85571bc7 | bellard | |
63 | 1d14ffa9 | bellard | /* Signed 16 bit */
|
64 | 85571bc7 | bellard | #define IN_T int16_t
|
65 | 85571bc7 | bellard | #define IN_MIN SHRT_MIN
|
66 | 85571bc7 | bellard | #define IN_MAX SHRT_MAX
|
67 | 85571bc7 | bellard | #define SIGNED
|
68 | 1d14ffa9 | bellard | #define SHIFT 16 |
69 | 1d14ffa9 | bellard | #define ENDIAN_CONVERSION natural
|
70 | 1d14ffa9 | bellard | #define ENDIAN_CONVERT(v) (v)
|
71 | 85571bc7 | bellard | #include "mixeng_template.h" |
72 | 1d14ffa9 | bellard | #undef ENDIAN_CONVERT
|
73 | 1d14ffa9 | bellard | #undef ENDIAN_CONVERSION
|
74 | 1d14ffa9 | bellard | #define ENDIAN_CONVERSION swap
|
75 | 1d14ffa9 | bellard | #define ENDIAN_CONVERT(v) bswap16 (v)
|
76 | 1d14ffa9 | bellard | #include "mixeng_template.h" |
77 | 1d14ffa9 | bellard | #undef ENDIAN_CONVERT
|
78 | 1d14ffa9 | bellard | #undef ENDIAN_CONVERSION
|
79 | 85571bc7 | bellard | #undef SIGNED
|
80 | 85571bc7 | bellard | #undef IN_MAX
|
81 | 85571bc7 | bellard | #undef IN_MIN
|
82 | 85571bc7 | bellard | #undef IN_T
|
83 | 1d14ffa9 | bellard | #undef SHIFT
|
84 | 85571bc7 | bellard | |
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 | 1d14ffa9 | bellard | t_sample *mixeng_conv[2][2][2][2] = { |
105 | 85571bc7 | bellard | { |
106 | 85571bc7 | bellard | { |
107 | 1d14ffa9 | bellard | { |
108 | 1d14ffa9 | bellard | conv_natural_uint8_t_to_mono, |
109 | 1d14ffa9 | bellard | conv_natural_uint16_t_to_mono |
110 | 1d14ffa9 | bellard | }, |
111 | 1d14ffa9 | bellard | { |
112 | 1d14ffa9 | bellard | conv_natural_uint8_t_to_mono, |
113 | 1d14ffa9 | bellard | conv_swap_uint16_t_to_mono |
114 | 1d14ffa9 | bellard | } |
115 | 85571bc7 | bellard | }, |
116 | 85571bc7 | bellard | { |
117 | 1d14ffa9 | bellard | { |
118 | 1d14ffa9 | bellard | conv_natural_int8_t_to_mono, |
119 | 1d14ffa9 | bellard | conv_natural_int16_t_to_mono |
120 | 1d14ffa9 | bellard | }, |
121 | 1d14ffa9 | bellard | { |
122 | 1d14ffa9 | bellard | conv_natural_int8_t_to_mono, |
123 | 1d14ffa9 | bellard | conv_swap_int16_t_to_mono |
124 | 1d14ffa9 | bellard | } |
125 | 85571bc7 | bellard | } |
126 | 85571bc7 | bellard | }, |
127 | 85571bc7 | bellard | { |
128 | 85571bc7 | bellard | { |
129 | 1d14ffa9 | bellard | { |
130 | 1d14ffa9 | bellard | conv_natural_uint8_t_to_stereo, |
131 | 1d14ffa9 | bellard | conv_natural_uint16_t_to_stereo |
132 | 1d14ffa9 | bellard | }, |
133 | 1d14ffa9 | bellard | { |
134 | 1d14ffa9 | bellard | conv_natural_uint8_t_to_stereo, |
135 | 1d14ffa9 | bellard | conv_swap_uint16_t_to_stereo |
136 | 1d14ffa9 | bellard | } |
137 | 85571bc7 | bellard | }, |
138 | 85571bc7 | bellard | { |
139 | 1d14ffa9 | bellard | { |
140 | 1d14ffa9 | bellard | conv_natural_int8_t_to_stereo, |
141 | 1d14ffa9 | bellard | conv_natural_int16_t_to_stereo |
142 | 1d14ffa9 | bellard | }, |
143 | 1d14ffa9 | bellard | { |
144 | 1d14ffa9 | bellard | conv_natural_int8_t_to_stereo, |
145 | 1d14ffa9 | bellard | conv_swap_int16_t_to_stereo |
146 | 1d14ffa9 | bellard | } |
147 | 85571bc7 | bellard | } |
148 | 85571bc7 | bellard | } |
149 | 85571bc7 | bellard | }; |
150 | 85571bc7 | bellard | |
151 | 1d14ffa9 | bellard | f_sample *mixeng_clip[2][2][2][2] = { |
152 | 85571bc7 | bellard | { |
153 | 85571bc7 | bellard | { |
154 | 1d14ffa9 | bellard | { |
155 | 1d14ffa9 | bellard | clip_natural_uint8_t_from_mono, |
156 | 1d14ffa9 | bellard | clip_natural_uint16_t_from_mono |
157 | 1d14ffa9 | bellard | }, |
158 | 1d14ffa9 | bellard | { |
159 | 1d14ffa9 | bellard | clip_natural_uint8_t_from_mono, |
160 | 1d14ffa9 | bellard | clip_swap_uint16_t_from_mono |
161 | 1d14ffa9 | bellard | } |
162 | 85571bc7 | bellard | }, |
163 | 85571bc7 | bellard | { |
164 | 1d14ffa9 | bellard | { |
165 | 1d14ffa9 | bellard | clip_natural_int8_t_from_mono, |
166 | 1d14ffa9 | bellard | clip_natural_int16_t_from_mono |
167 | 1d14ffa9 | bellard | }, |
168 | 1d14ffa9 | bellard | { |
169 | 1d14ffa9 | bellard | clip_natural_int8_t_from_mono, |
170 | 1d14ffa9 | bellard | clip_swap_int16_t_from_mono |
171 | 1d14ffa9 | bellard | } |
172 | 85571bc7 | bellard | } |
173 | 85571bc7 | bellard | }, |
174 | 85571bc7 | bellard | { |
175 | 85571bc7 | bellard | { |
176 | 1d14ffa9 | bellard | { |
177 | 1d14ffa9 | bellard | clip_natural_uint8_t_from_stereo, |
178 | 1d14ffa9 | bellard | clip_natural_uint16_t_from_stereo |
179 | 1d14ffa9 | bellard | }, |
180 | 1d14ffa9 | bellard | { |
181 | 1d14ffa9 | bellard | clip_natural_uint8_t_from_stereo, |
182 | 1d14ffa9 | bellard | clip_swap_uint16_t_from_stereo |
183 | 1d14ffa9 | bellard | } |
184 | 85571bc7 | bellard | }, |
185 | 85571bc7 | bellard | { |
186 | 1d14ffa9 | bellard | { |
187 | 1d14ffa9 | bellard | clip_natural_int8_t_from_stereo, |
188 | 1d14ffa9 | bellard | clip_natural_int16_t_from_stereo |
189 | 1d14ffa9 | bellard | }, |
190 | 1d14ffa9 | bellard | { |
191 | 1d14ffa9 | bellard | clip_natural_int8_t_from_stereo, |
192 | 1d14ffa9 | bellard | clip_swap_int16_t_from_stereo |
193 | 1d14ffa9 | bellard | } |
194 | 85571bc7 | bellard | } |
195 | 85571bc7 | bellard | } |
196 | 85571bc7 | bellard | }; |
197 | 85571bc7 | bellard | |
198 | 85571bc7 | bellard | /*
|
199 | 85571bc7 | bellard | * August 21, 1998
|
200 | 85571bc7 | bellard | * Copyright 1998 Fabrice Bellard.
|
201 | 85571bc7 | bellard | *
|
202 | 85571bc7 | bellard | * [Rewrote completly the code of Lance Norskog And Sundry
|
203 | 85571bc7 | bellard | * Contributors with a more efficient algorithm.]
|
204 | 85571bc7 | bellard | *
|
205 | 85571bc7 | bellard | * This source code is freely redistributable and may be used for
|
206 | 1d14ffa9 | bellard | * any purpose. This copyright notice must be maintained.
|
207 | 1d14ffa9 | bellard | * Lance Norskog And Sundry Contributors are not responsible for
|
208 | 1d14ffa9 | bellard | * the consequences of using this software.
|
209 | 85571bc7 | bellard | */
|
210 | 85571bc7 | bellard | |
211 | 85571bc7 | bellard | /*
|
212 | 85571bc7 | bellard | * Sound Tools rate change effect file.
|
213 | 85571bc7 | bellard | */
|
214 | 85571bc7 | bellard | /*
|
215 | 85571bc7 | bellard | * Linear Interpolation.
|
216 | 85571bc7 | bellard | *
|
217 | 85571bc7 | bellard | * The use of fractional increment allows us to use no buffer. It
|
218 | 85571bc7 | bellard | * avoid the problems at the end of the buffer we had with the old
|
219 | 85571bc7 | bellard | * method which stored a possibly big buffer of size
|
220 | 85571bc7 | bellard | * lcm(in_rate,out_rate).
|
221 | 85571bc7 | bellard | *
|
222 | 85571bc7 | bellard | * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
|
223 | 85571bc7 | bellard | * the input & output frequencies are equal, a delay of one sample is
|
224 | 85571bc7 | bellard | * introduced. Limited to processing 32-bit count worth of samples.
|
225 | 85571bc7 | bellard | *
|
226 | 85571bc7 | bellard | * 1 << FRAC_BITS evaluating to zero in several places. Changed with
|
227 | 85571bc7 | bellard | * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
|
228 | 85571bc7 | bellard | */
|
229 | 85571bc7 | bellard | |
230 | 85571bc7 | bellard | /* Private data */
|
231 | c0fe3827 | bellard | struct rate {
|
232 | 85571bc7 | bellard | uint64_t opos; |
233 | 85571bc7 | bellard | uint64_t opos_inc; |
234 | 85571bc7 | bellard | uint32_t ipos; /* position in the input stream (integer) */
|
235 | 85571bc7 | bellard | st_sample_t ilast; /* last sample in the input stream */
|
236 | c0fe3827 | bellard | }; |
237 | 85571bc7 | bellard | |
238 | 85571bc7 | bellard | /*
|
239 | 85571bc7 | bellard | * Prepare processing.
|
240 | 85571bc7 | bellard | */
|
241 | 85571bc7 | bellard | void *st_rate_start (int inrate, int outrate) |
242 | 85571bc7 | bellard | { |
243 | c0fe3827 | bellard | struct rate *rate = audio_calloc (AUDIO_FUNC, 1, sizeof (*rate)); |
244 | 85571bc7 | bellard | |
245 | 85571bc7 | bellard | if (!rate) {
|
246 | e7cad338 | bellard | dolog ("Could not allocate resampler (%zu bytes)\n", sizeof (*rate)); |
247 | 1d14ffa9 | bellard | return NULL; |
248 | 85571bc7 | bellard | } |
249 | 85571bc7 | bellard | |
250 | 85571bc7 | bellard | rate->opos = 0;
|
251 | 85571bc7 | bellard | |
252 | 85571bc7 | bellard | /* increment */
|
253 | 1d14ffa9 | bellard | rate->opos_inc = ((uint64_t) inrate << 32) / outrate;
|
254 | 85571bc7 | bellard | |
255 | 85571bc7 | bellard | rate->ipos = 0;
|
256 | 85571bc7 | bellard | rate->ilast.l = 0;
|
257 | 85571bc7 | bellard | rate->ilast.r = 0;
|
258 | 85571bc7 | bellard | return rate;
|
259 | 85571bc7 | bellard | } |
260 | 85571bc7 | bellard | |
261 | 1d14ffa9 | bellard | #define NAME st_rate_flow_mix
|
262 | 1d14ffa9 | bellard | #define OP(a, b) a += b
|
263 | 1d14ffa9 | bellard | #include "rate_template.h" |
264 | 85571bc7 | bellard | |
265 | 1d14ffa9 | bellard | #define NAME st_rate_flow
|
266 | 1d14ffa9 | bellard | #define OP(a, b) a = b
|
267 | 1d14ffa9 | bellard | #include "rate_template.h" |
268 | 85571bc7 | bellard | |
269 | 85571bc7 | bellard | void st_rate_stop (void *opaque) |
270 | 85571bc7 | bellard | { |
271 | 85571bc7 | bellard | qemu_free (opaque); |
272 | 85571bc7 | bellard | } |
273 | 1d14ffa9 | bellard | |
274 | 1d14ffa9 | bellard | void mixeng_clear (st_sample_t *buf, int len) |
275 | 1d14ffa9 | bellard | { |
276 | 1d14ffa9 | bellard | memset (buf, 0, len * sizeof (st_sample_t)); |
277 | 1d14ffa9 | bellard | } |