Vital
Loading...
Searching...
No Matches
synth_base.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "concurrentqueue/concurrentqueue.h"
5#include "line_generator.h"
6#include "synth_constants.h"
7#include "synth_types.h"
8#include "midi_manager.h"
9#include "tuning.h"
10#include "wavetable_creator.h"
11
12#include <set>
13#include <string>
14
15namespace vital {
16 class SoundEngine;
17 struct Output;
18 class StatusOutput;
19 class StereoMemory;
20 class Sample;
21 class WaveFrame;
22 class Wavetable;
23}
24
26
43public:
45 static constexpr float kOutputWindowMinNote = 16.0f;
46 static constexpr float kOutputWindowMaxNote = 128.0f;
47
51 SynthBase();
52
56 virtual ~SynthBase();
57
64 void valueChanged(const std::string& name, vital::mono_float value);
65
72 void valueChangedThroughMidi(const std::string& name, vital::mono_float value) override;
73
79 void pitchWheelMidiChanged(vital::mono_float value) override;
80
86 void modWheelMidiChanged(vital::mono_float value) override;
87
94
101
107 void presetChangedThroughMidi(File preset) override;
108
115 void valueChangedExternal(const std::string& name, vital::mono_float value);
116
123 void valueChangedInternal(const std::string& name, vital::mono_float value);
124
132 bool connectModulation(const std::string& source, const std::string& destination);
133
140
147 void disconnectModulation(const std::string& source, const std::string& destination);
148
155
159 void clearModulations();
160
167 void forceShowModulation(const std::string& source, bool force);
168
175 bool isModSourceEnabled(const std::string& source);
176
183 int getNumModulations(const std::string& destination);
184
192 int getConnectionIndex(const std::string& source, const std::string& destination);
193
200
207 std::vector<vital::ModulationConnection*> getSourceConnections(const std::string& source);
208
215 bool isSourceConnected(const std::string& source);
216
223 std::vector<vital::ModulationConnection*> getDestinationConnections(const std::string& destination);
224
231 const vital::StatusOutput* getStatusOutput(const std::string& name);
232
239 vital::Wavetable* getWavetable(int index);
240
248
255
262 LineGenerator* getLfoSource(int index);
263
269 int getSampleRate();
270
274 void initEngine();
275
281 void loadTuningFile(const File& file);
282
286 void loadInitPreset();
287
295 bool loadFromFile(File preset, std::string& error);
296
306 void renderAudioToFile(File file, float seconds, float bpm, std::vector<int> notes, bool render_images);
307
315 void renderAudioForResynthesis(float* data, int samples, int note);
316
323 bool saveToFile(File preset);
324
330 bool saveToActiveFile();
331
335 void clearActiveFile() { active_file_ = File(); }
336
342 File getActiveFile() { return active_file_; }
343
349 void setMpeEnabled(bool enabled);
350
356 virtual void beginChangeGesture(const std::string& name) { }
357
363 virtual void endChangeGesture(const std::string& name) { }
364
371 virtual void setValueNotifyHost(const std::string& name, vital::mono_float value) { }
372
378 void armMidiLearn(const std::string& name);
379
383 void cancelMidiLearn();
384
390 void clearMidiLearn(const std::string& name);
391
398 bool isMidiMapped(const std::string& name);
399
405 void setAuthor(const String& author);
406
412 void setComments(const String& comments);
413
419 void setStyle(const String& style);
420
426 void setPresetName(const String& preset_name);
427
434 void setMacroName(int index, const String& macro_name);
435
441 String getAuthor();
442
448 String getComments();
449
455 String getStyle();
456
462 String getPresetName();
463
470 String getMacroName(int index);
471
478
485
491 MidiKeyboardState* getKeyboardState() { return keyboard_state_.get(); }
492
499
506
513
520
525
529 void checkOversampling();
530
538 virtual const CriticalSection& getCriticalSection() = 0;
539
547 virtual void pauseProcessing(bool pause) = 0;
548
554 Tuning* getTuning() { return &tuning_; }
555
559 struct ValueChangedCallback : public CallbackMessage {
560 ValueChangedCallback(std::shared_ptr<SynthBase*> listener, std::string name, vital::mono_float val) :
561 listener(listener), control_name(std::move(name)), value(val) { }
562
563 void messageCallback() override;
564
565 std::weak_ptr<SynthBase*> listener;
566 std::string control_name;
568 };
569
570protected:
578
586
593
600
607 bool loadFromJson(const json& state);
608
616 vital::ModulationConnection* getConnection(const std::string& source, const std::string& destination);
617
625 return modulation_change_queue_.try_dequeue_non_interleaved(change);
626 }
627
631 inline void clearModulationQueue() {
633 while (modulation_change_queue_.try_dequeue_non_interleaved(change))
634 ;
635 }
636
645 void processAudio(AudioSampleBuffer* buffer, int channels, int samples, int offset);
646
656 void processAudioWithInput(AudioSampleBuffer* buffer, const vital::poly_float* input_buffer,
657 int channels, int samples, int offset);
658
667 void writeAudio(AudioSampleBuffer* buffer, int channels, int samples, int offset);
668
676 void processMidi(MidiBuffer& buffer, int start_sample = 0, int end_sample = 0);
677
684 void processKeyboardEvents(MidiBuffer& buffer, int num_samples);
685
690
697 void updateMemoryOutput(int samples, const vital::poly_float* audio);
698
699 std::unique_ptr<vital::SoundEngine> engine_;
700 std::unique_ptr<MidiManager> midi_manager_;
701 std::unique_ptr<MidiKeyboardState> keyboard_state_;
702
703 std::unique_ptr<WavetableCreator> wavetable_creators_[vital::kNumOscillators];
704 std::shared_ptr<SynthBase*> self_reference_;
705
709 std::unique_ptr<vital::StereoMemory> audio_memory_;
716
717 std::map<std::string, String> save_info_;
720 moodycamel::ConcurrentQueue<vital::control_change> value_change_queue_;
721 moodycamel::ConcurrentQueue<vital::modulation_change> modulation_change_queue_;
723
724 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SynthBase)
725};
726
733class HeadlessSynth : public SynthBase {
734public:
735 const CriticalSection& getCriticalSection() override {
736 return critical_section_;
737 }
738
739 void pauseProcessing(bool pause) override {
740 if (pause)
741 critical_section_.enter();
742 else
743 critical_section_.exit();
744 }
745
746protected:
747 SynthGuiInterface* getGuiInterface() override { return nullptr; }
748
749private:
750 CriticalSection critical_section_;
751};
A headless subclass of SynthBase that provides a critical section and pausing capability.
Definition synth_base.h:733
const CriticalSection & getCriticalSection() override
Provides access to the synth’s internal CriticalSection for thread safety.
Definition synth_base.h:735
SynthGuiInterface * getGuiInterface() override
Retrieves the GUI interface if available, for updating controls or notifications.
Definition synth_base.h:747
void pauseProcessing(bool pause) override
Pauses or resumes audio processing.
Definition synth_base.h:739
A class for generating and storing a line shape, defined by a series of points and associated powers.
Definition line_generator.h:20
An interface for classes that listen to MIDI-driven parameter changes.
Definition midi_manager.h:91
A base class providing foundational functionality for the Vital synthesizer’s engine,...
Definition synth_base.h:42
std::unique_ptr< WavetableCreator > wavetable_creators_[vital::kNumOscillators]
Definition synth_base.h:703
File active_file_
Definition synth_base.h:706
void setAuthor(const String &author)
Sets the author metadata of the current preset.
Definition synth_base.cpp:681
void processKeyboardEvents(MidiBuffer &buffer, int num_samples)
Processes keyboard events from a MidiBuffer, integrating them with the MidiKeyboardState.
Definition synth_base.cpp:604
virtual void beginChangeGesture(const std::string &name)
Called when a parameter change gesture begins. Typically not implemented in this base class.
Definition synth_base.h:356
bool saveToActiveFile()
Saves the current preset state to the active file (if any).
Definition synth_base.cpp:553
bool getNextModulationChange(vital::modulation_change &change)
Attempts to dequeue a modulation_change for processing.
Definition synth_base.h:624
std::vector< vital::ModulationConnection * > getSourceConnections(const std::string &source)
Returns all modulation connections from a particular source.
Definition synth_base.cpp:236
void valueChangedInternal(const std::string &name, vital::mono_float value)
Handles internal value changes, updating the parameter and optionally notifying the host.
Definition synth_base.cpp:54
MidiKeyboardState * getKeyboardState()
Retrieves the keyboard state (for processing MIDI note events from a virtual keyboard).
Definition synth_base.h:491
void writeAudio(AudioSampleBuffer *buffer, int channels, int samples, int offset)
Writes audio data from the engine to the output buffer.
Definition synth_base.cpp:581
void pitchWheelMidiChanged(vital::mono_float value) override
Called when the pitch wheel value changes via MIDI.
Definition synth_base.cpp:66
void setMacroName(int index, const String &macro_name)
Sets the name of a macro control by index.
Definition synth_base.cpp:697
bool expired_
Definition synth_base.h:715
void valueChangedExternal(const std::string &name, vital::mono_float value)
Handles external (non-GUI, non-MIDI) value changes to parameters.
Definition synth_base.cpp:92
bool isMidiMapped(const std::string &name)
Checks if a given parameter name is MIDI mapped.
Definition synth_base.cpp:677
Tuning tuning_
Definition synth_base.h:722
vital::poly_float oscilloscope_memory_[2 *vital::kOscilloscopeMemoryResolution]
Definition synth_base.h:707
bool connectModulation(const std::string &source, const std::string &destination)
Connects a modulation source to a destination parameter.
Definition synth_base.cpp:165
virtual void endChangeGesture(const std::string &name)
Called when a parameter change gesture ends. Typically not implemented in this base class.
Definition synth_base.h:363
Tuning * getTuning()
Returns a pointer to the synth's Tuning object.
Definition synth_base.h:554
void presetChangedThroughMidi(File preset) override
Called when a preset is changed through MIDI (e.g., program change messages).
Definition synth_base.cpp:84
vital::CircularQueue< vital::ModulationConnection * > getModulationConnections()
Gets all current modulation connections as a CircularQueue.
Definition synth_base.h:199
std::unique_ptr< MidiManager > midi_manager_
Definition synth_base.h:700
String getMacroName(int index)
Gets the name of a macro control by index.
Definition synth_base.cpp:717
std::unique_ptr< MidiKeyboardState > keyboard_state_
Definition synth_base.h:701
vital::control_map controls_
Definition synth_base.h:718
void setComments(const String &comments)
Sets the comments or description metadata of the current preset.
Definition synth_base.cpp:685
std::unique_ptr< vital::StereoMemory > audio_memory_
Definition synth_base.h:709
int getConnectionIndex(const std::string &source, const std::string &destination)
Gets the index of a modulation connection given its source and destination.
Definition synth_base.cpp:111
bool isModSourceEnabled(const std::string &source)
Checks if a modulation source is currently enabled.
Definition synth_base.cpp:223
vital::poly_float oscilloscope_memory_write_[2 *vital::kOscilloscopeMemoryResolution]
Definition synth_base.h:708
std::shared_ptr< SynthBase * > self_reference_
Definition synth_base.h:704
vital::ModulationConnectionBank & getModulationBank()
Retrieves the ModulationConnectionBank managing all modulation connections.
Definition synth_base.cpp:730
bool saveToFile(File preset)
Saves the current preset state to the specified file.
Definition synth_base.cpp:531
virtual ~SynthBase()
Destroys the SynthBase, cleaning up resources such as sound engine and memory buffers.
Definition synth_base.cpp:48
void initEngine()
Initializes the engine to a default state, clearing modulations and resetting parameters.
Definition synth_base.cpp:290
void setStyle(const String &style)
Sets the style metadata of the current preset (e.g., bass, lead, etc.).
Definition synth_base.cpp:689
bool loadFromFile(File preset, std::string &error)
Attempts to load a preset from a file.
Definition synth_base.cpp:338
void clearActiveFile()
Clears the currently active preset file, meaning changes are not saved to a previously loaded file.
Definition synth_base.h:335
static constexpr float kOutputWindowMaxNote
Definition synth_base.h:46
void clearMidiLearn(const std::string &name)
Clears the MIDI mapping for a given parameter name.
Definition synth_base.cpp:673
String getAuthor()
Gets the author of the current preset.
Definition synth_base.cpp:701
const vital::StereoMemory * getAudioMemory()
Retrieves stereo memory holding recent audio output samples for visualization.
Definition synth_base.h:505
bool loadFromJson(const json &state)
Deserializes and applies the synth state from a JSON object.
Definition synth_base.cpp:324
bool isSourceConnected(const std::string &source)
Checks if a given modulation source has any active connections.
Definition synth_base.cpp:245
File getActiveFile()
Gets the currently active preset file.
Definition synth_base.h:342
void clearModulationQueue()
Clears the modulation change queue, removing all pending changes.
Definition synth_base.h:631
void setMpeEnabled(bool enabled)
Enables or disables MPE (MIDI Polyphonic Expression) mode.
Definition synth_base.cpp:560
void processAudio(AudioSampleBuffer *buffer, int channels, int samples, int offset)
Processes audio into the given buffer. May be overridden or extended by subclasses.
Definition synth_base.cpp:564
virtual void pauseProcessing(bool pause)=0
Pauses or resumes audio processing.
json saveToJson()
Serializes the current synth state into a JSON object.
Definition synth_base.cpp:282
void checkOversampling()
Checks and updates oversampling settings. May be called if parameters affecting oversampling change.
Definition synth_base.cpp:741
std::map< std::string, String > save_info_
Definition synth_base.h:717
void cancelMidiLearn()
Cancels a previously armed MIDI learn operation.
Definition synth_base.cpp:669
int last_num_pressed_
Definition synth_base.h:711
String getComments()
Gets the comments for the current preset.
Definition synth_base.cpp:705
moodycamel::ConcurrentQueue< vital::control_change > value_change_queue_
Definition synth_base.h:720
void processMidi(MidiBuffer &buffer, int start_sample=0, int end_sample=0)
Processes MIDI messages from a MidiBuffer, applying them to the engine’s sound generation.
Definition synth_base.cpp:595
void forceShowModulation(const std::string &source, bool force)
Forces a modulation source to remain active even if not currently connected.
Definition synth_base.cpp:216
LineGenerator * getLfoSource(int index)
Retrieves an LFO source by index.
Definition synth_base.cpp:278
void disconnectModulation(const std::string &source, const std::string &destination)
Disconnects a modulation source from a destination parameter.
Definition synth_base.cpp:190
void renderAudioForResynthesis(float *data, int samples, int note)
Renders audio for the purpose of resynthesis into a provided buffer.
Definition synth_base.cpp:492
static constexpr float kOutputWindowMinNote
Minimum and maximum note values considered for output window display or related processing.
Definition synth_base.h:45
void processAudioWithInput(AudioSampleBuffer *buffer, const vital::poly_float *input_buffer, int channels, int samples, int offset)
Processes audio with an input buffer, often used for sidechain or feedback loops.
Definition synth_base.cpp:572
void updateMemoryOutput(int samples, const vital::poly_float *audio)
Updates the oscilloscope memory with the latest audio samples.
Definition synth_base.cpp:618
void modWheelGuiChanged(vital::mono_float value)
Called when the mod wheel changes via the GUI or another external source.
Definition synth_base.cpp:80
virtual const CriticalSection & getCriticalSection()=0
Provides access to the synth’s internal CriticalSection for thread safety.
virtual SynthGuiInterface * getGuiInterface()=0
Retrieves the GUI interface if available, for updating controls or notifications.
vital::ModulationConnection * getConnection(const std::string &source, const std::string &destination)
Finds a ModulationConnection by source and destination names.
Definition synth_base.cpp:103
String getPresetName()
Gets the current preset’s name.
Definition synth_base.cpp:713
const vital::poly_float * getOscilloscopeMemory()
Retrieves the oscilloscope memory for visualization of audio output waveforms.
Definition synth_base.h:498
WavetableCreator * getWavetableCreator(int index)
Gets a WavetableCreator for a given oscillator index.
Definition synth_base.cpp:270
std::unique_ptr< vital::SoundEngine > engine_
Definition synth_base.h:699
vital::CircularQueue< vital::ModulationConnection * > mod_connections_
Definition synth_base.h:719
virtual void setValueNotifyHost(const std::string &name, vital::mono_float value)
Called when a parameter changes to notify a potential host environment. Typically not implemented her...
Definition synth_base.h:371
int getSampleRate()
Retrieves the current audio sample rate used by the engine.
Definition synth_base.cpp:286
int getNumModulations(const std::string &destination)
Counts how many modulations target a given parameter.
Definition synth_base.cpp:227
const vital::StereoMemory * getEqualizerMemory()
Retrieves memory used for equalizer visualization, if available.
Definition synth_base.cpp:724
vital::Wavetable * getWavetable(int index)
Gets a wavetable object from the engine.
Definition synth_base.cpp:266
std::vector< vital::ModulationConnection * > getDestinationConnections(const std::string &destination)
Returns all modulation connections targeting a given destination parameter.
Definition synth_base.cpp:253
void pitchWheelGuiChanged(vital::mono_float value)
Called when the pitch wheel changes via the GUI or another external source.
Definition synth_base.cpp:76
bool isInvalidConnection(const vital::modulation_change &change)
Checks if a modulation_change is invalid (e.g., creates a loop).
Definition synth_base.cpp:148
moodycamel::ConcurrentQueue< vital::modulation_change > modulation_change_queue_
Definition synth_base.h:721
SynthBase()
Constructs a SynthBase, initializing the sound engine, MIDI manager, wavetables, and settings.
Definition synth_base.cpp:13
vital::Sample * getSample()
Gets the Sample object used by the engine.
Definition synth_base.cpp:274
void modWheelMidiChanged(vital::mono_float value) override
Called when the mod wheel value changes via MIDI.
Definition synth_base.cpp:71
vital::control_map & getControls()
Provides access to the controls map (parameter name to Parameter pointer).
Definition synth_base.h:477
vital::mono_float memory_input_offset_
Definition synth_base.h:713
void renderAudioToFile(File file, float seconds, float bpm, std::vector< int > notes, bool render_images)
Renders audio to a WAV file for a given duration and note sequence.
Definition synth_base.cpp:367
const vital::StatusOutput * getStatusOutput(const std::string &name)
Retrieves a status output by name.
Definition synth_base.cpp:262
void clearModulations()
Clears all modulation connections.
Definition synth_base.cpp:196
void loadInitPreset()
Loads an "init" preset, resetting Vital to its default initial state.
Definition synth_base.cpp:316
vital::SoundEngine * getEngine()
Returns a pointer to the SoundEngine used for audio and modulation processing.
Definition synth_base.h:484
void loadTuningFile(const File &file)
Loads a tuning file into the synthesizer’s tuning system.
Definition synth_base.cpp:312
vital::mono_float last_played_note_
Definition synth_base.h:710
void valueChanged(const std::string &name, vital::mono_float value)
Updates the value of a control parameter.
Definition synth_base.cpp:50
void processModulationChanges()
Processes pending modulation changes and updates the engine accordingly.
Definition synth_base.cpp:608
vital::mono_float memory_reset_period_
Definition synth_base.h:712
vital::modulation_change createModulationChange(vital::ModulationConnection *connection)
Creates a modulation_change structure for a given connection, preparing it for engine operations.
Definition synth_base.cpp:121
int memory_index_
Definition synth_base.h:714
void valueChangedThroughMidi(const std::string &name, vital::mono_float value) override
Handles parameter changes triggered through MIDI mappings.
Definition synth_base.cpp:59
void notifyOversamplingChanged()
Notifies that oversampling settings have changed, reinitializing the engine if needed.
Definition synth_base.cpp:734
void armMidiLearn(const std::string &name)
Arms the given parameter name for MIDI learn, associating the next received MIDI control with it.
Definition synth_base.cpp:665
void setPresetName(const String &preset_name)
Sets the preset name.
Definition synth_base.cpp:693
String getStyle()
Gets the style of the current preset.
Definition synth_base.cpp:709
An interface class linking the Vital synthesizer backend (SynthBase) with a GUI.
Definition synth_gui_interface.h:56
A class for managing microtonal tunings and custom pitch mappings in Vital.
Definition tuning.h:23
A class responsible for creating complete wavetables from groups of wavetable components.
Definition wavetable_creator.h:27
A generic circular buffer (FIFO) data structure that allows adding and removing elements efficiently.
Definition circular_queue.h:32
A container managing a fixed number of ModulationConnections.
Definition synth_types.h:87
Holds and manages a single sampled waveform, including stereo or mono data and multiple band-limited ...
Definition sample_source.h:25
Core class responsible for handling note events, oversampling, and the main effects chain.
Definition sound_engine.h:33
A helper class to track the "status" of a particular Output as a poly_float value.
Definition synth_module.h:35
A specialized MemoryTemplate for two-channel (stereo) audio.
Definition memory.h:216
force_inline poly_float get(poly_float past) const
Retrieves a poly_float of samples from the stereo memory using cubic interpolation.
Definition memory.h:235
A class representing a wavetable, holding multiple frames of waveforms and their frequency-domain rep...
Definition wavetable.h:20
nlohmann::json json
Definition line_generator.h:7
Contains classes and functions used within the Vital synthesizer framework.
constexpr int kNumOscillators
Number of oscillators available in Vital.
Definition synth_constants.h:16
constexpr int kOscilloscopeMemoryResolution
Resolution (number of samples) in the oscilloscope memory buffer.
Definition synth_constants.h:55
std::map< std::string, Value * > control_map
Maps parameter names to Value pointers representing synth control parameters.
Definition synth_types.h:214
float mono_float
Definition common.h:33
A callback message used when values change, allowing asynchronous updates to GUI or host.
Definition synth_base.h:559
ValueChangedCallback(std::shared_ptr< SynthBase * > listener, std::string name, vital::mono_float val)
Definition synth_base.h:560
void messageCallback() override
Definition synth_base.cpp:745
vital::mono_float value
Definition synth_base.h:567
std::string control_name
Definition synth_base.h:566
std::weak_ptr< SynthBase * > listener
Definition synth_base.h:565
A structure representing a single modulation connection between a modulation source and a destination...
Definition synth_types.h:30
A structure describing changes to the modulation routing in the engine.
Definition synth_types.h:199
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600