Debian Codegraph @

Qt/compressor_vertical_sliders.cpp

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 #ifdef INCLUDE_MYLINE_AND_MYRECT
18
19 namespace cvs{
20
21 struct MyLine{
22 int x1,y1,x2,y2;
23 MyLine(int x1,int y1, int x2, int y2)
24 : x1(x1),y1(y1),x2(x2),y2(y2)
25 {}
26 };
27
28 struct MyRect{
29 int x1,y1,x2,y2;
30 MyRect(int x1,int y1, int x2, int y2)
31 : x1(x1),y1(y1),x2(x2),y2(y2)
32 {}
33 int width() const {return x2-x1;}
34 int height() const {return y2-y1;}
35 };
36 }
37
38
39
40 #else // INCLUDE_MYLINE_AND_MYRECT
41
42
43
44 #include <string>
45
46 #define GLIDING_PEAK_AREA 0 // Set to 1 to get gliding peaks area, which most meters use. Looks good, but I think it is slightly more confusing than the array used now.
47
48 const int min_db = -40;
49 const int max_db = 40;
50
51 const int k_timer_interval = 25;
52
53 const int max_attack_release = 500;
54
55 const float def_threshold = 0.7f;
56 const float def_ratio = 0.3;
57 const float def_makeupgain = 0.3f;
58
59 namespace cvs{
60
61 struct Comp : public MyWidget, public MyTimer{
62 struct Patch *_patch;
63
64 // All values have the value 0 at top, and 1 at bottom, and:
65 // 0 <= val1 <= val2 <= 1
66 // (sometimes < instead of <=)
67
68 // slider 1
69 double in_val1; // 0
70 double in_val2; // threshold
71
72 // slider 2
73 double out_val1; // used to calculate ratio ( ratio = in_val2/(out_val1-in_val2) )
74 double out_val2; // threshold
75
76 // slider 3
77 double vol_val1; // used to calculate volume ( volume = out_val1/vol_val1 )
78 double vol_val2; // threshold * volume
79
80 // attack/release sliders
81 double attack_val;
82 double release_val;
83
84 int curr_slider;
85
86 struct Box{
87 int x1,y1,x2,y2;
88 int p1,p2;
89
90 Box(int _x1, int _y1, int _x2, int _y2, float val1,float val2)
91 : x1(_x1)
92 , y1(_y1)
93 , x2(_x2)
94 , y2(_y2)
95 {
96 p1 = scale(val1,0,1,y1,y2);
97 p2 = scale(val2,0,1,y1,y2);
98 }
99
100 bool inside(int x,int y){
101 return x>=x1 && x<x2 && y>y1 && y<y2;
102 }
103 bool in_val1(int y){
104 int p=(p1+p2)/2;
105 return y<p;
106 }
107 bool in_val2(int y){
108 int p=(p1+p2)/2;
109 return y>=p;
110 }
111 };
112
113 struct Slider : public Box{
114 Slider(int _x1, int _y1, int _x2, int _y2, float val1,float val2)
115 : Box(_x1,_y1,_x2,_y2,val1,val2)
116 {
117 p1 = scale(val1,0,1,x1,x2);
118 }
119 };
120
121 struct Peaks{
122 const static int num_peaks = 30;//24;
123
124 float peaks[num_peaks]; // in dB
125 int peak_pos;
126 float last_peak;
127
128 #if GLIDING_PEAK_AREA
129 float min_peak_db, max_peak_db; // in dB
130 #endif
131 int min_peak, max_peak; // in pixels
132
133 Peaks()
134 : peak_pos(0)
135 , last_peak(0)
136 #if GLIDING_PEAK_AREA
137 , min_peak_db(0), max_peak_db(0)
138 #endif
139 , min_peak(0)
140 , max_peak(0)
141 {
142 for(int i=0;i<num_peaks;i++)
143 peaks[i]=0.0f;
144 }
145
146 float get_min_peak(){
147 float ret=max_db;
148 for(int i=0;i<num_peaks;i++)
149 if(peaks[i]<ret)
150 ret=peaks[i];
151 return ret;
152 }
153
154 float get_max_peak(){
155 float ret=min_db;
156 for(int i=0;i<num_peaks;i++)
157 if(peaks[i]>ret)
158 ret=peaks[i];
159 return ret;
160 }
161
162
163 #if GLIDING_PEAK_AREA
164 // Trying gliding area instead of array. IMO it is less clear, and it also uses more CPU because of more gfx updates.
165 void update_peaks(const Box &box, float peak){
166 const float inc = 0.1;
167
168 if(peak>max_peak_db)
169 max_peak_db = peak;
170 else
171 max_peak_db = max_peak_db - inc;
172
173 if(peak<min_peak_db)
174 min_peak_db = peak;
175 else
176 min_peak_db = min_peak_db + inc;
177
178 if(min_peak_db < min_db)
179 min_peak_db = min_db;
180
181 if(max_peak_db > max_db)
182 max_peak_db = max_db;
183
184 if(min_peak_db>max_peak_db)
185 min_peak_db=max_peak_db;
186
187 //if(box.x1<50)
188 // printf("min_peak_db: %f\n",min_peak_db);
189
190 min_peak = scale(min_peak_db,min_db,max_db, box.y2, box.y1);
191 max_peak = scale(max_peak_db,min_db,max_db, box.y2, box.y1);
192 }
193
194 #else
195
196 void set_min_max(const Box &box){
197 min_peak = scale(get_min_peak(),min_db,max_db, box.y2, box.y1);
198 max_peak = scale(get_max_peak(),min_db,max_db, box.y2, box.y1);
199 }
200
201 #endif
202
203 const int get_y_pixel(const Box &box){
204 return scale(last_peak,min_db,max_db, box.y2, box.y1);
205 }
206
207 const MyLine get_line_line(const Box &box){
208 const int peak = get_y_pixel(box);
209 return MyLine(box.x1,peak,box.x2,peak);
210 }
211
212 const MyLine get_line_to_next_box(const Box &box1, Peaks *peaks2, const Box &box2){
213 Peaks *peaks1 = this;
214 int x1=box1.x2;
215 int y1=peaks1->get_y_pixel(box1);
216 int x2=box2.x1;
217 int y2=peaks2->get_y_pixel(box2);
218
219 if(y1 < box1.y1){
220 x1=scale(box1.y1, y1,y2, x1,x2);
221 y1=box1.y1 - 10; // FIX. why is - 10 needed?
222 }
223
224 if(y2 < box2.y1){
225 x2=scale(box2.y1, y1,y2, x1,x2);
226 y2=box2.y1 - 10;
227 }
228
229 if(y1 > box1.y2){
230 x1=scale(box1.y2, y1,y2, x1,x2);
231 y1=box1.y2 + 10;
232 }
233
234 if(y2 > box2.y2){
235 x2=scale(box2.y2, y1,y2, x1,x2);
236 y2=box2.y2 + 10;
237 }
238
239 if(x1<box1.x2)
240 x1=10000;
241 if(x1>box2.x1)
242 x1=10000;
243 if(x2<box1.x2)
244 x1=10000;
245 if(x2>box2.x1)
246 x1=10000;
247
248
249 return MyLine(x1,y1,x2,y2);
250 }
251
252 // For lines in boxes (those lines are always horizontal)
253 const MyRect get_line_rect(const Box &box){
254 const int peak = get_y_pixel(box);
255 return MyRect(box.x1,peak-2,box.x2,peak+3);
256 }
257
258 void update_line_rect(MyWidget *widget, const Box &box){
259 widget->update(get_line_rect(box));
260 }
261
262 // For the lines between boxes.
263 void update_line_region(MyWidget *widget, const Box &box1, Peaks *peaks2, const Box &box2){
264 const MyLine line = get_line_to_next_box(box1,peaks2,box2);
265
266 int x1=line.x1;
267 int y1=line.y1;
268 int x2=line.x2;
269 int y2=line.y2;
270
271 if(x1==10000)
272 return; // Line is not painted.
273
274
275 int h_square = y2 - y1;
276 h_square *= h_square;
277
278 int w_square = x2 - x1;
279 w_square *= w_square;
280
281 int h = ceil(4.0/2.0 * sqrt((double)h_square/(double)w_square + 1.0)); // "4.0" is the pen width.
282
283 int y1_1 = y1-h;
284 int y1_2 = y1+h;
285 int y2_1 = y2-h;
286 int y2_2 = y2+h;
287
288 const int dx = 8;
289
290 if(y1_1 < y2_1)
291 for(int x=x1;x<x2;x+=dx){
292 int y1=scale(x,x1,x2,y1_1,y2_1);
293 int y2=scale(x+dx,x1,x2,y1_2,y2_2)+1;
294 widget->update(x,y1,x+dx,y2);
295 }
296 else
297 for(int x=x1;x<x2;x+=dx){
298 int y1=scale(x+dx,x1,x2,y1_1,y2_1);
299 int y2=scale(x,x1,x2,y1_2,y2_2)+1;
300 widget->update(x,y1,x+dx,y2);
301 }
302 }
303
304 const MyRect get_area_rect(const Box &box){
305 return MyRect(box.x1,max_peak,
306 box.x2, min_peak);
307 }
308
309 void paint_peak_area(MyPainter *p, const Box &box){
310 MyColor col(40,90,140);
311 col = col.lighter(80);
312 col.setAlpha(120);
313
314 p->fillRect(get_area_rect(box), col);
315 }
316
317 void paint_peak_line(MyPainter *p, const Box &box, const MyColor &col){
318 p->drawLine(get_line_line(box),col);
319 }
320
321 void paint_peak_line_between_boxes(MyPainter *p, const Box &box1, Peaks *peaks2, const Box &box2, const MyColor &col){
322 const MyLine line = get_line_to_next_box(box1, peaks2, box2);
323 if(line.x1==10000)
324 return;
325
326 p->drawLine(line,col);
327 }
328
329 // Only works if rect1.x1==rect2.x1 && rect2.x2==rect2.x2
330 void update_xored_rects(MyWidget *widget, const MyRect &rect1, const MyRect &rect2){
331 if(rect1.y1==rect2.y1 && rect1.y2==rect2.y2)
332 return;
333
334 MyRect r1=rect1;
335 MyRect r2=rect2;
336 int x1=rect1.x1;
337 int x2=rect1.x2;
338
339 if(r1.y1 > r2.y1){
340 MyRect temp=r1;
341 r1=r2;
342 r2=temp;
343 }
344
345 #if 0 // for testing
346 widget->update(x1,r1.y1,x2,r1.y2);
347 widget->update(x1,r2.y1,x2,r2.y2);
348 return;
349 #endif
350
351 if(r1.y1==r2.y1 && r2.y2<r1.y2){
352 widget->update(x1, r2.y2, x2, r1.y2);
353 return;
354 }
355
356 if(r1.y1==r2.y1){
357 widget->update(x1, r1.y2, x2, r2.y2);
358 return;
359 }
360
361 if(r1.y2==r2.y2){
362 widget->update(x1, r1.y1, x2, r2.y1);
363 return;
364 }
365
366 if(r2.y2 < r1.y2){
367 widget->update(x1, r1.y1, x2, r2.y1);
368 widget->update(x1, r2.y2, x2, r1.y2);
369 return;
370 }
371
372 if(r2.y1 < r1.y2){
373 widget->update(x1, r1.y1, x2, r2.y1);
374 widget->update(x1, r1.y2, x2, r2.y2);
375 }
376
377 // i.e. they are non-overlapping
378 widget->update(x1, r1.y1, x2, r1.y2);
379 widget->update(x1, r2.y1, x2, r2.y2);
380 }
381
382 void add_peaks_and_update_area(MyWidget *widget, const Box &box, float peak){
383
384 MyRect rect_before = get_area_rect(box);
385
386 if(peak<-1000)
387 peak=-1000;
388 if(peak>1000)
389 peak=1000;
390
391 last_peak = peak;
392 //printf("adding peak %.2f. ",peak);
393 peaks[peak_pos] = peak;
394 peak_pos++;
395 if(peak_pos==num_peaks)
396 peak_pos=0;
397
398 #if GLIDING_PEAK_AREA
399 update_peaks(box,peak);
400 #else
401 set_min_max(box);
402 #endif
403
404 MyRect rect_after = get_area_rect(box);
405
406 update_xored_rects(widget, rect_before, rect_after);
407 }
408 };
409
410 Peaks peaks_in;
411 Peaks peaks_out;
412 Peaks peaks_vol;
413
414 float in_peak_value;
415 float out_peak_value;
416 float vol_peak_value;
417
418 bool background_image_must_be_updated;
419
420 MyImage *background_image;
421
422 void timer(){ // virtual method from MyTimer
423 if(isVisible())
424 update_peaks();
425 }
426
427 ~Comp(){
428 delete background_image;
429 }
430
431 Comp(struct Patch *patch, void *parent)
432 : MyWidget(parent)
433 , _patch(patch)
434 , in_val1(0.0)
435 , in_val2(def_threshold)
436 , out_val1(def_ratio)
437 , out_val2(def_threshold)
438 , vol_val1(def_makeupgain)
439 , vol_val2(def_threshold)
440 , curr_slider(0)
441 , background_image_must_be_updated(true)
442 , background_image(NULL)
443 {
444 createBackgroundImage();
445
446 startTimer(k_timer_interval); // MyTimer
447
448 set_threshold(def_threshold);
449 set_compressor_parameters();
450
451 update_peaks();
452 }
453
454 void createBackgroundImage(){
455 delete background_image;
456 background_image = new MyImage(width(),height());
457 updateBackgroundImage();
458 }
459
460 void resized(){
461 createBackgroundImage();
462 }
463
464 int get_box_width(){
465 return (width()-1) / 4;
466 }
467
468 int get_slider_height(){
469 return 20;
470 }
471
472 int get_box_height(){
473 //return height()-((get_slider_height()+1)*2);
474 return height()-1;
475 }
476
477 int get_width_between_boxes(){
478 return get_box_width() / 2;
479 }
480
481 Box get_slider1_parms(){
482 return Box(
483 0,0,
484 get_box_width(),
485 get_box_height(),
486 in_val1,in_val2);
487 }
488
489 Box get_slider2_parms(){
490 return Box(
491 get_box_width() + get_width_between_boxes(), 0,
492 get_box_width() + get_width_between_boxes() + get_box_width(),
493 get_box_height(),
494 out_val1,out_val2);
495 }
496
497 Box get_slider3_parms(){
498 return Box(
499 get_box_width() + get_width_between_boxes() + get_box_width() + get_width_between_boxes(), 0,
500 get_box_width() + get_width_between_boxes() + get_box_width() + get_width_between_boxes() + get_box_width(),
501 get_box_height(),
502 vol_val1, vol_val2);
503 }
504
505 #if 0
506 Slider get_attack_slider_parms(){
507 return Box(
508 0,get_box_height() + 1,
509 width(), get_box_height() + get_slider_height()
510 attack_val,0.0f);
511 }
512
513 Slider get_release_slider_parms(){
514 return Box(
515 0,get_box_height() + 1,
516 width(), get_box_height() + get_slider_height()
517 release_val,0.0f);
518 }
519 #endif
520
521 double get_threshold(){
522 return scale(in_val2,
523 in_val1,1,
524 max_db,min_db);
525 }
526
527 void set_threshold_gui(){
528 set_threshold(scale(get_compressor_parameter(_patch, COMP_EFF_THRESHOLD),max_db,min_db,in_val1,1));
529 }
530
531 double get_ratio(){
532 double len_in = in_val2-in_val1;
533 double len_out = out_val2-out_val1;
534
535 if(len_in==0.0 && len_out==0.0)
536 return 1.0;
537
538 if(len_out==0.0)
539 return 60;
540
541 double ratio = fabs(len_in-len_out)<0.00001
542 ? 1.0
543 : len_in / len_out;
544
545 if(ratio>60)
546 ratio=60;
547 return ratio;
548 }
549
550 void set_ratio_gui(){
551 double ratio = get_compressor_parameter(_patch, COMP_EFF_RATIO);
552 if(ratio<1.0){
553 set_ratio(scale(ratio,1,0,0,-1));
554 }else
555 set_ratio(out_val2 - out_val2/ratio);
556 }
557
558 double get_makeup_gain(){
559 return scale(vol_val1,0,1,max_db,min_db) - scale(out_val1,0,1,max_db,min_db);
560 }
561
562 // set_ratio must be called first.
563 void set_makeup_gain_gui(){
564 float vol = get_compressor_parameter(_patch, COMP_EFF_OUTPUT_VOLUME);
565 float addvol = scale(out_val1,0,1,max_db,min_db) + vol;
566 set_makeupgain(scale(addvol,max_db,min_db,0,1));
567 }
568
569 // After loading.
570 void set_gui_parameters(){
571 set_threshold_gui();
572 set_ratio_gui();
573 set_makeup_gain_gui();
574 }
575
576 void set_compressor_parameters(){
577 //double input_volume = 1.0;
578 double threshold = get_threshold();
579 double ratio = get_ratio();
580 double output_volume = get_makeup_gain();
581
582 set_compressor_parameter(_patch, COMP_EFF_RATIO,ratio);
583 set_compressor_parameter(_patch, COMP_EFF_THRESHOLD, threshold);
584 //set_compressor_parameter(2,attack);
585 //set_compressor_parameter(3,release);
586 //set_compressor_parameter(4,input_volume);
587 set_compressor_parameter(_patch, COMP_EFF_OUTPUT_VOLUME,output_volume);
588
589 #if 0
590 printf("%f / %f\n%f / %f\n%f / %f\ninput_volume:\t\t %.2f\n"
591 "threshold:\t\t %f.2f\n"
592 "ratio:\t\t\t %f.2f\n"
593 "output_volume:\t\t %.2f\n\n",in_val2,out_val2,scale(vol_val1,0,1,max_db,min_db),scale(out_val1,0,1,max_db,min_db),vol_val1,out_val1,
594 input_volume,
595 threshold,
596 ratio,
597 output_volume);
598 #endif
599 }
600
601 enum{
602 THRESHOLD_SLIDER = 1,
603 RATIO_SLIDER,
604 MAKEUPGAIN_SLIDER
605 };
606
607 double p_startpos;
608 int y_startpos;
609
610 void set_makeupgain(float val){
611 //double old = vol_val2;
612 vol_val1=val;
613 #if 0 // allow this.
614 if(vol_val1<0.0)
615 vol_val1=0.0;
616 #endif
617 if(vol_val1>0.9999)
618 vol_val1=0.9999;
619 vol_val2 = vol_val1 + (out_val2-out_val1);// scale(out_val2, out_val1, 1.0, vol_val1, 1.0);
620 //printf("setting makeupgain to %f. old/new vol_val2: %f / %f\n",val,vol_val2,old);
621 }
622
623 void set_ratio(float val){
624 double vol_diff = vol_val1 - out_val1;
625 out_val1=val;
626 // if(out_val1<0.0)
627 // out_val1=0.0;
628
629 if(out_val1<0){
630 double threshold=get_compressor_parameter(_patch, COMP_EFF_THRESHOLD); //get_threshold();
631 double threshold_scaled=scale(threshold,max_db,min_db,0,1);
632 double ratio = scale(val,0,-1,1,0); //1.0 + val; // val is the out slider value, which goes from 0 to -1 when we are in expanding mode.
633 if(ratio<0.001)
634 ratio=0.001;
635 in_val1 = ((ratio-1)*threshold_scaled) / (ratio*threshold_scaled - ratio - threshold_scaled); // From: i1 = i2*(1-ratio), i2=scale(threshold_scaled,0,1,i1,1), i1=in_va1, i2=in_val2
636 out_val1=0.0;
637 if(in_val1>0.999)
638 in_val1=0.999;
639 in_val2=scale(threshold,max_db,min_db,in_val1,1);
640 out_val2=in_val2;
641 }else{
642 in_val1=0.0;
643 }
644 if(out_val1>in_val2-0.0001)
645 out_val1=in_val2-0.0001;
646
647 set_makeupgain(vol_diff + out_val1);
648 }
649
650 void set_threshold(float val){
651 //double bef=in_val2;
652 double old_inval1_scaled = scale(in_val1,0,in_val2,0,1);
653 double old_ratio_factor = out_val1 / (in_val2-in_val1);
654
655 in_val2 = val;
656 if(in_val2<=0.0)
657 in_val2=0.00001;
658 if(in_val2>0.9999)
659 in_val2=0.9999;
660
661 out_val2 = in_val2;
662
663 if(in_val1<=0.0){
664 set_ratio(old_ratio_factor*in_val2);
665 }else{
666 in_val1 = scale(old_inval1_scaled,0,1,0,in_val2);
667 set_makeupgain(vol_val1);
668 //printf("b %.2f - %.2f. Bef: %.2f, now: %.2f\n",in_val1,old_inval1_scaled,bef,in_val2);
669 }
670 }
671
672 void handle_mouse_event (int x, int y){
673 //printf("Got mouse press event %d / %d\n",(int)event->x(),(int)event->y());
674
675 double new_val = p_startpos + scale(y-y_startpos,0,height(),0,1);
676 //printf("p_startpos: %f\n",p_startpos);
677
678 switch(curr_slider){
679 case THRESHOLD_SLIDER:
680 set_threshold(new_val);
681 break;
682 case RATIO_SLIDER:
683 set_ratio(new_val);
684 break;
685 case MAKEUPGAIN_SLIDER:
686 set_makeupgain(new_val);
687 break;
688 }
689
690 set_compressor_parameters();
691
692 background_image_must_be_updated=true;
693 update();
694 }
695
696 bool mousePress(int x, int y){
697 Box in_box = get_slider1_parms();
698 Box out_box = get_slider2_parms();
699 Box vol_box = get_slider3_parms();
700
701 if(in_box.inside(x,y)){
702 #ifdef COMPILING_RADIUM
703 SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
704 Undo_AudioEffect_CurrPos(_patch, plugin->type->num_effects+EFFNUM_COMP_THRESHOLD);
705 #endif
706
707 curr_slider = THRESHOLD_SLIDER;
708 p_startpos = in_box.p2;
709
710 }else if(out_box.inside(x,y)){
711 #ifdef COMPILING_RADIUM
712 SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
713 Undo_AudioEffect_CurrPos(_patch, plugin->type->num_effects+EFFNUM_COMP_RATIO);
714 #endif
715
716 curr_slider = RATIO_SLIDER;
717 if(in_box.p1>0){
718 p_startpos = scale(in_box.p1,0,in_box.p2,0,-height());
719 }else{
720 p_startpos = out_box.p1;
721 }
722
723 }else if(vol_box.inside(x,y)){
724 #ifdef COMPILING_RADIUM
725 SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
726 Undo_AudioEffect_CurrPos(_patch, plugin->type->num_effects+EFFNUM_COMP_OUTPUT_VOLUME);
727 #endif
728
729 curr_slider = MAKEUPGAIN_SLIDER;
730 p_startpos = vol_box.p1;
731
732 } else {
733 return false;
734 }
735
736 p_startpos = scale(p_startpos,0,height(),0,1);
737 y_startpos = y;
738
739 return true;
740 }
741
742 bool mouseMove (int x, int y)
743 {
744 handle_mouse_event(x,y);
745 return true;
746 }
747
748 bool mouseRelease (int x, int y)
749 {
750 handle_mouse_event(x,y);
751 curr_slider = 0;
752 return true;
753 }
754
755 void paint_box(MyPainter *p, const Box &b, const MyColor &col){
756 p->drawRect(b.x1, b.y1,
757 b.x2, b.y2,
758 col);
759 p->drawLine(b.x1,b.p1,
760 b.x2,b.p1,
761 col);
762 p->drawLine(b.x1,b.p2,
763 b.x2,b.p2,
764 col);
765 }
766
767 #if 0
768 void paint_slider(MyPainter *p, const Slider &s, const MyColor &col){
769 p->drawRect(s.x1, s.y1,
770 s.x2, s.y2);
771 p->drawLine(s.x1,s.p1,
772 s.x2,s.p1);
773 }
774 #endif
775
776 void paint_gradient_rectangle(MyPainter *p, const int x1, const int y1, const int x2, const int y2, const MyColor &c1, const MyColor &c2){
777 #if 0
778 for(int y=y1;y<y2;y++){
779 p->setPen(mix_colors(c1,c2,scale(y, y1, y2, 1.0f,0.0f)));
780 p->drawLine(x1,y,x2,y);
781 }
782 #else
783 p->setGradient(0,y1,0,y2,c1,c2);
784 p->fillRect(x1, y1, x2 ,y2, c1);
785 p->unsetGradient();
786 #endif
787 }
788
789 void paint_gradient_trapezoid(MyPainter *p, const int x1, const int y1_1, const int y1_2, const int x2, const int y2_1, const int y2_2, const int min_y, const int max_y, const MyColor &c1, const MyColor &c2){
790 #if 0
791 if(y1_2-y1_1 >= y2_2-y2_1)
792 for(int y1=y1_1;y1<y1_2;y1++){
793 int y2=scale(y1,y1_1,y1_2,y2_1,y2_2);
794 p->setPen(mix_colors(c1,c2,scale(y1, y1_1,y1_2, 1.0f,0.0f)));
795 p->drawLine(x1,y1,x2,y2);
796 }
797 else
798 for(int y2=y2_1;y2<y2_2;y2++){
799 int y1=scale(y2,y2_1,y2_2,y1_1,y1_2);
800 p->setPen(mix_colors(c1,c2,scale(y2, y2_1,y2_2, 1.0f,0.0f)));
801 p->drawLine(x1,y1,x2,y2);
802 }
803 #else
804 for(int x=x1;x<x2;x++){
805 int y1=scale(x,x1,x2,y1_1,y2_1);
806 int y2=scale(x,x1,x2,y1_2,y2_2);
807
808 p->setGradient(x,y1,x,y2,c1,c2);
809 p->drawLine(x,R_BOUNDARIES(min_y,y1,max_y),x,R_BOUNDARIES(min_y,y2,max_y),c1);
810 }
811 p->unsetGradient();
812 #endif
813 }
814
815 void paint_gradient(MyPainter *p, const Box &in, const Box &out, const Box &vol){
816 #if 0 // for testing if clipping is accurate
817 QColor col1(qrand()%255,qrand()%255,qrand()%255);
818 QColor col2(qrand()%255,qrand()%255,qrand()%255);
819 #else
820 MyColor col1(30,170,33);
821 MyColor col2(159,58,33);
822 #endif
823 col2=col2.lighter(80);
824
825 col1 = col1.lighter(80);
826 col2 = col2.lighter(60);
827
828 //col1.setAlpha(120);
829 //col2.setAlpha(180);
830
831 if(!isEnabled()){
832 //col1.setAlpha(40);
833 //col2.setAlpha(40);
834 col1 = mix_colors(col1, MyColor(), 0.4);
835 col2 = mix_colors(col2, MyColor(), 0.4);
836 }else{
837 col1 = mix_colors(col1, MyColor(), 0.8);
838 col2 = mix_colors(col2, MyColor(), 0.8);
839 }
840
841 // in slider
842 paint_gradient_rectangle(p, in.x1,in.p1, in.x2,in.y2, col1, col2);
843
844 float in_val = scale(in_val2,in_val1,1.0f,0.0f,1.0f);
845 MyColor col = mix_colors(col1,col2,1.0f-in_val);
846
847 // out slider top
848 paint_gradient_rectangle(p, out.x1,out.p1, out.x2,out.p2, col1,col);
849
850 // out slider bot.
851 paint_gradient_rectangle(p, out.x1,out.p2, out.x2,out.y2, col, col2);
852
853 // area between in slider and out slider, below threshold
854 paint_gradient_rectangle(p, in.x2,in.p2, out.x1,out.y2, col, col2);
855
856 // area between in slider and out slider, above threshold
857 paint_gradient_trapezoid(p, in.x2,in.p1,in.p2, out.x1,out.p1,out.p2, out.y1, out.y2, col1, col);
858
859 // volume slider
860 {
861 int p1 = vol.p1;
862 int p2 = vol.p2;//scale(out.p2, out.p1, out.y2, vol.p1, vol.y2);
863 int p3 = p2 + (out.y2-out.p2);
864
865 // top
866 paint_gradient_rectangle(p, vol.x1,p1, vol.x2,p2, col1, col);
867
868 // middle.
869 paint_gradient_rectangle(p, vol.x1,p2, vol.x2,p3, col, col2);
870
871 //QColor col3=col2.light(scale(p3,vol.y2,vol.y1,100,40));
872 MyColor col3=mix_colors(col2,MyColor(1,1,1),R_BOUNDARIES(0.0f, scale(p3,vol.y2,vol.y1,1.0f,0.0f), 1.0f));
873
874 // bot.
875 paint_gradient_rectangle(p, vol.x1,p3, vol.x2,vol.y2, col2, col3);
876
877 // area between out slider and vol slider, above threshold
878 paint_gradient_trapezoid(p,
879 out.x2, out.p1, out.p2,
880 vol.x1, vol.p1, vol.p2,
881 vol.y1, vol.y2,
882 col1,col);
883
884 // area between out slider and vol slider, below threshold, colors in the col-col2 range.
885 paint_gradient_trapezoid(p,
886 out.x2, out.p2,out.y2,
887 vol.x1, p2,p3,
888 vol.y1, vol.y2,
889 col,col2);
890
891 // area between out slider and vol slider, further below threshold, colors in the col2-col3 range.
892 if(p3 < vol.y2)
893 paint_gradient_trapezoid(p,
894 out.x2, out.y2, out.y2 + (vol.y2-p2),
895 vol.x1, p3, vol.y2,
896 vol.y1, vol.y2,
897 col2,col3);
898 }
899 }
900
901
902 // Called regularly
903 void update_peaks(){
904 Box in_box = get_slider1_parms();
905 Box out_box = get_slider2_parms();
906 Box vol_box = get_slider3_parms();
907
908 in_peak_value = get_graph_value(_patch, 0);
909 out_peak_value = in_peak_value + get_graph_value(_patch, 1);
910 vol_peak_value = out_peak_value + get_makeup_gain();
911
912 //printf("in_peak_value: %f. out: %f, vol: %f\n",in_peak_value,out_peak_value,vol_peak_value);
913
914 // update old line boxes
915 peaks_in.update_line_rect(this,in_box);
916 peaks_out.update_line_rect(this,out_box);
917 peaks_vol.update_line_rect(this,vol_box);
918
919 // update old lines between boxes
920 peaks_in.update_line_region(this,in_box,&peaks_out,out_box);
921 peaks_out.update_line_region(this,out_box,&peaks_vol,vol_box);
922
923 // Add new peaks, and update areas in boxes (both old and new)
924 peaks_in.add_peaks_and_update_area(this,in_box, scale(scale(in_peak_value,max_db,min_db,in_val1,1),0,1,max_db,min_db));
925 peaks_out.add_peaks_and_update_area(this,out_box,scale(scale(out_peak_value,max_db,min_db,in_val1,1),0,1,max_db,min_db));
926 peaks_vol.add_peaks_and_update_area(this,vol_box,scale(scale(vol_peak_value,max_db,min_db,in_val1,1),0,1,max_db,min_db));
927
928 // update new lines between boxes
929 peaks_in.update_line_region(this,in_box,&peaks_out,out_box);
930 peaks_out.update_line_region(this,out_box,&peaks_vol,vol_box);
931
932 // update new line boxes
933 peaks_in.update_line_rect(this,in_box);
934 peaks_out.update_line_rect(this,out_box);
935 peaks_vol.update_line_rect(this,vol_box);
936 }
937
938 void paintPeaks(MyPainter *p, const Box &in_box, const Box &out_box, const Box &vol_box){
939
940 // peak lines
941 {
942 MyColor col(0,90,180);
943 col = col.lighter(60);
944 col.setAlpha(130);
945
946 p->setThickness(4);
947
948 // paint lines
949 {
950 // in
951 peaks_in.paint_peak_line(p, in_box, col);
952
953 // between in and out
954 peaks_in.paint_peak_line_between_boxes(p, in_box, &peaks_out, out_box, col);
955
956 // out
957 peaks_out.paint_peak_line(p, out_box, col);
958
959 // between out and vol
960 peaks_out.paint_peak_line_between_boxes(p, out_box, &peaks_vol, vol_box, col);
961
962 // vol
963 peaks_vol.paint_peak_line(p, vol_box, col);
964 }
965
966 p->setThickness(1);
967 }
968
969 // peak areas
970 {
971 peaks_in.paint_peak_area(p,in_box);
972 peaks_out.paint_peak_area(p,out_box);
973 peaks_vol.paint_peak_area(p,vol_box);
974 }
975
976 #if 0 // for checking that get_line_region returns correct value.
977 p->setPen(QPen());
978 p->drawPolygon(peaks_in.get_line_region(in_box,&peaks_out,out_box));
979 p->drawPolygon(peaks_out.get_line_region(out_box,&peaks_vol,vol_box));
980 #endif
981 }
982
983 void paintSliderText(MyPainter *p, const Box &box, std::string text, double value, std::string unit){
984 MyColor c(1);
985 c.setAlpha(160);
986 if(!isEnabled())
987 c.setAlpha(80);
988
989 char temp[32];
990 sprintf(temp,"%.1f",value);
991 std::string value_text = temp + unit;
992 std::string one_line = text + " " + value_text;
993
994 MyRect rect(box.x1+2,box.y1+2,box.x2-4,box.y2-4);
995
996 if(p->getTextWidth(text) < rect.width() && p->getTextWidth(value_text) < rect.width()){
997 // horizontal
998
999 p->drawText(rect, p->getTextWidth(one_line)<rect.width()
1000 ? one_line
1001 : text+"\n"+value_text,
1002 c);
1003
1004 }else{
1005
1006 p->drawVerticalText(box.x1,box.y1,one_line,c);
1007
1008 }
1009 //p->drawText(box.x1,box.y2-20,QString::number(value));
1010 }
1011
1012 void paint_box_fill(MyPainter *p, const Box &box){
1013 MyColor col(150,190,10);
1014 col.setAlpha(10);
1015
1016 if(box.p1>box.y1)
1017 p->fillRect(box.x1, box.y1,
1018 box.x2, box.p1,
1019 col);
1020 if(box.p2<box.y2)
1021 p->fillRect(box.x1, box.y2,
1022 box.x2, box.p2,
1023 col);
1024 }
1025
1026 void paintBackgroundImage(MyPainter *p){
1027
1028 p->fillRect(0,0,width(),height(),MyColor());
1029
1030 //printf("Paint\n");
1031
1032 Box in_box = get_slider1_parms();
1033 Box out_box = get_slider2_parms();
1034 Box vol_box = get_slider3_parms();
1035
1036 paint_gradient(p,in_box,out_box,vol_box);
1037
1038 MyColor black(1,1,1);
1039 black.setAlpha(80);
1040
1041 paint_box(p, in_box, black);
1042 paint_box(p, out_box, black);
1043 paint_box(p, vol_box, black);
1044
1045 // bottom line between sliders
1046 if(1){
1047 MyColor c = mix_colors(MyColor(1,1,1), MyColor(), 0.4); // Alpha color will not look correct here since the color it's drawn over differs.
1048 p->drawLine(in_box.x2,in_box.y2,vol_box.x1,out_box.y2,c);
1049 p->drawLine(out_box.x2,out_box.y2,vol_box.x1,vol_box.y2,c);
1050 }
1051
1052 // threshold line between sliders
1053 if(1){
1054 // between in and out
1055 p->drawLine(in_box.x2,in_box.p2,
1056 out_box.x1,out_box.p2,
1057 black
1058 );
1059
1060 // between out and vol
1061 p->drawLine(out_box.x2,out_box.p2,
1062 vol_box.x1,vol_box.p2,
1063 black);
1064 }
1065
1066 paint_box_fill(p,in_box);
1067 paint_box_fill(p,out_box);
1068 paint_box_fill(p,vol_box);
1069
1070 // //
1071
1072 // border line between sliders
1073 if(1){
1074 black.setAlpha(80);
1075
1076 if(in_box.p1!=out_box.p1)
1077 p->setThickness(1);
1078
1079 // between in and out
1080 p->drawLine(in_box.x2,in_box.p1,
1081 out_box.x1,out_box.p1,
1082 black);
1083
1084 if(out_box.p1!=vol_box.p1)
1085 p->setThickness(1);
1086 else
1087 p->setThickness(1);
1088
1089 // between out and vol
1090 p->drawLine(out_box.x2,out_box.p1,
1091 vol_box.x1,vol_box.p1,
1092 black);
1093
1094 p->setThickness(1);
1095 }
1096
1097 paintSliderText(p,in_box,"Threshold: ",get_threshold(), "dB");
1098 paintSliderText(p,out_box,"Ratio: ",get_ratio(),":1");
1099 paintSliderText(p,vol_box,"Makeup Gain: ",get_makeup_gain(),"dB");
1100
1101 background_image_must_be_updated = false;
1102 }
1103
1104 void updateBackgroundImage(){
1105 MyPainter p(background_image);
1106 //background_image->fill(colors[11]);
1107 paintBackgroundImage(&p);
1108 }
1109
1110 void repaint(MyPainter *p){
1111 //printf("Comp::repaint(). Ratio: %f\n", get_compressor_parameter(_patch, COMP_EFF_RATIO));
1112 if(background_image_must_be_updated==true)
1113 updateBackgroundImage();
1114
1115 Box in_box = get_slider1_parms();
1116 Box out_box = get_slider2_parms();
1117 Box vol_box = get_slider3_parms();
1118
1119 //paintBackgroundImage(&p);
1120 p->drawImage(0,0,background_image);
1121
1122 paintPeaks(p, in_box, out_box, vol_box);
1123 }
1124
1125 };
1126 }
1127
1128 #endif // INCLUDE_MYLINE_AND_MYRECT