The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. template <typename FloatType>
  18. static inline FloatType ncos (size_t order, size_t i, size_t size) noexcept
  19. {
  20. return std::cos (static_cast<FloatType> (order * i)
  21. * MathConstants<FloatType>::pi / static_cast<FloatType> (size - 1));
  22. }
  23. template <typename FloatType>
  24. WindowingFunction<FloatType>::WindowingFunction (size_t size, WindowingMethod type, bool normalise, FloatType beta)
  25. {
  26. fillWindowingTables (size, type, normalise, beta);
  27. }
  28. template <typename FloatType>
  29. void WindowingFunction<FloatType>::fillWindowingTables (size_t size, WindowingMethod type,
  30. bool normalise, FloatType beta) noexcept
  31. {
  32. windowTable.resize (static_cast<int> (size));
  33. fillWindowingTables (windowTable.getRawDataPointer(), size, type, normalise, beta);
  34. }
  35. template <typename FloatType>
  36. void WindowingFunction<FloatType>::fillWindowingTables (FloatType* samples, size_t size,
  37. WindowingMethod type, bool normalise,
  38. FloatType beta) noexcept
  39. {
  40. switch (type)
  41. {
  42. case rectangular:
  43. {
  44. for (size_t i = 0; i < size; ++i)
  45. samples[i] = static_cast<FloatType> (1);
  46. }
  47. break;
  48. case triangular:
  49. {
  50. auto halfSlots = static_cast<FloatType> (0.5) * static_cast<FloatType> (size - 1);
  51. for (size_t i = 0; i < size; ++i)
  52. samples[i] = static_cast<FloatType> (1.0) - std::abs ((static_cast<FloatType> (i) - halfSlots) / halfSlots);
  53. }
  54. break;
  55. case hann:
  56. {
  57. for (size_t i = 0; i < size; ++i)
  58. {
  59. auto cos2 = ncos<FloatType> (2, i, size);
  60. samples[i] = static_cast<FloatType> (0.5 - 0.5 * cos2);
  61. }
  62. }
  63. break;
  64. case hamming:
  65. {
  66. for (size_t i = 0; i < size; ++i)
  67. {
  68. auto cos2 = ncos<FloatType> (2, i, size);
  69. samples[i] = static_cast<FloatType> (0.54 - 0.46 * cos2);
  70. }
  71. }
  72. break;
  73. case blackman:
  74. {
  75. constexpr FloatType alpha = 0.16f;
  76. for (size_t i = 0; i < size; ++i)
  77. {
  78. auto cos2 = ncos<FloatType> (2, i, size);
  79. auto cos4 = ncos<FloatType> (4, i, size);
  80. samples[i] = static_cast<FloatType> (0.5 * (1 - alpha) - 0.5 * cos2 + 0.5 * alpha * cos4);
  81. }
  82. }
  83. break;
  84. case blackmanHarris:
  85. {
  86. for (size_t i = 0; i < size; ++i)
  87. {
  88. auto cos2 = ncos<FloatType> (2, i, size);
  89. auto cos4 = ncos<FloatType> (4, i, size);
  90. auto cos6 = ncos<FloatType> (6, i, size);
  91. samples[i] = static_cast<FloatType> (0.35875 - 0.48829 * cos2 + 0.14128 * cos4 - 0.01168 * cos6);
  92. }
  93. }
  94. break;
  95. case flatTop:
  96. {
  97. for (size_t i = 0; i < size; ++i)
  98. {
  99. auto cos2 = ncos<FloatType> (2, i, size);
  100. auto cos4 = ncos<FloatType> (4, i, size);
  101. auto cos6 = ncos<FloatType> (6, i, size);
  102. auto cos8 = ncos<FloatType> (8, i, size);
  103. samples[i] = static_cast<FloatType> (1.0 - 1.93 * cos2 + 1.29 * cos4 - 0.388 * cos6 + 0.028 * cos8);
  104. }
  105. }
  106. break;
  107. case kaiser:
  108. {
  109. const double factor = 1.0 / SpecialFunctions::besselI0 (beta);
  110. for (size_t i = 0; i < size; ++i)
  111. samples[i] = static_cast<FloatType> (SpecialFunctions::besselI0 (beta * std::sqrt (1.0 - std::pow ((i - 0.5 * (size - 1.0))
  112. / ( 0.5 * (size - 1.0)), 2.0)))
  113. * factor);
  114. }
  115. break;
  116. case numWindowingMethods:
  117. default:
  118. jassertfalse;
  119. break;
  120. }
  121. // DC frequency amplitude must be one
  122. if (normalise)
  123. {
  124. FloatType sum (0);
  125. for (size_t i = 0; i < size; ++i)
  126. sum += samples[i];
  127. auto factor = static_cast<FloatType> (size) / sum;
  128. FloatVectorOperations::multiply (samples, factor, static_cast<int> (size));
  129. }
  130. }
  131. template <typename FloatType>
  132. void WindowingFunction<FloatType>::multiplyWithWindowingTable (FloatType* samples, size_t size) noexcept
  133. {
  134. FloatVectorOperations::multiply (samples, windowTable.getRawDataPointer(), jmin (static_cast<int> (size), windowTable.size()));
  135. }
  136. template <typename FloatType>
  137. const char* WindowingFunction<FloatType>::getWindowingMethodName (WindowingMethod type) noexcept
  138. {
  139. switch (type)
  140. {
  141. case rectangular: return "Rectangular";
  142. case triangular: return "Triangular";
  143. case hann: return "Hann";
  144. case hamming: return "Hamming";
  145. case blackman: return "Blackman";
  146. case blackmanHarris: return "Blackman-Harris";
  147. case flatTop: return "Flat Top";
  148. case kaiser: return "Kaiser";
  149. case numWindowingMethods:
  150. default: jassertfalse; return "";
  151. }
  152. }
  153. template class WindowingFunction<float>;
  154. template class WindowingFunction<double>;
  155. } // namespace dsp
  156. } // namespace juce