mirror of
https://codeberg.org/armin/chromaflock.git
synced 2026-09-01 04:10:47 +02:00
Add 11 additional oscillator waveforms
This commit is contained in:
parent
c5a624f559
commit
0e9a90a89b
2 changed files with 67 additions and 3 deletions
|
|
@ -2,7 +2,25 @@
|
|||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
enum class Waveform { Sine = 0, Saw, Square, Triangle, Noise, NumWaveforms };
|
||||
enum class Waveform {
|
||||
Sine = 0,
|
||||
Saw,
|
||||
Square,
|
||||
Triangle,
|
||||
Noise,
|
||||
Pulse25,
|
||||
Pulse12,
|
||||
ReverseSaw,
|
||||
FullRectSine,
|
||||
HalfRectSine,
|
||||
Stair4,
|
||||
Stair8,
|
||||
SoftSine,
|
||||
Sinc,
|
||||
ExpPulse,
|
||||
DoubleSine,
|
||||
NumWaveforms
|
||||
};
|
||||
|
||||
class Oscillator {
|
||||
public:
|
||||
|
|
@ -46,6 +64,48 @@ public:
|
|||
case Waveform::Noise:
|
||||
output = random.nextFloat() * 2.0f - 1.0f;
|
||||
break;
|
||||
case Waveform::Pulse25:
|
||||
output = (phase < 0.25f) ? 1.0f : -1.0f;
|
||||
break;
|
||||
case Waveform::Pulse12:
|
||||
output = (phase < 0.12f) ? 1.0f : -1.0f;
|
||||
break;
|
||||
case Waveform::ReverseSaw:
|
||||
output = 2.0f * phase - 1.0f;
|
||||
break;
|
||||
case Waveform::FullRectSine:
|
||||
output = std::fabs(std::sin(phase * 2.0f * pi));
|
||||
break;
|
||||
case Waveform::HalfRectSine:
|
||||
output = std::max(0.0f, std::sin(phase * 2.0f * pi));
|
||||
break;
|
||||
case Waveform::Stair4: {
|
||||
float s = std::floor(phase * 4.0f);
|
||||
output = -1.0f + 2.0f * ((s + 0.5f) / 4.0f);
|
||||
break;
|
||||
}
|
||||
case Waveform::Stair8: {
|
||||
float s = std::floor(phase * 8.0f);
|
||||
output = -1.0f + 2.0f * ((s + 0.5f) / 8.0f);
|
||||
break;
|
||||
}
|
||||
case Waveform::SoftSine:
|
||||
output = std::tanh(3.0f * std::sin(phase * 2.0f * pi)) / std::tanh(3.0f);
|
||||
break;
|
||||
case Waveform::Sinc: {
|
||||
float x = phase * 2.0f * pi * 6.0f;
|
||||
output = (x != 0.0f) ? std::sin(x) / x : 1.0f;
|
||||
break;
|
||||
}
|
||||
case Waveform::ExpPulse:
|
||||
output = (phase < 0.5f)
|
||||
? std::exp(-phase * 10.0f)
|
||||
: -std::exp(-(phase - 0.5f) * 10.0f);
|
||||
break;
|
||||
case Waveform::DoubleSine:
|
||||
output = 0.5f * std::sin(phase * 2.0f * pi)
|
||||
+ 0.5f * std::sin(phase * 4.0f * pi);
|
||||
break;
|
||||
default:
|
||||
output = 0.0f;
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue