LMMS
Loading...
Searching...
No Matches
juce_Oscillator.h
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
36template <typename SampleType>
38{
39public:
44
46 Oscillator() = default;
47
53 Oscillator (const std::function<NumericType (NumericType)>& function,
54 size_t lookupTableNumPoints = 0)
55 {
56 initialise (function, lookupTableNumPoints);
57 }
58
60 bool isInitialised() const noexcept { return static_cast<bool> (generator); }
61
63 void initialise (const std::function<NumericType (NumericType)>& function,
64 size_t lookupTableNumPoints = 0)
65 {
66 if (lookupTableNumPoints != 0)
67 {
71 lookupTableNumPoints);
72
73 lookupTable.reset (table);
74 generator = [table] (NumericType x) { return (*table) (x); };
75 }
76 else
77 {
79 }
80 }
81
82 //==============================================================================
84 void setFrequency (NumericType newFrequency, bool force = false) noexcept
85 {
86 if (force)
87 {
88 frequency.setCurrentAndTargetValue (newFrequency);
89 return;
90 }
91
92 frequency.setTargetValue (newFrequency);
93 }
94
96 NumericType getFrequency() const noexcept { return frequency.getTargetValue(); }
97
98 //==============================================================================
100 void prepare (const ProcessSpec& spec) noexcept
101 {
102 sampleRate = static_cast<NumericType> (spec.sampleRate);
103 rampBuffer.resize ((int) spec.maximumBlockSize);
104
105 reset();
106 }
107
110 {
111 phase.reset();
112
113 if (sampleRate > 0)
114 frequency.reset (sampleRate, 0.05);
115 }
116
117 //==============================================================================
119 SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType input) noexcept
120 {
122 auto increment = MathConstants<NumericType>::twoPi * frequency.getNextValue() / sampleRate;
123 return input + generator (phase.advance (increment) - MathConstants<NumericType>::pi);
124 }
125
127 template <typename ProcessContext>
128 void process (const ProcessContext& context) noexcept
129 {
131 auto&& outBlock = context.getOutputBlock();
132 auto&& inBlock = context.getInputBlock();
133
134 // this is an output-only processor
135 jassert (outBlock.getNumSamples() <= static_cast<size_t> (rampBuffer.size()));
136
137 auto len = outBlock.getNumSamples();
138 auto numChannels = outBlock.getNumChannels();
139 auto inputChannels = inBlock.getNumChannels();
140 auto baseIncrement = MathConstants<NumericType>::twoPi / sampleRate;
141
142 if (context.isBypassed)
143 context.getOutputBlock().clear();
144
145 if (frequency.isSmoothing())
146 {
147 auto* buffer = rampBuffer.getRawDataPointer();
148
149 for (size_t i = 0; i < len; ++i)
150 buffer[i] = phase.advance (baseIncrement * frequency.getNextValue())
152
153 if (! context.isBypassed)
154 {
155 size_t ch;
156
157 if (context.usesSeparateInputAndOutputBlocks())
158 {
159 for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
160 {
161 auto* dst = outBlock.getChannelPointer (ch);
162 auto* src = inBlock.getChannelPointer (ch);
163
164 for (size_t i = 0; i < len; ++i)
165 dst[i] = src[i] + generator (buffer[i]);
166 }
167 }
168 else
169 {
170 for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
171 {
172 auto* dst = outBlock.getChannelPointer (ch);
173
174 for (size_t i = 0; i < len; ++i)
175 dst[i] += generator (buffer[i]);
176 }
177 }
178
179 for (; ch < numChannels; ++ch)
180 {
181 auto* dst = outBlock.getChannelPointer (ch);
182
183 for (size_t i = 0; i < len; ++i)
184 dst[i] = generator (buffer[i]);
185 }
186 }
187 }
188 else
189 {
190 auto freq = baseIncrement * frequency.getNextValue();
191 auto p = phase;
192
193 if (context.isBypassed)
194 {
195 frequency.skip (static_cast<int> (len));
196 p.advance (freq * static_cast<NumericType> (len));
197 }
198 else
199 {
200 size_t ch;
201
202 if (context.usesSeparateInputAndOutputBlocks())
203 {
204 for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
205 {
206 p = phase;
207 auto* dst = outBlock.getChannelPointer (ch);
208 auto* src = inBlock.getChannelPointer (ch);
209
210 for (size_t i = 0; i < len; ++i)
211 dst[i] = src[i] + generator (p.advance (freq) - MathConstants<NumericType>::pi);
212 }
213 }
214 else
215 {
216 for (ch = 0; ch < jmin (numChannels, inputChannels); ++ch)
217 {
218 p = phase;
219 auto* dst = outBlock.getChannelPointer (ch);
220
221 for (size_t i = 0; i < len; ++i)
222 dst[i] += generator (p.advance (freq) - MathConstants<NumericType>::pi);
223 }
224 }
225
226 for (; ch < numChannels; ++ch)
227 {
228 p = phase;
229 auto* dst = outBlock.getChannelPointer (ch);
230
231 for (size_t i = 0; i < len; ++i)
232 dst[i] = generator (p.advance (freq) - MathConstants<NumericType>::pi);
233 }
234 }
235
236 phase = p;
237 }
238 }
239
240private:
241 //==============================================================================
243 std::unique_ptr<LookupTableTransform<NumericType>> lookupTable;
248};
249
250} // namespace dsp
251} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_Array.h:56
Definition juce_SmoothedValue.h:227
Definition juce_LookupTable.h:175
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType input) noexcept
Definition juce_Oscillator.h:119
Array< NumericType > rampBuffer
Definition juce_Oscillator.h:244
SmoothedValue< NumericType > frequency
Definition juce_Oscillator.h:245
Oscillator(const std::function< NumericType(NumericType)> &function, size_t lookupTableNumPoints=0)
Definition juce_Oscillator.h:53
void prepare(const ProcessSpec &spec) noexcept
Definition juce_Oscillator.h:100
bool isInitialised() const noexcept
Definition juce_Oscillator.h:60
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition juce_Oscillator.h:43
Phase< NumericType > phase
Definition juce_Oscillator.h:247
void reset() noexcept
Definition juce_Oscillator.h:109
NumericType getFrequency() const noexcept
Definition juce_Oscillator.h:96
void process(const ProcessContext &context) noexcept
Definition juce_Oscillator.h:128
void setFrequency(NumericType newFrequency, bool force=false) noexcept
Definition juce_Oscillator.h:84
void initialise(const std::function< NumericType(NumericType)> &function, size_t lookupTableNumPoints=0)
Definition juce_Oscillator.h:63
std::unique_ptr< LookupTableTransform< NumericType > > lookupTable
Definition juce_Oscillator.h:243
std::function< NumericType(NumericType)> generator
Definition juce_Oscillator.h:242
NumericType sampleRate
Definition juce_Oscillator.h:246
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
JHUFF_TBL long freq[]
Definition jchuff.h:50
#define jassert(expression)
#define JUCE_VECTOR_CALLTYPE
Definition juce_dsp.h:100
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
jack_client_t client jack_client_t client jack_client_t client jack_client_t JackInfoShutdownCallback function
Definition juce_linux_JackAudio.cpp:63
@ table
Definition juce_AccessibilityRole.h:50
static constexpr FloatType twoPi
Definition juce_MathsFunctions.h:385
static constexpr FloatType pi
Definition juce_MathsFunctions.h:382
Definition juce_Phase.h:41
Definition juce_ProcessContext.h:38
T Type
Definition juce_AudioBlock.h:37
uch * p
Definition crypt.c:594
#define const
Definition zconf.h:137