-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4f21c2
commit d2793ad
Showing
10 changed files
with
518 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,41 @@ | ||
#ifndef SIGNALSMITH_DSP_COMMON_H | ||
#define SIGNALSMITH_DSP_COMMON_H | ||
|
||
#ifndef M_PI | ||
#define M_PI 3.14159265358979323846264338327950288 | ||
#endif | ||
|
||
namespace signalsmith { | ||
/** @defgroup Common Common | ||
@brief helper classes used by the rest of the library | ||
@{ | ||
@file | ||
*/ | ||
// | ||
// /// Helpful base-class for anything which could be either fixed-size or variable size (`-1`) | ||
// template<int size=-1> | ||
// class Size { | ||
// public: | ||
// Size() {} | ||
// | ||
// static constexpr int size() { | ||
// return size; | ||
// } | ||
// } | ||
// | ||
// /// Variable-size specialisation | ||
// template<> | ||
// class Size<-1> { | ||
// int _size; | ||
// public: | ||
// Size(int size=0) : _size(size) {} | ||
// | ||
// int size() const { | ||
// return _size; | ||
// } | ||
// } | ||
|
||
/** @} */ | ||
} // signalsmith:: | ||
#endif // include guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#ifndef SIGNALSMITH_DSP_ENVELOPES_H | ||
#define SIGNALSMITH_DSP_ENVELOPES_H | ||
|
||
#include "./common.h" | ||
|
||
#include <cmath> | ||
#include <random> | ||
|
||
namespace signalsmith { | ||
namespace envelopes { | ||
/** @defgroup Envelopes Envelopes and LFOs | ||
@brief Envelopes, mostly fast and approximate | ||
@{ | ||
@file | ||
*/ | ||
|
||
/** A cheap vaguely-sine-like LFO with random speed variation. | ||
Currently based on a cubic polynomial, but may change in future. | ||
It supports random variation in the rate, and the depth. It will always remain bounded by the limits you specify (once it updates) so randomising the depth will produce smaller oscillations on average. | ||
*/ | ||
class CheapLfo { | ||
float ratio = 0; | ||
float ratioStep = 0; | ||
|
||
float valueFrom = 0, valueTo = 1, valueRange = 1; | ||
float targetLow = 0, targetHigh = 1; | ||
float targetRate = 0; | ||
float rateRandom = 0.5, depthRandom = 0; | ||
bool freshReset = true; | ||
|
||
std::default_random_engine randomEngine; | ||
std::uniform_real_distribution<float> randomUnit; | ||
float random() { | ||
return randomUnit(randomEngine); | ||
} | ||
float randomRate() { | ||
return targetRate*exp(rateRandom*(random() - 0.5)); | ||
} | ||
float randomTarget(float previous) { | ||
float randomOffset = depthRandom*random()*(targetLow - targetHigh); | ||
if (previous < (targetLow + targetHigh)*0.5f) { | ||
return targetHigh + randomOffset; | ||
} else { | ||
return targetLow - randomOffset; | ||
} | ||
} | ||
public: | ||
CheapLfo() : randomEngine(std::random_device()()), randomUnit(0, 1) { | ||
reset(); | ||
} | ||
|
||
/// Resets the LFO state, with random phase. | ||
void reset() { | ||
ratio = random(); | ||
ratioStep = randomRate(); | ||
if (random() < 0.5) { | ||
valueFrom = targetLow; | ||
valueTo = targetHigh; | ||
} else { | ||
valueFrom = targetHigh; | ||
valueTo = targetLow; | ||
} | ||
valueRange = valueTo - valueFrom; | ||
freshReset = true; | ||
} | ||
/** Smoothly updates the LFO parameters. | ||
If called directly after `.reset()`, oscillation will immediately start within the specified range. Otherwise, it will follow the new parameters after at most one cycle. | ||
The LFO will complete a full oscillation in (approximately) `1/rate` samples. `rateVariation` can be any number, but 0-1 is a good range. `depthVariation` must be in [0, 1], where 0.5 has random amplitude but still alternates up/down. | ||
*/ | ||
void set(float low, float high, float rate, float rateVariation=0.5, float depthVariation=0) { | ||
rate *= 2; // We want to go up and down during this period | ||
targetRate = rate; | ||
targetLow = std::min(low, high); | ||
targetHigh = std::max(low, high); | ||
rateRandom = rateVariation; | ||
depthRandom = depthVariation; | ||
|
||
// If we haven't called .next() yet, don't bother being smooth. | ||
if (freshReset) return reset(); | ||
|
||
// Only update the current rate if it's outside our new random-variation range | ||
float maxRandomRatio = exp((float)0.5*rateRandom); | ||
if (ratioStep > rate*maxRandomRatio || ratioStep < rate/maxRandomRatio) { | ||
ratioStep = randomRate(); | ||
} | ||
} | ||
|
||
/// get the next output sample | ||
float next() { | ||
freshReset = false; | ||
float result = ratio*ratio*(3 - 2*ratio)*valueRange + valueFrom; | ||
|
||
ratio += ratioStep; | ||
if (ratio >= 1) { | ||
ratio = 0; | ||
ratioStep = randomRate(); | ||
valueFrom = valueTo; | ||
valueTo = randomTarget(valueFrom); | ||
valueRange = valueTo - valueFrom; | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
|
||
/** @} */ | ||
}} // signalsmith::envelopes:: | ||
#endif // include guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.