forked from neutral-labs/elmyra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOscillator.hpp
66 lines (56 loc) · 1.32 KB
/
Oscillator.hpp
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
#ifndef OSCILLATOR_HPP
#define OSCILLATOR_HPP
class Oscillator
{
private:
int index, state, frequency, halfCycleBase, halfCycleCurrent, sampleRate, slewRate, modAmount;
public:
Oscillator()
{
sampleRate = 1;
index = 0;
state = -AMP_MAX;
frequency = 1;
halfCycleBase = halfCycleCurrent = 1;
slewRate = 0;
modAmount = 0;
}
void setFreq(int mHz)
{
frequency = mHz;
halfCycleBase = halfCycleCurrent = (1000 * sampleRate) / (frequency * 2);
}
void setSampleRate(int rate)
{
sampleRate = rate;
setFreq(frequency);
}
void setModAmount(int mod)
{
modAmount = mod;
}
void setSlew(int slew)
{
slewRate = slew;
}
int getSample()
{
int maxOffset;
if (index++ >= halfCycleCurrent)
{
index = 0;
state = -state;
maxOffset = halfCycleBase / ((OSC_MOD_AMOUNT_MAX + 1) - modAmount);
halfCycleCurrent = halfCycleBase - (maxOffset / 2) + random(0, maxOffset + 1);
}
if (state < 0)
{
return state + (slewRate * AMP_MAX * index) / (OSC_SLEW_SCALE * halfCycleCurrent);
}
else
{
return state - (slewRate * AMP_MAX * index) / (OSC_SLEW_SCALE * halfCycleCurrent);
}
}
};
#endif