InterpolateShannon.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// Sample interpolation routine using 8-tap band-limited Shannon interpolation
  4. /// with kaiser window.
  5. ///
  6. /// Notice. This algorithm is remarkably much heavier than linear or cubic
  7. /// interpolation, and not remarkably better than cubic algorithm. Thus mostly
  8. /// for experimental purposes
  9. ///
  10. /// Author : Copyright (c) Olli Parviainen
  11. /// Author e-mail : oparviai 'at' iki.fi
  12. /// SoundTouch WWW: http://www.surina.net/soundtouch
  13. ///
  14. ////////////////////////////////////////////////////////////////////////////////
  15. //
  16. // License :
  17. //
  18. // SoundTouch audio processing library
  19. // Copyright (c) Olli Parviainen
  20. //
  21. // This library is free software; you can redistribute it and/or
  22. // modify it under the terms of the GNU Lesser General Public
  23. // License as published by the Free Software Foundation; either
  24. // version 2.1 of the License, or (at your option) any later version.
  25. //
  26. // This library is distributed in the hope that it will be useful,
  27. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. // Lesser General Public License for more details.
  30. //
  31. // You should have received a copy of the GNU Lesser General Public
  32. // License along with this library; if not, write to the Free Software
  33. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  34. //
  35. ////////////////////////////////////////////////////////////////////////////////
  36. #include <math.h>
  37. #include "InterpolateShannon.h"
  38. #include "STTypes.h"
  39. using namespace soundtouch;
  40. /// Kaiser window with beta = 2.0
  41. /// Values scaled down by 5% to avoid overflows
  42. static const double _kaiser8[8] =
  43. {
  44. 0.41778693317814,
  45. 0.64888025049173,
  46. 0.83508562409944,
  47. 0.93887857733412,
  48. 0.93887857733412,
  49. 0.83508562409944,
  50. 0.64888025049173,
  51. 0.41778693317814
  52. };
  53. InterpolateShannon::InterpolateShannon()
  54. {
  55. fract = 0;
  56. }
  57. void InterpolateShannon::resetRegisters()
  58. {
  59. fract = 0;
  60. }
  61. #define PI 3.1415926536
  62. #define sinc(x) (sin(PI * (x)) / (PI * (x)))
  63. /// Transpose mono audio. Returns number of produced output samples, and
  64. /// updates "srcSamples" to amount of consumed source samples
  65. int InterpolateShannon::transposeMono(SAMPLETYPE *pdest,
  66. const SAMPLETYPE *psrc,
  67. int &srcSamples)
  68. {
  69. int i;
  70. int srcSampleEnd = srcSamples - 8;
  71. int srcCount = 0;
  72. i = 0;
  73. while (srcCount < srcSampleEnd)
  74. {
  75. double out;
  76. assert(fract < 1.0);
  77. out = psrc[0] * sinc(-3.0 - fract) * _kaiser8[0];
  78. out += psrc[1] * sinc(-2.0 - fract) * _kaiser8[1];
  79. out += psrc[2] * sinc(-1.0 - fract) * _kaiser8[2];
  80. if (fract < 1e-6)
  81. {
  82. out += psrc[3] * _kaiser8[3]; // sinc(0) = 1
  83. }
  84. else
  85. {
  86. out += psrc[3] * sinc(- fract) * _kaiser8[3];
  87. }
  88. out += psrc[4] * sinc( 1.0 - fract) * _kaiser8[4];
  89. out += psrc[5] * sinc( 2.0 - fract) * _kaiser8[5];
  90. out += psrc[6] * sinc( 3.0 - fract) * _kaiser8[6];
  91. out += psrc[7] * sinc( 4.0 - fract) * _kaiser8[7];
  92. pdest[i] = (SAMPLETYPE)out;
  93. i ++;
  94. // update position fraction
  95. fract += rate;
  96. // update whole positions
  97. int whole = (int)fract;
  98. fract -= whole;
  99. psrc += whole;
  100. srcCount += whole;
  101. }
  102. srcSamples = srcCount;
  103. return i;
  104. }
  105. /// Transpose stereo audio. Returns number of produced output samples, and
  106. /// updates "srcSamples" to amount of consumed source samples
  107. int InterpolateShannon::transposeStereo(SAMPLETYPE *pdest,
  108. const SAMPLETYPE *psrc,
  109. int &srcSamples)
  110. {
  111. int i;
  112. int srcSampleEnd = srcSamples - 8;
  113. int srcCount = 0;
  114. i = 0;
  115. while (srcCount < srcSampleEnd)
  116. {
  117. double out0, out1, w;
  118. assert(fract < 1.0);
  119. w = sinc(-3.0 - fract) * _kaiser8[0];
  120. out0 = psrc[0] * w; out1 = psrc[1] * w;
  121. w = sinc(-2.0 - fract) * _kaiser8[1];
  122. out0 += psrc[2] * w; out1 += psrc[3] * w;
  123. w = sinc(-1.0 - fract) * _kaiser8[2];
  124. out0 += psrc[4] * w; out1 += psrc[5] * w;
  125. w = _kaiser8[3] * ((fract < 1e-5) ? 1.0 : sinc(- fract)); // sinc(0) = 1
  126. out0 += psrc[6] * w; out1 += psrc[7] * w;
  127. w = sinc( 1.0 - fract) * _kaiser8[4];
  128. out0 += psrc[8] * w; out1 += psrc[9] * w;
  129. w = sinc( 2.0 - fract) * _kaiser8[5];
  130. out0 += psrc[10] * w; out1 += psrc[11] * w;
  131. w = sinc( 3.0 - fract) * _kaiser8[6];
  132. out0 += psrc[12] * w; out1 += psrc[13] * w;
  133. w = sinc( 4.0 - fract) * _kaiser8[7];
  134. out0 += psrc[14] * w; out1 += psrc[15] * w;
  135. pdest[2*i] = (SAMPLETYPE)out0;
  136. pdest[2*i+1] = (SAMPLETYPE)out1;
  137. i ++;
  138. // update position fraction
  139. fract += rate;
  140. // update whole positions
  141. int whole = (int)fract;
  142. fract -= whole;
  143. psrc += 2*whole;
  144. srcCount += whole;
  145. }
  146. srcSamples = srcCount;
  147. return i;
  148. }
  149. /// Transpose stereo audio. Returns number of produced output samples, and
  150. /// updates "srcSamples" to amount of consumed source samples
  151. int InterpolateShannon::transposeMulti(SAMPLETYPE *pdest,
  152. const SAMPLETYPE *psrc,
  153. int &srcSamples)
  154. {
  155. // not implemented
  156. assert(false);
  157. return 0;
  158. }