1 /***************************************************************************
2 **                                                                        **
3 **  Polyphone, a soundfont editor                                         **
4 **  Copyright (C) 2013-2020 Davy Triponney                                **
5 **                                                                        **
6 **  This program is free software: you can redistribute it and/or modify  **
7 **  it under the terms of the GNU General Public License as published by  **
8 **  the Free Software Foundation, either version 3 of the License, or     **
9 **  (at your option) any later version.                                   **
10 **                                                                        **
11 **  This program is distributed in the hope that it will be useful,       **
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          **
14 **  GNU General Public License for more details.                          **
15 **                                                                        **
16 **  You should have received a copy of the GNU General Public License     **
17 **  along with this program. If not, see http://www.gnu.org/licenses/.    **
18 **                                                                        **
19 ****************************************************************************
20 **           Author: Davy Triponney                                       **
21 **  Website/Contact: https://www.polyphone-soundfonts.com                 **
22 **             Date: 01.01.2013                                           **
23 ***************************************************************************/
24 
25 #include "controllerarea.h"
26 #include "ui_controllerarea.h"
27 #include "contextmanager.h"
28 
ControllerArea(QWidget * parent)29 ControllerArea::ControllerArea(QWidget *parent) :
30     QWidget(parent),
31     ui(new Ui::ControllerArea),
32     _ledState(false)
33 {
34     ui->setupUi(this);
35 
36     // Led on / off
37     QMap<QString, QString> replacement;
38     replacement["border1"] = this->palette().dark().color().darker(130).name();
39     replacement["border2"] = this->palette().dark().color().name();
40     replacement["color1"] = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND).darker(130).name();
41     replacement["color2"] = ContextManager::theme()->getColor(ThemeManager::HIGHLIGHTED_BACKGROUND).name();
42     _ledOn = ContextManager::theme()->getColoredSvg(":/icons/led.svg", QSize(48, 48), replacement);
43     replacement["color1"] = replacement["border1"];
44     replacement["color2"] = replacement["border2"];
45     _ledOff = ContextManager::theme()->getColoredSvg(":/icons/led.svg", QSize(48, 48), replacement);
46     ui->push4->setIcon(_ledOff);
47 
48     // Initialization of the sensitivity slider
49     updateBendSensitivity(ContextManager::midi()->getBendSensitivityValue());
50 
51     // Initialization of the pressure slider
52     updateMonoPressure(ContextManager::midi()->getMonoPressure());
53 
54     // Initialization of the wheel
55     ui->sliderPitchWheel->setColorFromMiddle(true);
56     ui->sliderPitchWheel->setBackToValue(64);
57     updateBend(0); // Always in the middle
58 
59     // Initialization of the controllers
60     ui->comboControl1->blockSignals(true);
61     ui->comboControl1->selectCC(ContextManager::configuration()->getValue(ConfManager::SECTION_MIDI, "controller_1", 1).toInt());
62     on_comboControl1_currentIndexChanged(-1);
63     ui->comboControl1->blockSignals(false);
64     ui->comboControl2->blockSignals(true);
65     ui->comboControl2->selectCC(ContextManager::configuration()->getValue(ConfManager::SECTION_MIDI, "controller_2", 10).toInt());
66     on_comboControl2_currentIndexChanged(-1);
67     ui->comboControl2->blockSignals(false);
68     ui->comboControl3->blockSignals(true);
69     ui->comboControl3->selectCC(ContextManager::configuration()->getValue(ConfManager::SECTION_MIDI, "controller_3", 11).toInt());
70     on_comboControl3_currentIndexChanged(-1);
71     ui->comboControl3->blockSignals(false);
72     ui->comboControl4->blockSignals(true);
73     ui->comboControl4->selectCC(ContextManager::configuration()->getValue(ConfManager::SECTION_MIDI, "controller_4", 64).toInt());
74     on_comboControl4_currentIndexChanged(-1);
75     ui->comboControl4->blockSignals(false);
76 }
77 
~ControllerArea()78 ControllerArea::~ControllerArea()
79 {
80     delete ui;
81 }
82 
updateMonoPressure(int value)83 void ControllerArea::updateMonoPressure(int value)
84 {
85     if (value < 0)
86         value = 0;
87     else if (value > 127)
88         value = 127;
89 
90     ui->sliderPressure->blockSignals(true);
91     ui->sliderPressure->setValue(value);
92     ui->sliderPressure->blockSignals(false);
93 
94     ui->labelPressureValue->setText(QString::number(value));
95 }
96 
updateController(int num,int value)97 void ControllerArea::updateController(int num, int value)
98 {
99     // Update first input?
100     if (ui->comboControl1->getCurrentCC() == num)
101     {
102         ui->knob1->blockSignals(true);
103         ui->knob1->setValue(value);
104         ui->labelValue1->setText(QString::number(value));
105         ui->knob1->blockSignals(false);
106     }
107 
108     // Update second input?
109     if (ui->comboControl2->getCurrentCC() == num)
110     {
111         ui->knob2->blockSignals(true);
112         ui->knob2->setValue(value);
113         ui->labelValue2->setText(QString::number(value));
114         ui->knob2->blockSignals(false);
115     }
116 
117     // Update third input?
118     if (ui->comboControl3->getCurrentCC() == num)
119     {
120         ui->knob3->blockSignals(true);
121         ui->knob3->setValue(value);
122         ui->labelValue3->setText(QString::number(value));
123         ui->knob3->blockSignals(false);
124     }
125 
126     // Update fourth input?
127     if (ui->comboControl4->getCurrentCC() == num)
128     {
129         _ledState = (value >= 64);
130         updateInput4Display();
131     }
132 }
133 
updateBend(double value,bool stopTimer)134 void ControllerArea::updateBend(double value, bool stopTimer)
135 {
136     if (value < -1)
137         value = -1;
138     else if (value > 1)
139         value = 1;
140 
141     ui->sliderPitchWheel->blockSignals(true);
142     ui->sliderPitchWheel->setValue(static_cast<int>((value + 1) * 64));
143     if (stopTimer)
144         ui->sliderPitchWheel->stopTimer();
145     ui->sliderPitchWheel->blockSignals(false);
146 
147     ui->labelWheelValue->setText(QString::number(value, 'f', 2));
148 }
149 
updateBendSensitivity(double semitones)150 void ControllerArea::updateBendSensitivity(double semitones)
151 {
152     if (semitones < 0)
153         semitones = 0;
154     else if (semitones > 15.0)
155         semitones = 15.0;
156 
157     ui->sliderSensitivity->blockSignals(true);
158     ui->sliderSensitivity->setValue(static_cast<int>(semitones * 100));
159     ui->sliderSensitivity->blockSignals(false);
160 
161     ui->labelSensitivityValue->setText(QString::number(semitones, 'f', 2));
162 }
163 
on_sliderPitchWheel_valueChanged(int value)164 void ControllerArea::on_sliderPitchWheel_valueChanged(int value)
165 {
166     double fVal = static_cast<double>(value - 64) / 64;
167     updateBend(fVal, false);
168     emit(bendChanged(fVal));
169 }
170 
on_sliderSensitivity_valueChanged(int value)171 void ControllerArea::on_sliderSensitivity_valueChanged(int value)
172 {
173     double semitones = 0.01 * value;
174     updateBendSensitivity(semitones);
175     emit(bendSensitivityChanged(semitones));
176 }
177 
on_sliderPressure_valueChanged(int value)178 void ControllerArea::on_sliderPressure_valueChanged(int value)
179 {
180     updateMonoPressure(value);
181     emit(monoPressureChanged(value));
182 }
183 
on_comboControl1_currentIndexChanged(int index)184 void ControllerArea::on_comboControl1_currentIndexChanged(int index)
185 {
186     Q_UNUSED(index)
187     ContextManager::configuration()->setValue(ConfManager::SECTION_MIDI, "controller_1", ui->comboControl1->getCurrentCC());
188 
189     int previousValue = ContextManager::midi()->getControllerValue(ui->comboControl1->getCurrentCC());
190     if (previousValue < 0) // Keep the current one
191         on_knob1_valueChanged(ui->knob1->value());
192     else
193     {
194         ui->knob1->blockSignals(true);
195         ui->knob1->setValue(previousValue);
196         ui->labelValue1->setText(QString::number(previousValue));
197         ui->knob1->blockSignals(false);
198     }
199 }
200 
on_comboControl2_currentIndexChanged(int index)201 void ControllerArea::on_comboControl2_currentIndexChanged(int index)
202 {
203     Q_UNUSED(index)
204     ContextManager::configuration()->setValue(ConfManager::SECTION_MIDI, "controller_2", ui->comboControl2->getCurrentCC());
205 
206     int previousValue = ContextManager::midi()->getControllerValue(ui->comboControl2->getCurrentCC());
207     if (previousValue < 0) // Keep the current one
208         on_knob2_valueChanged(ui->knob2->value());
209     else
210     {
211         ui->knob2->blockSignals(true);
212         ui->knob2->setValue(previousValue);
213         ui->labelValue2->setText(QString::number(previousValue));
214         ui->knob2->blockSignals(false);
215     }
216 }
217 
on_comboControl3_currentIndexChanged(int index)218 void ControllerArea::on_comboControl3_currentIndexChanged(int index)
219 {
220     Q_UNUSED(index)
221     ContextManager::configuration()->setValue(ConfManager::SECTION_MIDI, "controller_3", ui->comboControl3->getCurrentCC());
222 
223     int previousValue = ContextManager::midi()->getControllerValue(ui->comboControl3->getCurrentCC());
224     if (previousValue < 0) // Keep the current one
225         on_knob3_valueChanged(ui->knob3->value());
226     else
227     {
228         ui->knob3->blockSignals(true);
229         ui->knob3->setValue(previousValue);
230         ui->labelValue3->setText(QString::number(previousValue));
231         ui->knob3->blockSignals(false);
232     }
233 }
234 
on_comboControl4_currentIndexChanged(int index)235 void ControllerArea::on_comboControl4_currentIndexChanged(int index)
236 {
237     Q_UNUSED(index)
238     ContextManager::configuration()->setValue(ConfManager::SECTION_MIDI, "controller_4", ui->comboControl4->getCurrentCC());
239 
240     int previousValue = ContextManager::midi()->getControllerValue(ui->comboControl4->getCurrentCC());
241     if (previousValue < 0) // Keep the current one
242     {
243         _ledState = !_ledState; // Will be back to the same value next line
244         on_push4_clicked();
245     }
246     else
247     {
248         _ledState = (previousValue >= 64);
249         updateInput4Display();
250     }
251 }
252 
on_knob1_valueChanged(int value)253 void ControllerArea::on_knob1_valueChanged(int value)
254 {
255     ui->labelValue1->setText(QString::number(value));
256     emit(controllerChanged(ui->comboControl1->getCurrentCC(), value));
257 }
258 
on_knob2_valueChanged(int value)259 void ControllerArea::on_knob2_valueChanged(int value)
260 {
261     ui->labelValue2->setText(QString::number(value));
262     emit(controllerChanged(ui->comboControl2->getCurrentCC(), value));
263 }
264 
on_knob3_valueChanged(int value)265 void ControllerArea::on_knob3_valueChanged(int value)
266 {
267     ui->labelValue3->setText(QString::number(value));
268     emit(controllerChanged(ui->comboControl3->getCurrentCC(), value));
269 }
270 
on_push4_clicked()271 void ControllerArea::on_push4_clicked()
272 {
273     _ledState = !_ledState;
274     updateInput4Display();
275     emit(controllerChanged(ui->comboControl4->getCurrentCC(), _ledState ? 127 : 0));
276 }
277 
updateInput4Display()278 void ControllerArea::updateInput4Display()
279 {
280     if (_ledState)
281     {
282         ui->labelValue4->setText(tr("on"));
283         ui->push4->setIcon(_ledOn);
284     }
285     else
286     {
287         ui->labelValue4->setText(tr("off"));
288         ui->push4->setIcon(_ledOff);
289     }
290 }
291