InterpolateLinear.cpp (7813B)
1 //////////////////////////////////////////////////////////////////////////////// 2 /// 3 /// Linear interpolation algorithm. 4 /// 5 /// Author : Copyright (c) Olli Parviainen 6 /// Author e-mail : oparviai 'at' iki.fi 7 /// SoundTouch WWW: http://www.surina.net/soundtouch 8 /// 9 //////////////////////////////////////////////////////////////////////////////// 10 // 11 // License : 12 // 13 // SoundTouch audio processing library 14 // Copyright (c) Olli Parviainen 15 // 16 // This library is free software; you can redistribute it and/or 17 // modify it under the terms of the GNU Lesser General Public 18 // License as published by the Free Software Foundation; either 19 // version 2.1 of the License, or (at your option) any later version. 20 // 21 // This library is distributed in the hope that it will be useful, 22 // but WITHOUT ANY WARRANTY; without even the implied warranty of 23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 // Lesser General Public License for more details. 25 // 26 // You should have received a copy of the GNU Lesser General Public 27 // License along with this library; if not, write to the Free Software 28 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 29 // 30 //////////////////////////////////////////////////////////////////////////////// 31 32 #include <assert.h> 33 #include <stdlib.h> 34 #include "InterpolateLinear.h" 35 36 using namespace soundtouch; 37 38 ////////////////////////////////////////////////////////////////////////////// 39 // 40 // InterpolateLinearInteger - integer arithmetic implementation 41 // 42 43 /// fixed-point interpolation routine precision 44 #define SCALE 65536 45 46 47 // Constructor 48 InterpolateLinearInteger::InterpolateLinearInteger() : TransposerBase() 49 { 50 // Notice: use local function calling syntax for sake of clarity, 51 // to indicate the fact that C++ constructor can't call virtual functions. 52 resetRegisters(); 53 setRate(1.0f); 54 } 55 56 57 void InterpolateLinearInteger::resetRegisters() 58 { 59 iFract = 0; 60 } 61 62 63 // Transposes the sample rate of the given samples using linear interpolation. 64 // 'Mono' version of the routine. Returns the number of samples returned in 65 // the "dest" buffer 66 int InterpolateLinearInteger::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) 67 { 68 int i; 69 int srcSampleEnd = srcSamples - 1; 70 int srcCount = 0; 71 72 i = 0; 73 while (srcCount < srcSampleEnd) 74 { 75 LONG_SAMPLETYPE temp; 76 77 assert(iFract < SCALE); 78 79 temp = (SCALE - iFract) * src[0] + iFract * src[1]; 80 dest[i] = (SAMPLETYPE)(temp / SCALE); 81 i++; 82 83 iFract += iRate; 84 85 int iWhole = iFract / SCALE; 86 iFract -= iWhole * SCALE; 87 srcCount += iWhole; 88 src += iWhole; 89 } 90 srcSamples = srcCount; 91 92 return i; 93 } 94 95 96 // Transposes the sample rate of the given samples using linear interpolation. 97 // 'Stereo' version of the routine. Returns the number of samples returned in 98 // the "dest" buffer 99 int InterpolateLinearInteger::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) 100 { 101 int i; 102 int srcSampleEnd = srcSamples - 1; 103 int srcCount = 0; 104 105 i = 0; 106 while (srcCount < srcSampleEnd) 107 { 108 LONG_SAMPLETYPE temp0; 109 LONG_SAMPLETYPE temp1; 110 111 assert(iFract < SCALE); 112 113 temp0 = (SCALE - iFract) * src[0] + iFract * src[2]; 114 temp1 = (SCALE - iFract) * src[1] + iFract * src[3]; 115 dest[0] = (SAMPLETYPE)(temp0 / SCALE); 116 dest[1] = (SAMPLETYPE)(temp1 / SCALE); 117 dest += 2; 118 i++; 119 120 iFract += iRate; 121 122 int iWhole = iFract / SCALE; 123 iFract -= iWhole * SCALE; 124 srcCount += iWhole; 125 src += 2*iWhole; 126 } 127 srcSamples = srcCount; 128 129 return i; 130 } 131 132 133 int InterpolateLinearInteger::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) 134 { 135 int i; 136 int srcSampleEnd = srcSamples - 1; 137 int srcCount = 0; 138 139 i = 0; 140 while (srcCount < srcSampleEnd) 141 { 142 LONG_SAMPLETYPE temp, vol1; 143 144 assert(iFract < SCALE); 145 vol1 = (LONG_SAMPLETYPE)(SCALE - iFract); 146 for (int c = 0; c < numChannels; c ++) 147 { 148 temp = vol1 * src[c] + iFract * src[c + numChannels]; 149 dest[0] = (SAMPLETYPE)(temp / SCALE); 150 dest ++; 151 } 152 i++; 153 154 iFract += iRate; 155 156 int iWhole = iFract / SCALE; 157 iFract -= iWhole * SCALE; 158 srcCount += iWhole; 159 src += iWhole * numChannels; 160 } 161 srcSamples = srcCount; 162 163 return i; 164 } 165 166 167 // Sets new target iRate. Normal iRate = 1.0, smaller values represent slower 168 // iRate, larger faster iRates. 169 void InterpolateLinearInteger::setRate(double newRate) 170 { 171 iRate = (int)(newRate * SCALE + 0.5); 172 TransposerBase::setRate(newRate); 173 } 174 175 176 ////////////////////////////////////////////////////////////////////////////// 177 // 178 // InterpolateLinearFloat - floating point arithmetic implementation 179 // 180 ////////////////////////////////////////////////////////////////////////////// 181 182 183 // Constructor 184 InterpolateLinearFloat::InterpolateLinearFloat() : TransposerBase() 185 { 186 // Notice: use local function calling syntax for sake of clarity, 187 // to indicate the fact that C++ constructor can't call virtual functions. 188 resetRegisters(); 189 setRate(1.0); 190 } 191 192 193 void InterpolateLinearFloat::resetRegisters() 194 { 195 fract = 0; 196 } 197 198 199 // Transposes the sample rate of the given samples using linear interpolation. 200 // 'Mono' version of the routine. Returns the number of samples returned in 201 // the "dest" buffer 202 int InterpolateLinearFloat::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) 203 { 204 int i; 205 int srcSampleEnd = srcSamples - 1; 206 int srcCount = 0; 207 208 i = 0; 209 while (srcCount < srcSampleEnd) 210 { 211 double out; 212 assert(fract < 1.0); 213 214 out = (1.0 - fract) * src[0] + fract * src[1]; 215 dest[i] = (SAMPLETYPE)out; 216 i ++; 217 218 // update position fraction 219 fract += rate; 220 // update whole positions 221 int whole = (int)fract; 222 fract -= whole; 223 src += whole; 224 srcCount += whole; 225 } 226 srcSamples = srcCount; 227 return i; 228 } 229 230 231 // Transposes the sample rate of the given samples using linear interpolation. 232 // 'Mono' version of the routine. Returns the number of samples returned in 233 // the "dest" buffer 234 int InterpolateLinearFloat::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) 235 { 236 int i; 237 int srcSampleEnd = srcSamples - 1; 238 int srcCount = 0; 239 240 i = 0; 241 while (srcCount < srcSampleEnd) 242 { 243 double out0, out1; 244 assert(fract < 1.0); 245 246 out0 = (1.0 - fract) * src[0] + fract * src[2]; 247 out1 = (1.0 - fract) * src[1] + fract * src[3]; 248 dest[2*i] = (SAMPLETYPE)out0; 249 dest[2*i+1] = (SAMPLETYPE)out1; 250 i ++; 251 252 // update position fraction 253 fract += rate; 254 // update whole positions 255 int whole = (int)fract; 256 fract -= whole; 257 src += 2*whole; 258 srcCount += whole; 259 } 260 srcSamples = srcCount; 261 return i; 262 } 263 264 265 int InterpolateLinearFloat::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) 266 { 267 int i; 268 int srcSampleEnd = srcSamples - 1; 269 int srcCount = 0; 270 271 i = 0; 272 while (srcCount < srcSampleEnd) 273 { 274 float temp, vol1, fract_float; 275 276 vol1 = (float)(1.0 - fract); 277 fract_float = (float)fract; 278 for (int c = 0; c < numChannels; c ++) 279 { 280 temp = vol1 * src[c] + fract_float * src[c + numChannels]; 281 *dest = (SAMPLETYPE)temp; 282 dest ++; 283 } 284 i++; 285 286 fract += rate; 287 288 int iWhole = (int)fract; 289 fract -= iWhole; 290 srcCount += iWhole; 291 src += iWhole * numChannels; 292 } 293 srcSamples = srcCount; 294 295 return i; 296 }