Debian Codegraph @

Qt/Qt_compressor_widget_callbacks.h

raw symbols SCIP
1 /* Copyright 2013 Kjetil S. Matheussen
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License
5 as published by the Free Software Foundation; either version 2
6 of the License, or (at your option) any later version.
7
8 This program 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 this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17 #include <stdio.h>
18
19 #include "Qt_compressor_widget.h"
20
21 #include <QPainter>
22 #include <QMouseEvent>
23 #include <QTimer>
24 #include <QFileDialog>
25 #include <QMessageBox>
26
27 #define INCLUDE_MYLINE_AND_MYRECT 1
28 # include "compressor_vertical_sliders.cpp"
29 #undef INCLUDE_MYLINE_AND_MYRECT
30
31 namespace cvs{
32
33 struct MyColor{
34 QColor col;
35 MyColor(int r,int g,int b)
36 : col(r,g,b)
37 {}
38
39 MyColor(QColor col)
40 : col(col)
41 {}
42
43 MyColor(int colnum)
44 #ifdef COMPILING_RADIUM
45 : col(static_cast<EditorWidget*>(root->song->tracker_windows->os_visual.widget)->colors[colnum])
46 #else
47 : col(g_colors[colnum])
48 #endif
49 {}
50
51 MyColor()
52 #ifdef COMPILING_RADIUM
53 : col(static_cast<EditorWidget*>(root->song->tracker_windows->os_visual.widget)->colors[11])
54 #else
55 : col(g_colors[11])
56 #endif
57 {}
58
59 MyColor lighter(int p){
60 return MyColor(col.light(80));
61 }
62
63 MyColor darker(int p){
64 return MyColor(col.light(80));
65 }
66
67 void setAlpha(int a){
68 col.setAlpha(a);
69 }
70 };
71
72 static inline MyColor mix_colors(const MyColor &c1, const MyColor &c2, float how_much){
73 return MyColor(mix_colors(c1.col,c2.col,how_much));
74 }
75
76 struct MyImage{
77 QImage image;
78 QPainter qp;
79 MyImage(int width,int height)
80 : image(width,height,QImage::Format_RGB32)
81 , qp(&image)
82 {
83 }
84 };
85
86 struct MyPainter{
87 QPainter *p;
88 MyColor col;
89 QPen pen;
90 QLinearGradient gradient;
91 bool use_gradient;
92 int _thickness;
93
94 MyPainter(QPainter *p)
95 : p(p)
96 , use_gradient(false)
97 , _thickness(1)
98 {
99 p->setPen(pen);
100 }
101
102 MyPainter(MyImage *image)
103 : p(&image->qp)
104 , use_gradient(false)
105 , _thickness(1)
106 {
107 p->setPen(pen);
108 }
109
110 void setGradient(int x1, int y1, int x2, int y2, const MyColor &c1, const MyColor &c2){
111 gradient = QLinearGradient(x1,y1,x2,y2);
112 gradient.setColorAt(0,c1.col);
113 gradient.setColorAt(1,c2.col);
114 p->setPen(QPen(QBrush(gradient),_thickness));
115 use_gradient = true;
116 }
117
118 void unsetGradient(){
119 use_gradient=false;
120 }
121
122 void drawImage(int x,int y,MyImage *image){
123 p->drawImage(x,y,image->image);
124 }
125
126 void drawRect(const int x1,const int y1,const int x2,const int y2, const MyColor &col){
127 if(use_gradient==false){
128 pen.setColor(col.col);
129 p->setPen(pen);
130 }
131 p->drawRect(x1,y1,x2-x1,y2-y1);
132 }
133 void drawRect(const MyRect &rect,const MyColor &col){
134 drawRect(rect.x1,rect.y1,rect.x2,rect.y2,col);
135 }
136
137
138 void fillRect(const int x1,const int y1,const int x2,const int y2, const MyColor &col){
139 if(use_gradient==false)
140 p->fillRect(x1,y1,x2-x1,y2-y1,col.col);
141 else
142 p->fillRect(x1,y1,x2-x1,y2-y1,QBrush(gradient));
143 }
144
145 void fillRect(const MyRect &rect,const MyColor &col){
146 fillRect(rect.x1,rect.y1,rect.x2,rect.y2,col);
147 }
148
149 void setThickness(int thickness){
150 _thickness=thickness;
151 pen.setWidth(thickness);
152 }
153
154 void drawLine(const int x1,const int y1,const int x2,const int y2, const MyColor &col){
155 if(use_gradient==false){
156 pen.setColor(col.col);
157 p->setPen(pen);
158 }
159
160 bool anti = x1!=x2 && y1!=y2;
161
162 if(anti)
163 p->setRenderHints(QPainter::Antialiasing,true);
164
165 p->drawLine(x1,y1,x2,y2);
166
167 if(anti)
168 p->setRenderHints(QPainter::Antialiasing,false);
169 }
170
171 void drawLine(const MyLine &l, const MyColor &col){
172 drawLine(l.x1,l.y1,l.x2,l.y2, col);
173 }
174
175 int getTextWidth(std::string text){
176 const QFontMetrics fn = QFontMetrics(QFont());
177 return fn.width(QString::fromStdString(text));
178 }
179
180 void drawText(const MyRect &rect, std::string text, const MyColor &col){
181 if(use_gradient==false){
182 pen.setColor(col.col);
183 p->setPen(pen);
184 }
185
186 p->drawText(rect.x1,rect.y1,rect.width(),rect.height(),
187 Qt::AlignTop|Qt::AlignLeft,
188 QString::fromStdString(text));
189 }
190
191 void drawVerticalText(int x, int y, std::string text, const MyColor &col){
192 if(use_gradient==false){
193 pen.setColor(col.col);
194 p->setPen(pen);
195 }
196 p->save();
197 QPoint point(x,y);
198 p->translate(x,0);//point);//x, y);
199 p->rotate(90); // or 270
200 //p->scale((box.x2-box.x1)/12,1.0);
201 //point = p->xFormDev(point);
202 p->drawText(5,-5,QString::fromStdString(text));
203 p->restore();
204 }
205 };
206
207 struct MyWidget{
208 struct MyQWidget : public QWidget{
209
210 MyWidget *mywidget;
211 MyQWidget(QWidget *parent, MyWidget *mywidget) : QWidget(parent),mywidget(mywidget){
212 }
213
214 void mousePressEvent ( QMouseEvent * event ){
215 if(mywidget->mousePress(event->x(),event->y()))
216 event->accept();
217 }
218 void mouseMoveEvent ( QMouseEvent * event ){
219 if(mywidget->mouseMove(event->x(),event->y()))
220 event->accept();
221 }
222 void mouseReleaseEvent ( QMouseEvent * event ){
223 if(mywidget->mouseRelease(event->x(),event->y()))
224 event->accept();
225 }
226
227 void paintEvent ( QPaintEvent * ev ){
228 QPainter qp(this);
229 MyPainter p(&qp);
230 mywidget->repaint(&p);
231 }
232
233 virtual void resizeEvent ( QResizeEvent * event ){
234 mywidget->resized();
235 }
236 };
237
238 MyQWidget w;
239
240 MyWidget(void *parent)
241 : w(static_cast<QWidget*>(parent),this)
242 {}
243
244 void update(int x1,int y1,int x2,int y2){
245 w.update(x1,y1,x2-x1,y2-y1);
246 }
247 void update(const MyRect &r){
248 update(r.x1,r.y1,r.x2,r.y2);
249 }
250
251 void update(){
252 w.update();
253 }
254
255 int width(){return w.width();}
256 int height(){return w.height();}
257
258 bool isVisible(){return w.isVisible();}
259 bool isEnabled(){
260 //printf("w is %s\n",w.isEnabled()?"enabled":"disabled");
261 return w.isEnabled();
262 }
263
264 virtual void resized() = 0;
265
266 //virtual void repaint(MyPainter *p){}
267 virtual void repaint(MyPainter *p) = 0;
268
269 virtual bool mousePress(int x, int y) = 0;
270
271 virtual bool mouseMove(int x, int y) = 0;
272
273 virtual bool mouseRelease(int x, int y) = 0;
274 };
275
276 struct MyTimer{
277
278 struct MyQTimer : public QTimer{
279 MyTimer *mytimer;
280
281 MyQTimer(MyTimer *mytimer) : mytimer(mytimer) {}
282
283 void timerEvent(QTimerEvent * e){ // virtual method from QTimer
284 mytimer->timer();
285 }
286 };
287
288 MyQTimer qtimer;
289
290 MyTimer() : qtimer(this) {}
291
292 virtual void timer() = 0;
293
294 void startTimer(int interval_in_milliseconds){
295 qtimer.setInterval(interval_in_milliseconds);
296 qtimer.start();
297 }
298 };
299
300 } // namespace cvs
301
302
303
304 #include "../audio/system_compressor_wrapper_proc.h"
305
306
307 #ifdef COMPILING_RADIUM
308
309 // I'm pretty sure it's not necessary to test for plugin!=NULL in these three functons.
310 // Normally, I'm 100% sure it's safe, but these functions can also be called from a timer.
311 //
312 static void set_compressor_parameter(struct Patch *patch, int num,float value){
313 SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
314 if(plugin!=NULL)
315 PLUGIN_set_effect_value(plugin, 0, plugin->type->num_effects+EFFNUM_COMP_RATIO+num, value, PLUGIN_NONSTORED_TYPE, PLUGIN_STORE_VALUE);
316 }
317
318 #else // COMPILING_RADIUM
319
320 static void set_compressor_parameter(struct Patch *patch, int num,float value){
321 SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
322 if(plugin!=NULL)
323 COMPRESSOR_set_parameter(plugin->compressor,num,value);
324 }
325
326 #endif // #else COMPILING_RADIUM
327
328 static float get_compressor_parameter(struct Patch *patch, int num){
329 SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
330 if(plugin!=NULL)
331 return COMPRESSOR_get_parameter(plugin->compressor,num);
332 else
333 return 0.0f;
334 }
335
336 static float get_graph_value(struct Patch *patch, int num){
337 SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
338 if(plugin!=NULL)
339 return COMPRESSOR_get_graph_value(plugin->compressor,num);
340 else
341 return 0.0;
342 }
343
344
345 //#ifdef COMPILING_RADIUM
346 //namespace radium_comp{ // got an error trying to use anonymous namespace here.
347 //#endif
348
349
350 #include "compressor_vertical_sliders.cpp"
351
352
353 static double OS_get_double_from_string(const char *s){
354 QLocale::setDefault(QLocale::C);
355 QString string(s);
356 return string.toDouble();
357 }
358
359 static float read_float(FILE *file){
360 char temp[512] = {0};
361
362 fgets(temp,500,file);
363
364 return OS_get_double_from_string(temp);
365 }
366
367
368 class Compressor_widget : public QWidget, public Ui::Compressor_widget{
369 Q_OBJECT
370
371 public:
372 bool initing;
373 cvs::Comp *comp;
374 struct Patch *_patch;
375
376 Compressor_widget(struct Patch *patch, QWidget *parent=NULL)
377 : QWidget(parent)
378 , _patch(patch)
379 {
380 initing = true;
381 setupUi(this);
382
383 QWidget *parent_for_comp = this;
384 comp = new cvs::Comp(patch,parent_for_comp);
385
386 #ifdef COMPILING_RADIUM
387 comp->w.setEnabled(false);
388 #else
389 attack_slider->setEnabled(true);
390 release_slider->setEnabled(true);
391 enable_checkbox->setVisible(false);
392 #endif
393 verticalLayout->insertWidget(1,&comp->w);
394
395 #ifdef COMPILING_RADIUM
396 // Enable MyQSlider and MyQCheckBox to take care of undo/redo.
397 {
398 SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
399 attack_slider->_patch = patch;
400 attack_slider->_effect_num = plugin->type->num_effects+EFFNUM_COMP_ATTACK;
401 release_slider->_patch = patch;
402 release_slider->_effect_num = plugin->type->num_effects+EFFNUM_COMP_RELEASE;
403 enable_checkbox->_patch = patch;
404 enable_checkbox->_effect_num = plugin->type->num_effects+EFFNUM_COMP_ONOFF;
405 }
406 #endif
407
408 update_gui();
409 initing = false;
410 }
411
412 float get_exp_value(double val, double max_val, double y1, double y2){
413 return scale_double(exp(val/max_val),
414 exp(0.0),expf(1.0),
415 y1,y2
416 );
417 }
418
419 float get_exp_inverted_value(double y, double max_val, double y1, double y2){
420 return max_val * log(scale_double(y,y1,y2,exp(0.0),exp(1.0)));
421 }
422
423 void update_gui(){
424 comp->set_gui_parameters();
425 attack_slider->setValue(get_exp_inverted_value(get_compressor_parameter(_patch, COMP_EFF_ATTACK),1000,0,max_attack_release));
426 release_slider->setValue(get_exp_inverted_value(get_compressor_parameter(_patch, COMP_EFF_RELEASE),1000,0,max_attack_release));
427
428 SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
429 enable_checkbox->setChecked(plugin->comp.is_on);
430
431 //paint_all=true;
432 //updateBackgroundImage();
433 //comp->update();
434 update();
435 }
436
437 void load(QString filename, bool complain_if_file_not_found = true){
438 FILE *file = fopen(filename.toUtf8().constData(),"r");
439
440 if(file==NULL){
441 if(complain_if_file_not_found){
442 QMessageBox msgBox;
443 msgBox.setText("Could not open file \""+filename+"\".");
444 msgBox.setStandardButtons(QMessageBox::Ok);
445 msgBox.setDefaultButton(QMessageBox::Ok);
446 msgBox.exec();
447 }
448 return;
449 }
450
451 set_compressor_parameter(_patch, COMP_EFF_RATIO,read_float(file)); // ratio
452 set_compressor_parameter(_patch, COMP_EFF_THRESHOLD,read_float(file)); // threshold
453 set_compressor_parameter(_patch, COMP_EFF_ATTACK,read_float(file)); // attack
454 set_compressor_parameter(_patch, COMP_EFF_RELEASE,read_float(file)); // release
455 //set_compressor_parameter(INPUT_VOLUME,read_float(file)); // input volume (currently not used)
456 read_float(file); // input volume (currently not used)
457 set_compressor_parameter(_patch, COMP_EFF_OUTPUT_VOLUME,read_float(file)); // output volume
458
459 int x = read_float(file);
460 int y = read_float(file);
461 int width = read_float(file);
462 int height = read_float(file);
463
464 if(width>0 && height>0){
465 move(x,y);
466 resize(width,height);
467 }
468
469 fclose(file);
470
471 comp->background_image_must_be_updated=true;
472
473 update_gui();
474 }
475
476 void save(QString filename){
477 FILE *file = fopen(filename.toUtf8().constData(),"w");
478
479 if(file==NULL){
480 QMessageBox msgBox;
481 msgBox.setText("Could not save file.");
482 msgBox.setStandardButtons(QMessageBox::Ok);
483 msgBox.setDefaultButton(QMessageBox::Ok);
484 msgBox.exec();
485 return;
486 }
487
488 fprintf(file,"%f\n%f\n%f\n%f\n%f\n%f\n",
489 get_compressor_parameter(_patch, COMP_EFF_RATIO),
490 get_compressor_parameter(_patch, COMP_EFF_THRESHOLD),
491 get_compressor_parameter(_patch, COMP_EFF_ATTACK),
492 get_compressor_parameter(_patch, COMP_EFF_RELEASE),
493 //get_compressor_parameter(INPUT_VOLUME),
494 0.0f, // input volume (in dB)
495 get_compressor_parameter(_patch, COMP_EFF_OUTPUT_VOLUME)
496 );
497
498 fprintf(file,"%d\n%d\n%d\n%d\n",
499 pos().x(),pos().y(),
500 width(),height()
501 );
502
503 fclose(file);
504 }
505
506 public slots:
507
508 void on_enable_checkbox_toggled(bool val){
509 #ifdef COMPILING_RADIUM
510 SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
511 PLUGIN_set_effect_value(plugin, 0, plugin->type->num_effects+EFFNUM_COMP_ONOFF, val==true?1.0:0.0, PLUGIN_NONSTORED_TYPE, PLUGIN_STORE_VALUE);
512
513 attack_slider->setEnabled(val);
514 release_slider->setEnabled(val);
515 comp->background_image_must_be_updated=true;
516 comp->w.setEnabled(val);
517 #endif
518 }
519
520 void on_attack_slider_valueChanged(int val){
521 float attack = get_exp_value(val,1000,0,max_attack_release);
522 set_compressor_parameter(_patch, COMP_EFF_ATTACK,attack);
523 char temp[512];
524 sprintf(temp,"Attack: %.2fms",attack);
525 SLIDERPAINTER_set_string(attack_slider->_painter, temp);
526 }
527
528 void on_release_slider_valueChanged(int val){
529 float release = get_exp_value(val,1000,0,max_attack_release);
530 set_compressor_parameter(_patch, COMP_EFF_RELEASE,release);
531 char temp[512];
532 sprintf(temp,"Release: %.2fms",release);
533 SLIDERPAINTER_set_string(release_slider->_painter, temp);
534 }
535
536 void on_bypass_toggled(bool val){
537 printf("bypass: %d\n",(int)val);
538 set_compressor_parameter(_patch, COMP_EFF_BYPASS,val==true?1.0f:0.0f);
539 }
540
541 void on_load_button_pressed(void){
542 printf("load pressed\n");
543
544 QString filename = QFileDialog::getOpenFileName(this, "Load Effect configuration", "", "Radium Compressor Configuration (*.rcc)");
545
546 if(filename=="")
547 return;
548
549 load(filename);
550 }
551
552 void on_save_button_pressed(void){
553 printf("save pressed\n");
554
555 QString filename = QFileDialog::getSaveFileName(this, "Save Effect configuration", "", "Radium Compressor Configuration (*.rcc)");
556
557 if(filename=="")
558 return;
559
560 save(filename);
561 }
562 };
563
564 //#ifdef COMPILING_RADIUM
565 //} // radium_comp namespace
566 //#endif
567