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.

196 lines
6.5KB

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