2003-07-24 17:46:38 +00:00
|
|
|
/*
|
|
|
|
* August 21, 1998
|
|
|
|
* Copyright 1998 Fabrice Bellard.
|
|
|
|
*
|
|
|
|
* [Rewrote completly the code of Lance Norskog And Sundry
|
|
|
|
* Contributors with a more efficient algorithm.]
|
|
|
|
*
|
|
|
|
* This source code is freely redistributable and may be used for
|
|
|
|
* any purpose. This copyright notice must be maintained.
|
|
|
|
* Lance Norskog And Sundry Contributors are not responsible for
|
|
|
|
* the consequences of using this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sound Tools rate change effect file.
|
|
|
|
*/
|
|
|
|
|
2003-07-29 01:35:37 +00:00
|
|
|
#include "stdafx.h"
|
2003-07-24 17:46:38 +00:00
|
|
|
#include "rate.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2003-07-31 19:08:57 +00:00
|
|
|
|
2003-07-24 17:46:38 +00:00
|
|
|
/*
|
|
|
|
* Linear Interpolation.
|
|
|
|
*
|
|
|
|
* The use of fractional increment allows us to use no buffer. It
|
|
|
|
* avoid the problems at the end of the buffer we had with the old
|
|
|
|
* method which stored a possibly big buffer of size
|
|
|
|
* lcm(in_rate,out_rate).
|
|
|
|
*
|
|
|
|
* Limited to 16 bit samples and sampling frequency <= 65535 Hz. If
|
|
|
|
* the input & output frequencies are equal, a delay of one sample is
|
|
|
|
* introduced. Limited to processing 32-bit count worth of samples.
|
|
|
|
*
|
|
|
|
* 1 << FRAC_BITS evaluating to zero in several places. Changed with
|
|
|
|
* an (unsigned long) cast to make it safe. MarkMLl 2/1/99
|
|
|
|
*
|
|
|
|
* Replaced all uses of floating point arithmetic by fixed point
|
|
|
|
* calculations (Max Horn 2003-07-18).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define FRAC_BITS 16
|
|
|
|
|
|
|
|
/* Private data */
|
|
|
|
|
|
|
|
typedef struct ratestuff
|
|
|
|
{
|
2003-07-31 19:08:57 +00:00
|
|
|
/** fractional position of the output stream in input stream unit */
|
|
|
|
unsigned long opos, opos_frac;
|
2003-07-24 17:46:38 +00:00
|
|
|
|
2003-07-31 19:08:57 +00:00
|
|
|
/** fractional position increment in the output stream */
|
|
|
|
unsigned long opos_inc, opos_inc_frac;
|
2003-07-24 17:46:38 +00:00
|
|
|
|
2003-07-31 19:08:57 +00:00
|
|
|
/** position in the input stream (integer) */
|
|
|
|
unsigned long ipos;
|
2003-07-24 17:46:38 +00:00
|
|
|
|
2003-07-31 19:08:57 +00:00
|
|
|
/** last sample(s) in the input stream (left/right channel) */
|
|
|
|
st_sample_t ilast[2];
|
|
|
|
/** current sample(s) in the input stream (left/right channel) */
|
|
|
|
st_sample_t icur[2];
|
2003-07-24 17:46:38 +00:00
|
|
|
} *rate_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare processing.
|
|
|
|
*/
|
|
|
|
int st_rate_start(eff_t effp, st_rate_t inrate, st_rate_t outrate)
|
|
|
|
{
|
|
|
|
rate_t rate = (rate_t) effp->priv;
|
|
|
|
unsigned long incr;
|
|
|
|
|
|
|
|
if (inrate == outrate) {
|
|
|
|
st_fail("Input and Output rates must be different to use rate effect");
|
|
|
|
return (ST_EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inrate >= 65536 || outrate >= 65536) {
|
|
|
|
st_fail("rate effect can only handle rates < 65536");
|
|
|
|
return (ST_EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
rate->opos_frac = 0;
|
|
|
|
rate->opos = 0;
|
|
|
|
|
|
|
|
/* increment */
|
|
|
|
incr = (inrate << FRAC_BITS) / outrate;
|
|
|
|
|
|
|
|
rate->opos_inc_frac = incr & ((1UL << FRAC_BITS) - 1);
|
|
|
|
rate->opos_inc = incr >> FRAC_BITS;
|
|
|
|
|
|
|
|
rate->ipos = 0;
|
|
|
|
|
2003-07-31 19:08:57 +00:00
|
|
|
rate->ilast[0] = rate->ilast[1] = 0;
|
|
|
|
rate->icur[0] = rate->icur[1] = 0;
|
|
|
|
|
2003-07-24 17:46:38 +00:00
|
|
|
return (ST_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Processed signed long samples from ibuf to obuf.
|
|
|
|
* Return number of samples processed.
|
|
|
|
*/
|
2003-07-28 11:13:01 +00:00
|
|
|
template<bool stereo>
|
2003-07-24 21:26:00 +00:00
|
|
|
int st_rate_flow(eff_t effp, AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp, st_volume_t vol)
|
2003-07-24 17:46:38 +00:00
|
|
|
{
|
|
|
|
rate_t rate = (rate_t) effp->priv;
|
|
|
|
st_sample_t *ostart, *oend;
|
2003-07-28 01:13:31 +00:00
|
|
|
st_sample_t ilast[2], icur[2], out;
|
2003-07-24 17:46:38 +00:00
|
|
|
unsigned long tmp;
|
|
|
|
|
2003-07-28 11:13:01 +00:00
|
|
|
ilast[0] = rate->ilast[0];
|
2003-07-31 19:08:57 +00:00
|
|
|
icur[0] = rate->icur[0];
|
|
|
|
if (stereo) {
|
2003-07-28 11:13:01 +00:00
|
|
|
ilast[1] = rate->ilast[1];
|
2003-07-31 19:08:57 +00:00
|
|
|
icur[1] = rate->icur[1];
|
|
|
|
}
|
2003-07-24 17:46:38 +00:00
|
|
|
|
|
|
|
ostart = obuf;
|
2003-07-28 01:50:46 +00:00
|
|
|
oend = obuf + *osamp * 2;
|
2003-07-24 17:46:38 +00:00
|
|
|
|
2003-07-28 11:13:01 +00:00
|
|
|
// If the input position exceeds the output position, then we aborted the
|
|
|
|
// previous conversion run because the output buffer was full. Resume!
|
|
|
|
if (rate->ipos > rate->opos)
|
|
|
|
goto resume;
|
|
|
|
|
2003-07-24 17:46:38 +00:00
|
|
|
while (obuf < oend && !input.eof()) {
|
|
|
|
|
2003-07-28 01:13:31 +00:00
|
|
|
/* read enough input samples so that ipos > opos */
|
2003-07-24 17:46:38 +00:00
|
|
|
while (rate->ipos <= rate->opos) {
|
2003-07-28 11:13:01 +00:00
|
|
|
ilast[0] = input.read();
|
|
|
|
if (stereo)
|
|
|
|
ilast[1] = input.read();
|
2003-07-24 17:46:38 +00:00
|
|
|
rate->ipos++;
|
|
|
|
/* See if we finished the input buffer yet */
|
|
|
|
|
|
|
|
if (input.eof())
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
|
2003-07-28 01:13:31 +00:00
|
|
|
// read the input sample(s)
|
|
|
|
icur[0] = input.read();
|
2003-07-28 11:13:01 +00:00
|
|
|
if (stereo)
|
2003-07-28 01:13:31 +00:00
|
|
|
icur[1] = input.read();
|
2003-07-24 17:46:38 +00:00
|
|
|
|
2003-07-28 11:13:01 +00:00
|
|
|
resume:
|
|
|
|
// Loop as long as the outpos trails behind, and as long as there is
|
|
|
|
// still space in the output buffer.
|
2003-08-02 11:38:23 +00:00
|
|
|
while (rate->ipos > rate->opos) {
|
2003-07-28 11:13:01 +00:00
|
|
|
|
|
|
|
// interpolate
|
2003-07-30 09:26:54 +00:00
|
|
|
out = (st_sample_t) (ilast[0] + (((icur[0] - ilast[0]) * rate->opos_frac + (1UL << (FRAC_BITS-1))) >> FRAC_BITS));
|
2003-07-28 11:13:01 +00:00
|
|
|
// adjust volume
|
|
|
|
out = out * vol / 256;
|
|
|
|
|
|
|
|
// output left channel sample
|
|
|
|
clampedAdd(*obuf++, out);
|
|
|
|
|
|
|
|
if (stereo) {
|
2003-07-28 01:13:31 +00:00
|
|
|
// interpolate
|
2003-07-30 09:26:54 +00:00
|
|
|
out = (st_sample_t) (ilast[1] + (((icur[1] - ilast[1]) * rate->opos_frac + (1UL << (FRAC_BITS-1))) >> FRAC_BITS));
|
2003-07-28 01:13:31 +00:00
|
|
|
// adjust volume
|
|
|
|
out = out * vol / 256;
|
|
|
|
}
|
|
|
|
|
2003-07-28 11:13:01 +00:00
|
|
|
// output right channel sample
|
|
|
|
clampedAdd(*obuf++, out);
|
2003-07-28 01:13:31 +00:00
|
|
|
|
|
|
|
// Increment output position
|
|
|
|
tmp = rate->opos_frac + rate->opos_inc_frac;
|
|
|
|
rate->opos = rate->opos + rate->opos_inc + (tmp >> FRAC_BITS);
|
|
|
|
rate->opos_frac = tmp & ((1UL << FRAC_BITS) - 1);
|
2003-08-02 11:38:23 +00:00
|
|
|
|
2003-08-02 11:42:53 +00:00
|
|
|
if (obuf >= oend)
|
2003-08-02 11:38:23 +00:00
|
|
|
goto the_end;
|
2003-07-28 01:13:31 +00:00
|
|
|
}
|
2003-07-24 17:46:38 +00:00
|
|
|
|
2003-07-28 01:13:31 +00:00
|
|
|
// Increment input position again (for the sample we read now)
|
|
|
|
rate->ipos++;
|
2003-07-28 11:13:01 +00:00
|
|
|
ilast[0] = icur[0];
|
|
|
|
if (stereo)
|
|
|
|
ilast[1] = icur[1];
|
2003-07-24 17:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
the_end:
|
2003-07-28 01:50:46 +00:00
|
|
|
*osamp = (obuf - ostart) / 2;
|
2003-08-02 11:38:23 +00:00
|
|
|
rate->ilast[0] = ilast[0];
|
|
|
|
rate->icur[0] = icur[0];
|
|
|
|
if (stereo) {
|
|
|
|
rate->ilast[1] = ilast[1];
|
|
|
|
rate->icur[1] = icur[1];
|
2003-07-31 19:08:57 +00:00
|
|
|
}
|
2003-07-28 01:13:31 +00:00
|
|
|
return (ST_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
LinearRateConverter::LinearRateConverter(st_rate_t inrate, st_rate_t outrate) {
|
|
|
|
st_rate_start(&effp, inrate, outrate);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LinearRateConverter::flow(AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp, st_volume_t vol) {
|
|
|
|
if (input.isStereo())
|
2003-07-28 11:13:01 +00:00
|
|
|
return st_rate_flow<true>(&effp, input, obuf, osamp, vol);
|
2003-07-28 01:13:31 +00:00
|
|
|
else
|
2003-07-28 11:13:01 +00:00
|
|
|
return st_rate_flow<false>(&effp, input, obuf, osamp, vol);
|
2003-07-28 01:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LinearRateConverter::drain(st_sample_t *obuf, st_size_t *osamp, st_volume_t vol) {
|
2003-07-24 17:46:38 +00:00
|
|
|
return (ST_SUCCESS);
|
|
|
|
}
|