You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.8KB

  1. /* Copyright 2013-2019 Matt Tytel
  2. *
  3. * vital is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * vital is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with vital. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "synth_gui_interface.h"
  17. #include "authentication.h"
  18. #include "modulation_connection_processor.h"
  19. #include "sound_engine.h"
  20. #include "load_save.h"
  21. #include "synth_base.h"
  22. SynthGuiData::SynthGuiData(SynthBase* synth_base) : synth(synth_base) {
  23. controls = synth->getControls();
  24. mono_modulations = synth->getEngine()->getMonoModulations();
  25. poly_modulations = synth->getEngine()->getPolyModulations();
  26. modulation_sources = synth->getEngine()->getModulationSources();
  27. for (int i = 0; i < vital::kNumOscillators; ++i)
  28. wavetable_creators[i] = synth->getWavetableCreator(i);
  29. }
  30. #if HEADLESS
  31. SynthGuiInterface::SynthGuiInterface(SynthBase* synth, bool use_gui) : synth_(synth) { }
  32. SynthGuiInterface::~SynthGuiInterface() { }
  33. void SynthGuiInterface::updateFullGui() { }
  34. void SynthGuiInterface::updateGuiControl(const std::string& name, vital::mono_float value) { }
  35. vital::mono_float SynthGuiInterface::getControlValue(const std::string& name) { return 0.0f; }
  36. void SynthGuiInterface::connectModulation(std::string source, std::string destination) { }
  37. void SynthGuiInterface::connectModulation(vital::ModulationConnection* connection) { }
  38. void SynthGuiInterface::setModulationValues(const std::string& source, const std::string& destination,
  39. vital::mono_float amount, bool bipolar, bool stereo, bool bypass) { }
  40. void SynthGuiInterface::disconnectModulation(std::string source, std::string destination) { }
  41. void SynthGuiInterface::disconnectModulation(vital::ModulationConnection* connection) { }
  42. void SynthGuiInterface::setFocus() { }
  43. void SynthGuiInterface::notifyChange() { }
  44. void SynthGuiInterface::notifyFresh() { }
  45. void SynthGuiInterface::openSaveDialog() { }
  46. void SynthGuiInterface::externalPresetLoaded(File preset) { }
  47. void SynthGuiInterface::setGuiSize(float scale) { }
  48. #else
  49. #include "default_look_and_feel.h"
  50. #include "full_interface.h"
  51. SynthGuiInterface::SynthGuiInterface(SynthBase* synth, bool use_gui) : synth_(synth) {
  52. if (use_gui) {
  53. LineGenerator* lfo_sources[vital::kNumLfos];
  54. for (int i = 0; i < vital::kNumLfos; ++i)
  55. lfo_sources[i] = synth->getLfoSource(i);
  56. SynthGuiData synth_data(synth_);
  57. gui_ = std::make_unique<FullInterface>(&synth_data);
  58. }
  59. }
  60. SynthGuiInterface::~SynthGuiInterface() { }
  61. void SynthGuiInterface::updateFullGui() {
  62. if (gui_ == nullptr)
  63. return;
  64. gui_->setAllValues(synth_->getControls());
  65. gui_->reset();
  66. }
  67. void SynthGuiInterface::updateGuiControl(const std::string& name, vital::mono_float value) {
  68. if (gui_ == nullptr)
  69. return;
  70. gui_->setValue(name, value, NotificationType::dontSendNotification);
  71. }
  72. vital::mono_float SynthGuiInterface::getControlValue(const std::string& name) {
  73. return synth_->getControls()[name]->value();
  74. }
  75. void SynthGuiInterface::notifyModulationsChanged() {
  76. gui_->modulationChanged();
  77. }
  78. void SynthGuiInterface::notifyModulationValueChanged(int index) {
  79. gui_->modulationValueChanged(index);
  80. }
  81. void SynthGuiInterface::connectModulation(std::string source, std::string destination) {
  82. bool created = synth_->connectModulation(source, destination);
  83. if (created)
  84. initModulationValues(source, destination);
  85. notifyModulationsChanged();
  86. }
  87. void SynthGuiInterface::connectModulation(vital::ModulationConnection* connection) {
  88. synth_->connectModulation(connection);
  89. notifyModulationsChanged();
  90. }
  91. void SynthGuiInterface::initModulationValues(const std::string& source, const std::string& destination) {
  92. int connection_index = synth_->getConnectionIndex(source, destination);
  93. if (connection_index < 0)
  94. return;
  95. vital::ModulationConnection* connection = synth_->getModulationBank().atIndex(connection_index);
  96. LineGenerator* map_generator = connection->modulation_processor->lineMapGenerator();
  97. map_generator->initLinear();
  98. std::string power_name = "modulation_" + std::to_string(connection_index + 1) + "_power";
  99. synth_->valueChanged(power_name, 0.0f);
  100. gui_->setValue(power_name, 0.0f, NotificationType::dontSendNotification);
  101. }
  102. void SynthGuiInterface::setModulationValues(const std::string& source, const std::string& destination,
  103. vital::mono_float amount, bool bipolar, bool stereo, bool bypass) {
  104. int connection_index = synth_->getConnectionIndex(source, destination);
  105. if (connection_index < 0)
  106. return;
  107. std::string number = std::to_string(connection_index + 1);
  108. std::string amount_name = "modulation_" + number + "_amount";
  109. std::string bipolar_name = "modulation_" + number + "_bipolar";
  110. std::string stereo_name = "modulation_" + number + "_stereo";
  111. std::string bypass_name = "modulation_" + number + "_bypass";
  112. float bipolar_amount = bipolar ? 1.0f : 0.0f;
  113. float stereo_amount = stereo ? 1.0f : 0.0f;
  114. float bypass_amount = bypass ? 1.0f : 0.0f;
  115. synth_->valueChanged(amount_name, amount);
  116. synth_->valueChanged(bipolar_name, bipolar_amount);
  117. synth_->valueChanged(stereo_name, stereo_amount);
  118. synth_->valueChanged(bypass_name, bypass_amount);
  119. gui_->setValue(amount_name, amount, NotificationType::dontSendNotification);
  120. gui_->setValue(bipolar_name, bipolar_amount, NotificationType::dontSendNotification);
  121. gui_->setValue(stereo_name, stereo_amount, NotificationType::dontSendNotification);
  122. gui_->setValue(bypass_name, bypass_amount, NotificationType::dontSendNotification);
  123. }
  124. void SynthGuiInterface::disconnectModulation(std::string source, std::string destination) {
  125. synth_->disconnectModulation(source, destination);
  126. notifyModulationsChanged();
  127. }
  128. void SynthGuiInterface::disconnectModulation(vital::ModulationConnection* connection) {
  129. synth_->disconnectModulation(connection);
  130. notifyModulationsChanged();
  131. }
  132. void SynthGuiInterface::setFocus() {
  133. if (gui_ == nullptr)
  134. return;
  135. gui_->setFocus();
  136. }
  137. void SynthGuiInterface::notifyChange() {
  138. if (gui_ == nullptr)
  139. return;
  140. gui_->notifyChange();
  141. }
  142. void SynthGuiInterface::notifyFresh() {
  143. if (gui_ == nullptr)
  144. return;
  145. gui_->notifyFresh();
  146. }
  147. void SynthGuiInterface::openSaveDialog() {
  148. if (gui_ == nullptr)
  149. return;
  150. gui_->openSaveDialog();
  151. }
  152. void SynthGuiInterface::externalPresetLoaded(File preset) {
  153. if (gui_ == nullptr)
  154. return;
  155. gui_->externalPresetLoaded(preset);
  156. }
  157. void SynthGuiInterface::setGuiSize(float scale) {
  158. if (gui_ == nullptr)
  159. return;
  160. Point<int> position = gui_->getScreenBounds().getCentre();
  161. const Displays::Display& display = Desktop::getInstance().getDisplays().findDisplayForPoint(position);
  162. Rectangle<int> display_area = Desktop::getInstance().getDisplays().getTotalBounds(true);
  163. ComponentPeer* peer = gui_->getPeer();
  164. if (peer)
  165. peer->getFrameSize().subtractFrom(display_area);
  166. float window_size = scale / display.scale;
  167. window_size = std::min(window_size, display_area.getWidth() * 1.0f / vital::kDefaultWindowWidth);
  168. window_size = std::min(window_size, display_area.getHeight() * 1.0f / vital::kDefaultWindowHeight);
  169. LoadSave::saveWindowSize(window_size);
  170. int width = std::round(window_size * vital::kDefaultWindowWidth);
  171. int height = std::round(window_size * vital::kDefaultWindowHeight);
  172. Rectangle<int> bounds = gui_->getBounds();
  173. bounds.setWidth(width);
  174. bounds.setHeight(height);
  175. gui_->getParentComponent()->setBounds(bounds);
  176. gui_->redoBackground();
  177. }
  178. #endif