-
Notifications
You must be signed in to change notification settings - Fork 2
Jan/fix forcing lfo change #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a908245
7e5d420
9c44023
9accab9
7c460d9
4572798
4309c28
e86ad51
71ba2a1
0992122
991f6e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,16 @@ | ||
| #pragma once | ||
|
|
||
| namespace tremolo { | ||
| enum class ApplySmoothing { no, yes }; | ||
|
|
||
| class Tremolo { | ||
| public: | ||
| enum class LfoWaveform : size_t { | ||
| sine = 0, | ||
| triangle = 1, | ||
| }; | ||
|
|
||
| Tremolo() { | ||
| for (auto& lfo : lfos) { | ||
| lfo.setFrequency(5.f /* Hz */, true); | ||
| } | ||
| } | ||
| Tremolo() { setModulationRateHz(5.f, ApplySmoothing::no); } | ||
|
|
||
| void prepare(double sampleRate, int expectedMaxFramesPerBlock) { | ||
| const juce::dsp::ProcessSpec processSpec{ | ||
|
|
@@ -31,16 +29,24 @@ class Tremolo { | |
| lfoSamples.resize(4u * static_cast<size_t>(expectedMaxFramesPerBlock)); | ||
| } | ||
|
|
||
| void setModulationRate(float rateHz) noexcept { | ||
| void setModulationRateHz( | ||
| float rateHz, | ||
| ApplySmoothing applySmoothing = ApplySmoothing::yes) noexcept { | ||
| const auto force = applySmoothing == ApplySmoothing::no; | ||
| for (auto& lfo : lfos) { | ||
| lfo.setFrequency(rateHz); | ||
| lfo.setFrequency(rateHz, force); | ||
| } | ||
| } | ||
|
|
||
| void setLfoWaveform(LfoWaveform waveform) { | ||
| void setLfoWaveform(LfoWaveform waveform, | ||
| ApplySmoothing applySmoothing = ApplySmoothing::yes) { | ||
| jassert(waveform == LfoWaveform::sine || waveform == LfoWaveform::triangle); | ||
|
|
||
| lfoToSet = waveform; | ||
|
|
||
| if (applySmoothing == ApplySmoothing::no) { | ||
| currentLfo = waveform; | ||
| } | ||
| } | ||
|
|
||
| void process(juce::AudioBuffer<float>& buffer) noexcept { | ||
|
|
@@ -118,9 +124,14 @@ class Tremolo { | |
| static constexpr auto modulationDepth = 0.4f; | ||
|
|
||
| static float triangle(float phase) { | ||
| // offset the phase by pi/2 to return 0 if phase equals 0 | ||
| // and match the sine waveform | ||
| // (otherwise, the waveform starts at 1) | ||
| const auto offsetPhase = phase - juce::MathConstants<float>::halfPi; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider instead introducing a new variable with a different name (maybe just
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| // Source: | ||
| // https://thewolfsound.com/sine-saw-square-triangle-pulse-basic-waveforms-in-synthesis/#triangle | ||
| const auto ft = phase / juce::MathConstants<float>::twoPi; | ||
| const auto ft = offsetPhase / juce::MathConstants<float>::twoPi; | ||
| return 4.f * std::abs(ft - std::floor(ft + 0.5f)) - 1.f; | ||
| } | ||
|
|
||
|
|
@@ -146,7 +157,10 @@ class Tremolo { | |
| } | ||
|
|
||
| std::array<juce::dsp::Oscillator<float>, 2u> lfos{ | ||
| juce::dsp::Oscillator<float>{[](auto phase) { return std::sin(phase); }}, | ||
| juce::dsp::Oscillator<float>{[](auto phase) { | ||
| // start phase is -pi -> change it to 0 to match the mathematical sine | ||
| return std::sin(phase + juce::MathConstants<float>::pi); | ||
| }}, | ||
| juce::dsp::Oscillator<float>{triangle}}; | ||
|
|
||
| LfoWaveform currentLfo = LfoWaveform::sine; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again, maybe this should be named
setModulationRateHzso that the units are slightly more obviousThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
For more clarity, I'd use the
ClosedRangeValue<float>orFrequencyclasses from mywolfsound_dsp_utilslibrary. Then, you could writeor even
I don't like using
floats as parameters in general!