-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMultiWaveNumOsc.h
188 lines (162 loc) · 5.24 KB
/
MultiWaveNumOsc.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
//
// 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 MultiWaveNumOsc_h
#define MultiWaveNumOsc_h
#include "Num.h"
#include "Util.h"
using namespace Fixie;
class MultiWaveNumOsc {
public:
MultiWaveNumOsc(void) {
for (int idx = 0; idx < numWaveTableSlots; idx++) {
mWaveTables[idx].waveTableLen = 0;
mWaveTables[idx].waveTable = 0;
}
}
~MultiWaveNumOsc(void) {
for (int idx = 0; idx < numWaveTableSlots; idx++) {
int8_t *temp = mWaveTables[idx].waveTable;
if (temp != 0)
delete [] temp;
}
}
void SetFrequency(double freq, double sampleRate)
{
SetFrequency (Num(freq/sampleRate));
}
//
// SetFrequency: Set normalized frequency, typically 0-0.5 (must be positive and less than 1!)
//
void SetFrequency(Num inc) {
mPhaseInc = inc;
}
void SetWaveform(int index)
{
if(index>=0 && index<mNumWaveTables)
{
mCurWaveTable = index;
}
else
{
mCurWaveTable = 0;
}
}
//
// SetPhaseOffset: Phase offset for PWM, 0-1
//
void SetPhaseOffset(double offset) {
mPhaseOfs = offset;
}
//
// UpdatePhase: Call once per sample
//
void UpdatePhase(void) {
mPhasor += mPhaseInc;
if (mPhasor >= Num(1.0))
mPhasor -= Num(1.0);
}
//
// Process: Update phase and get output
//
Num Process(void) {
UpdatePhase();
return GetOutput();
}
//
// GetOutput: Returns the current oscillator output
//
Num GetOutput(void) {
waveTable *waveTable = &mWaveTables[mCurWaveTable];
// linear interpolation
Num temp = mPhasor * (waveTable->waveTableLen);
Num intPart = Fixie::Util::floor(temp);
Num fracPart = temp - intPart;
Num samp0 = Num(waveTable->waveTable[intPart]);
Num samp1 = Num(waveTable->waveTable[intPart + Num(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
//
Fixie::Num GetOutputMinusOffset() {
waveTable *waveTable = &mWaveTables[mCurWaveTable];
Num len = waveTable->waveTableLen;
int8_t *wave = waveTable->waveTable;
// linear
Num temp = mPhasor * len;
Num intPart = Fixie::Util::floor(temp);
Num fracPart = temp - intPart;
Num samp0 = Num(wave[intPart]);
Num samp1 = Num(wave[intPart+Num(1)]);
Num samp = samp0 + (samp1 - samp0) * fracPart;
// and linear again for the offset part
Fixie::Num offsetPhasor = mPhasor + mPhaseOfs;
if (offsetPhasor > Num(1.0))
offsetPhasor -= Num(1.0);
temp = offsetPhasor * Num(len);
intPart = temp;
fracPart = temp - intPart;
samp0 = wave[intPart];
samp1 = wave[intPart+Num(1)];
return samp - (samp0 + (samp1 - samp0) * fracPart);
}
//
// AddWaveTable
//
// add wavetables in order of lowest frequency to highest
// topFreq is the highest frequency supported by a wavetable
// wavetables within an oscillator can be different lengths
//
// returns 0 upon success, or the number of wavetables if no more room is available
//
int AddWaveTable(int len, int8_t *waveTableIn) {
if (mNumWaveTables < numWaveTableSlots) {
int8_t *waveTable = mWaveTables[mNumWaveTables].waveTable = new int8_t[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;
}
protected:
Num mPhasor = Num(0.0); // phase accumulator
Num mPhaseInc = Num(0.0); // phase increment
Num mPhaseOfs = Num(0.5); // phase offset for PWM
// array of wavetables
int mCurWaveTable = 0; // current table, based on index
int mNumWaveTables = 0; // number of wavetable slots in use
struct waveTable {
Num waveTableLen;
int8_t *waveTable;
};
static constexpr int numWaveTableSlots = 256; // simplify allocation with reasonable maximum
waveTable mWaveTables[numWaveTableSlots];
};
#endif