-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathswe_synth_osc.cpp
More file actions
67 lines (55 loc) · 1.71 KB
/
swe_synth_osc.cpp
File metadata and controls
67 lines (55 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "swe_synth_osc.h"
namespace olc::sound::synth
{
///[OLC_HM] START SYNTH_OSCILLATOR_CPP
namespace modules
{
void Oscillator::Update(uint32_t nChannel, double dTime, double dTimeStep)
{
// We use phase accumulation to combat change in parameter glitches
double w = frequency.value * max_frequency * dTimeStep;
phase_acc += w + lfo_input.value * frequency.value;
if (phase_acc >= 2.0) phase_acc -= 2.0;
switch (waveform)
{
case Type::Sine:
output = amplitude.value * sin(phase_acc * 2.0 * 3.14159);
break;
case Type::Saw:
output = amplitude.value * (phase_acc - 1.0) * 2.0;
break;
case Type::Square:
output = amplitude.value * ((phase_acc >= 1.0) ? 1.0 : -1.0);
break;
case Type::Triangle:
output = amplitude.value * ((phase_acc < 1.0) ? (phase_acc * 0.5) : (1.0 - phase_acc * 0.5));
break;
case Type::PWM:
output = amplitude.value * ((phase_acc >= (parameter.value + 1.0)) ? 1.0 : -1.0);
break;
case Type::Wave:
if(pWave != nullptr)
output = amplitude.value * pWave->vChannelView[nChannel].GetSample(phase_acc * 0.5 * pWave->file.durationInSamples());
break;
case Type::Noise:
output = amplitude.value * rndDouble(-1.0, 1.0);
break;
}
}
double Oscillator::rndDouble(double min, double max)
{
return ((double)rnd() / (double)(0x7FFFFFFF)) * (max - min) + min;
}
uint32_t Oscillator::rnd()
{
random_seed += 0xe120fc15;
uint64_t tmp;
tmp = (uint64_t)random_seed * 0x4a39b70d;
uint32_t m1 = uint32_t(((tmp >> 32) ^ tmp) & 0xFFFFFFFF);
tmp = (uint64_t)m1 * 0x12fad5c9;
uint32_t m2 = uint32_t(((tmp >> 32) ^ tmp) & 0xFFFFFFFF);
return m2;
}
}
///[OLC_HM] END SYNTH_OSCILLATOR_CPP
}