00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00040
00041 #include <memory.h>
00042 #include <assert.h>
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <limits.h>
00046 #include "RateTransposer.h"
00047 #include "AAFilter.h"
00048
00049 using namespace soundtouch;
00050
00051
00054 class RateTransposerInteger : public RateTransposer
00055 {
00056 protected:
00057 int iSlopeCount;
00058 uint uRate;
00059 SAMPLETYPE sPrevSampleL, sPrevSampleR;
00060
00061 virtual void resetRegisters();
00062
00063 virtual uint transposeStereo(SAMPLETYPE *dest,
00064 const SAMPLETYPE *src,
00065 uint numSamples);
00066 virtual uint transposeMono(SAMPLETYPE *dest,
00067 const SAMPLETYPE *src,
00068 uint numSamples);
00069
00070 public:
00071 RateTransposerInteger();
00072 virtual ~RateTransposerInteger();
00073
00076 virtual void setRate(float newRate);
00077
00078 };
00079
00080
00083 class RateTransposerFloat : public RateTransposer
00084 {
00085 protected:
00086 float fSlopeCount;
00087 float fRateStep;
00088 SAMPLETYPE sPrevSampleL, sPrevSampleR;
00089
00090 virtual void resetRegisters();
00091
00092 virtual uint transposeStereo(SAMPLETYPE *dest,
00093 const SAMPLETYPE *src,
00094 uint numSamples);
00095 virtual uint transposeMono(SAMPLETYPE *dest,
00096 const SAMPLETYPE *src,
00097 uint numSamples);
00098
00099 public:
00100 RateTransposerFloat();
00101 virtual ~RateTransposerFloat();
00102 };
00103
00104
00105
00106 #ifndef min
00107 #define min(a,b) ((a > b) ? b : a)
00108 #define max(a,b) ((a < b) ? b : a)
00109 #endif
00110
00111
00112
00113
00114 void * RateTransposer::operator new(size_t s)
00115 {
00116
00117 assert(FALSE);
00118 return NULL;
00119 }
00120
00121
00122 RateTransposer *RateTransposer::newInstance()
00123 {
00124 #ifdef INTEGER_SAMPLES
00125 return ::new RateTransposerInteger;
00126 #else
00127 return ::new RateTransposerFloat;
00128 #endif
00129 }
00130
00131
00132
00133 RateTransposer::RateTransposer() : FIFOProcessor(&outputBuffer)
00134 {
00135 uChannels = 2;
00136 bUseAAFilter = TRUE;
00137
00138
00139
00140 pAAFilter = new AAFilter(32);
00141 }
00142
00143
00144
00145 RateTransposer::~RateTransposer()
00146 {
00147 delete pAAFilter;
00148 }
00149
00150
00151
00153 void RateTransposer::enableAAFilter(const BOOL newMode)
00154 {
00155 bUseAAFilter = newMode;
00156 }
00157
00158
00160 BOOL RateTransposer::isAAFilterEnabled() const
00161 {
00162 return bUseAAFilter;
00163 }
00164
00165
00166 AAFilter *RateTransposer::getAAFilter() const
00167 {
00168 return pAAFilter;
00169 }
00170
00171
00172
00173
00174
00175 void RateTransposer::setRate(float newRate)
00176 {
00177 float fCutoff;
00178
00179 fRate = newRate;
00180
00181
00182 if (newRate > 1.0f)
00183 {
00184 fCutoff = 0.5f / newRate;
00185 }
00186 else
00187 {
00188 fCutoff = 0.5f * newRate;
00189 }
00190 pAAFilter->setCutoffFreq(fCutoff);
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200 void RateTransposer::flushStoreBuffer()
00201 {
00202 if (storeBuffer.isEmpty()) return;
00203
00204 outputBuffer.moveSamples(storeBuffer);
00205 }
00206
00207
00208
00209
00210 void RateTransposer::putSamples(const SAMPLETYPE *samples, uint numSamples)
00211 {
00212 processSamples(samples, numSamples);
00213 }
00214
00215
00216
00217
00218
00219 void RateTransposer::upsample(const SAMPLETYPE *src, uint numSamples)
00220 {
00221 int count, sizeTemp, num;
00222
00223
00224
00225
00226
00227
00228 sizeTemp = (int)((float)numSamples / fRate + 16.0f);
00229
00230
00231 count = transpose(storeBuffer.ptrEnd(sizeTemp), src, numSamples);
00232 storeBuffer.putSamples(count);
00233
00234
00235
00236 num = storeBuffer.numSamples();
00237 count = pAAFilter->evaluate(outputBuffer.ptrEnd(num),
00238 storeBuffer.ptrBegin(), num, uChannels);
00239 outputBuffer.putSamples(count);
00240
00241
00242 storeBuffer.receiveSamples(count);
00243 }
00244
00245
00246
00247
00248 void RateTransposer::downsample(const SAMPLETYPE *src, uint numSamples)
00249 {
00250 int count, sizeTemp;
00251
00252
00253
00254
00255
00256
00257 storeBuffer.putSamples(src, numSamples);
00258
00259
00260
00261
00262 assert(tempBuffer.isEmpty());
00263 sizeTemp = storeBuffer.numSamples();
00264
00265 count = pAAFilter->evaluate(tempBuffer.ptrEnd(sizeTemp),
00266 storeBuffer.ptrBegin(), sizeTemp, uChannels);
00267
00268
00269 storeBuffer.receiveSamples(count);
00270
00271
00272 sizeTemp = (int)((float)numSamples / fRate + 16.0f);
00273 count = transpose(outputBuffer.ptrEnd(sizeTemp), tempBuffer.ptrBegin(), count);
00274 outputBuffer.putSamples(count);
00275 }
00276
00277
00278
00279
00280
00281
00282 void RateTransposer::processSamples(const SAMPLETYPE *src, uint numSamples)
00283 {
00284 uint count;
00285 uint sizeReq;
00286
00287 if (numSamples == 0) return;
00288 assert(pAAFilter);
00289
00290
00291
00292 if (bUseAAFilter == FALSE)
00293 {
00294 sizeReq = (int)((float)numSamples / fRate + 1.0f);
00295 count = transpose(outputBuffer.ptrEnd(sizeReq), src, numSamples);
00296 outputBuffer.putSamples(count);
00297 return;
00298 }
00299
00300
00301 if (fRate < 1.0f)
00302 {
00303 upsample(src, numSamples);
00304 }
00305 else
00306 {
00307 downsample(src, numSamples);
00308 }
00309 }
00310
00311
00312
00313
00314 inline uint RateTransposer::transpose(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples)
00315 {
00316 if (uChannels == 2)
00317 {
00318 return transposeStereo(dest, src, numSamples);
00319 }
00320 else
00321 {
00322 return transposeMono(dest, src, numSamples);
00323 }
00324 }
00325
00326
00327
00328 void RateTransposer::setChannels(const uint numchannels)
00329 {
00330 if (uChannels == numchannels) return;
00331
00332 #ifdef MULTICHANNEL
00333 assert(numchannels >= 1 && numchannels <= MULTICHANNEL);
00334 #else
00335 assert(numchannels == 1 || numchannels == 2);
00336 #endif
00337 uChannels = numchannels;
00338
00339 storeBuffer.setChannels(uChannels);
00340 tempBuffer.setChannels(uChannels);
00341 outputBuffer.setChannels(uChannels);
00342
00343
00344 resetRegisters();
00345 }
00346
00347
00348
00349 void RateTransposer::clear()
00350 {
00351 outputBuffer.clear();
00352 storeBuffer.clear();
00353 }
00354
00355
00356
00357 uint RateTransposer::isEmpty()
00358 {
00359 int res;
00360
00361 res = FIFOProcessor::isEmpty();
00362 if (res == 0) return 0;
00363 return storeBuffer.isEmpty();
00364 }
00365
00366
00368
00369
00370
00371
00373 #define SCALE 65536
00374
00375
00376 RateTransposerInteger::RateTransposerInteger() : RateTransposer()
00377 {
00378
00379
00380
00381 resetRegisters();
00382 setRate(1.0f);
00383 }
00384
00385
00386 RateTransposerInteger::~RateTransposerInteger()
00387 {
00388 }
00389
00390
00391 void RateTransposerInteger::resetRegisters()
00392 {
00393 iSlopeCount = 0;
00394 sPrevSampleL =
00395 sPrevSampleR = 0;
00396 }
00397
00398
00399
00400
00401
00402
00403 uint RateTransposerInteger::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples)
00404 {
00405 unsigned int i, used;
00406 LONG_SAMPLETYPE temp, vol1;
00407
00408 used = 0;
00409 i = 0;
00410
00411
00412 while (iSlopeCount <= SCALE)
00413 {
00414 vol1 = (LONG_SAMPLETYPE)(SCALE - iSlopeCount);
00415 temp = vol1 * sPrevSampleL + iSlopeCount * src[0];
00416 dest[i] = (SAMPLETYPE)(temp / SCALE);
00417 i++;
00418 iSlopeCount += uRate;
00419 }
00420
00421 iSlopeCount -= SCALE;
00422
00423 while (1)
00424 {
00425 while (iSlopeCount > SCALE)
00426 {
00427 iSlopeCount -= SCALE;
00428 used ++;
00429 if (used >= numSamples - 1) goto end;
00430 }
00431 vol1 = (LONG_SAMPLETYPE)(SCALE - iSlopeCount);
00432 temp = src[used] * vol1 + iSlopeCount * src[used + 1];
00433 dest[i] = (SAMPLETYPE)(temp / SCALE);
00434
00435 i++;
00436 iSlopeCount += uRate;
00437 }
00438 end:
00439
00440 sPrevSampleL = src[numSamples - 1];
00441
00442 return i;
00443 }
00444
00445
00446
00447
00448
00449 uint RateTransposerInteger::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples)
00450 {
00451 unsigned int srcPos, i, used;
00452 LONG_SAMPLETYPE temp, vol1;
00453
00454 if (numSamples == 0) return 0;
00455
00456 used = 0;
00457 i = 0;
00458
00459
00460 while (iSlopeCount <= SCALE)
00461 {
00462 vol1 = (LONG_SAMPLETYPE)(SCALE - iSlopeCount);
00463 temp = vol1 * sPrevSampleL + iSlopeCount * src[0];
00464 dest[2 * i] = (SAMPLETYPE)(temp / SCALE);
00465 temp = vol1 * sPrevSampleR + iSlopeCount * src[1];
00466 dest[2 * i + 1] = (SAMPLETYPE)(temp / SCALE);
00467 i++;
00468 iSlopeCount += uRate;
00469 }
00470
00471 iSlopeCount -= SCALE;
00472
00473 while (1)
00474 {
00475 while (iSlopeCount > SCALE)
00476 {
00477 iSlopeCount -= SCALE;
00478 used ++;
00479 if (used >= numSamples - 1) goto end;
00480 }
00481 srcPos = 2 * used;
00482 vol1 = (LONG_SAMPLETYPE)(SCALE - iSlopeCount);
00483 temp = src[srcPos] * vol1 + iSlopeCount * src[srcPos + 2];
00484 dest[2 * i] = (SAMPLETYPE)(temp / SCALE);
00485 temp = src[srcPos + 1] * vol1 + iSlopeCount * src[srcPos + 3];
00486 dest[2 * i + 1] = (SAMPLETYPE)(temp / SCALE);
00487
00488 i++;
00489 iSlopeCount += uRate;
00490 }
00491 end:
00492
00493 sPrevSampleL = src[2 * numSamples - 2];
00494 sPrevSampleR = src[2 * numSamples - 1];
00495
00496 return i;
00497 }
00498
00499
00500
00501
00502 void RateTransposerInteger::setRate(float newRate)
00503 {
00504 uRate = (int)(newRate * SCALE + 0.5f);
00505 RateTransposer::setRate(newRate);
00506 }
00507
00508
00510
00511
00512
00514
00515
00516 RateTransposerFloat::RateTransposerFloat() : RateTransposer()
00517 {
00518
00519
00520
00521 resetRegisters();
00522 setRate(1.0f);
00523 }
00524
00525
00526 RateTransposerFloat::~RateTransposerFloat()
00527 {
00528 }
00529
00530
00531 void RateTransposerFloat::resetRegisters()
00532 {
00533 fSlopeCount = 0;
00534 sPrevSampleL =
00535 sPrevSampleR = 0;
00536 }
00537
00538
00539
00540
00541
00542
00543 uint RateTransposerFloat::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples)
00544 {
00545 unsigned int i, used;
00546
00547 used = 0;
00548 i = 0;
00549
00550
00551 while (fSlopeCount <= 1.0f)
00552 {
00553 dest[i] = (SAMPLETYPE)((1.0f - fSlopeCount) * sPrevSampleL + fSlopeCount * src[0]);
00554 i++;
00555 fSlopeCount += fRate;
00556 }
00557 fSlopeCount -= 1.0f;
00558
00559 if (numSamples == 1) goto end;
00560
00561 while (1)
00562 {
00563 while (fSlopeCount > 1.0f)
00564 {
00565 fSlopeCount -= 1.0f;
00566 used ++;
00567 if (used >= numSamples - 1) goto end;
00568 }
00569 dest[i] = (SAMPLETYPE)((1.0f - fSlopeCount) * src[used] + fSlopeCount * src[used + 1]);
00570 i++;
00571 fSlopeCount += fRate;
00572 }
00573 end:
00574
00575 sPrevSampleL = src[numSamples - 1];
00576
00577 return i;
00578 }
00579
00580
00581
00582
00583
00584 uint RateTransposerFloat::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples)
00585 {
00586 unsigned int srcPos, i, used;
00587
00588 if (numSamples == 0) return 0;
00589
00590 used = 0;
00591 i = 0;
00592
00593
00594 while (fSlopeCount <= 1.0f)
00595 {
00596 dest[2 * i] = (SAMPLETYPE)((1.0f - fSlopeCount) * sPrevSampleL + fSlopeCount * src[0]);
00597 dest[2 * i + 1] = (SAMPLETYPE)((1.0f - fSlopeCount) * sPrevSampleR + fSlopeCount * src[1]);
00598 i++;
00599 fSlopeCount += fRate;
00600 }
00601
00602 fSlopeCount -= 1.0f;
00603
00604 if (numSamples == 1) goto end;
00605
00606 while (1)
00607 {
00608 while (fSlopeCount > 1.0f)
00609 {
00610 fSlopeCount -= 1.0f;
00611 used ++;
00612 if (used >= numSamples - 1) goto end;
00613 }
00614 srcPos = 2 * used;
00615
00616 dest[2 * i] = (SAMPLETYPE)((1.0f - fSlopeCount) * src[srcPos]
00617 + fSlopeCount * src[srcPos + 2]);
00618 dest[2 * i + 1] = (SAMPLETYPE)((1.0f - fSlopeCount) * src[srcPos + 1]
00619 + fSlopeCount * src[srcPos + 3]);
00620
00621 i++;
00622 fSlopeCount += fRate;
00623 }
00624 end:
00625
00626 sPrevSampleL = src[2 * numSamples - 2];
00627 sPrevSampleR = src[2 * numSamples - 1];
00628
00629 return i;
00630 }