SynthLab SDK
synthlabwtsource.h
1 #ifndef _SynthLabWTSource_h
2 #define _SynthLabWTSource_h
3 
4 #include "synthfunctions.h"
5 
6 // -----------------------------
7 // --- SynthLab SDK File --- //
8 // ----------------------------
16 // -----------------------------------------------------------------------------
17 namespace SynthLab
18 {
40  {
41  public:
44 
48  virtual const char* getWaveformName() override
49  {
51  }
52 
57  inline virtual void selectTable(uint32_t midiNoteNumber) override
58  {
59  selectedTable = wavetableSet[midiNoteNumber];
60  }
61 
70  inline virtual double readWaveTable(double normalizedPhaseInc) override
71  {
72  // --- two samples from table
73  double wtData[2] = { 0.0, 0.0 };
74 
75  // --- location = N(fo/fs)
76  double wtReadLocation = selectedTable.tableLength * normalizedPhaseInc;
77 
78  // --- split the fractional index into int.frac parts
79  double dIntPart = 0.0;
80  double fracPart = modf(wtReadLocation, &dIntPart);
81  uint32_t readIndex = (uint32_t)dIntPart;
82  uint32_t nextReadIndex = (readIndex + 1) & selectedTable.wrapMask;
83 
84  // --- two table reads
85  wtData[0] = uint64ToDouble(selectedTable.uTable[readIndex]);
86  wtData[1] = uint64ToDouble(selectedTable.uTable[nextReadIndex]);
87 
88  // --- interpolate the output
89  double output = doLinearInterpolation(0.0, 1.0, wtData[0], wtData[1], fracPart);
90 
91  // --- scale as needed
92  return selectedTable.outputComp * output;
93  }
94 
98  virtual uint32_t getWaveTableLength() { return selectedTable.tableLength; }
99 
110  inline void addSynthLabTableSet(SynthLabTableSet* synthLabTableSet)
111  {
112  for (uint32_t i = 0; i < NUM_MIDI_NOTES; i++)
113  {
114  StaticWavetable wt(synthLabTableSet->ppHexTableSet[i],
115  synthLabTableSet->tableLengths[i],
116  synthLabTableSet->waveformName,
117  synthLabTableSet->outputComp,
118  synthLabTableSet->tableFs);
119 
120  wavetableSet[i] = wt;
121  }
122  // --- set a default table
123  selectedTable = wavetableSet[MIDI_NOTE_A4];
124  }
125 
126  protected:
127  // --- 128 wavetables
128  StaticWavetable wavetableSet[NUM_MIDI_NOTES];
130  };
131 
149  {
150  public:
153 
157  virtual const char* getWaveformName() override
158  {
159  return drumTable.waveformName;
160  }
161 
166  inline virtual void selectTable(uint32_t midiNoteNumber) override { }
167 
176  inline virtual double readWaveTable(double normalizedPhaseInc) override
177  {
178  // --- core must calculate correct oscClocking (its easy)
179  double wtReadLocation = drumTable.tableLength * normalizedPhaseInc;
180  double output = 0.0;
181 
182  if (drumTable.dTable)
183  output = drumTable.dTable[(uint32_t)wtReadLocation];
184  else
185  output = uint64ToDouble(drumTable.uTable[(uint32_t)wtReadLocation]);
186 
187  // --- scale as needed
188  return drumTable.outputComp * output;
189  }
190 
194  virtual uint32_t getWaveTableLength() override { return drumTable.tableLength; }
195 
206  inline void addWavetable(const double* _table, uint32_t length, const char* name, double outputComp = 1.0)
207  {
208  // --- create the static table
209  StaticWavetable wt(_table, length, name, outputComp);
210  drumTable = wt;
211  }
212 
223  inline void addWavetable(const uint64_t* _table, uint32_t length, const char* name, double outputComp = 1.0)
224  {
225  // --- create the static table
226  StaticWavetable wt(_table, length, name, outputComp);
227  drumTable = wt;
228  }
229 
230  protected:
231  // --- one table per source
233  };
234 
235 }
236 #endif // definer
237 
uint32_t tableLength
length
Definition: synthbase.h:1184
virtual const char * getWaveformName() override
Definition: synthlabwtsource.h:48
double outputComp
output scaling factor
Definition: synthbase.h:1095
StaticWavetable drumTable
one table per drum
Definition: synthlabwtsource.h:232
Interface for wavetable sources.
Definition: synthbase.h:588
Storage for one static table source, specifically for drums which are pitchless and one-shot...
Definition: synthlabwtsource.h:148
double doLinearInterpolation(double x1, double x2, double y1, double y2, double x)
performs linear interpolation of x distance between two (x,y) points; returns interpolated value ...
Definition: synthfunctions.h:1350
double uint64ToDouble(uint64_t u)
maps a uint64 value to a double value without casting or mangling bits
Definition: synthfunctions.h:1848
DrumWTSource()
empty constructor
Definition: synthlabwtsource.h:151
const char * waveformName
string for the GUI
Definition: synthbase.h:1098
uint32_t wrapMask
wrapping mask = length - 1
Definition: synthbase.h:1185
Definition: synthengine.cpp:16
uint64_t ** ppHexTableSet
pointers to sets of hex encoded tables
Definition: synthbase.h:1089
virtual uint32_t getWaveTableLength()
Definition: synthlabwtsource.h:98
Structure for holding information about a static wavetable, that is read from a static location...
Definition: synthbase.h:1153
virtual uint32_t getWaveTableLength() override
Definition: synthlabwtsource.h:194
const char * waveformName
waveform name string
Definition: synthbase.h:1188
double outputComp
output scaling factor (NOT volume or attenuation, waveform specific)
Definition: synthbase.h:1186
virtual void selectTable(uint32_t midiNoteNumber) override
Select a table based on MIDI note number; nothing to do here.
Definition: synthlabwtsource.h:166
virtual const char * getWaveformName() override
Definition: synthlabwtsource.h:157
virtual double readWaveTable(double normalizedPhaseInc) override
Read and interpolate the table; uses linear interpolation but could be changed to 4th order LaGrange ...
Definition: synthlabwtsource.h:176
StaticTableSource()
empty constructor
Definition: synthlabwtsource.h:42
Storage for one static table source; a static table is pre-compiled into the synth, or (optionally) read from a file. The "source" stores a set of these tables to maximize frequency content while prohibiting aliasing.
Definition: synthlabwtsource.h:39
virtual double readWaveTable(double normalizedPhaseInc) override
Read and interpolate the table; uses linear interpolation but could be changed to 4th order LaGrange ...
Definition: synthlabwtsource.h:70
StaticWavetable wavetableSet[NUM_MIDI_NOTES]
— prefab table valid for all MIDI notes
Definition: synthlabwtsource.h:128
const double * dTable
table of 64-bit doubles
Definition: synthbase.h:1183
StaticWavetable selectedTable
— selected table, stored here
Definition: synthlabwtsource.h:129
void addWavetable(const uint64_t *_table, uint32_t length, const char *name, double outputComp=1.0)
Adds a new wavetable to the array of 128 tables, one for each MIDI note.
Definition: synthlabwtsource.h:223
uint32_t * tableLengths
pointers to lengths of each of the hex encoded tables
Definition: synthbase.h:1088
~StaticTableSource()
empty destructor
Definition: synthlabwtsource.h:43
const uint64_t * uTable
table of 64-bit HEX values
Definition: synthbase.h:1182
void addSynthLabTableSet(SynthLabTableSet *synthLabTableSet)
Adds a new SET of wavetables to the array of 128 tables, one for each MIDI note.
Definition: synthlabwtsource.h:110
This structure defines a set of wavetables that are usually found in .h files and compiled into the s...
Definition: synthbase.h:1075
~DrumWTSource()
empty destructor
Definition: synthlabwtsource.h:152
void addWavetable(const double *_table, uint32_t length, const char *name, double outputComp=1.0)
Adds a new wavetable to the array of 128 tables, one for each MIDI note.
Definition: synthlabwtsource.h:206
virtual void selectTable(uint32_t midiNoteNumber) override
Select a table based on MIDI note number.
Definition: synthlabwtsource.h:57