Skip to content

Commit 7ca9e00

Browse files
committed
LFOProcessor: set up initial scaffolding for noise function generator support.
1 parent ac12120 commit 7ca9e00

File tree

4 files changed

+119
-39
lines changed

4 files changed

+119
-39
lines changed

demo/source/demos/EffectChainDemo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ EffectChainDemo::EffectChainDemo (SharedObjects& sharedObjs) :
390390
{
391391
label.setJustificationType (Justification::centredLeft);
392392
label.setColour (Label::backgroundColourId, Colours::transparentBlack);
393-
label.setColour (Label::textColourId, Colours::black);
394393
addAndMakeVisible (label);
395394
};
396395

@@ -514,7 +513,7 @@ void EffectChainDemo::resized()
514513
b.removeFromTop (margin);
515514

516515
{
517-
auto row = b.removeFromTop (32);
516+
auto row = b.removeFromTop (64);
518517
filePath.setBounds (row.removeFromLeft (row.getWidth() / 2));
519518
timeDisplay.setBounds (row);
520519
}
@@ -531,7 +530,7 @@ void EffectChainDemo::paint (Graphics& g)
531530
const auto lengthSeconds = audioThumbnail->getTotalLength();
532531
const auto timeSeconds = transport->getCurrentTimeSeconds();
533532

534-
g.setColour (Colours::darkgrey);
533+
g.setColour (filePath.findColour (Label::textColourId));
535534
audioThumbnail->drawChannels (g, audioThumbnailArea, 0.0, lengthSeconds, 0.9f);
536535

537536
g.setColour (Colours::dodgerblue);

modules/squarepine_audio/effects/LFOProcessor.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ LFOProcessor::LFOProcessor (double minFreqHz, double maxFreqHz,
3131
InternalProcessor (false),
3232
isMultiplying (isMult)
3333
{
34+
jassert (minFreqHz > 0.0f);
35+
jassert (maxFreqHz > minFreqHz);
36+
3437
auto layout = createDefaultParameterLayout();
3538

3639
auto tp = std::make_unique<TypeParameter>();
@@ -54,13 +57,17 @@ void LFOProcessor::setLFOType (dsp::Oscillator<FloatType>& osc, LFOType lfoType)
5457

5558
switch (lfoType)
5659
{
57-
case LFOType::sine: func = (FloatType (*) (FloatType)) &std::sin; break;
58-
case LFOType::cosine: func = (FloatType (*) (FloatType)) &std::cos; break;
59-
case LFOType::tangent: func = (FloatType (*) (FloatType)) &std::tan; break;
60-
case LFOType::triangle: func = oscillatorFunctions::triangle<FloatType>; break;
61-
case LFOType::ramp: func = oscillatorFunctions::ramp<FloatType>; break;
62-
case LFOType::sawtooth: func = oscillatorFunctions::saw<FloatType>; break;
63-
case LFOType::square: func = oscillatorFunctions::square<FloatType>; break;
60+
case LFOType::sine: func = (FloatType (*) (FloatType)) &std::sin; break;
61+
case LFOType::cosine: func = (FloatType (*) (FloatType)) &std::cos; break;
62+
case LFOType::tangent: func = (FloatType (*) (FloatType)) &std::tan; break;
63+
case LFOType::triangle: func = oscillatorFunctions::triangle<FloatType>; break;
64+
case LFOType::ramp: func = oscillatorFunctions::ramp<FloatType>; break;
65+
case LFOType::sawtooth: func = oscillatorFunctions::saw<FloatType>; break;
66+
case LFOType::square: func = oscillatorFunctions::square<FloatType>; break;
67+
case LFOType::whiteNoise: func = [this] (FloatType v) { return whiteNoiseGenerator.process (v); }; break;
68+
case LFOType::pinkNoise: func = [this] (FloatType v) { return pinkNoiseGenerator.process (v); }; break;
69+
case LFOType::blueNoise: func = [this] (FloatType v) { return blueNoiseGenerator.process (v); }; break;
70+
case LFOType::brownianNoise: func = [this] (FloatType v) { return brownianNoiseGenerator.process (v); }; break;
6471

6572
default:
6673
jassertfalse;
@@ -80,23 +87,13 @@ void LFOProcessor::setLFOType (LFOType lfoType, bool force)
8087
setLFOType (doubleOsc, lfoType);
8188
}
8289

83-
void LFOProcessor::setLFOType (LFOType lfoType)
84-
{
85-
setLFOType (lfoType, true);
86-
}
87-
88-
void LFOProcessor::setFrequencyHz (const double newFrequency)
89-
{
90-
*frequency = (float) newFrequency;
91-
}
92-
93-
void LFOProcessor::setFrequencyFromMidiNote (const int midiNote)
94-
{
95-
setFrequencyHz (MidiMessage::getMidiNoteInHertz (midiNote));
96-
}
97-
98-
double LFOProcessor::getFrequency() const { return frequency->get(); }
99-
LFOProcessor::LFOType LFOProcessor::getLFOType() const { return (LFOType) type->getIndex(); }
90+
void LFOProcessor::setLFOType (LFOType lfoType) { setLFOType (lfoType, true); }
91+
void LFOProcessor::setFrequencyHz (double newFrequency) { *frequency = (float) newFrequency; }
92+
void LFOProcessor::setFrequency (const Pitch& pitch) { setFrequencyHz (pitch.getFrequencyHz()); }
93+
void LFOProcessor::setFrequencyFromMidiNote (int midiNote) { setFrequencyHz (MidiMessage::getMidiNoteInHertz (midiNote)); }
94+
double LFOProcessor::getFrequencyHz() const { return frequency->get(); }
95+
Pitch LFOProcessor::getFrequencyPitch() const { return getFrequencyHz(); }
96+
LFOProcessor::LFOType LFOProcessor::getLFOType() const { return (LFOType) type->getIndex(); }
10097

10198
//==============================================================================
10299
void LFOProcessor::prepareToPlay (const double newSampleRate, const int samplesPerBlock)
@@ -132,7 +129,7 @@ void LFOProcessor::process (dsp::Oscillator<FloatType>& osc,
132129
juce::AudioBuffer<FloatType>& buffer)
133130
{
134131
{
135-
auto v = getFrequency();
132+
const auto v = getFrequencyHz();
136133
floatOsc.setFrequency ((float) v);
137134
doubleOsc.setFrequency (v);
138135
}

modules/squarepine_audio/effects/LFOProcessor.h

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//==============================================================================
12
namespace oscillatorFunctions
23
{
34
/** */
@@ -29,8 +30,77 @@ namespace oscillatorFunctions
2930
constexpr auto one = static_cast<FloatType> (1);
3031
return phase > static_cast<FloatType> (0) ? one : -one;
3132
}
33+
34+
//==============================================================================
35+
/** */
36+
struct NoiseFunctionGenerator
37+
{
38+
NoiseFunctionGenerator() noexcept = default;
39+
virtual ~NoiseFunctionGenerator() noexcept = default;
40+
};
41+
42+
/** White noise generator using Gaussian distribution. */
43+
struct WhiteNoiseGenerator final : NoiseFunctionGenerator
44+
{
45+
WhiteNoiseGenerator() noexcept = default;
46+
47+
template<typename FloatType>
48+
FloatType process (FloatType) noexcept { return static_cast<FloatType> (dist (generator)); }
49+
50+
private:
51+
std::default_random_engine generator;
52+
std::normal_distribution<double> dist;
53+
54+
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WhiteNoiseGenerator)
55+
};
56+
57+
/** Pink noise generator. */
58+
struct PinkNoiseGenerator final : NoiseFunctionGenerator
59+
{
60+
PinkNoiseGenerator() noexcept = default;
61+
62+
template<typename FloatType>
63+
FloatType process (FloatType) noexcept { return static_cast<FloatType> (dist (generator)); }
64+
65+
private:
66+
std::default_random_engine generator;
67+
std::normal_distribution<double> dist;
68+
69+
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PinkNoiseGenerator)
70+
};
71+
72+
/** Blue noise generator. */
73+
struct BlueNoiseGenerator final : NoiseFunctionGenerator
74+
{
75+
BlueNoiseGenerator() noexcept = default;
76+
77+
template<typename FloatType>
78+
FloatType process (FloatType) noexcept { return static_cast<FloatType> (dist (generator)); }
79+
80+
private:
81+
std::default_random_engine generator;
82+
std::normal_distribution<double> dist;
83+
84+
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BlueNoiseGenerator)
85+
};
86+
87+
/** Brownian noise generator. */
88+
struct BrownianNoiseGenerator final : NoiseFunctionGenerator
89+
{
90+
BrownianNoiseGenerator() noexcept = default;
91+
92+
template<typename FloatType>
93+
FloatType process (FloatType) noexcept { return static_cast<FloatType> (dist (generator)); }
94+
95+
private:
96+
std::default_random_engine generator;
97+
std::normal_distribution<double> dist;
98+
99+
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BrownianNoiseGenerator)
100+
};
32101
}
33102

103+
//==============================================================================
34104
/** A processor that can act as an LFO or a function generator.
35105
36106
This acts as an LFO by default. You may want to use its
@@ -65,23 +135,32 @@ class LFOProcessor final : public InternalProcessor
65135
triangle,
66136
ramp,
67137
sawtooth,
68-
square
138+
square,
139+
whiteNoise,
140+
pinkNoise,
141+
blueNoise,
142+
brownianNoise
69143
};
70144

71-
/** */
145+
/** Changes the current LFO function. */
72146
void setLFOType (LFOType);
73147

74-
/** */
148+
/** @returns the currently used LFO function. s*/
75149
LFOType getLFOType() const;
76150

77-
/** */
151+
/** Changes the frequency of the LFO in Hz. */
78152
void setFrequencyHz (double);
79153

80-
/** */
154+
/** Changes the frequency of the LFO using a MIDI note pitch. */
81155
void setFrequencyFromMidiNote (int);
82156

83-
/** */
84-
double getFrequency() const;
157+
/** Changes the frequency of the LFO using a simple pitch. */
158+
void setFrequency (const Pitch&);
159+
160+
/** @returns the currently set frequency in Hz. */
161+
double getFrequencyHz() const;
162+
/** @returns the currently set frequency as a Pitch object. */
163+
Pitch getFrequencyPitch() const;
85164

86165
//==============================================================================
87166
/** @internal */
@@ -100,6 +179,11 @@ class LFOProcessor final : public InternalProcessor
100179
const bool isMultiplying = true;
101180
bool isFirstRun = true;
102181

182+
oscillatorFunctions::WhiteNoiseGenerator whiteNoiseGenerator;
183+
oscillatorFunctions::PinkNoiseGenerator pinkNoiseGenerator;
184+
oscillatorFunctions::BlueNoiseGenerator blueNoiseGenerator;
185+
oscillatorFunctions::BrownianNoiseGenerator brownianNoiseGenerator;
186+
103187
juce::AudioBuffer<float> floatMulter;
104188
juce::AudioBuffer<double> doubleMulter;
105189

modules/squarepine_audio/squarepine_audio.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ namespace sp
138138
#include "core/LastKnownPluginDetails.h"
139139
#include "core/MetadataUtilities.h"
140140
#include "core/MIDIChannel.h"
141+
#include "music/Chord.h"
142+
#include "music/Genre.h"
143+
#include "music/Pitch.h"
144+
#include "music/Scale.h"
141145
#include "codecs/ALACAudioFormat.h"
142146
#include "codecs/REXAudioFormat.h"
143147
#include "devices/DummyAudioIODevice.h"
@@ -169,10 +173,6 @@ namespace sp
169173
#include "graphics/GraphObserver.h"
170174
#include "graphics/Meter.h"
171175
#include "graphics/ProgramAudioProcessorEditor.h"
172-
#include "music/Chord.h"
173-
#include "music/Genre.h"
174-
#include "music/Pitch.h"
175-
#include "music/Scale.h"
176176
#include "resamplers/Resampler.h"
177177
#include "resamplers/ResamplingAudioFormatReader.h"
178178
#include "resamplers/ResamplingProcessor.h"

0 commit comments

Comments
 (0)