-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wavetable.h
250 lines (199 loc) · 5.69 KB
/
Wavetable.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#pragma once
using namespace iplug;
#define WAVETABLE_DEFAULT_SIZE 1024
#define WAVE_TYPE_NAMES "sine", "square", "triangle", "saw", "noise"
typedef double wt_datatype;
enum class WaveType {
SINE,
SQUARE,
TRIANGLE,
SAW,
NOISE,
NUM_WAVE_TYPES
};
class Wavetable
{
public:
Wavetable(int size = WAVETABLE_DEFAULT_SIZE)
{
m_iWavetableSize = size;
m_wavetable = std::make_shared<std::vector<wt_datatype>>(std::vector(size, (wt_datatype)0.0));
m_wrapMask = size - 1;
for (int i = 0; i < 128; i++)
{
m_pWavetables[i] = m_wavetable;
}
}
std::shared_ptr<std::vector<wt_datatype>> GetWavetable()
{
return m_wavetable;
}
std::shared_ptr<std::vector<wt_datatype>> GetWavetableForMidiNote(int noteId)
{
return m_pWavetables[noteId];
}
void SetWavetableForMidiNote(int noteId, std::shared_ptr<std::vector<wt_datatype>> p_wtVector)
{
m_pWavetables[noteId] = p_wtVector;
}
void ShareWavetables(int srcNote, int destNote)
{
m_pWavetables[destNote] = m_pWavetables[srcNote];
}
public:
uint32_t m_iWavetableSize;
uint32_t m_wrapMask;
private:
std::shared_ptr<std::vector<wt_datatype>> m_wavetable;
std::shared_ptr<std::vector<wt_datatype>> m_pWavetables[128];
};
class DefaultWavetables
{
public:
DefaultWavetables(int samplingRate)
{
CreateSineWavetable(m_wtSine);
CreateDefaultWavetable(m_wtSquare, WaveType::SQUARE, samplingRate);
CreateDefaultWavetable(m_wtTriangle, WaveType::TRIANGLE, samplingRate);
CreateDefaultWavetable(m_wtSaw, WaveType::SAW, samplingRate);
}
void CreateSineWavetable(Wavetable& wt)
{
std::vector<wt_datatype> &wt_vector = *(wt.GetWavetable());
for (int i = 0; i < wt.m_iWavetableSize; i++)
{
wt_vector[i] = std::sin(TWOPI * i / wt.m_iWavetableSize);
}
}
std::shared_ptr<std::vector<wt_datatype>> MakeSquareTable(int size, int numHarmonics)
{
std::vector<wt_datatype> table = std::vector(size, (wt_datatype)0.0);
for (int i = 0; i < size; i++)
{
for (int k = 0; k < numHarmonics; k++)
{
// Lanczos Sigma Factor
double x = PI * (k + 1.0) / numHarmonics;
double sigma = sin(x) / x;
double phi = TWOPI * i * (2*k + 1.0) / size;
table[i] += (1.0 / (2*k + 1.0)) * sigma * std::sin(phi);
}
}
return std::make_shared<std::vector<wt_datatype>>(table);
}
std::shared_ptr<std::vector<wt_datatype>> MakeTriangleTable(int size, int numHarmonics)
{
std::vector<wt_datatype> table = std::vector(size, (wt_datatype)0.0);
for (int i = 0; i < size; i++)
{
for (int k = 0; k < numHarmonics; k++)
{
// Lanczos Sigma Factor
double x = PI * (k + 1.0) / numHarmonics;
double sigma = sin(x) / x;
double phi = TWOPI * i * (2*k + 1.0) / size;
table[i] += std::pow(-1.0, k) * (1.0 / std::pow(2*k + 1.0, 2)) * std::sin(phi);
}
}
return std::make_shared<std::vector<wt_datatype>>(table);
}
std::shared_ptr<std::vector<wt_datatype>> MakeSawTable(int size, int numHarmonics)
{
std::vector<wt_datatype> table = std::vector(size, (wt_datatype)0.0);
for (int i = 0; i < size; i++)
{
for (int k = 1; k <= numHarmonics; k++)
{
// Lanczos Sigma Factor
double x = PI * k / numHarmonics;
double sigma = sin(x) / x;
double phi = TWOPI * i * k / size;
table[i] += std::pow(-1.0, k + 1.0) * (1.0 / k) * sigma * std::sin(phi);
}
}
return std::make_shared<std::vector<wt_datatype>>(table);
}
void CreateDefaultWavetable(Wavetable& wt, WaveType waveType, int samplingRate)
{
int numHarmonicsPrev = -1;
for (int i = 0; i < 128; i++)
{
double pitchHz = FreqForNote(i);
int numHarmonics = samplingRate / (2.0 * pitchHz);
// No. harmonics should not exceed tableSize/2
if (numHarmonics > (wt.m_iWavetableSize - 1) / 2)
{
numHarmonics = (wt.m_iWavetableSize - 1) / 2;
}
// If the number of harmonics is the same as the previous note, just reuse the wavetable
if (numHarmonics == numHarmonicsPrev)
{
//wt.SetWavetableForMidiNote(i, wt.GetWavetableForMidiNote(i - 1));
wt.ShareWavetables(i - 1, i);
continue;
}
numHarmonicsPrev = numHarmonics;
// Otherwise, make a new table
std::shared_ptr<std::vector<wt_datatype>> table = nullptr;
switch (waveType)
{
case WaveType::SQUARE:
table = MakeSquareTable(wt.m_iWavetableSize, numHarmonics);
break;
case WaveType::TRIANGLE:
table = MakeTriangleTable(wt.m_iWavetableSize, numHarmonics);
break;
case WaveType::SAW:
table = MakeSawTable(wt.m_iWavetableSize, numHarmonics);
break;
default:
table = MakeSawTable(wt.m_iWavetableSize, numHarmonics);
break;
}
wt.SetWavetableForMidiNote(i, table);
}
}
Wavetable& GetSineWavetable()
{
return m_wtSine;
}
Wavetable& GetSquareWavetable()
{
return m_wtSquare;
}
Wavetable& GetTriangleWavetable()
{
return m_wtTriangle;
}
Wavetable& GetSawWavetable()
{
return m_wtSaw;
}
Wavetable* GetWavetableFromWaveType(WaveType type)
{
switch (type)
{
case WaveType::SINE:
return &m_wtSine;
case WaveType::SQUARE:
return &m_wtSquare;
case WaveType::TRIANGLE:
return &m_wtTriangle;
case WaveType::SAW:
return &m_wtSaw;
case WaveType::NOISE:
return &m_wtSine;
default:
return &m_wtSine;
}
}
double FreqForNote(int noteNum)
{
return 440.0 * std::pow(2.0, (noteNum - 69) / 12.0);
}
private:
Wavetable m_wtSine;
Wavetable m_wtSquare;
Wavetable m_wtTriangle;
Wavetable m_wtSaw;
};