VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_dsp / frequency / juce_Windowing.cpp
blob3a12366ab5978f28dbd6dffc14d115911d131118
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
23 ==============================================================================
26 namespace juce
28 namespace dsp
31 template <typename FloatType>
32 static FloatType ncos (size_t order, size_t i, size_t size) noexcept
34 return std::cos (static_cast<FloatType> (order * i)
35 * MathConstants<FloatType>::pi / static_cast<FloatType> (size - 1));
38 template <typename FloatType>
39 WindowingFunction<FloatType>::WindowingFunction (size_t size, WindowingMethod type, bool normalise, FloatType beta)
41 fillWindowingTables (size, type, normalise, beta);
44 template <typename FloatType>
45 void WindowingFunction<FloatType>::fillWindowingTables (size_t size, WindowingMethod type,
46 bool normalise, FloatType beta) noexcept
48 windowTable.resize (static_cast<int> (size));
49 fillWindowingTables (windowTable.getRawDataPointer(), size, type, normalise, beta);
52 template <typename FloatType>
53 void WindowingFunction<FloatType>::fillWindowingTables (FloatType* samples, size_t size,
54 WindowingMethod type, bool normalise,
55 FloatType beta) noexcept
57 switch (type)
59 case rectangular:
61 for (size_t i = 0; i < size; ++i)
62 samples[i] = static_cast<FloatType> (1);
64 break;
66 case triangular:
68 auto halfSlots = static_cast<FloatType> (0.5) * static_cast<FloatType> (size - 1);
70 for (size_t i = 0; i < size; ++i)
71 samples[i] = static_cast<FloatType> (1.0) - std::abs ((static_cast<FloatType> (i) - halfSlots) / halfSlots);
73 break;
75 case hann:
77 for (size_t i = 0; i < size; ++i)
79 auto cos2 = ncos<FloatType> (2, i, size);
80 samples[i] = static_cast<FloatType> (0.5 - 0.5 * cos2);
83 break;
85 case hamming:
87 for (size_t i = 0; i < size; ++i)
89 auto cos2 = ncos<FloatType> (2, i, size);
90 samples[i] = static_cast<FloatType> (0.54 - 0.46 * cos2);
93 break;
95 case blackman:
97 constexpr FloatType alpha = 0.16f;
99 for (size_t i = 0; i < size; ++i)
101 auto cos2 = ncos<FloatType> (2, i, size);
102 auto cos4 = ncos<FloatType> (4, i, size);
104 samples[i] = static_cast<FloatType> (0.5 * (1 - alpha) - 0.5 * cos2 + 0.5 * alpha * cos4);
107 break;
109 case blackmanHarris:
111 for (size_t i = 0; i < size; ++i)
113 auto cos2 = ncos<FloatType> (2, i, size);
114 auto cos4 = ncos<FloatType> (4, i, size);
115 auto cos6 = ncos<FloatType> (6, i, size);
117 samples[i] = static_cast<FloatType> (0.35875 - 0.48829 * cos2 + 0.14128 * cos4 - 0.01168 * cos6);
120 break;
122 case flatTop:
124 for (size_t i = 0; i < size; ++i)
126 auto cos2 = ncos<FloatType> (2, i, size);
127 auto cos4 = ncos<FloatType> (4, i, size);
128 auto cos6 = ncos<FloatType> (6, i, size);
129 auto cos8 = ncos<FloatType> (8, i, size);
131 samples[i] = static_cast<FloatType> (1.0 - 1.93 * cos2 + 1.29 * cos4 - 0.388 * cos6 + 0.028 * cos8);
134 break;
136 case kaiser:
138 const double factor = 1.0 / SpecialFunctions::besselI0 (beta);
139 const auto doubleSize = (double) size;
141 for (size_t i = 0; i < size; ++i)
142 samples[i] = static_cast<FloatType> (SpecialFunctions::besselI0 (beta * std::sqrt (1.0 - std::pow (((double) i - 0.5 * (doubleSize - 1.0))
143 / ( 0.5 * (doubleSize - 1.0)), 2.0)))
144 * factor);
146 break;
148 case numWindowingMethods:
149 default:
150 jassertfalse;
151 break;
154 // DC frequency amplitude must be one
155 if (normalise)
157 FloatType sum (0);
159 for (size_t i = 0; i < size; ++i)
160 sum += samples[i];
162 auto factor = static_cast<FloatType> (size) / sum;
164 FloatVectorOperations::multiply (samples, factor, static_cast<int> (size));
168 template <typename FloatType>
169 void WindowingFunction<FloatType>::multiplyWithWindowingTable (FloatType* samples, size_t size) noexcept
171 FloatVectorOperations::multiply (samples, windowTable.getRawDataPointer(), jmin (static_cast<int> (size), windowTable.size()));
174 template <typename FloatType>
175 const char* WindowingFunction<FloatType>::getWindowingMethodName (WindowingMethod type) noexcept
177 switch (type)
179 case rectangular: return "Rectangular";
180 case triangular: return "Triangular";
181 case hann: return "Hann";
182 case hamming: return "Hamming";
183 case blackman: return "Blackman";
184 case blackmanHarris: return "Blackman-Harris";
185 case flatTop: return "Flat Top";
186 case kaiser: return "Kaiser";
187 case numWindowingMethods:
188 default: jassertfalse; return "";
192 template class WindowingFunction<float>;
193 template class WindowingFunction<double>;
195 } // namespace dsp
196 } // namespace juce