-
Notifications
You must be signed in to change notification settings - Fork 0
/
am_Oscillators.h
152 lines (124 loc) · 2.67 KB
/
am_Oscillators.h
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
==============================================================================
am_Oscillators.h
Created: 21 Nov 2022 10:01:20am
Author: Andrew McGillivray
==============================================================================
*/
#pragma once
#ifndef Oscillators_h
#define Oscillators_h
#include <cmath>
/**
* NOTE: When using this class and its inheritance classes, all gains are set to oscillate between 1.0dB and -1.0dB.
*/
class Oscillator
{
public:
/**Sets up a basic oscillator completely
* @param sample rate in Hz
* @param frequency in Hz
* @param wave index: 0 = Phasor, 1 = Sin, 2 = Square, 3 = Triangle, 4 = Sawtooth
*/
void setUp(float sampleRate, float _freq, int waveIndex)
{
setSampleRate(sampleRate);
setFrequency(_freq);
setWaveIndex(waveIndex);
}
//Individual Functions
//set the sample rate in Hz
void setSampleRate(float _sampleRate)
{
sampleRate = _sampleRate;
}
//set the frequency of the wave in Hz
void setFrequency(float _freq)
{
freq = _freq;
phaseDelta = freq / sampleRate;
}
//sets where the phase will start
void setOffset(float offset)
{
phaseOffset = offset;
}
//Specific Functions
//set the wave index: 0 = Phasor, 1 = Sin, 2 = Square, 3 = Triangle, 4 = Sawtooth
void setWaveIndex(int waveIndex)
{
waveIndexVal = waveIndex;
}
//set phi value for a sine wave
void setPhi(float phiInput)
{
phi = phiInput;
}
//set the phi modulation value
void setPhiMod(float modInput)
{
phiMod = modInput;
}
//set phase width for a square wave
void setPhaseWidth(float _pw)
{
pw = _pw;
}
//Output Functions
//creates a wave
//for Wave Index: 0 = Phasor, 1 = Sin, 2 = Square, 3 = Triangle, 4 = Sawtooth
float process()
{
phase += (phaseDelta + phaseOffset);
//wrap the phase
if (phase > 1.0)
{
phase -= 1.0;
}
//phasor
if (waveIndexVal == 0)
{
return phase;
}
//sine wave
if (waveIndexVal == 1)
{
return std::sin(phase * 2.0 * 3.14159 + (phi * phiMod));
}
//square wave
if (waveIndexVal == 2)
{
if (phase > pw)
{
outVal = -1.0f;
}
else
{
outVal = 1.0f;
}
return outVal;
}
//triangle wave
if (waveIndexVal == 3)
{
return 4 * (fabs(phase - 0.5f) - 0.25);
}
//sawtooth wave
if (waveIndexVal == 4)
{
return 2 * (phase - 0.5f);
}
}
private:
float freq;
float phase = 0.0f;
float phaseDelta;
float phaseOffset = 0.0f;
float sampleRate;
int waveIndexVal = 0;
float phi = 0;
float phiMod = 1;
float outVal;
float pw = 0.5;
};
#endif /* Oscillators_h */