Vital
Loading...
Searching...
No Matches
phase_modifier_overlay.cpp
Go to the documentation of this file.
2
3#include "skin.h"
4#include "wave_frame.h"
6
7namespace {
8 // Lookup names for different phase styles.
9 const std::string phase_style_lookup[PhaseModifier::kNumPhaseStyles] = {
10 "Normal",
11 "+Even/-Odd",
12 "Harmonic",
13 "Harm +Even/-Odd",
14 "Clear"
15 };
16} // namespace
17
18PhaseModifierOverlay::PhaseModifierOverlay() : WavetableComponentOverlay("PHASE SHIFTER"), phase_modifier_(nullptr) {
19 current_frame_ = nullptr;
20
21 // Create and set up editors and controls.
22 editor_ = std::make_unique<PhaseEditor>();
24 editor_->addListener(this);
25 editor_->setAlwaysOnTop(true);
26
27 slider_ = std::make_unique<PhaseEditor>();
29 slider_->addListener(this);
30 slider_->setMaxTickHeight(1.0f);
31 slider_->setAlwaysOnTop(true);
32
33 phase_text_ = std::make_unique<TextEditor>();
34 addChildComponent(phase_text_.get());
35 phase_text_->addListener(this);
36 phase_text_->setSelectAllWhenFocused(true);
37 phase_text_->setMouseClickGrabsKeyboardFocus(true);
38 phase_text_->setText("0");
39
40 phase_style_ = std::make_unique<TextSelector>("Harmonic Phase");
42 phase_style_->setAlwaysOnTop(true);
43 phase_style_->getImageComponent()->setAlwaysOnTop(true);
44 phase_style_->setLookAndFeel(TextLookAndFeel::instance());
45 phase_style_->addListener(this);
47 phase_style_->setLongStringLookup(phase_style_lookup);
48 phase_style_->setStringLookup(phase_style_lookup);
49 phase_style_->setSliderStyle(Slider::RotaryHorizontalVerticalDrag);
50
51 mix_ = std::make_unique<SynthSlider>("Phase Mix");
52 addSlider(mix_.get());
53 mix_->setAlwaysOnTop(true);
54 mix_->getQuadComponent()->setAlwaysOnTop(true);
55 mix_->setRange(0.0, 1.0);
56 mix_->setDoubleClickReturnValue(true, 1.0);
57 mix_->addListener(this);
58 mix_->setSliderStyle(Slider::LinearBar);
59
64}
65
67 if (keyframe == nullptr) {
68 editor_->setVisible(false);
69 current_frame_ = nullptr;
70 }
71 else if (keyframe->owner() == phase_modifier_) {
72 editor_->setVisible(true);
75 slider_->setPhase(current_frame_->getPhase());
76 mix_->setValue(current_frame_->getMix(), dontSendNotification);
77 mix_->redoImage();
79 }
80}
81
82void PhaseModifierOverlay::setEditBounds(Rectangle<int> bounds) {
83 // Arrange controls horizontally:
84 // [STYLE][PHASE EDITORS][MIX SLIDER]
85 static constexpr float kPhaseStyleWidthHeightRatio = 4.0f;
86 static constexpr float kPhaseWidthHeightRatio = 8.0f;
87 static constexpr float kMixWidthHeightRatio = 5.0f;
88 static constexpr float kMixPaddingRatio = 0.5f;
89
90 int padding = getPadding();
91 int phase_style_width = bounds.getHeight() * kPhaseStyleWidthHeightRatio;
92 int phase_width = bounds.getHeight() * kPhaseWidthHeightRatio;
93 int mix_width = bounds.getHeight() * kMixWidthHeightRatio;
94 int mix_padding = bounds.getHeight() * kMixPaddingRatio;
95 int total_width = phase_style_width + phase_width + mix_width + 2 * padding;
96 setControlsWidth(total_width);
98
99 int x = bounds.getX() + (bounds.getWidth() - total_width) / 2;
100 int title_height = WavetableComponentOverlay::kTitleHeightRatio * bounds.getHeight();
101 int y = bounds.getY();
102 int y_title = y + title_height;
103 int height = bounds.getHeight();
104 int height_title = height - title_height;
105
106 phase_style_->setTextHeightPercentage(0.4f);
107 phase_style_->setBounds(x, y_title, phase_style_width, height_title);
108 slider_->setBounds(phase_style_->getRight() + padding, y, phase_width, height);
109 mix_->setBounds(slider_->getRight() + padding + mix_padding, y_title, mix_width - 2 * mix_padding, height_title);
110
111 phase_style_->redoImage();
112 mix_->redoImage();
113
115 controls_background_.addLine(phase_style_width);
116 controls_background_.addLine(phase_style_width + phase_width + padding);
117}
118
119bool PhaseModifierOverlay::setTimeDomainBounds(Rectangle<int> bounds) {
120 editor_->setBounds(bounds);
121 editor_->setColor(findColour(Skin::kLightenScreen, true));
122 slider_->setColor(findColour(Skin::kLightenScreen, true));
123 return false;
124}
125
127 setPhase(text_editor.getText());
128 notifyChanged(true);
129}
130
131void PhaseModifierOverlay::textEditorFocusLost(TextEditor& text_editor) {
132 setPhase(text_editor.getText());
133 notifyChanged(true);
134}
135
136void PhaseModifierOverlay::phaseChanged(float phase, bool mouse_up) {
137 if (current_frame_ == nullptr)
138 return;
139
140 phase_text_->setText(String(phase * vital::kDegreesPerCycle / (2.0f * vital::kPi)));
141 slider_->setPhase(phase);
142 editor_->setPhase(phase);
143 current_frame_->setPhase(phase);
144
145 notifyChanged(mouse_up);
146}
147
149 if (phase_modifier_ == nullptr || current_frame_ == nullptr)
150 return;
151
152 if (moved_slider == phase_style_.get()) {
153 int value = phase_style_->getValue();
155 notifyChanged(true);
156 }
157 else if (moved_slider == mix_.get()) {
158 if (current_frame_)
159 current_frame_->setMix(mix_->getValue());
160 notifyChanged(false);
161 }
162 else
163 notifyChanged(false);
164}
165
166void PhaseModifierOverlay::sliderDragEnded(Slider* moved_slider) {
167 notifyChanged(true);
168}
169
170void PhaseModifierOverlay::setPhase(String phase_string) {
171 float phase = 2.0f * vital::kPi / vital::kDegreesPerCycle * phase_string.getFloatValue();
172 if (current_frame_)
173 current_frame_->setPhase(phase);
174 editor_->setPhase(phase);
175}
float getMix()
Gets the mix ratio blending between original and modified phases.
Definition phase_modifier.h:68
void setMix(float mix)
Sets the mix ratio.
Definition phase_modifier.h:82
float getPhase()
Gets the phase offset applied to harmonics.
Definition phase_modifier.h:61
void setPhase(float phase)
Sets the phase offset (in radians).
Definition phase_modifier.h:75
PhaseStyle getPhaseStyle() const
Gets the current phase modification style.
Definition phase_modifier.h:131
PhaseModifierKeyframe * getKeyframe(int index)
Retrieves a PhaseModifierKeyframe by index.
Definition phase_modifier.cpp:128
void setPhaseStyle(PhaseStyle style)
Sets the style of phase modification.
Definition phase_modifier.h:124
PhaseStyle
Various methods to modify harmonic phase.
Definition phase_modifier.h:25
@ kNumPhaseStyles
Definition phase_modifier.h:31
PhaseModifier * phase_modifier_
The assigned PhaseModifier.
Definition phase_modifier_overlay.h:120
void sliderDragEnded(Slider *moved_slider) override
Called when a slider drag operation ends.
Definition phase_modifier_overlay.cpp:166
std::unique_ptr< TextSelector > phase_style_
Selector for phase style.
Definition phase_modifier_overlay.h:125
virtual void frameSelected(WavetableKeyframe *keyframe) override
Called when a new frame is selected.
Definition phase_modifier_overlay.cpp:66
std::unique_ptr< SynthSlider > mix_
Slider for phase mix amount.
Definition phase_modifier_overlay.h:126
void phaseChanged(float phase, bool mouse_up) override
Callback from PhaseEditor when the phase has changed.
Definition phase_modifier_overlay.cpp:136
virtual bool setTimeDomainBounds(Rectangle< int > bounds) override
Set the bounds for the time-domain waveform display area.
Definition phase_modifier_overlay.cpp:119
PhaseModifier::PhaseModifierKeyframe * current_frame_
Currently selected frame from the PhaseModifier.
Definition phase_modifier_overlay.h:121
std::unique_ptr< TextEditor > phase_text_
Text editor for manual phase input.
Definition phase_modifier_overlay.h:124
void sliderValueChanged(Slider *moved_slider) override
Called when a slider value changes.
Definition phase_modifier_overlay.cpp:148
virtual void textEditorFocusLost(TextEditor &text_editor) override
Called when the phase text editor loses focus.
Definition phase_modifier_overlay.cpp:131
void setPhase(String phase_string)
Sets the phase from a text string (in degrees).
Definition phase_modifier_overlay.cpp:170
std::unique_ptr< PhaseEditor > editor_
Interactive phase editor.
Definition phase_modifier_overlay.h:122
virtual void textEditorReturnKeyPressed(TextEditor &text_editor) override
Called when the user presses 'Enter' in the phase text editor.
Definition phase_modifier_overlay.cpp:126
virtual void setEditBounds(Rectangle< int > bounds) override
Set the bounds of editing controls within the overlay.
Definition phase_modifier_overlay.cpp:82
std::unique_ptr< PhaseEditor > slider_
Phase editor used as a reference slider line.
Definition phase_modifier_overlay.h:123
PhaseModifierOverlay()
Constructor.
Definition phase_modifier_overlay.cpp:18
@ kLightenScreen
Definition skin.h:141
void addSlider(SynthSlider *slider, bool show=true, bool listen=true)
Definition synth_section.cpp:445
void addOpenGlComponent(OpenGlComponent *open_gl_component, bool to_beginning=false)
Definition synth_section.cpp:489
static TextLookAndFeel * instance()
Singleton instance access.
Definition text_look_and_feel.h:106
void addTitle(const std::string &title)
Adds a title string for the next control section.
Definition wavetable_component_overlay.h:94
void addLine(int position)
Adds a vertical line divider at the given position.
Definition wavetable_component_overlay.h:88
void clearTitles()
Clears all control section titles.
Definition wavetable_component_overlay.h:82
void clearLines()
Clears all line divider positions.
Definition wavetable_component_overlay.h:77
A base overlay component for editing and interacting with a wavetable component's parameters.
Definition wavetable_component_overlay.h:22
void setControlsWidth(int width)
Sets the total width for controls in the overlay.
Definition wavetable_component_overlay.h:267
ControlsBackground controls_background_
Definition wavetable_component_overlay.h:300
virtual void setEditBounds(Rectangle< int > bounds)
Sets the editing bounds within which controls and titles are placed.
Definition wavetable_component_overlay.cpp:67
static constexpr float kTitleHeightRatio
Definition wavetable_component_overlay.h:32
int getPadding()
Gets the current padding value.
Definition wavetable_component_overlay.h:248
void notifyChanged(bool mouse_up)
Notifies listeners that a change has occurred to the frame.
Definition wavetable_component_overlay.cpp:86
Represents a single state of a waveform at a specific position in a wavetable.
Definition wavetable_keyframe.h:35
int index()
Gets the index of this keyframe within its owner component.
Definition wavetable_keyframe.cpp:32
WavetableComponent * owner()
Gets the WavetableComponent that owns this keyframe.
Definition wavetable_keyframe.h:152
constexpr int kDegreesPerCycle
Degrees in a full rotation (for LFO phases).
Definition common.h:49
constexpr mono_float kPi
Pi constant.
Definition common.h:36