-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatWaveTableOsc.hpp
213 lines (176 loc) · 5.62 KB
/
FloatWaveTableOsc.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
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
//
// WaveTableOsc.h
//
// Created by Nigel Redmon on 2018-10-05
// EarLevel Engineering: earlevel.com
// Copyright 2018 Nigel Redmon
//
// For a complete explanation of the wavetable oscillator and code,
// read the series of articles by the author, starting here:
// www.earlevel.com/main/2012/05/03/a-wavetable-oscillator—introduction/
//
// This version has optimizations described here:
// www.earlevel.com/main/2019/04/28/wavetableosc-optimized/
//
// License:
//
// This source code is provided as is, without warranty.
// You may copy and distribute verbatim copies of this document.
// You may modify and use this source code to create binary code for your own purposes, free or commercial.
//
#ifndef FloatWaveTableOsc_h
#define FloatWaveTableOsc_h
#include <Arduino.h>
class FloatWaveTableOsc {
public:
FloatWaveTableOsc(void) {
for (int idx = 0; idx < numWaveTableSlots; idx++) {
mWaveTables[idx].waveTableLen = 0;
mWaveTables[idx].waveTable = 0;
}
}
~FloatWaveTableOsc(void) {
for (int idx = 0; idx < numWaveTableSlots; idx++) {
float *temp = mWaveTables[idx].waveTable;
if (temp != 0)
delete [] temp;
}
}
void SetFrequency(double freq, double sampleRate)
{
SetFrequency (freq/sampleRate);
}
//
// SetFrequency: Set normalized frequency, typically 0-0.5 (must be positive and less than 1!)
//
void SetFrequency(float inc) {
mPhaseInc = inc;
// update the current wave table selector
}
//
// SetPhaseOffset: Phase offset for PWM, 0-1
//
void SetPhaseOffset(float offset) {
mPhaseOfs = offset;
}
//
// UpdatePhase: Call once per sample
//
void UpdatePhase(void) {
mPhasor += mPhaseInc;
if (mPhasor >= 1.0)
mPhasor -= 1.0;
}
//
// Process: Update phase and get output
//
float Process(void) {
UpdatePhase();
if(mPhaseOfs!=0)
{
return GetOutputMinusOffset();
}
else
{
return GetOutput();
}
}
//
// GetOutput: Returns the current oscillator output
//
float GetOutput(void) {
waveTable *waveTable = &mWaveTables[mCurWaveTable];
// linear interpolation
float temp = mPhasor * (waveTable->waveTableLen);
int32_t intPart = floor(temp);
float fracPart = temp - intPart;
float samp0 = (waveTable->waveTable[intPart]);
float samp1 = (waveTable->waveTable[intPart + 1]);
return samp0 + (samp1 - samp0) * fracPart;
}
//
// getOutputMinusOffset
//
// for variable pulse width: initialize to sawtooth,
// set phaseOfs to duty cycle, use this for osc output
//
// returns the current oscillator output
//
float GetOutputMinusOffset() {
waveTable *waveTable = &mWaveTables[mCurWaveTable];
float len = waveTable->waveTableLen;
float *wave = waveTable->waveTable;
float temp = mPhasor * len;
int32_t intPart = floor(temp);
float fracPart = temp - intPart;
float samp0 = (wave[intPart]);
float samp1 = (wave[intPart+ 1]);
float samp = samp0 + (samp1 - samp0) * fracPart;
// and linear again for the offset part
float offsetPhasor = mPhasor + mPhaseOfs;
if (offsetPhasor > 1.0)
offsetPhasor -= 1.0;
temp = offsetPhasor * len;
intPart = temp;
fracPart = temp - intPart;
samp0 = wave[intPart];
samp1 = wave[intPart+1];
return samp - (samp0 + (samp1 - samp0) * fracPart);
}
//
// AddWaveTable
//
// add wavetables
// wavetables within an oscillator can be different lengths
//
// returns 0 upon success, or the number of wavetables if no more room is available
//
int AddSharedWaveTable(int32_t len, float *waveTableIn) {
if (mNumWaveTables < numWaveTableSlots) {
mWaveTables[mNumWaveTables].waveTable = waveTableIn;
mWaveTables[mNumWaveTables].waveTableLen = len-1;
++mNumWaveTables;
return 0;
}
return mNumWaveTables;
}
int AddWaveTable(int32_t len, float *waveTableIn) {
if (mNumWaveTables < numWaveTableSlots) {
float *waveTable = mWaveTables[mNumWaveTables].waveTable = new float[len + 1];
mWaveTables[mNumWaveTables].waveTableLen = len;
++mNumWaveTables;
// fill in wave
for (long idx = 0; idx < len; idx++)
waveTable[idx] = waveTableIn[idx];
waveTable[len] = waveTable[0]; // duplicate for interpolation wraparound
return 0;
}
return mNumWaveTables;
}
void ResetPhase()
{
mPhasor = 0.0;
}
void SetWaveTable(int waveTableIdx)
{
this->mCurWaveTable = waveTableIdx%mNumWaveTables;
}
int GetWaveTableCount()
{
return this->mNumWaveTables;
}
protected:
float mPhasor = 0.0; // phase accumulator
float mPhaseInc = 0.0; // phase increment
float mPhaseOfs = 0.0; // phase offset for PWM
// array of wavetables
int32_t mCurWaveTable = 0; // current table, based on current frequency
int32_t mNumWaveTables = 0; // number of wavetable slots in use
struct waveTable {
int32_t waveTableLen;
float *waveTable;
};
static constexpr int numWaveTableSlots = 256; // simplify allocation with reasonable maximum
waveTable mWaveTables[numWaveTableSlots];
};
#endif