SynthLab SDK
dynamictablesource.h
Go to the documentation of this file.
1 #ifndef _DynSource_h
2 #define _DynSource_h
3 
4 #include "synthfunctions.h"
5 
6 // -----------------------------
7 // --- SynthLab SDK File --- //
8 // ----------------------------
16 // -----------------------------------------------------------------------------
17 namespace SynthLab
18 {
39  {
40  public:
43 
47  virtual const char* getWaveformName() override
48  {
50  }
51 
56  inline virtual void selectTable(uint32_t midiNoteNumber) override
57  {
58  if (midiNoteNumber >= NUM_MIDI_NOTES) return;
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] = selectedTable.table.get()[readIndex];
86  wtData[1] = selectedTable.table.get()[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() override { return selectedTable.tableLength; }
99 
100 
113  inline void addWavetable(uint32_t startNoteNumber, uint32_t endNoteNumber,
114  std::shared_ptr<double> _table, uint32_t length, const char* name)
115  {
116  if (endNoteNumber >= NUM_MIDI_NOTES) return;
117 
118  DynamicWavetable wt(_table, length, name);
119 
120  for (uint32_t i = startNoteNumber; i <= endNoteNumber; i++)
121  {
122  wavetableSet[i] = wt;
123  }
124 
125  selectedTable = wavetableSet[endNoteNumber];
126  }
127 
132  inline void clearAllWavetables()
133  {
134  for (uint32_t i = 0; i < NUM_MIDI_NOTES; i++)
135  {
136  if (wavetableSet->table)
137  wavetableSet->table = nullptr;
138  }
139  }
140 
141  protected:
142  // --- prefab table valid for all MIDI notes
145  };
146 
147 }
148 #endif // definer
149 
double outputComp
output scaling factor (NOT volume or attenuation, waveform specific)
Definition: synthbase.h:1282
virtual void selectTable(uint32_t midiNoteNumber) override
Select a table based on MIDI note number.
Definition: dynamictablesource.h:56
Interface for wavetable sources.
Definition: synthbase.h:609
virtual const char * getWaveformName() override
Definition: dynamictablesource.h:47
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:1387
DynamicTableSource()
empty constructor
Definition: dynamictablesource.h:41
Definition: addosccore.cpp:4
~DynamicTableSource()
empty destructor
Definition: dynamictablesource.h:42
virtual uint32_t getWaveTableLength() override
Definition: dynamictablesource.h:98
std::shared_ptr< double > table
the table (shared)
Definition: synthbase.h:1279
uint32_t tableLength
length
Definition: synthbase.h:1280
Structure for holding information about a static wavetable, that is read from a static location...
Definition: synthbase.h:1264
uint32_t wrapMask
mask = length - 1
Definition: synthbase.h:1281
DynamicWavetable selectedTable
— selected table, stored here
Definition: dynamictablesource.h:144
virtual double readWaveTable(double normalizedPhaseInc) override
Read and interpolate the table; uses linear interpolation but could be changed to 4th order LaGrange ...
Definition: dynamictablesource.h:70
void addWavetable(uint32_t startNoteNumber, uint32_t endNoteNumber, std::shared_ptr< double > _table, uint32_t length, const char *name)
Adds a new wavetable or tables to the array of 128 tables, one for each MIDI note.
Definition: dynamictablesource.h:113
const uint32_t NUM_MIDI_NOTES
Definition: synthconstants.h:618
DynamicWavetable wavetableSet[NUM_MIDI_NOTES]
— prefab table valid for all MIDI notes
Definition: dynamictablesource.h:143
void clearAllWavetables()
Clear out the wavetables to initialize or re-initialize.
Definition: dynamictablesource.h:132
const char * waveformName
waveform name
Definition: synthbase.h:1284
hard-coded arrays of FIR filter coefficients for the sample rate conversion objects (Interpolator and...
Storage for one dynamic table source; a wavetable that is created dynamically at load time...
Definition: dynamictablesource.h:38