LMMS
Loading...
Searching...
No Matches
juce_Windowing.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
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.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28namespace dsp
29{
30
31template <typename FloatType>
32static FloatType ncos (size_t order, size_t i, size_t size) noexcept
33{
34 return std::cos (static_cast<FloatType> (order * i)
35 * MathConstants<FloatType>::pi / static_cast<FloatType> (size - 1));
36}
37
38template <typename FloatType>
40{
41 fillWindowingTables (size, type, normalise, beta);
42}
43
44template <typename FloatType>
46 bool normalise, FloatType beta) noexcept
47{
48 windowTable.resize (static_cast<int> (size));
49 fillWindowingTables (windowTable.getRawDataPointer(), size, type, normalise, beta);
50}
51
52template <typename FloatType>
54 WindowingMethod type, bool normalise,
55 FloatType beta) noexcept
56{
57 switch (type)
58 {
59 case rectangular:
60 {
61 for (size_t i = 0; i < size; ++i)
62 samples[i] = static_cast<FloatType> (1);
63 }
64 break;
65
66 case triangular:
67 {
68 auto halfSlots = static_cast<FloatType> (0.5) * static_cast<FloatType> (size - 1);
69
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);
72 }
73 break;
74
75 case hann:
76 {
77 for (size_t i = 0; i < size; ++i)
78 {
79 auto cos2 = ncos<FloatType> (2, i, size);
80 samples[i] = static_cast<FloatType> (0.5 - 0.5 * cos2);
81 }
82 }
83 break;
84
85 case hamming:
86 {
87 for (size_t i = 0; i < size; ++i)
88 {
89 auto cos2 = ncos<FloatType> (2, i, size);
90 samples[i] = static_cast<FloatType> (0.54 - 0.46 * cos2);
91 }
92 }
93 break;
94
95 case blackman:
96 {
97 constexpr FloatType alpha = 0.16f;
98
99 for (size_t i = 0; i < size; ++i)
100 {
101 auto cos2 = ncos<FloatType> (2, i, size);
102 auto cos4 = ncos<FloatType> (4, i, size);
103
104 samples[i] = static_cast<FloatType> (0.5 * (1 - alpha) - 0.5 * cos2 + 0.5 * alpha * cos4);
105 }
106 }
107 break;
108
109 case blackmanHarris:
110 {
111 for (size_t i = 0; i < size; ++i)
112 {
113 auto cos2 = ncos<FloatType> (2, i, size);
114 auto cos4 = ncos<FloatType> (4, i, size);
115 auto cos6 = ncos<FloatType> (6, i, size);
116
117 samples[i] = static_cast<FloatType> (0.35875 - 0.48829 * cos2 + 0.14128 * cos4 - 0.01168 * cos6);
118 }
119 }
120 break;
121
122 case flatTop:
123 {
124 for (size_t i = 0; i < size; ++i)
125 {
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);
130
131 samples[i] = static_cast<FloatType> (1.0 - 1.93 * cos2 + 1.29 * cos4 - 0.388 * cos6 + 0.028 * cos8);
132 }
133 }
134 break;
135
136 case kaiser:
137 {
138 const double factor = 1.0 / SpecialFunctions::besselI0 (beta);
139 const auto doubleSize = (double) size;
140
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);
145 }
146 break;
147
149 default:
151 break;
152 }
153
154 // DC frequency amplitude must be one
155 if (normalise)
156 {
157 FloatType sum (0);
158
159 for (size_t i = 0; i < size; ++i)
160 sum += samples[i];
161
162 auto factor = static_cast<FloatType> (size) / sum;
163
164 FloatVectorOperations::multiply (samples, factor, static_cast<int> (size));
165 }
166}
167
168template <typename FloatType>
169void WindowingFunction<FloatType>::multiplyWithWindowingTable (FloatType* samples, size_t size) noexcept
170{
171 FloatVectorOperations::multiply (samples, windowTable.getRawDataPointer(), jmin (static_cast<int> (size), windowTable.size()));
172}
173
174template <typename FloatType>
176{
177 switch (type)
178 {
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";
188 default: jassertfalse; return "";
189 }
190}
191
192template class WindowingFunction<float>;
193template class WindowingFunction<double>;
194
195} // namespace dsp
196} // namespace juce
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition juce_Windowing.h:43
WindowingMethod
Definition juce_Windowing.h:48
@ blackman
Definition juce_Windowing.h:53
@ hamming
Definition juce_Windowing.h:52
@ kaiser
Definition juce_Windowing.h:56
@ blackmanHarris
Definition juce_Windowing.h:54
@ triangular
Definition juce_Windowing.h:50
@ flatTop
Definition juce_Windowing.h:55
@ rectangular
Definition juce_Windowing.h:49
@ numWindowingMethods
Definition juce_Windowing.h:57
@ hann
Definition juce_Windowing.h:51
void multiplyWithWindowingTable(FloatType *samples, size_t size) noexcept
Definition juce_Windowing.cpp:169
WindowingFunction(size_t size, WindowingMethod, bool normalise=true, FloatType beta=0)
Definition juce_Windowing.cpp:39
Array< FloatType > windowTable
Definition juce_Windowing.h:105
static const char * getWindowingMethodName(WindowingMethod) noexcept
Definition juce_Windowing.cpp:175
void fillWindowingTables(size_t size, WindowingMethod type, bool normalise=true, FloatType beta=0) noexcept
Definition juce_Windowing.cpp:45
register unsigned i
Definition inflate.c:1575
#define jassertfalse
Definition juce_AudioBlock.h:29
static FloatType ncos(size_t order, size_t i, size_t size) noexcept
Definition juce_Windowing.cpp:32
Definition carla_juce.cpp:31
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
static constexpr FloatType pi
Definition juce_MathsFunctions.h:382
static double besselI0(double x) noexcept
Definition juce_SpecialFunctions.cpp:31
ulg size
Definition extract.c:2350