Vital
Loading...
Searching...
No Matches
synth_gui_interface.cpp
Go to the documentation of this file.
1/*
2Summary:
3The SynthGuiInterface class mediates between the SynthBase (synth engine backend) and a graphical front end (FullInterface). It updates GUI elements when parameters or modulations change, and also handles user actions from the GUI such as connecting modulations or resizing the window. In HEADLESS mode, these functionalities become no-ops. The SynthGuiData struct provides a data snapshot of engine state (controls, modulations, wavetable creators) to the GUI for display and interaction.
4 */
5
6
8#include "authentication.h"
10#include "sound_engine.h"
11#include "load_save.h"
12#include "synth_base.h"
13
14SynthGuiData::SynthGuiData(SynthBase* synth_base) : synth(synth_base) {
15 // Capture current control and modulation maps, as well as wavetable creators.
17 mono_modulations = synth->getEngine()->getMonoModulations();
18 poly_modulations = synth->getEngine()->getPolyModulations();
19 modulation_sources = synth->getEngine()->getModulationSources();
20 for (int i = 0; i < vital::kNumOscillators; ++i)
22}
23
24#if HEADLESS
25// In HEADLESS mode, these methods do nothing.
26SynthGuiInterface::SynthGuiInterface(SynthBase* synth, bool use_gui) : synth_(synth) { }
29void SynthGuiInterface::updateGuiControl(const std::string& name, vital::mono_float value) { }
30vital::mono_float SynthGuiInterface::getControlValue(const std::string& name) { return 0.0f; }
31void SynthGuiInterface::connectModulation(std::string source, std::string destination) { }
33void SynthGuiInterface::setModulationValues(const std::string& source, const std::string& destination,
34 vital::mono_float amount, bool bipolar, bool stereo, bool bypass) { }
35void SynthGuiInterface::disconnectModulation(std::string source, std::string destination) { }
42void SynthGuiInterface::setGuiSize(float scale) { }
43
44#else
45
47#include "full_interface.h"
48
49SynthGuiInterface::SynthGuiInterface(SynthBase* synth, bool use_gui) : synth_(synth) {
50 if (use_gui) {
51 // Create the FullInterface GUI and initialize it with current synth data.
52 LineGenerator* lfo_sources[vital::kNumLfos];
53 for (int i = 0; i < vital::kNumLfos; ++i)
54 lfo_sources[i] = synth->getLfoSource(i);
55 SynthGuiData synth_data(synth_);
56 gui_ = std::make_unique<FullInterface>(&synth_data);
57 }
58}
59
61
63 if (gui_ == nullptr)
64 return;
65
66 // Refresh all parameter values and GUI components.
67 gui_->setAllValues(synth_->getControls());
68 gui_->reset();
69}
70
71void SynthGuiInterface::updateGuiControl(const std::string& name, vital::mono_float value) {
72 if (gui_ == nullptr)
73 return;
74
75 // Update a single control in the GUI.
76 gui_->setValue(name, value, NotificationType::dontSendNotification);
77}
78
80 return synth_->getControls()[name]->value();
81}
82
84 gui_->modulationChanged();
85}
86
88 gui_->modulationValueChanged(index);
89}
90
91void SynthGuiInterface::connectModulation(std::string source, std::string destination) {
92 bool created = synth_->connectModulation(source, destination);
93 if (created)
94 initModulationValues(source, destination);
96}
97
102
103void SynthGuiInterface::initModulationValues(const std::string& source, const std::string& destination) {
104 // Initialize newly created modulations with default line mappings and values.
105 int connection_index = synth_->getConnectionIndex(source, destination);
106 if (connection_index < 0)
107 return;
108
109 vital::ModulationConnection* connection = synth_->getModulationBank().atIndex(connection_index);
110 LineGenerator* map_generator = connection->modulation_processor->lineMapGenerator();
111 map_generator->initLinear();
112
113 // Reset power to zero and update GUI.
114 std::string power_name = "modulation_" + std::to_string(connection_index + 1) + "_power";
115 synth_->valueChanged(power_name, 0.0f);
116 gui_->setValue(power_name, 0.0f, NotificationType::dontSendNotification);
117}
118
119void SynthGuiInterface::setModulationValues(const std::string& source, const std::string& destination,
120 vital::mono_float amount, bool bipolar, bool stereo, bool bypass) {
121 // Update modulation parameters in both the synth and GUI.
122 int connection_index = synth_->getConnectionIndex(source, destination);
123 if (connection_index < 0)
124 return;
125
126 std::string number = std::to_string(connection_index + 1);
127 std::string amount_name = "modulation_" + number + "_amount";
128 std::string bipolar_name = "modulation_" + number + "_bipolar";
129 std::string stereo_name = "modulation_" + number + "_stereo";
130 std::string bypass_name = "modulation_" + number + "_bypass";
131
132 float bipolar_amount = bipolar ? 1.0f : 0.0f;
133 float stereo_amount = stereo ? 1.0f : 0.0f;
134 float bypass_amount = bypass ? 1.0f : 0.0f;
135
136 synth_->valueChanged(amount_name, amount);
137 synth_->valueChanged(bipolar_name, bipolar_amount);
138 synth_->valueChanged(stereo_name, stereo_amount);
139 synth_->valueChanged(bypass_name, bypass_amount);
140 gui_->setValue(amount_name, amount, NotificationType::dontSendNotification);
141 gui_->setValue(bipolar_name, bipolar_amount, NotificationType::dontSendNotification);
142 gui_->setValue(stereo_name, stereo_amount, NotificationType::dontSendNotification);
143 gui_->setValue(bypass_name, bypass_amount, NotificationType::dontSendNotification);
144}
145
146void SynthGuiInterface::disconnectModulation(std::string source, std::string destination) {
147 // Remove a modulation connection and notify GUI.
148 synth_->disconnectModulation(source, destination);
150}
151
153 // Remove a modulation connection given a ModulationConnection object.
154 synth_->disconnectModulation(connection);
156}
157
159 if (gui_ == nullptr)
160 return;
161
162 gui_->setFocus();
163}
164
166 if (gui_ == nullptr)
167 return;
168
169 gui_->notifyChange();
170}
171
173 if (gui_ == nullptr)
174 return;
175
176 gui_->notifyFresh();
177}
178
180 if (gui_ == nullptr)
181 return;
182
183 gui_->openSaveDialog();
184}
185
187 if (gui_ == nullptr)
188 return;
189
190 gui_->externalPresetLoaded(preset);
191}
192
194 if (gui_ == nullptr)
195 return;
196
197 // Adjust GUI size based on given scale and display constraints.
198 Point<int> position = gui_->getScreenBounds().getCentre();
199 const Displays::Display& display = Desktop::getInstance().getDisplays().findDisplayForPoint(position);
200
201 Rectangle<int> display_area = Desktop::getInstance().getDisplays().getTotalBounds(true);
202 ComponentPeer* peer = gui_->getPeer();
203 if (peer)
204 peer->getFrameSize().subtractFrom(display_area);
205
206 float window_size = scale / display.scale;
207 window_size = std::min(window_size, display_area.getWidth() * 1.0f / vital::kDefaultWindowWidth);
208 window_size = std::min(window_size, display_area.getHeight() * 1.0f / vital::kDefaultWindowHeight);
209 LoadSave::saveWindowSize(window_size);
210
211 int width = std::round(window_size * vital::kDefaultWindowWidth);
212 int height = std::round(window_size * vital::kDefaultWindowHeight);
213
214 Rectangle<int> bounds = gui_->getBounds();
215 bounds.setWidth(width);
216 bounds.setHeight(height);
217 gui_->getParentComponent()->setBounds(bounds);
218 gui_->redoBackground();
219}
220#endif
A class for generating and storing a line shape, defined by a series of points and associated powers.
Definition line_generator.h:20
void initLinear()
Initializes the line to a simple linear shape (from 1.0 at x=0 to 0.0 at x=1).
Definition line_generator.cpp:11
static void saveWindowSize(float window_size)
Saves the window size scaling factor.
Definition load_save.cpp:1324
A base class providing foundational functionality for the Vital synthesizer’s engine,...
Definition synth_base.h:42
bool connectModulation(const std::string &source, const std::string &destination)
Connects a modulation source to a destination parameter.
Definition synth_base.cpp:165
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
vital::ModulationConnectionBank & getModulationBank()
Retrieves the ModulationConnectionBank managing all modulation connections.
Definition synth_base.cpp:730
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
WavetableCreator * getWavetableCreator(int index)
Gets a WavetableCreator for a given oscillator index.
Definition synth_base.cpp:270
vital::control_map & getControls()
Provides access to the controls map (parameter name to Parameter pointer).
Definition synth_base.h:477
vital::SoundEngine * getEngine()
Returns a pointer to the SoundEngine used for audio and modulation processing.
Definition synth_base.h:484
void valueChanged(const std::string &name, vital::mono_float value)
Updates the value of a control parameter.
Definition synth_base.cpp:50
void notifyModulationsChanged()
Notifies the GUI that modulation connections or states have changed.
Definition synth_gui_interface.cpp:83
SynthBase * synth_
The backend SynthBase this GUI interface controls.
Definition synth_gui_interface.h:226
std::unique_ptr< FullInterface > gui_
The primary GUI component (if applicable).
Definition synth_gui_interface.h:227
SynthGuiInterface(SynthBase *synth, bool use_gui=true)
Constructs the SynthGuiInterface, optionally creating a FullInterface GUI.
Definition synth_gui_interface.cpp:49
vital::mono_float getControlValue(const std::string &name)
Retrieves the current value of a named control from the synth.
Definition synth_gui_interface.cpp:79
void notifyChange()
Notifies the GUI that a parameter or modulation changed, prompting GUI updates.
Definition synth_gui_interface.cpp:165
void setGuiSize(float scale)
Sets the GUI window or component size based on a scale factor.
Definition synth_gui_interface.cpp:193
void initModulationValues(const std::string &source, const std::string &destination)
Initializes modulation values for a newly created modulation connection.
Definition synth_gui_interface.cpp:103
void setModulationValues(const std::string &source, const std::string &destination, vital::mono_float amount, bool bipolar, bool stereo, bool bypass)
Sets various modulation parameters (amount, bipolar, stereo, bypass) for a given connection.
Definition synth_gui_interface.cpp:119
virtual void updateGuiControl(const std::string &name, vital::mono_float value)
Updates a single GUI control to reflect a new parameter value.
Definition synth_gui_interface.cpp:71
void disconnectModulation(std::string source, std::string destination)
Disconnects a modulation from the GUI layer.
Definition synth_gui_interface.cpp:146
void connectModulation(std::string source, std::string destination)
Connects a modulation source to a destination parameter through the GUI.
Definition synth_gui_interface.cpp:91
void setFocus()
Brings the GUI window or main component into focus.
Definition synth_gui_interface.cpp:158
virtual ~SynthGuiInterface()
Destroys the SynthGuiInterface, cleaning up any associated GUI resources.
Definition synth_gui_interface.cpp:60
void openSaveDialog()
Opens a save dialog (e.g., to save a preset) through the GUI.
Definition synth_gui_interface.cpp:179
void notifyModulationValueChanged(int index)
Notifies the GUI that a specific modulation's value changed.
Definition synth_gui_interface.cpp:87
virtual void updateFullGui()
Updates the entire GUI to reflect the current synth state.
Definition synth_gui_interface.cpp:62
void externalPresetLoaded(File preset)
Notifies the GUI that a preset was loaded externally (outside the GUI controls).
Definition synth_gui_interface.cpp:186
void notifyFresh()
Notifies the GUI that a fresh state (like a new preset load) has occurred, prompting a full refresh.
Definition synth_gui_interface.cpp:172
ModulationConnection * atIndex(int index)
Retrieves a ModulationConnection by index.
Definition synth_types.h:114
constexpr int kNumOscillators
Number of oscillators available in Vital.
Definition synth_constants.h:16
constexpr int kDefaultWindowHeight
Default height of the Vital window (in pixels).
Definition synth_constants.h:64
constexpr int kDefaultWindowWidth
Default width of the Vital window (in pixels).
Definition synth_constants.h:61
constexpr int kNumLfos
Number of LFO sources available in the Vital synthesizer.
Definition synth_constants.h:13
float mono_float
Definition common.h:33
A struct holding references and data used by the GUI to interact with the SynthBase.
Definition synth_gui_interface.h:27
vital::output_map poly_modulations
Polyphonic modulation values.
Definition synth_gui_interface.h:37
vital::control_map controls
Current set of parameter controls from the synth.
Definition synth_gui_interface.h:35
vital::output_map modulation_sources
All available modulation sources.
Definition synth_gui_interface.h:38
vital::output_map mono_modulations
Mono (global) modulation values.
Definition synth_gui_interface.h:36
SynthBase * synth
Pointer back to the associated SynthBase.
Definition synth_gui_interface.h:40
WavetableCreator * wavetable_creators[vital::kNumOscillators]
Array of pointers to wavetable creators for each oscillator.
Definition synth_gui_interface.h:39
SynthGuiData(SynthBase *synth_base)
Constructs SynthGuiData from a given SynthBase instance.
Definition synth_gui_interface.cpp:14
A structure representing a single modulation connection between a modulation source and a destination...
Definition synth_types.h:30
std::unique_ptr< ModulationConnectionProcessor > modulation_processor
Processor applying scaling/mapping.
Definition synth_types.h:75