Audio plugin host https://kx.studio/carla
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.

197 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. const auto doubleSize = (double) size;
  116. for (size_t i = 0; i < size; ++i)
  117. samples[i] = static_cast<FloatType> (SpecialFunctions::besselI0 (beta * std::sqrt (1.0 - std::pow (((double) i - 0.5 * (doubleSize - 1.0))
  118. / ( 0.5 * (doubleSize - 1.0)), 2.0)))
  119. * factor);
  120. }
  121. break;
  122. case numWindowingMethods:
  123. default:
  124. jassertfalse;
  125. break;
  126. }
  127. // DC frequency amplitude must be one
  128. if (normalise)
  129. {
  130. FloatType sum (0);
  131. for (size_t i = 0; i < size; ++i)
  132. sum += samples[i];
  133. auto factor = static_cast<FloatType> (size) / sum;
  134. FloatVectorOperations::multiply (samples, factor, static_cast<int> (size));
  135. }
  136. }
  137. template <typename FloatType>
  138. void WindowingFunction<FloatType>::multiplyWithWindowingTable (FloatType* samples, size_t size) noexcept
  139. {
  140. FloatVectorOperations::multiply (samples, windowTable.getRawDataPointer(), jmin (static_cast<int> (size), windowTable.size()));
  141. }
  142. template <typename FloatType>
  143. const char* WindowingFunction<FloatType>::getWindowingMethodName (WindowingMethod type) noexcept
  144. {
  145. switch (type)
  146. {
  147. case rectangular: return "Rectangular";
  148. case triangular: return "Triangular";
  149. case hann: return "Hann";
  150. case hamming: return "Hamming";
  151. case blackman: return "Blackman";
  152. case blackmanHarris: return "Blackman-Harris";
  153. case flatTop: return "Flat Top";
  154. case kaiser: return "Kaiser";
  155. case numWindowingMethods:
  156. default: jassertfalse; return "";
  157. }
  158. }
  159. template class WindowingFunction<float>;
  160. template class WindowingFunction<double>;
  161. } // namespace dsp
  162. } // namespace juce