SynthLab SDK
wtsource.h
1 #ifndef _WTSource_h
2 #define _WTSource_h
3 
4 #include "synthfunctions.h"
5 
6 // -----------------------------
7 // --- SynthLab SDK File --- //
8 // ----------------------------
16 // -----------------------------------------------------------------------------
17 namespace SynthLab
18 {
19  // --- constants
20  const uint32_t MAX_WAVE_TABLES = 128;
21 
22  // -------------------------------------------------
24  {
25  public:
26  WavetableSource() {
27  // --- clear the array of 128 table-pointers
28  memset(wavetableSet, 0, NUM_MIDI_NOTES*(sizeof(double*)));
29  }
30 
31  // --- clean up
32  ~WavetableSource() { }
33 
34  virtual const char* getWaveformName()
35  {
36  return selectedTable.waveformName;
37  }
38 
39  // --- set the current table for note event
40  inline virtual void selectTable(uint32_t midiNoteNumber)
41  {
42  selectedTable = wavetableSet[midiNoteNumber];
43  }
44 
45  // --- read and interpolate: could add lagrange here
46  inline virtual double readWaveTable(double oscClockIndex)
47  {
48  // --- two samples from table
49  double wtData[2] = { 0.0, 0.0 };
50 
51  // --- location = N(fo/fs)
52  double wtReadLocation = selectedTable.tableLength * oscClockIndex;
53 
54  // --- split the fractional index into int.frac parts
55  double dIntPart = 0.0;
56  double fracPart = modf(wtReadLocation, &dIntPart);
57  uint32_t readIndex = (uint32_t)dIntPart;
58  uint32_t nextReadIndex = (readIndex + 1) & selectedTable.wrapMask;
59 
60  // --- two table reads
61  wtData[0] = uint64ToDouble(selectedTable.table[readIndex]);
62  wtData[1] = uint64ToDouble(selectedTable.table[nextReadIndex]);
63 
64  // --- interpolate the output
65  double output = doLinearInterpolation(0.0, 1.0, wtData[0], wtData[1], fracPart);
66 
67  // --- scale as needed
68  return selectedTable.outputComp * output;
69  }
70 
71  // --- get len
72  virtual uint32_t getWaveTableLength() { return selectedTable.tableLength; }
73 
74  // --- for init with HiResWTSet in a .h file
75  inline void addWavetable(Wavetable* table, uint32_t midiNoteNumber)
76  {
77  //wavetableSet[midiNoteNumber] = table;
78  }
79 
80  protected:
81  // --- 128 wavetables
82  Wavetable wavetableSet[NUM_MIDI_NOTES];
83  Wavetable selectedTable;
84  };
85 
86 }
87 #endif // definer
88 
virtual const char * getWaveformName()
Definition: wtsource.h:34
Definition: wtsource.h:23
Interface for wavetable sources.
Definition: synthbase.h:609
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
double uint64ToDouble(uint64_t u)
maps a uint64 value to a double value without casting or mangling bits
Definition: synthfunctions.h:1944
Definition: addosccore.cpp:4
virtual uint32_t getWaveTableLength()
Definition: wtsource.h:72
const uint32_t NUM_MIDI_NOTES
Definition: synthconstants.h:618
virtual void selectTable(uint32_t midiNoteNumber)
Objects that access the database will select a table based on the user's waveform selection...
Definition: wtsource.h:40
hard-coded arrays of FIR filter coefficients for the sample rate conversion objects (Interpolator and...
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: wtsource.h:46