michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: /// michael@0: /// Linear interpolation algorithm. michael@0: /// michael@0: /// Author : Copyright (c) Olli Parviainen michael@0: /// Author e-mail : oparviai 'at' iki.fi michael@0: /// SoundTouch WWW: http://www.surina.net/soundtouch michael@0: /// michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // $Id: InterpolateLinear.cpp 180 2014-01-06 19:16:02Z oparviai $ michael@0: // michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // License : michael@0: // michael@0: // SoundTouch audio processing library michael@0: // Copyright (c) Olli Parviainen michael@0: // michael@0: // This library is free software; you can redistribute it and/or michael@0: // modify it under the terms of the GNU Lesser General Public michael@0: // License as published by the Free Software Foundation; either michael@0: // version 2.1 of the License, or (at your option) any later version. michael@0: // michael@0: // This library is distributed in the hope that it will be useful, michael@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: // Lesser General Public License for more details. michael@0: // michael@0: // You should have received a copy of the GNU Lesser General Public michael@0: // License along with this library; if not, write to the Free Software michael@0: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA michael@0: // michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include michael@0: #include michael@0: #include "InterpolateLinear.h" michael@0: michael@0: using namespace soundtouch; michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // InterpolateLinearInteger - integer arithmetic implementation michael@0: // michael@0: michael@0: /// fixed-point interpolation routine precision michael@0: #define SCALE 65536 michael@0: michael@0: michael@0: // Constructor michael@0: InterpolateLinearInteger::InterpolateLinearInteger() : TransposerBase() michael@0: { michael@0: // Notice: use local function calling syntax for sake of clarity, michael@0: // to indicate the fact that C++ constructor can't call virtual functions. michael@0: resetRegisters(); michael@0: setRate(1.0f); michael@0: } michael@0: michael@0: michael@0: void InterpolateLinearInteger::resetRegisters() michael@0: { michael@0: iFract = 0; michael@0: } michael@0: michael@0: michael@0: // Transposes the sample rate of the given samples using linear interpolation. michael@0: // 'Mono' version of the routine. Returns the number of samples returned in michael@0: // the "dest" buffer michael@0: int InterpolateLinearInteger::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) michael@0: { michael@0: int i; michael@0: int srcSampleEnd = srcSamples - 1; michael@0: int srcCount = 0; michael@0: michael@0: i = 0; michael@0: while (srcCount < srcSampleEnd) michael@0: { michael@0: LONG_SAMPLETYPE temp; michael@0: michael@0: assert(iFract < SCALE); michael@0: michael@0: temp = (SCALE - iFract) * src[0] + iFract * src[1]; michael@0: dest[i] = (SAMPLETYPE)(temp / SCALE); michael@0: i++; michael@0: michael@0: iFract += iRate; michael@0: michael@0: int iWhole = iFract / SCALE; michael@0: iFract -= iWhole * SCALE; michael@0: srcCount += iWhole; michael@0: src += iWhole; michael@0: } michael@0: srcSamples = srcCount; michael@0: michael@0: return i; michael@0: } michael@0: michael@0: michael@0: // Transposes the sample rate of the given samples using linear interpolation. michael@0: // 'Stereo' version of the routine. Returns the number of samples returned in michael@0: // the "dest" buffer michael@0: int InterpolateLinearInteger::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) michael@0: { michael@0: int i; michael@0: int srcSampleEnd = srcSamples - 1; michael@0: int srcCount = 0; michael@0: michael@0: i = 0; michael@0: while (srcCount < srcSampleEnd) michael@0: { michael@0: LONG_SAMPLETYPE temp0; michael@0: LONG_SAMPLETYPE temp1; michael@0: michael@0: assert(iFract < SCALE); michael@0: michael@0: temp0 = (SCALE - iFract) * src[0] + iFract * src[2]; michael@0: temp1 = (SCALE - iFract) * src[1] + iFract * src[3]; michael@0: dest[0] = (SAMPLETYPE)(temp0 / SCALE); michael@0: dest[1] = (SAMPLETYPE)(temp1 / SCALE); michael@0: dest += 2; michael@0: i++; michael@0: michael@0: iFract += iRate; michael@0: michael@0: int iWhole = iFract / SCALE; michael@0: iFract -= iWhole * SCALE; michael@0: srcCount += iWhole; michael@0: src += 2*iWhole; michael@0: } michael@0: srcSamples = srcCount; michael@0: michael@0: return i; michael@0: } michael@0: michael@0: michael@0: int InterpolateLinearInteger::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) michael@0: { michael@0: int i; michael@0: int srcSampleEnd = srcSamples - 1; michael@0: int srcCount = 0; michael@0: michael@0: i = 0; michael@0: while (srcCount < srcSampleEnd) michael@0: { michael@0: LONG_SAMPLETYPE temp, vol1; michael@0: michael@0: assert(iFract < SCALE); michael@0: vol1 = (SCALE - iFract); michael@0: for (int c = 0; c < numChannels; c ++) michael@0: { michael@0: temp = vol1 * src[c] + iFract * src[c + numChannels]; michael@0: dest[0] = (SAMPLETYPE)(temp / SCALE); michael@0: dest ++; michael@0: } michael@0: i++; michael@0: michael@0: iFract += iRate; michael@0: michael@0: int iWhole = iFract / SCALE; michael@0: iFract -= iWhole * SCALE; michael@0: srcCount += iWhole; michael@0: src += iWhole * numChannels; michael@0: } michael@0: srcSamples = srcCount; michael@0: michael@0: return i; michael@0: } michael@0: michael@0: michael@0: // Sets new target iRate. Normal iRate = 1.0, smaller values represent slower michael@0: // iRate, larger faster iRates. michael@0: void InterpolateLinearInteger::setRate(float newRate) michael@0: { michael@0: iRate = (int)(newRate * SCALE + 0.5f); michael@0: TransposerBase::setRate(newRate); michael@0: } michael@0: michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // InterpolateLinearFloat - floating point arithmetic implementation michael@0: // michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: michael@0: // Constructor michael@0: InterpolateLinearFloat::InterpolateLinearFloat() : TransposerBase() michael@0: { michael@0: // Notice: use local function calling syntax for sake of clarity, michael@0: // to indicate the fact that C++ constructor can't call virtual functions. michael@0: resetRegisters(); michael@0: setRate(1.0f); michael@0: } michael@0: michael@0: michael@0: void InterpolateLinearFloat::resetRegisters() michael@0: { michael@0: fract = 0; michael@0: } michael@0: michael@0: michael@0: // Transposes the sample rate of the given samples using linear interpolation. michael@0: // 'Mono' version of the routine. Returns the number of samples returned in michael@0: // the "dest" buffer michael@0: int InterpolateLinearFloat::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) michael@0: { michael@0: int i; michael@0: int srcSampleEnd = srcSamples - 1; michael@0: int srcCount = 0; michael@0: michael@0: i = 0; michael@0: while (srcCount < srcSampleEnd) michael@0: { michael@0: double out; michael@0: assert(fract < 1.0); michael@0: michael@0: out = (1.0 - fract) * src[0] + fract * src[1]; michael@0: dest[i] = (SAMPLETYPE)out; michael@0: i ++; michael@0: michael@0: // update position fraction michael@0: fract += rate; michael@0: // update whole positions michael@0: int whole = (int)fract; michael@0: fract -= whole; michael@0: src += whole; michael@0: srcCount += whole; michael@0: } michael@0: srcSamples = srcCount; michael@0: return i; michael@0: } michael@0: michael@0: michael@0: // Transposes the sample rate of the given samples using linear interpolation. michael@0: // 'Mono' version of the routine. Returns the number of samples returned in michael@0: // the "dest" buffer michael@0: int InterpolateLinearFloat::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) michael@0: { michael@0: int i; michael@0: int srcSampleEnd = srcSamples - 1; michael@0: int srcCount = 0; michael@0: michael@0: i = 0; michael@0: while (srcCount < srcSampleEnd) michael@0: { michael@0: double out0, out1; michael@0: assert(fract < 1.0); michael@0: michael@0: out0 = (1.0 - fract) * src[0] + fract * src[2]; michael@0: out1 = (1.0 - fract) * src[1] + fract * src[3]; michael@0: dest[2*i] = (SAMPLETYPE)out0; michael@0: dest[2*i+1] = (SAMPLETYPE)out1; michael@0: i ++; michael@0: michael@0: // update position fraction michael@0: fract += rate; michael@0: // update whole positions michael@0: int whole = (int)fract; michael@0: fract -= whole; michael@0: src += 2*whole; michael@0: srcCount += whole; michael@0: } michael@0: srcSamples = srcCount; michael@0: return i; michael@0: } michael@0: michael@0: michael@0: int InterpolateLinearFloat::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) michael@0: { michael@0: int i; michael@0: int srcSampleEnd = srcSamples - 1; michael@0: int srcCount = 0; michael@0: michael@0: i = 0; michael@0: while (srcCount < srcSampleEnd) michael@0: { michael@0: float temp, vol1; michael@0: michael@0: vol1 = (1.0f- fract); michael@0: for (int c = 0; c < numChannels; c ++) michael@0: { michael@0: temp = vol1 * src[c] + fract * src[c + numChannels]; michael@0: *dest = (SAMPLETYPE)temp; michael@0: dest ++; michael@0: } michael@0: i++; michael@0: michael@0: fract += rate; michael@0: michael@0: int iWhole = (int)fract; michael@0: fract -= iWhole; michael@0: srcCount += iWhole; michael@0: src += iWhole * numChannels; michael@0: } michael@0: srcSamples = srcCount; michael@0: michael@0: return i; michael@0: }