4 * Copyright (c) 2004 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
27 #include "audio/mixeng.h"
30 #define IN_MIN CHAR_MIN
31 #define IN_MAX CHAR_MAX
33 #include "mixeng_template.h"
41 #define IN_MAX UCHAR_MAX
42 #include "mixeng_template.h"
48 #define IN_MIN SHRT_MIN
49 #define IN_MAX SHRT_MAX
51 #include "mixeng_template.h"
59 #define IN_MAX USHRT_MAX
60 #include "mixeng_template.h"
65 t_sample *mixeng_conv[2][2][2] = {
78 conv_uint8_t_to_stereo,
79 conv_uint16_t_to_stereo
82 conv_int8_t_to_stereo,
83 conv_int16_t_to_stereo
88 f_sample *mixeng_clip[2][2][2] = {
91 clip_uint8_t_from_mono,
92 clip_uint16_t_from_mono
95 clip_int8_t_from_mono,
96 clip_int16_t_from_mono
101 clip_uint8_t_from_stereo,
102 clip_uint16_t_from_stereo
105 clip_int8_t_from_stereo,
106 clip_int16_t_from_stereo
113 * Copyright 1998 Fabrice Bellard.
115 * [Rewrote completly the code of Lance Norskog And Sundry
116 * Contributors with a more efficient algorithm.]
118 * This source code is freely redistributable and may be used for
119 * any purpose. This copyright notice must be maintained.
120 * Lance Norskog And Sundry Contributors are not responsible for
121 * the consequences of using this software.
125 * Sound Tools rate change effect file.
128 * Linear Interpolation.
130 * The use of fractional increment allows us to use no buffer. It
131 * avoid the problems at the end of the buffer we had with the old
132 * method which stored a possibly big buffer of size
133 * lcm(in_rate,out_rate).
135 * Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
136 * the input & output frequencies are equal, a delay of one sample is
137 * introduced. Limited to processing 32-bit count worth of samples.
139 * 1 << FRAC_BITS evaluating to zero in several places. Changed with
140 * an (unsigned long) cast to make it safe. MarkMLl 2/1/99
144 typedef struct ratestuff {
147 uint32_t ipos; /* position in the input stream (integer) */
148 st_sample_t ilast; /* last sample in the input stream */
152 * Prepare processing.
154 void *st_rate_start (int inrate, int outrate)
156 rate_t rate = (rate_t) qemu_mallocz (sizeof (struct ratestuff));
162 if (inrate == outrate) {
163 // exit (EXIT_FAILURE);
166 if (inrate >= 65535 || outrate >= 65535) {
167 // exit (EXIT_FAILURE);
173 rate->opos_inc = (inrate * ((int64_t) UINT_MAX)) / outrate;
182 * Processed signed long samples from ibuf to obuf.
183 * Return number of samples processed.
185 void st_rate_flow (void *opaque, st_sample_t *ibuf, st_sample_t *obuf,
186 int *isamp, int *osamp)
188 rate_t rate = (rate_t) opaque;
189 st_sample_t *istart, *iend;
190 st_sample_t *ostart, *oend;
191 st_sample_t ilast, icur, out;
197 iend = ibuf + *isamp;
200 oend = obuf + *osamp;
202 if (rate->opos_inc == 1ULL << 32) {
203 int i, n = *isamp > *osamp ? *osamp : *isamp;
204 for (i = 0; i < n; i++) {
205 obuf[i].l += ibuf[i].r;
206 obuf[i].r += ibuf[i].r;
213 while (obuf < oend) {
215 /* Safety catch to make sure we have input samples. */
219 /* read as many input samples so that ipos > opos */
221 while (rate->ipos <= (rate->opos >> 32)) {
224 /* See if we finished the input buffer yet */
225 if (ibuf >= iend) goto the_end;
231 t = rate->opos & 0xffffffff;
232 out.l = (ilast.l * (INT_MAX - t) + icur.l * t) / INT_MAX;
233 out.r = (ilast.r * (INT_MAX - t) + icur.r * t) / INT_MAX;
235 /* output sample & increment position */
243 rate->opos += rate->opos_inc;
247 *isamp = ibuf - istart;
248 *osamp = obuf - ostart;
252 void st_rate_stop (void *opaque)