SynthLab SDK
dynamictablesource.h
1 #ifndef _DynSource_h
2 #define _DynSource_h
3 
4 #include "synthfunctions.h"
5 
6 namespace SynthLab
7 {
8  // -------------------------------------------------
9  class DynamicTableSource : public IWavetableSource
10  {
11  public:
13  {
14  // memset(&wavetableSet[0], 0, NUM_MIDI_NOTES * sizeof(Wavetable));
15  }
16 
17  // --- clean up
19 
20  virtual const char* getWaveformName()
21  {
23  }
24 
25  // --- nothing to do
26  inline virtual void selectTable(uint32_t midiNoteNumber)
27  {
28  if (midiNoteNumber >= NUM_MIDI_NOTES) return;
29  selectedTable = wavetableSet[midiNoteNumber];
30  }
31 
32  // --- read and interpolate: could add lagrange here
33  inline virtual double readWaveTable(double oscClockIndex)
34  {
35  // --- two samples from table
36  double wtData[2] = { 0.0, 0.0 };
37 
38  // --- location = N(fo/fs)
39  double wtReadLocation = selectedTable.tableLength * oscClockIndex;
40 
41  // --- split the fractional index into int.frac parts
42  double dIntPart = 0.0;
43  double fracPart = modf(wtReadLocation, &dIntPart);
44  uint32_t readIndex = (uint32_t)dIntPart;
45  uint32_t nextReadIndex = (readIndex + 1) & selectedTable.wrapMask;
46 
47  // --- two table reads
48  wtData[0] = selectedTable.table.get()[readIndex];
49  wtData[1] = selectedTable.table.get()[nextReadIndex];
50 
51  // --- interpolate the output
52  double output = doLinearInterpolation(0.0, 1.0, wtData[0], wtData[1], fracPart);
53 
54  // --- scale as needed
55  return selectedTable.outputComp * output;
56  }
57 
58  // --- get len
59  virtual uint32_t getWaveTableLength() { return selectedTable.tableLength; }
60 
61  // --- add
62  inline void addWavetable(uint32_t startNoteNumber, uint32_t endNoteNumber, std::shared_ptr<double> _table, uint32_t length, const char* name)
63  {
64  if (endNoteNumber >= NUM_MIDI_NOTES) return;
65 
66  DynamicWavetable wt(_table, length, name);
67 
68  for (uint32_t i = startNoteNumber; i <= endNoteNumber; i++)
69  {
70  wavetableSet[i] = wt;
71  }
72 
73  selectedTable = wavetableSet[endNoteNumber];
74  }
75 
76  inline void clearAllWavetables()
77  {
78  for (uint32_t i = 0; i < NUM_MIDI_NOTES; i++)
79  {
80  if (wavetableSet->table)
81  wavetableSet->table = nullptr;
82  }
83  }
84 
85  protected:
86  // --- prefab table valid for all MIDI notes
87  DynamicWavetable wavetableSet[NUM_MIDI_NOTES];
88  DynamicWavetable selectedTable;
89  };
90 
91 }
92 #endif // definer
93 
virtual void selectTable(uint32_t midiNoteNumber)
Objects that access the database will select a table based on the user&#39;s waveform selection...
Definition: dynamictablesource.h:26
double outputComp
output scaling factor (NOT volume or attenuation, waveform specific)
Definition: synthbase.h:1227
virtual const char * getWaveformName()
Definition: dynamictablesource.h:20
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
DynamicTableSource()
empty constructor
Definition: dynamictablesource.h:41
Definition: synthengine.cpp:16
~DynamicTableSource()
empty destructor
Definition: dynamictablesource.h:42
uint32_t tableLength
length
Definition: synthbase.h:1225
Structure for holding information about a static wavetable, that is read from a static location...
Definition: synthbase.h:1209
uint32_t wrapMask
mask = length - 1
Definition: synthbase.h:1226
DynamicWavetable selectedTable
— selected table, stored here
Definition: dynamictablesource.h:144
const char * waveformName
waveform name
Definition: synthbase.h:1229
virtual uint32_t getWaveTableLength()
Definition: dynamictablesource.h:59
DynamicWavetable wavetableSet[NUM_MIDI_NOTES]
— prefab table valid for all MIDI notes
Definition: dynamictablesource.h:143
std::shared_ptr< double > table
the table (shared)
Definition: synthbase.h:1224
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
virtual double readWaveTable(double oscClockIndex)
Read a table at a normalized index where 0.0 is the start of the table and 1.0 is the end of it...
Definition: dynamictablesource.h:33
void clearAllWavetables()
Clear out the wavetables to initialize or re-initialize.
Definition: dynamictablesource.h:132