SynthLab SDK
synthlabwtsource.h
1 #ifndef _SynthLabWTSource_h
2 #define _SynthLabWTSource_h
3 
4 #include "synthfunctions.h"
5 
6 namespace SynthLab
7 {
8  // --- constants
9  const uint32_t MAX_WAVE_TABLES = 128;
10 
11  // -------------------------------------------------
13  {
14  public:
15  StaticTableSource() { }
16 
17  // --- clean up
18  ~StaticTableSource() { }
19 
20  virtual const char* getWaveformName()
21  {
23  }
24 
25  // --- set the current table for note event
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] = uint64ToDouble(selectedTable.uTable[readIndex]);
48  wtData[1] = uint64ToDouble(selectedTable.uTable[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  // --- for init with HiResWTSet in a .h file
61  // NOTE: THESE ARE ALL STATIC TABLES, PREFABRICATED AND NON-DYNAMIC
62  //
63  inline void addSynthLabTableSet(SynthLabTableSet* synthLabTableSet)
64  {
65  for (uint32_t i = 0; i < NUM_MIDI_NOTES; i++)
66  {
67  StaticWavetable wt(synthLabTableSet->ppHexTableSet[i],
68  synthLabTableSet->tableLengths[i],
69  synthLabTableSet->waveformName,
70  synthLabTableSet->outputComp,
71  synthLabTableSet->tableFs);
72 
73  wavetableSet[i] = wt;
74  }
75  // --- set a default table
76  selectedTable = wavetableSet[MIDI_NOTE_A4];
77  }
78 
79  protected:
80  // --- 128 wavetables
81  StaticWavetable wavetableSet[NUM_MIDI_NOTES];
83  };
84 
85  // -------------------------------------------------
87  {
88  public:
89  DrumWTSource() { }
90 
91  // --- clean up
92  ~DrumWTSource() { }
93 
94  virtual const char* getWaveformName()
95  {
96  return drumTable.waveformName;
97  }
98 
99  // --- set the current table for note event
100  inline virtual void selectTable(uint32_t midiNoteNumber)
101  {
102  return;
103  }
104 
105  // --- NOTE: this is for pitchless one-shot drum samples
106  inline virtual double readWaveTable(double oscClockIndex)
107  {
108  // --- core must calculate correct oscClocking (its easy)
109  double wtReadLocation = drumTable.tableLength * oscClockIndex;
110  double output = 0.0;
111 
112  if (drumTable.dTable)
113  output = drumTable.dTable[(uint32_t)wtReadLocation];
114  else
115  output = uint64ToDouble(drumTable.uTable[(uint32_t)wtReadLocation]);
116 
117  // --- scale as needed
118  return drumTable.outputComp * output;
119  }
120 
121  // --- get len
122  virtual uint32_t getWaveTableLength() { return drumTable.tableLength; }
123 
124  // --- for init with HiResWTSet in a .h file
125  // NOTE: THESE ARE ALL STATIC TABLES, PREFABRICATED AND NON-DYNAMIC
126  //
127  inline void addWavetable(const double* _table, uint32_t length, const char* name, double outputComp = 1.0)
128  {
129  // --- create the static table
130  StaticWavetable wt(_table, length, name, outputComp);
131  drumTable = wt;
132  }
133 
134  inline void addWavetable(const uint64_t* _table, uint32_t length, const char* name, double outputComp = 1.0)
135  {
136  // --- create the static table
137  StaticWavetable wt(_table, length, name, outputComp);
138  drumTable = wt;
139  }
140 
141  protected:
142  // --- one table per source
144  };
145 
146 }
147 #endif // definer
148 
uint32_t tableLength
length
Definition: synthbase.h:1397
virtual const char * getWaveformName()
Definition: synthlabwtsource.h:94
double outputComp
output scaling factor
Definition: synthbase.h:1308
StaticWavetable drumTable
one table per drum
Definition: synthlabwtsource.h:143
Interface for wavetable sources.
Definition: synthbase.h:801
virtual void selectTable(uint32_t midiNoteNumber)
Objects that access the database will select a table based on the user&#39;s waveform selection...
Definition: synthlabwtsource.h:100
Storage for one static table source, specifically for drums which are pitchless and one-shot...
Definition: synthlabwtsource.h:86
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:1339
double uint64ToDouble(uint64_t u)
maps a uint64 value to a double value without casting or mangling bits
Definition: synthfunctions.h:1837
const char * waveformName
string for the GUI
Definition: synthbase.h:1311
uint32_t wrapMask
wrapping mask = length - 1
Definition: synthbase.h:1398
Definition: synthlabcore.cpp:4
uint64_t ** ppHexTableSet
pointers to sets of hex encoded tables
Definition: synthbase.h:1302
virtual uint32_t getWaveTableLength()
Definition: synthlabwtsource.h:58
Structure for holding information about a static wavetable, that is read from a static location...
Definition: synthbase.h:1366
virtual const char * getWaveformName()
Definition: synthlabwtsource.h:20
virtual void selectTable(uint32_t midiNoteNumber)
Objects that access the database will select a table based on the user&#39;s waveform selection...
Definition: synthlabwtsource.h:26
const char * waveformName
waveform name string
Definition: synthbase.h:1401
double outputComp
output scaling factor (NOT volume or attenuation, waveform specific)
Definition: synthbase.h:1399
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:12
StaticWavetable wavetableSet[NUM_MIDI_NOTES]
— prefab table valid for all MIDI notes
Definition: synthlabwtsource.h:81
virtual uint32_t getWaveTableLength()
Definition: synthlabwtsource.h:122
const double * dTable
table of 64-bit doubles
Definition: synthbase.h:1396
StaticWavetable selectedTable
— selected table, stored here
Definition: synthlabwtsource.h:82
uint32_t * tableLengths
pointers to lengths of each of the hex encoded tables
Definition: synthbase.h:1301
const uint64_t * uTable
table of 64-bit HEX values
Definition: synthbase.h:1395
This structure defines a set of wavetables that are usually found in .h files and compiled into the s...
Definition: synthbase.h:1288
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: synthlabwtsource.h:106
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: synthlabwtsource.h:32