4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 * Copyright (c) 1998 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/bswap.h"
27 #include "qemu/error-report.h"
30 #define AUDIO_CAP "mixeng"
31 #include "audio_int.h"
34 #define ENDIAN_CONVERSION natural
35 #define ENDIAN_CONVERT(v) (v)
40 #define IN_MIN SCHAR_MIN
41 #define IN_MAX SCHAR_MAX
44 #include "mixeng_template.h"
56 #define IN_MAX UCHAR_MAX
58 #include "mixeng_template.h"
66 #undef ENDIAN_CONVERSION
71 #define IN_MIN SHRT_MIN
72 #define IN_MAX SHRT_MAX
75 #define ENDIAN_CONVERSION natural
76 #define ENDIAN_CONVERT(v) (v)
77 #include "mixeng_template.h"
79 #undef ENDIAN_CONVERSION
80 #define ENDIAN_CONVERSION swap
81 #define ENDIAN_CONVERT(v) bswap16 (v)
82 #include "mixeng_template.h"
84 #undef ENDIAN_CONVERSION
96 #define IN_MAX USHRT_MAX
98 #define ENDIAN_CONVERSION natural
99 #define ENDIAN_CONVERT(v) (v)
100 #include "mixeng_template.h"
101 #undef ENDIAN_CONVERT
102 #undef ENDIAN_CONVERSION
103 #define ENDIAN_CONVERSION swap
104 #define ENDIAN_CONVERT(v) bswap16 (v)
105 #include "mixeng_template.h"
106 #undef ENDIAN_CONVERT
107 #undef ENDIAN_CONVERSION
117 #define IN_MIN INT32_MIN
118 #define IN_MAX INT32_MAX
121 #define ENDIAN_CONVERSION natural
122 #define ENDIAN_CONVERT(v) (v)
123 #include "mixeng_template.h"
124 #undef ENDIAN_CONVERT
125 #undef ENDIAN_CONVERSION
126 #define ENDIAN_CONVERSION swap
127 #define ENDIAN_CONVERT(v) bswap32 (v)
128 #include "mixeng_template.h"
129 #undef ENDIAN_CONVERT
130 #undef ENDIAN_CONVERSION
138 /* Unsigned 32 bit */
142 #define IN_MAX UINT32_MAX
144 #define ENDIAN_CONVERSION natural
145 #define ENDIAN_CONVERT(v) (v)
146 #include "mixeng_template.h"
147 #undef ENDIAN_CONVERT
148 #undef ENDIAN_CONVERSION
149 #define ENDIAN_CONVERSION swap
150 #define ENDIAN_CONVERT(v) bswap32 (v)
151 #include "mixeng_template.h"
152 #undef ENDIAN_CONVERT
153 #undef ENDIAN_CONVERSION
160 t_sample
*mixeng_conv
[2][2][2][3] = {
164 conv_natural_uint8_t_to_mono
,
165 conv_natural_uint16_t_to_mono
,
166 conv_natural_uint32_t_to_mono
169 conv_natural_uint8_t_to_mono
,
170 conv_swap_uint16_t_to_mono
,
171 conv_swap_uint32_t_to_mono
,
176 conv_natural_int8_t_to_mono
,
177 conv_natural_int16_t_to_mono
,
178 conv_natural_int32_t_to_mono
181 conv_natural_int8_t_to_mono
,
182 conv_swap_int16_t_to_mono
,
183 conv_swap_int32_t_to_mono
190 conv_natural_uint8_t_to_stereo
,
191 conv_natural_uint16_t_to_stereo
,
192 conv_natural_uint32_t_to_stereo
195 conv_natural_uint8_t_to_stereo
,
196 conv_swap_uint16_t_to_stereo
,
197 conv_swap_uint32_t_to_stereo
202 conv_natural_int8_t_to_stereo
,
203 conv_natural_int16_t_to_stereo
,
204 conv_natural_int32_t_to_stereo
207 conv_natural_int8_t_to_stereo
,
208 conv_swap_int16_t_to_stereo
,
209 conv_swap_int32_t_to_stereo
,
215 f_sample
*mixeng_clip
[2][2][2][3] = {
219 clip_natural_uint8_t_from_mono
,
220 clip_natural_uint16_t_from_mono
,
221 clip_natural_uint32_t_from_mono
224 clip_natural_uint8_t_from_mono
,
225 clip_swap_uint16_t_from_mono
,
226 clip_swap_uint32_t_from_mono
231 clip_natural_int8_t_from_mono
,
232 clip_natural_int16_t_from_mono
,
233 clip_natural_int32_t_from_mono
236 clip_natural_int8_t_from_mono
,
237 clip_swap_int16_t_from_mono
,
238 clip_swap_int32_t_from_mono
245 clip_natural_uint8_t_from_stereo
,
246 clip_natural_uint16_t_from_stereo
,
247 clip_natural_uint32_t_from_stereo
250 clip_natural_uint8_t_from_stereo
,
251 clip_swap_uint16_t_from_stereo
,
252 clip_swap_uint32_t_from_stereo
257 clip_natural_int8_t_from_stereo
,
258 clip_natural_int16_t_from_stereo
,
259 clip_natural_int32_t_from_stereo
262 clip_natural_int8_t_from_stereo
,
263 clip_swap_int16_t_from_stereo
,
264 clip_swap_int32_t_from_stereo
271 #define CONV_NATURAL_FLOAT(x) (x)
272 #define CLIP_NATURAL_FLOAT(x) (x)
274 /* macros to map [-1.f, 1.f] <-> [INT32_MIN, INT32_MAX + 1] */
275 static const float float_scale
= (int64_t)INT32_MAX
+ 1;
276 #define CONV_NATURAL_FLOAT(x) ((x) * float_scale)
279 static const float float_scale_reciprocal
= 1.f
/ ((int64_t)INT32_MAX
+ 1);
280 #define CLIP_NATURAL_FLOAT(x) ((x) * float_scale_reciprocal)
282 #define CLIP_NATURAL_FLOAT(x) ((x) / float_scale)
286 static void conv_natural_float_to_mono(struct st_sample
*dst
, const void *src
,
289 float *in
= (float *)src
;
292 dst
->r
= dst
->l
= CONV_NATURAL_FLOAT(*in
++);
297 static void conv_natural_float_to_stereo(struct st_sample
*dst
, const void *src
,
300 float *in
= (float *)src
;
303 dst
->l
= CONV_NATURAL_FLOAT(*in
++);
304 dst
->r
= CONV_NATURAL_FLOAT(*in
++);
309 t_sample
*mixeng_conv_float
[2] = {
310 conv_natural_float_to_mono
,
311 conv_natural_float_to_stereo
,
314 static void clip_natural_float_from_mono(void *dst
, const struct st_sample
*src
,
317 float *out
= (float *)dst
;
320 *out
++ = CLIP_NATURAL_FLOAT(src
->l
+ src
->r
);
325 static void clip_natural_float_from_stereo(
326 void *dst
, const struct st_sample
*src
, int samples
)
328 float *out
= (float *)dst
;
331 *out
++ = CLIP_NATURAL_FLOAT(src
->l
);
332 *out
++ = CLIP_NATURAL_FLOAT(src
->r
);
337 f_sample
*mixeng_clip_float
[2] = {
338 clip_natural_float_from_mono
,
339 clip_natural_float_from_stereo
,
342 void audio_sample_to_uint64(const void *samples
, int pos
,
343 uint64_t *left
, uint64_t *right
)
347 "Coreaudio and floating point samples are not supported by replay yet");
350 const struct st_sample
*sample
= samples
;
357 void audio_sample_from_uint64(void *samples
, int pos
,
358 uint64_t left
, uint64_t right
)
362 "Coreaudio and floating point samples are not supported by replay yet");
365 struct st_sample
*sample
= samples
;
374 * Copyright 1998 Fabrice Bellard.
376 * [Rewrote completely the code of Lance Norskog And Sundry
377 * Contributors with a more efficient algorithm.]
379 * This source code is freely redistributable and may be used for
380 * any purpose. This copyright notice must be maintained.
381 * Lance Norskog And Sundry Contributors are not responsible for
382 * the consequences of using this software.
386 * Sound Tools rate change effect file.
389 * Linear Interpolation.
391 * The use of fractional increment allows us to use no buffer. It
392 * avoid the problems at the end of the buffer we had with the old
393 * method which stored a possibly big buffer of size
394 * lcm(in_rate,out_rate).
396 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
397 * the input & output frequencies are equal, a delay of one sample is
398 * introduced. Limited to processing 32-bit count worth of samples.
400 * 1 << FRAC_BITS evaluating to zero in several places. Changed with
401 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
408 uint32_t ipos
; /* position in the input stream (integer) */
409 struct st_sample ilast
; /* last sample in the input stream */
413 * Prepare processing.
415 void *st_rate_start (int inrate
, int outrate
)
417 struct rate
*rate
= g_new0(struct rate
, 1);
422 rate
->opos_inc
= ((uint64_t) inrate
<< 32) / outrate
;
430 #define NAME st_rate_flow_mix
431 #define OP(a, b) a += b
432 #include "rate_template.h"
434 #define NAME st_rate_flow
435 #define OP(a, b) a = b
436 #include "rate_template.h"
438 void st_rate_stop (void *opaque
)
444 * st_rate_frames_out() - returns the number of frames the resampling code
445 * generates from frames_in frames
447 * @opaque: pointer to struct rate
448 * @frames_in: number of frames
450 * When upsampling, there may be more than one correct result. In this case,
451 * the function returns the maximum number of output frames the resampling
454 uint32_t st_rate_frames_out(void *opaque
, uint32_t frames_in
)
456 struct rate
*rate
= opaque
;
457 uint64_t opos_end
, opos_delta
;
461 if (rate
->opos_inc
== 1ULL << 32) {
465 /* no output frame without at least one input frame */
470 /* last frame read was at rate->ipos - 1 */
471 ipos_end
= rate
->ipos
- 1 + frames_in
;
472 opos_end
= (uint64_t)ipos_end
<< 32;
474 /* last frame written was at rate->opos - rate->opos_inc */
475 if (opos_end
+ rate
->opos_inc
<= rate
->opos
) {
478 opos_delta
= opos_end
- rate
->opos
+ rate
->opos_inc
;
479 frames_out
= opos_delta
/ rate
->opos_inc
;
481 return opos_delta
% rate
->opos_inc
? frames_out
: frames_out
- 1;
485 * st_rate_frames_in() - returns the number of frames needed to
486 * get frames_out frames after resampling
488 * @opaque: pointer to struct rate
489 * @frames_out: number of frames
491 * When downsampling, there may be more than one correct result. In this
492 * case, the function returns the maximum number of input frames needed.
494 uint32_t st_rate_frames_in(void *opaque
, uint32_t frames_out
)
496 struct rate
*rate
= opaque
;
497 uint64_t opos_start
, opos_end
;
498 uint32_t ipos_start
, ipos_end
;
500 if (rate
->opos_inc
== 1ULL << 32) {
505 opos_start
= rate
->opos
;
506 ipos_start
= rate
->ipos
;
510 /* add offset = ceil(opos_inc) to opos and ipos to avoid an underflow */
511 offset
= (rate
->opos_inc
+ (1ULL << 32) - 1) & ~((1ULL << 32) - 1);
512 opos_start
= rate
->opos
+ offset
;
513 ipos_start
= rate
->ipos
+ (offset
>> 32);
515 /* last frame written was at opos_start - rate->opos_inc */
516 opos_end
= opos_start
- rate
->opos_inc
+ rate
->opos_inc
* frames_out
;
517 ipos_end
= (opos_end
>> 32) + 1;
519 /* last frame read was at ipos_start - 1 */
520 return ipos_end
+ 1 > ipos_start
? ipos_end
+ 1 - ipos_start
: 0;
523 void mixeng_clear (struct st_sample
*buf
, int len
)
525 memset (buf
, 0, len
* sizeof (struct st_sample
));
528 void mixeng_volume (struct st_sample
*buf
, int len
, struct mixeng_volume
*vol
)
531 mixeng_clear (buf
, len
);
537 buf
->l
= buf
->l
* vol
->l
;
538 buf
->r
= buf
->r
* vol
->r
;
540 buf
->l
= (buf
->l
* vol
->l
) >> 32;
541 buf
->r
= (buf
->r
* vol
->r
) >> 32;