SynthLab SDK
dyntablesource.h
1 #ifndef _DynamicSource_h
2 #define _DynamicSource_h
3 
4 #include "synthfunctions.h"
5 
6 namespace SynthLab
7 {
8  // -------------------------------------------------
9  class DynamicTableSource : public IWavetableSource
10  {
11  public:
12  DynamicTableSource(){
13  // --- clear the array of 128 table-pointers
14  memset(wavetableSet, 0, NUM_MIDI_NOTES*(sizeof(Wavetable*)));
15  }
16 
17  // --- clean up
18  ~DynamicTableSource() { }
19 
20  virtual const char* getWaveformName()
21  {
22  return selectedTable->waveformName;
23  }
24 
25  // --- nothing to do
26  inline virtual void selectTable(uint32_t midiNoteNumber)
27  {
28  selectedTable = wavetableSet[midiNoteNumber];
29  }
30 
31  // --- read and interpolate: could add lagrange here
32  inline virtual double readWaveTable(double oscClockIndex)
33  {
34  // --- two samples from table
35  double wtData[2] = { 0.0, 0.0 };
36 
37  // --- location = N(fo/fs)
38  double wtReadLocation = selectedTable->tableLength * oscClockIndex;
39 
40  // --- split the fractional index into int.frac parts
41  double dIntPart = 0.0;
42  double fracPart = modf(wtReadLocation, &dIntPart);
43  uint32_t readIndex = (uint32_t)dIntPart;
44  uint32_t nextReadIndex = (readIndex + 1) & selectedTable->wrapMask;
45 
46  // --- two table reads
47  wtData[0] = selectedTable->table[readIndex];
48  wtData[1] = selectedTable->table[nextReadIndex];
49 
50  // --- interpolate the output
51  double output = doLinearInterpolation(0.0, 1.0, wtData[0], wtData[1], fracPart);
52 
53  // --- scale as needed
54  return selectedTable->outputComp * output;
55  }
56 
57  // --- get len
58  virtual uint32_t getWaveTableLength() { return selectedTable->tableLength; }
59 
60  void addWavetable(Wavetable* table, uint32_t midiNoteNumber)
61  {
62  if (midiNoteNumber >= NUM_MIDI_NOTES) return;
63  if (wavetableSet[midiNoteNumber])
64  delete wavetableSet[midiNoteNumber];
65 
66  wavetableSet[midiNoteNumber] = table;
67  selectedTable = table; // ensures selection
68  }
69 
70  protected:
71  // --- 128 wavetables
72  Wavetable* wavetableSet[NUM_MIDI_NOTES];
73  Wavetable* selectedTable = nullptr;
74  };
75 
76 }
77 #endif // definer
78 
Definition: analogegcore.cpp:4