SynthLab SDK
synthvoice.h
1 #ifndef __synthVoice_h__
2 #define __synthVoice_h__
3 
4 // --- SynthLab only
5 #include "synthbase.h"
6 #include "synthfunctions.h"
7 
8 namespace SynthLab
9 {
29  struct SynthVoiceParameters
30  {
31  SynthVoiceParameters() {}
32 
33  // --- synth mode; engine has same variable
34  uint32_t synthModeIndex = enumToInt(SynthMode::kMono);
35 
36  // --- synth mode; engine has same variable
37  uint32_t filterModeIndex = enumToInt(FilterMode::kSeries);
38 
39  // --- portamento (glide)
40  bool enablePortamento = false;// false;
41 
42  // --- glide time
43  double glideTime_mSec = 0.0;
44 
45  // --- legato mode
46  bool legatoMode = false;
47 
48  // --- unison Detune - each voice will be detuned differently
49  double unisonDetuneCents = 0.0;
50  double unisonStartPhase = 0.0;
51  double unisonPan = 0.0;
52 
53  // --- add your module GUI parametrs here
54  };
55 
56  // --- voice mode: note on or note off states
57  enum class voiceState { kNoteOnState, kNoteOffState };
58 
126  class SynthVoice
127  {
128  public:
129  SynthVoice(std::shared_ptr<MidiInputData> _midiInputData,
130  std::shared_ptr<MidiOutputData> _midiOutputData,
131  std::shared_ptr<SynthVoiceParameters> _parameters,
132  std::shared_ptr<WavetableDatabase> _wavetableDatabase,
133  std::shared_ptr<PCMSampleDatabase> _sampleDatabase,
134  uint32_t _blockSize = 64);
135 
136  virtual ~SynthVoice() {}
137 
139  virtual bool reset(double _sampleRate);
140  virtual bool update();
141  virtual bool render(SynthProcessInfo& synthProcessInfo);
142  virtual bool processMIDIEvent(midiEvent& event);
143  virtual bool initialize(const char* dllPath = nullptr);
144  virtual bool doNoteOn(midiEvent& event);
145  virtual bool doNoteOff(midiEvent& event);
146 
148  bool isVoiceActive() { return voiceIsActive; }
149 
151  voiceState getVoiceState() { return voiceNoteState; }
152 
153  // --- timestamps for determining note age;
154  // public because containing object needs to manipulate them
155  uint32_t getTimestamp() { return timestamp; }
157  void clearTimestamp() { timestamp = 0; }
158 
159  // --- MIDI note stuff
160  unsigned int getMIDINoteNumber() { return voiceMIDIEvent.midiData1; }
161 
162  // --- voice steal
164  bool voiceIsStealing() { return stealPending; }
165 
166  protected:
168  std::shared_ptr<SynthVoiceParameters> parameters = nullptr;
169 
170  // --- local storage
171  double sampleRate = 0.0;
172  uint32_t blockSize = 64;
173 
174  // --- interface pointer
175  std::shared_ptr<MidiInputData> midiInputData = nullptr;
176  std::shared_ptr<MidiOutputData> midiOutputData = nullptr;
177  std::shared_ptr<WavetableDatabase> wavetableDatabase = nullptr;
178  std::shared_ptr<PCMSampleDatabase> sampleDatabase = nullptr;
179 
180  std::shared_ptr<AudioBuffer> mixBuffers = nullptr;
181  void accumulateToMixBuffer(std::shared_ptr<AudioBuffer> oscBuffers, uint32_t samplesInBlock, double scaling);
182  void writeToMixBuffer(std::shared_ptr<AudioBuffer> oscBuffers, uint32_t samplesInBlock, double scaling);
183 
184  // --- voice timestamp, for knowing the age of a voice
185  uint32_t timestamp = 0;
186  int32_t currentMIDINote = -1;
187 
188  // --- note message state
189  voiceState voiceNoteState = voiceState::kNoteOffState;
190 
191  // --- per-voice stuff
192  bool voiceIsActive = false;
194 
195  // --- for voice stealing
196  bool stealPending = false;
198 
199  // ---- place your individual synth components here
200 
201 
202 
203  };
204 
205 }
206 #endif /* defined(__synthVoice_h__) */
std::shared_ptr< MidiOutputData > midiOutputData
shared MIDI output data (not used in SynthLab)
Definition: synthvoice.h:483
#define enumToInt(ENUM)
macro helper to cast a typed enum to an int
Definition: synthfunctions.h:251
midiEvent voiceMIDIEvent
MIDI note event for current voice.
Definition: synthvoice.h:500
virtual bool initialize(const char *dllPath=nullptr)
Initialize the voice sub-components; this really only applies to PCM oscillators that need DLL path B...
Definition: synthvoice.cpp:120
uint32_t midiData1
BYTE data 1 as UINT.
Definition: synthstructures.h:196
void accumulateToMixBuffer(std::shared_ptr< AudioBuffer > oscBuffers, uint32_t samplesInBlock, double scaling)
accumulating voice audio data
Definition: synthvoice.cpp:177
virtual bool reset(double _sampleRate)
Reset all SynthModules on init or when sample rate changes.
Definition: synthvoice.cpp:136
int32_t currentMIDINote
voice timestamp, for knowing the age of a voice
Definition: synthvoice.h:493
unsigned int getStealMIDINoteNumber()
note is data byte 1, velocity is byte 2
Definition: synthvoice.h:163
void clearTimestamp()
reset timestamp after voice is turned off
Definition: synthvoice.h:157
void writeToMixBuffer(std::shared_ptr< AudioBuffer > oscBuffers, uint32_t samplesInBlock, double scaling)
write to final mix buffer
Definition: synthvoice.cpp:201
Definition: addosccore.cpp:4
bool stealPending
stealing is inevitible
Definition: synthvoice.h:503
virtual bool doNoteOn(midiEvent &event)
Note-on handler for voice.
Definition: synthvoice.cpp:291
SynthVoice(std::shared_ptr< MidiInputData > _midiInputData, std::shared_ptr< MidiOutputData > _midiOutputData, std::shared_ptr< SynthVoiceParameters > _parameters, std::shared_ptr< WavetableDatabase > _wavetableDatabase, std::shared_ptr< PCMSampleDatabase > _sampleDatabase, uint32_t _blockSize=64)
This is a template file with a minimal implementation.
Definition: synthvoice.cpp:40
voiceState getVoiceState()
Definition: synthvoice.h:151
std::shared_ptr< WavetableDatabase > wavetableDatabase
shared wavetable database
Definition: synthvoice.h:484
std::shared_ptr< PCMSampleDatabase > sampleDatabase
shared PCM database
Definition: synthvoice.h:485
virtual bool processMIDIEvent(midiEvent &event)
MIDI Event handler.
Definition: synthvoice.cpp:366
bool voiceIsStealing()
trur if voice will be stolen
Definition: synthvoice.h:164
std::shared_ptr< MidiInputData > midiInputData
shared MIDI input data
Definition: synthvoice.h:482
uint32_t timestamp
voice timestamp, for knowing the age of a voice
Definition: synthvoice.h:492
midiEvent voiceStealMIDIEvent
MIDI note event for the new (stolen) voice.
Definition: synthvoice.h:504
std::shared_ptr< AudioBuffer > mixBuffers
buffers for mixing audio and procesisng the voice digital audio engine
Definition: synthvoice.h:487
This structure holds all of the information needed to for the plugin framework to send MIDI informati...
Definition: synthbase.h:1384
virtual bool render(SynthProcessInfo &synthProcessInfo)
Render a block of audio data for an active note event.
Definition: synthvoice.cpp:231
virtual ~SynthVoice()
empty destructor
Definition: synthvoice.h:136
bool isVoiceActive()
Definition: synthvoice.h:148
uint32_t getTimestamp()
get current timestamp, the higher the value, the older the voice has been running ...
Definition: synthvoice.h:155
bool voiceIsActive
activity flag
Definition: synthvoice.h:499
void incrementTimestamp()
increment timestamp when a new note is triggered
Definition: synthvoice.h:156
voiceState voiceNoteState
state variable
Definition: synthvoice.h:496
unsigned int getMIDINoteNumber()
note is data byte 1, velocity is byte 2
Definition: synthvoice.h:160
hard-coded arrays of FIR filter coefficients for the sample rate conversion objects (Interpolator and...
virtual bool doNoteOff(midiEvent &event)
Note-off handler for voice.
Definition: synthvoice.cpp:338
Information about a MIDI event.
Definition: synthstructures.h:166
std::shared_ptr< SynthVoiceParameters > parameters
Definition: synthvoice.h:467
virtual bool update()
Update voice specific stuff.
Definition: synthvoice.cpp:157
See also Designing Software Synthesizers in C++ 2nd Ed. by Will Pirkle.