File: PluginEditor.cpp

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (318 lines) | stat: -rw-r--r-- 11,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Daniel Rudrich
 Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#include "PluginEditor.h"
#include "PluginProcessor.h"

//==============================================================================
MultiEQAudioProcessorEditor::MultiEQAudioProcessorEditor (MultiEQAudioProcessor& p,
                                                          juce::AudioProcessorValueTreeState& vts) :
    juce::AudioProcessorEditor (&p),
    processor (p),
    valueTreeState (vts),
    footer (p.getOSCParameterInterface())
{
    // ============== BEGIN: essentials ======================
    // set GUI size and lookAndFeel
    //setSize(500, 300); // use this to create a fixed-size GUI
    setResizeLimits (880, 330, 1000, 800); // use this to create a resizable GUI
    setResizable (true, true);
    setLookAndFeel (&globalLaF);

    // make title and footer visible, and set the PluginName
    addAndMakeVisible (&title);
    title.setTitle (juce::String ("Multi"), juce::String ("EQ"));
    title.setFont (globalLaF.robotoBold, globalLaF.robotoLight);
    addAndMakeVisible (&footer);
    // ============= END: essentials ========================

    // create the connection between title component's comboBoxes and parameters
    cbNumInputChannelsAttachment.reset (
        new ComboBoxAttachment (valueTreeState,
                                "inputChannelsSetting",
                                *title.getInputWidgetPtr()->getChannelsCbPointer()));

    tooltipWin.setLookAndFeel (&globalLaF);
    tooltipWin.setMillisecondsBeforeTipAppears (500);
    tooltipWin.setOpaque (false);

    const juce::Colour colours[numFilterBands] = {
        juce::Colours::cadetblue, // make sure you have enough colours in here
        juce::Colours::mediumpurple, juce::Colours::cornflowerblue, juce::Colours::greenyellow,
        juce::Colours::yellow,       juce::Colours::orangered
    };

    for (int f = 0; f < numFilterBands; ++f)
    {
        gainEnabled[f] = true;
        qEnabled[f] = true;
    }

    // Select state of gain and Q slider for first and last band
    auto filterTypeFirst = valueTreeState.getRawParameterValue ("filterType0");
    auto filterTypeLast =
        valueTreeState.getRawParameterValue ("filterType" + juce::String (numFilterBands - 1));

    if (*filterTypeFirst < 2.5f)
    {
        gainEnabled[0] = false;

        if (*filterTypeFirst < 0.5f || *filterTypeFirst > 1.5f)
            qEnabled[0] = false;
    }

    if (*filterTypeLast > 5.5f) // Disable gain, if high shelf is selected
    {
        gainEnabled[numFilterBands - 1] = false;

        if (*filterTypeLast < 6.5f
            || *filterTypeLast > 7.5f) // Disable Q, if 1st order or LR LP is selected
            qEnabled[numFilterBands - 1] = false;
    }

    auto filterPtr = processor.getFilter();

    addAndMakeVisible (fv);

    if (filterPtr != nullptr)
        for (int f = 0; f < numFilterBands; ++f)
            fv.addCoefficients (filterPtr->getCoefficientsForGui (f),
                                colours[f],
                                &slFilterFrequency[f],
                                &slFilterGain[f],
                                &slFilterQ[f]);

    fv.enableFilter (2, false);

    for (int i = 0; i < numFilterBands; ++i)
    {
        addAndMakeVisible (&tbFilterOn[i]);
        tbFilterOn[i].setColour (juce::ToggleButton::tickColourId, colours[i]);
        tbFilterOn[i].addListener (this);
        tbFilterOnAttachment[i].reset (new ButtonAttachment (valueTreeState,
                                                             "filterEnabled" + juce::String (i),
                                                             tbFilterOn[i]));

        const bool enabled = tbFilterOn[i].getToggleState();

        addAndMakeVisible (&cbFilterType[i]);

        if (i == 0)
        {
            cbFilterType[i].addItem ("HP (6dB/oct)", 2);
            cbFilterType[i].addItem ("HP (12dB/oct)", 3);
            cbFilterType[i].addItem ("HP (24db/oct)", 4);
            cbFilterType[i].addItem ("Low-shelf", 1);
        }
        else if (i == numFilterBands - 1)
        {
            cbFilterType[i].addItem ("High-shelf", 1);
            cbFilterType[i].addItem ("LP (6dB/oct)", 2);
            cbFilterType[i].addItem ("LP (12dB/oct)", 3);
            cbFilterType[i].addItem ("LP (24dB/oct)", 4);
        }
        else
        {
            cbFilterType[i].addItem ("Low-shelf", 1);
            cbFilterType[i].addItem ("Peak", 2);
            cbFilterType[i].addItem ("High-shelf", 3);
        }

        cbFilterType[i].setJustificationType (juce::Justification::centred);
        cbFilterTypeAttachment[i].reset (new ComboBoxAttachment (valueTreeState,
                                                                 "filterType" + juce::String (i),
                                                                 cbFilterType[i]));

        addAndMakeVisible (&slFilterFrequency[i]);
        slFilterFrequency[i].setSliderStyle (juce::Slider::RotaryHorizontalVerticalDrag);
        slFilterFrequency[i].setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 15);
        slFilterFrequency[i].setColour (juce::Slider::rotarySliderOutlineColourId, colours[i]);
        slFilterFrequencyAttachment[i].reset (
            new SliderAttachment (valueTreeState,
                                  "filterFrequency" + juce::String (i),
                                  slFilterFrequency[i]));

        addAndMakeVisible (&slFilterQ[i]);
        slFilterQ[i].setSliderStyle (juce::Slider::RotaryHorizontalVerticalDrag);
        slFilterQ[i].setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 15);
        slFilterQ[i].setColour (juce::Slider::rotarySliderOutlineColourId, colours[i]);
        slFilterQAttachment[i].reset (
            new SliderAttachment (valueTreeState, "filterQ" + juce::String (i), slFilterQ[i]));

        addAndMakeVisible (&slFilterGain[i]);
        slFilterGain[i].setSliderStyle (juce::Slider::RotaryHorizontalVerticalDrag);
        slFilterGain[i].setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 15);
        slFilterGain[i].setColour (juce::Slider::rotarySliderOutlineColourId, colours[i]);
        slFilterGainAttachment[i].reset (new SliderAttachment (valueTreeState,
                                                               "filterGain" + juce::String (i),
                                                               slFilterGain[i]));

        updateEnablement (i, enabled);
    }

    cbFilterType[0].addListener (this);
    cbFilterType[numFilterBands - 1].addListener (this);

    updateFilterVisualizer();

    // start timer after everything is set up properly
    startTimer (20);
}

MultiEQAudioProcessorEditor::~MultiEQAudioProcessorEditor()
{
    setLookAndFeel (nullptr);
}

//==============================================================================
void MultiEQAudioProcessorEditor::paint (juce::Graphics& g)
{
    g.fillAll (globalLaF.ClBackground);
}

void MultiEQAudioProcessorEditor::resized()
{
    DBG ("GUI resized to " << getLocalBounds().getWidth() << "x" << getLocalBounds().getHeight());
    // ============ BEGIN: header and footer ============
    const int leftRightMargin = 30;
    const int headerHeight = 60;
    const int footerHeight = 25;
    juce::Rectangle<int> area (getLocalBounds());

    juce::Rectangle<int> footerArea (area.removeFromBottom (footerHeight));
    footer.setBounds (footerArea);

    area.removeFromLeft (leftRightMargin);
    area.removeFromRight (leftRightMargin);
    juce::Rectangle<int> headerArea = area.removeFromTop (headerHeight);
    title.setBounds (headerArea);
    area.removeFromTop (10);
    area.removeFromBottom (5);
    // =========== END: header and footer =================

    // try to not use explicit coordinates to position your GUI components
    // the removeFrom...() methods are quite handy to create scalable areas
    // best practice would be the use of flexBoxes...
    // the following is medium level practice ;-)
    juce::Rectangle<int> filterArea = area;
    { // upper row

        juce::Rectangle<int> cbArea (filterArea.removeFromBottom (50));
        for (int i = 0; i < numFilterBands; ++i)
        {
            slFilterFrequency[i].setBounds (cbArea.removeFromLeft (42));
            slFilterGain[i].setBounds (cbArea.removeFromLeft (42));
            slFilterQ[i].setBounds (cbArea.removeFromLeft (42));
            cbArea.removeFromLeft (14);
        }

        cbArea = filterArea.removeFromBottom (21);
        cbArea.removeFromLeft (3);
        for (int i = 0; i < numFilterBands; ++i)
        {
            tbFilterOn[i].setBounds (cbArea.removeFromLeft (18));
            cbArea.removeFromLeft (5);
            cbFilterType[i].setBounds (cbArea.removeFromLeft (92).reduced (0, 3));
            cbArea.removeFromLeft (25);
        }

        fv.setBounds (filterArea);
    }
}

void MultiEQAudioProcessorEditor::updateFilterVisualizer()
{
    auto filterPtr = processor.getFilter();

    if (filterPtr != nullptr)
    {
        filterPtr->updateGuiCoefficients();

        fv.setSampleRate (processor.getSampleRate());
        for (int f = 0; f < numFilterBands; ++f)
            fv.replaceCoefficients (f, filterPtr->getCoefficientsForGui (f));
    }
}

void MultiEQAudioProcessorEditor::timerCallback()
{
    // === update titleBar widgets according to available input/output channel counts
    title.setMaxSize (processor.getMaxSize());
    // ==========================================

    if (processor.repaintFV.get())
    {
        processor.repaintFV = false;
        updateFilterVisualizer();
    }
}

void MultiEQAudioProcessorEditor::updateEnablement (const int idx, const bool shouldBeEnabled)
{
    slFilterFrequency[idx].setEnabled (shouldBeEnabled);
    slFilterGain[idx].setEnabled (shouldBeEnabled && gainEnabled[idx]);
    slFilterQ[idx].setEnabled (shouldBeEnabled && qEnabled[idx]);
    cbFilterType[idx].setEnabled (shouldBeEnabled);
    fv.enableFilter (idx, shouldBeEnabled);
}

void MultiEQAudioProcessorEditor::buttonClicked (juce::Button* button)
{
    for (int f = 0; f < numFilterBands; ++f)
    {
        if (button == &tbFilterOn[f])
        {
            const bool state = button->getToggleState();
            updateEnablement (f, state);
        }
    }
}

void MultiEQAudioProcessorEditor::comboBoxChanged (juce::ComboBox* comboBoxThatHasChanged)
{
    int idx = -1;
    if (comboBoxThatHasChanged == &cbFilterType[0])
        idx = 0;
    else if (comboBoxThatHasChanged == &cbFilterType[numFilterBands - 1])
        idx = numFilterBands - 1;
    else
        return;

    const auto id = comboBoxThatHasChanged->getSelectedId();
    if (id == 2 || id == 4) // 1st order LP/HP or LR filter
    {
        qEnabled[idx] = false;
        gainEnabled[idx] = false;
    }
    else if (id == 3) // 2nd order LP/HP
    {
        qEnabled[idx] = true;
        gainEnabled[idx] = false;
    }
    else // Shelving filter
    {
        qEnabled[idx] = true;
        gainEnabled[idx] = true;
    }

    updateEnablement (idx, tbFilterOn[idx].getToggleState());
}