KnobsWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "KnobsWidget.h"
  28. #include "AudioEngine.h"
  29. #include "MainWidget.h"
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/Slider.h>
  33. KnobsWidget::KnobsWidget(GUI::Widget* parent, AudioEngine& audio_engine, MainWidget& main_widget)
  34. : GUI::Frame(parent)
  35. , m_audio_engine(audio_engine)
  36. , m_main_widget(main_widget)
  37. {
  38. set_frame_thickness(2);
  39. set_frame_shadow(Gfx::FrameShadow::Sunken);
  40. set_frame_shape(Gfx::FrameShape::Container);
  41. set_layout(make<GUI::VerticalBoxLayout>());
  42. set_fill_with_background_color(true);
  43. m_labels_container = GUI::Widget::construct(this);
  44. m_labels_container->set_layout(make<GUI::HorizontalBoxLayout>());
  45. m_labels_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  46. m_labels_container->set_preferred_size(0, 20);
  47. m_octave_label = GUI::Label::construct("Octave", m_labels_container);
  48. m_wave_label = GUI::Label::construct("Wave", m_labels_container);
  49. m_attack_label = GUI::Label::construct("Attack", m_labels_container);
  50. m_decay_label = GUI::Label::construct("Decay", m_labels_container);
  51. m_sustain_label = GUI::Label::construct("Sustain", m_labels_container);
  52. m_release_label = GUI::Label::construct("Release", m_labels_container);
  53. m_delay_label = GUI::Label::construct("Delay", m_labels_container);
  54. m_values_container = GUI::Widget::construct(this);
  55. m_values_container->set_layout(make<GUI::HorizontalBoxLayout>());
  56. m_values_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  57. m_values_container->set_preferred_size(0, 10);
  58. m_octave_value = GUI::Label::construct(String::number(m_audio_engine.octave()), m_values_container);
  59. m_wave_value = GUI::Label::construct(wave_strings[m_audio_engine.wave()], m_values_container);
  60. m_attack_value = GUI::Label::construct(String::number(m_audio_engine.attack()), m_values_container);
  61. m_decay_value = GUI::Label::construct(String::number(m_audio_engine.decay()), m_values_container);
  62. m_sustain_value = GUI::Label::construct(String::number(m_audio_engine.sustain()), m_values_container);
  63. m_release_value = GUI::Label::construct(String::number(m_audio_engine.release()), m_values_container);
  64. m_delay_value = GUI::Label::construct(String::number(m_audio_engine.delay() / m_audio_engine.tick()), m_values_container);
  65. m_knobs_container = GUI::Widget::construct(this);
  66. m_knobs_container->set_layout(make<GUI::HorizontalBoxLayout>());
  67. // FIXME: Implement vertical flipping in GSlider, not here.
  68. m_octave_knob = GUI::VerticalSlider::construct(m_knobs_container);
  69. m_octave_knob->set_tooltip("Z: octave down, X: octave up");
  70. m_octave_knob->set_range(octave_min - 1, octave_max - 1);
  71. m_octave_knob->set_value((octave_max - 1) - (m_audio_engine.octave() - 1));
  72. m_octave_knob->on_value_changed = [this](int value) {
  73. int new_octave = octave_max - value;
  74. if (m_change_octave)
  75. m_main_widget.set_octave_and_ensure_note_change(new_octave == m_audio_engine.octave() + 1 ? Up : Down);
  76. ASSERT(new_octave == m_audio_engine.octave());
  77. m_octave_value->set_text(String::number(new_octave));
  78. };
  79. m_wave_knob = GUI::VerticalSlider::construct(m_knobs_container);
  80. m_wave_knob->set_tooltip("C: cycle through waveforms");
  81. m_wave_knob->set_range(0, last_wave);
  82. m_wave_knob->set_value(last_wave - m_audio_engine.wave());
  83. m_wave_knob->on_value_changed = [this](int value) {
  84. int new_wave = last_wave - value;
  85. m_audio_engine.set_wave(new_wave);
  86. ASSERT(new_wave == m_audio_engine.wave());
  87. m_wave_value->set_text(wave_strings[new_wave]);
  88. };
  89. constexpr int max_attack = 1000;
  90. m_attack_knob = GUI::VerticalSlider::construct(m_knobs_container);
  91. m_attack_knob->set_range(0, max_attack);
  92. m_attack_knob->set_value(max_attack - m_audio_engine.attack());
  93. m_attack_knob->set_step(100);
  94. m_attack_knob->on_value_changed = [this](int value) {
  95. int new_attack = max_attack - value;
  96. m_audio_engine.set_attack(new_attack);
  97. ASSERT(new_attack == m_audio_engine.attack());
  98. m_attack_value->set_text(String::number(new_attack));
  99. };
  100. constexpr int max_decay = 1000;
  101. m_decay_knob = GUI::VerticalSlider::construct(m_knobs_container);
  102. m_decay_knob->set_range(0, max_decay);
  103. m_decay_knob->set_value(max_decay - m_audio_engine.decay());
  104. m_decay_knob->set_step(100);
  105. m_decay_knob->on_value_changed = [this](int value) {
  106. int new_decay = max_decay - value;
  107. m_audio_engine.set_decay(new_decay);
  108. ASSERT(new_decay == m_audio_engine.decay());
  109. m_decay_value->set_text(String::number(new_decay));
  110. };
  111. constexpr int max_sustain = 1000;
  112. m_sustain_knob = GUI::VerticalSlider::construct(m_knobs_container);
  113. m_sustain_knob->set_range(0, max_sustain);
  114. m_sustain_knob->set_value(max_sustain - m_audio_engine.sustain());
  115. m_sustain_knob->set_step(100);
  116. m_sustain_knob->on_value_changed = [this](int value) {
  117. int new_sustain = max_sustain - value;
  118. m_audio_engine.set_sustain(new_sustain);
  119. ASSERT(new_sustain == m_audio_engine.sustain());
  120. m_sustain_value->set_text(String::number(new_sustain));
  121. };
  122. constexpr int max_release = 1000;
  123. m_release_knob = GUI::VerticalSlider::construct(m_knobs_container);
  124. m_release_knob->set_range(0, max_release);
  125. m_release_knob->set_value(max_release - m_audio_engine.release());
  126. m_release_knob->set_step(100);
  127. m_release_knob->on_value_changed = [this](int value) {
  128. int new_release = max_release - value;
  129. m_audio_engine.set_release(new_release);
  130. ASSERT(new_release == m_audio_engine.release());
  131. m_release_value->set_text(String::number(new_release));
  132. };
  133. constexpr int max_delay = 8;
  134. m_delay_knob = GUI::VerticalSlider::construct(m_knobs_container);
  135. m_delay_knob->set_range(0, max_delay);
  136. m_delay_knob->set_value(max_delay - (m_audio_engine.delay() / m_audio_engine.tick()));
  137. m_delay_knob->on_value_changed = [this](int value) {
  138. int new_delay = m_audio_engine.tick() * (max_delay - value);
  139. m_audio_engine.set_delay(new_delay);
  140. ASSERT(new_delay == m_audio_engine.delay());
  141. m_delay_value->set_text(String::number(new_delay / m_audio_engine.tick()));
  142. };
  143. }
  144. KnobsWidget::~KnobsWidget()
  145. {
  146. }
  147. void KnobsWidget::update_knobs()
  148. {
  149. m_wave_knob->set_value(last_wave - m_audio_engine.wave());
  150. // FIXME: This is needed because when the slider is changed directly, it
  151. // needs to change the octave, but if the octave was changed elsewhere, we
  152. // need to change the slider without changing the octave.
  153. m_change_octave = false;
  154. m_octave_knob->set_value(octave_max - m_audio_engine.octave());
  155. m_change_octave = true;
  156. }