iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
Oscillator.h
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
13#include "IPlugPlatform.h"
14
15BEGIN_IPLUG_NAMESPACE
16
17template <typename T>
19{
20public:
21 IOscillator(double startPhase = 0., double startFreq = 1.)
22 : mPhase(startPhase)
23 , mStartPhase(startPhase)
24 {
25 SetFreqCPS(startFreq);
26 }
27
28 virtual inline T Process(double freqHz) = 0;
29
30 inline void SetFreqCPS(double freqHz)
31 {
32 mPhaseIncr = (1./mSampleRate) * freqHz;
33 }
34
35 void SetSampleRate(double sampleRate)
36 {
37 mSampleRate = sampleRate;
38 }
39
40 void Reset()
41 {
42 mPhase = mStartPhase;
43 }
44
45 void SetPhase(double phase)
46 {
47 mPhase = phase;
48 }
49
50protected:
51 double mPhase = 0.; // float phase (goes between 0. and 1.)
52 double mPhaseIncr = 0.; // how much to add to the phase on each T
53 double mSampleRate = 44100.;
54 double mStartPhase;
55};
56
57template <typename T>
58class SinOscillator : public IOscillator<T>
59{
60public:
61 SinOscillator(double startPhase = 0., double startFreq = 1.)
62 : IOscillator<T>(startPhase, startFreq)
63 {
64 }
65
66 inline T Process()
67 {
69 return std::sin(IOscillator<T>::mPhase * PI * 2.);
70 }
71
72 inline T Process(double freqHz) override
73 {
76 return std::sin(IOscillator<T>::mPhase * PI * 2.);
77 }
78};
79
80/*
81 FastSinOscillator - fast sinusoidal oscillator / table look up, based on an approach and code used by Miller Puckette in Pure Data, originally by Robert Höldrich
82
83 From some correspondence with M.S.P...
84
85 The basic idea is this: if you have a double precision floating point number
86 between -2^19 (-524288) and +2^19 (+524288), and you want the fractional part as an
87 integer, proceed as follows:
88
89 Add 3*2^19 (1572864) to it. The sum will be between 2^20 (1048576) and 2^21 (2097152) and so, in the usual
90 double precision format, the units bit will be the LSB of the higher-order
91 32 bits, and the fractional part will be neatly represented as an unsigned
92 fixed-point number between 0 and 2^32-1.
93
94 You can then use a C union to read these lower 32 bits as an integer. Or, if
95 you want to access then as a floating-point number without having to use
96 fix-to-float conversion, just bash the upper 32 bits to their original
97 state for representing 3*2^19. So far this is what's going on in the wrap~
98 and phasor~ objects in Pd.
99
100 The objects that do interpolating table lookup (e.g., cos~) take this one step
101 further. Here you end up with a number (now supposing it to be between
102 -2^10 and +2^10). Multiply by 2^9 (the size of the table) so that the
103 9-bit table address is in the 9 lowest bits of the upper 32 bit 'word', and
104 the fractional part is in the lower 32 bits as before. Grab the address as
105 an integer (masking to get just the 9 bits we want).
106
107 The you need to re-convert the fractional address into floating-point format
108 so you can use it to interpolate. Just bash the upper 32 bits with the
109 original value for 3*2^19, and the result will be a 64-bit floating-point number
110 between 3*2^19 and 3*2^19+1. Subtracting 3*2^19 from this gives us a floating-
111 point representation of the fractional part.
112 */
113
114#define UNITBIT32 1572864. /* 3*2^19; bit 32 has place value 1 */
115#define HIOFFSET 1
116#define LOWOFFSET 0
117
118#ifdef _MSC_VER
119#define ALIGN16 __declspec(align(16))
120#define ALIGNED(x)
121#else
122#define ALIGN16 alignas(16)
123#define ALIGNED(x) __attribute__ ((aligned (x)))
124#endif
125
126template <typename T>
128{
129 union tabfudge
130 {
131 double d;
132 int i[2];
133 } ALIGNED(8);
134
135public:
136 FastSinOscillator(double startPhase = 0., double startFreq = 1.)
137 : IOscillator<T>(startPhase, startFreq)
138 {
139 }
140
141 inline T Process()
142 {
143 T output = 0.;
144 ProcessBlock(&output, 1);
145
146 return output;
147 }
148
149 inline T Process(double freqCPS) override
150 {
152
153 T output = 0.;
154 ProcessBlock(&output, 1);
155
156 return output;
157 }
158
159 static inline T Lookup(double phaseRadians)
160 {
161 double tPhase = phaseRadians / (PI * 2.) * tableSize;
162
163 tPhase += (double) UNITBIT32;
164
165 union tabfudge tf;
166 tf.d = UNITBIT32;
167 const int normhipart = tf.i[HIOFFSET];
168
169 tf.d = tPhase;
170 const T* addr = mLUT + (tf.i[HIOFFSET] & tableSizeM1);
171 tf.i[HIOFFSET] = normhipart;
172 const double frac = tf.d - UNITBIT32;
173 const T f1 = addr[0];
174 const T f2 = addr[1];
175 return f1 + frac * (f2 - f1);
176 }
177
178 void ProcessBlock(T* pOutput, int nFrames)
179 {
180 double phase = IOscillator<T>::mPhase * tableSize + (double) UNITBIT32;
181 const double phaseIncr = IOscillator<T>::mPhaseIncr * tableSize;
182
183 union tabfudge tf;
184 tf.d = UNITBIT32;
185 const int normhipart = tf.i[HIOFFSET];
186
187 for (auto s = 0; s < nFrames; s++)
188 {
189 tf.d = phase;
190 const int index = tf.i[HIOFFSET] & tableSizeM1; // Obtain the integer portion
191 const T* addr = mLUT + index;
192 tf.i[HIOFFSET] = normhipart; // Force the double to wrap.
193 const double frac = tf.d - UNITBIT32;
194 phase = tf.d + index + phaseIncr; // Advance from the wrapped phase, so that it stays in range however long the block is
195 const T f1 = addr[0];
196 const T f2 = addr[1];
197 mLastOutput = pOutput[s] = T(f1 + frac * (f2 - f1));
198 }
199
200 // Restore mPhase
201 tf.d = UNITBIT32 * tableSize;
202 const int normhipart2 = tf.i[HIOFFSET];
203 tf.d = phase + (UNITBIT32 * tableSize - UNITBIT32); // Remove the offset we introduced at the start of UNITBIT32.
204 tf.i[HIOFFSET] = normhipart2;
205 IOscillator<T>::mPhase = (tf.d - UNITBIT32 * tableSize) / tableSize;
206 }
207
208 T mLastOutput = 0.;
209private:
210 static const int tableSize = 512; // 2^9
211 static const int tableSizeM1 = 511; // 2^9 -1
212 static const T mLUT[513];
213} ALIGNED(8);
214
215#include "Oscillator_table.h"
216
217END_IPLUG_NAMESPACE
Include to get consistently named preprocessor macros for different platforms and logging functionali...