11d14ffa9Sbellard /*
21d14ffa9Sbellard * QEMU Mixing engine
31d14ffa9Sbellard *
41d14ffa9Sbellard * Copyright (c) 2004-2005 Vassili Karpov (malc)
51d14ffa9Sbellard * Copyright (c) 1998 Fabrice Bellard
61d14ffa9Sbellard *
71d14ffa9Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy
81d14ffa9Sbellard * of this software and associated documentation files (the "Software"), to deal
91d14ffa9Sbellard * in the Software without restriction, including without limitation the rights
101d14ffa9Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
111d14ffa9Sbellard * copies of the Software, and to permit persons to whom the Software is
121d14ffa9Sbellard * furnished to do so, subject to the following conditions:
131d14ffa9Sbellard *
141d14ffa9Sbellard * The above copyright notice and this permission notice shall be included in
151d14ffa9Sbellard * all copies or substantial portions of the Software.
161d14ffa9Sbellard *
171d14ffa9Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
181d14ffa9Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191d14ffa9Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
201d14ffa9Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
211d14ffa9Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
221d14ffa9Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
231d14ffa9Sbellard * THE SOFTWARE.
241d14ffa9Sbellard */
251d14ffa9Sbellard
261d14ffa9Sbellard /*
271d14ffa9Sbellard * Processed signed long samples from ibuf to obuf.
281d14ffa9Sbellard * Return number of samples processed.
291d14ffa9Sbellard */
NAME(void * opaque,struct st_sample * ibuf,struct st_sample * obuf,size_t * isamp,size_t * osamp)301ea879e5Smalc void NAME (void *opaque, struct st_sample *ibuf, struct st_sample *obuf,
317520462bSKővágó, Zoltán size_t *isamp, size_t *osamp)
321d14ffa9Sbellard {
33c0fe3827Sbellard struct rate *rate = opaque;
341ea879e5Smalc struct st_sample *istart, *iend;
351ea879e5Smalc struct st_sample *ostart, *oend;
361ea879e5Smalc struct st_sample ilast, icur, out;
371d14ffa9Sbellard #ifdef FLOAT_MIXENG
381ea879e5Smalc mixeng_real t;
391d14ffa9Sbellard #else
401d14ffa9Sbellard int64_t t;
411d14ffa9Sbellard #endif
421d14ffa9Sbellard
431d14ffa9Sbellard istart = ibuf;
441d14ffa9Sbellard iend = ibuf + *isamp;
451d14ffa9Sbellard
461d14ffa9Sbellard ostart = obuf;
471d14ffa9Sbellard oend = obuf + *osamp;
481d14ffa9Sbellard
491d14ffa9Sbellard if (rate->opos_inc == (1ULL + UINT_MAX)) {
501d14ffa9Sbellard int i, n = *isamp > *osamp ? *osamp : *isamp;
511d14ffa9Sbellard for (i = 0; i < n; i++) {
526c270db7Sbellard OP (obuf[i].l, ibuf[i].l);
531d14ffa9Sbellard OP (obuf[i].r, ibuf[i].r);
541d14ffa9Sbellard }
551d14ffa9Sbellard *isamp = n;
561d14ffa9Sbellard *osamp = n;
571d14ffa9Sbellard return;
581d14ffa9Sbellard }
591d14ffa9Sbellard
60*8933882dSVolker Rümelin /* without input samples, there's nothing to do */
611d14ffa9Sbellard if (ibuf >= iend) {
62*8933882dSVolker Rümelin *osamp = 0;
63*8933882dSVolker Rümelin return;
641d14ffa9Sbellard }
651d14ffa9Sbellard
66*8933882dSVolker Rümelin ilast = rate->ilast;
671d14ffa9Sbellard
68*8933882dSVolker Rümelin while (true) {
69*8933882dSVolker Rümelin
70*8933882dSVolker Rümelin /* read as many input samples so that ipos > opos */
711d14ffa9Sbellard while (rate->ipos <= (rate->opos >> 32)) {
721d14ffa9Sbellard ilast = *ibuf++;
731d14ffa9Sbellard rate->ipos++;
74facd0e97SPeng Hao
751d14ffa9Sbellard /* See if we finished the input buffer yet */
761d14ffa9Sbellard if (ibuf >= iend) {
771d14ffa9Sbellard goto the_end;
781d14ffa9Sbellard }
791d14ffa9Sbellard }
801d14ffa9Sbellard
81*8933882dSVolker Rümelin /* make sure that the next output sample can be written */
82*8933882dSVolker Rümelin if (obuf >= oend) {
83*8933882dSVolker Rümelin break;
84*8933882dSVolker Rümelin }
85*8933882dSVolker Rümelin
861d14ffa9Sbellard icur = *ibuf;
871d14ffa9Sbellard
88b6d93282SVolker Rümelin /* wrap ipos and opos around long before they overflow */
89b6d93282SVolker Rümelin if (rate->ipos >= 0x10001) {
90b6d93282SVolker Rümelin rate->ipos = 1;
91b6d93282SVolker Rümelin rate->opos &= 0xffffffff;
92b6d93282SVolker Rümelin }
93b6d93282SVolker Rümelin
941d14ffa9Sbellard /* interpolate */
951d14ffa9Sbellard #ifdef FLOAT_MIXENG
961d14ffa9Sbellard #ifdef RECIPROCAL
971d14ffa9Sbellard t = (rate->opos & UINT_MAX) * (1.f / UINT_MAX);
981d14ffa9Sbellard #else
991ea879e5Smalc t = (rate->opos & UINT_MAX) / (mixeng_real) UINT_MAX;
1001d14ffa9Sbellard #endif
1011d14ffa9Sbellard out.l = (ilast.l * (1.0 - t)) + icur.l * t;
1021d14ffa9Sbellard out.r = (ilast.r * (1.0 - t)) + icur.r * t;
1031d14ffa9Sbellard #else
1041d14ffa9Sbellard t = rate->opos & 0xffffffff;
1051d14ffa9Sbellard out.l = (ilast.l * ((int64_t) UINT_MAX - t) + icur.l * t) >> 32;
1061d14ffa9Sbellard out.r = (ilast.r * ((int64_t) UINT_MAX - t) + icur.r * t) >> 32;
1071d14ffa9Sbellard #endif
1081d14ffa9Sbellard
1091d14ffa9Sbellard /* output sample & increment position */
1101d14ffa9Sbellard OP (obuf->l, out.l);
1111d14ffa9Sbellard OP (obuf->r, out.r);
1121d14ffa9Sbellard obuf += 1;
1131d14ffa9Sbellard rate->opos += rate->opos_inc;
1141d14ffa9Sbellard }
1151d14ffa9Sbellard
1161d14ffa9Sbellard the_end:
1171d14ffa9Sbellard *isamp = ibuf - istart;
1181d14ffa9Sbellard *osamp = obuf - ostart;
1191d14ffa9Sbellard rate->ilast = ilast;
1201d14ffa9Sbellard }
1211d14ffa9Sbellard
1221d14ffa9Sbellard #undef NAME
1231d14ffa9Sbellard #undef OP
124