File: Qt_compressor_widget_callbacks.h

package info (click to toggle)
radium-compressor 0.5.1%2Bgit20190909-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,064 kB
  • sloc: cpp: 8,708; ansic: 1,636; makefile: 91; sh: 42
file content (567 lines) | stat: -rw-r--r-- 15,287 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* Copyright 2013 Kjetil S. Matheussen

This program 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 2
of the License, or (at your option) any later version.

This program 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 program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

#include <stdio.h>

#include "Qt_compressor_widget.h"

#include <QPainter>
#include <QMouseEvent>
#include <QTimer>
#include <QFileDialog>
#include <QMessageBox>

#define INCLUDE_MYLINE_AND_MYRECT 1
#  include "compressor_vertical_sliders.cpp"
#undef INCLUDE_MYLINE_AND_MYRECT

namespace cvs{

  struct MyColor{
    QColor col;
    MyColor(int r,int g,int b)
      : col(r,g,b)
    {}

    MyColor(QColor col)
      : col(col)
    {}

    MyColor(int colnum)
#ifdef COMPILING_RADIUM
      : col(static_cast<EditorWidget*>(root->song->tracker_windows->os_visual.widget)->colors[colnum])
#else
        : col(g_colors[colnum])
#endif
    {}

    MyColor()
#ifdef COMPILING_RADIUM
      : col(static_cast<EditorWidget*>(root->song->tracker_windows->os_visual.widget)->colors[11])
#else
        : col(g_colors[11])
#endif
    {}

    MyColor lighter(int p){
      return MyColor(col.light(80));
    }

    MyColor darker(int p){
      return MyColor(col.light(80));
    }

    void setAlpha(int a){
      col.setAlpha(a);
    }
  };

  static inline MyColor mix_colors(const MyColor &c1, const MyColor &c2, float how_much){
    return MyColor(mix_colors(c1.col,c2.col,how_much));
  }

  struct MyImage{
    QImage image;
    QPainter qp;
    MyImage(int width,int height)
      : image(width,height,QImage::Format_RGB32)
      , qp(&image)
    {
    }
  };

  struct MyPainter{
    QPainter *p;
    MyColor col;
    QPen pen;
    QLinearGradient gradient;
    bool use_gradient;
    int _thickness;

    MyPainter(QPainter *p) 
      : p(p) 
      , use_gradient(false)
      , _thickness(1)
    {
      p->setPen(pen);
    }

    MyPainter(MyImage *image)
      : p(&image->qp)
      , use_gradient(false)
      , _thickness(1)
    {
      p->setPen(pen);
    }

    void setGradient(int x1, int y1, int x2, int y2, const MyColor &c1, const MyColor &c2){
      gradient = QLinearGradient(x1,y1,x2,y2);
      gradient.setColorAt(0,c1.col);
      gradient.setColorAt(1,c2.col);
      p->setPen(QPen(QBrush(gradient),_thickness));
      use_gradient = true;
    }

    void unsetGradient(){
      use_gradient=false;
    }

    void drawImage(int x,int y,MyImage *image){
      p->drawImage(x,y,image->image);
    }

    void drawRect(const int x1,const int y1,const int x2,const int y2, const MyColor &col){
      if(use_gradient==false){
        pen.setColor(col.col);
        p->setPen(pen);
      }
      p->drawRect(x1,y1,x2-x1,y2-y1);
    }
    void drawRect(const MyRect &rect,const MyColor &col){
      drawRect(rect.x1,rect.y1,rect.x2,rect.y2,col);
    }


    void fillRect(const int x1,const int y1,const int x2,const int y2, const MyColor &col){
      if(use_gradient==false)
        p->fillRect(x1,y1,x2-x1,y2-y1,col.col);
      else
        p->fillRect(x1,y1,x2-x1,y2-y1,QBrush(gradient));
    }

    void fillRect(const MyRect &rect,const MyColor &col){
      fillRect(rect.x1,rect.y1,rect.x2,rect.y2,col);
    }

    void setThickness(int thickness){
      _thickness=thickness;
      pen.setWidth(thickness);
    }

    void drawLine(const int x1,const int y1,const int x2,const int y2, const MyColor &col){
      if(use_gradient==false){
        pen.setColor(col.col);
        p->setPen(pen);
      }

      bool anti = x1!=x2 && y1!=y2;

      if(anti)
        p->setRenderHints(QPainter::Antialiasing,true);

      p->drawLine(x1,y1,x2,y2);

      if(anti)
        p->setRenderHints(QPainter::Antialiasing,false);
    }

    void drawLine(const MyLine &l, const MyColor &col){
      drawLine(l.x1,l.y1,l.x2,l.y2, col);
    }

    int getTextWidth(std::string text){
      const QFontMetrics fn = QFontMetrics(QFont());
      return fn.width(QString::fromStdString(text));
    }

    void drawText(const MyRect &rect, std::string text, const MyColor &col){
      if(use_gradient==false){
        pen.setColor(col.col);
        p->setPen(pen);
      }

      p->drawText(rect.x1,rect.y1,rect.width(),rect.height(),
                  Qt::AlignTop|Qt::AlignLeft,
                  QString::fromStdString(text));
    }

    void drawVerticalText(int x, int y, std::string text, const MyColor &col){
      if(use_gradient==false){
        pen.setColor(col.col);
        p->setPen(pen);
      }
      p->save();
      QPoint point(x,y); 
      p->translate(x,0);//point);//x, y);
      p->rotate(90); // or 270
      //p->scale((box.x2-box.x1)/12,1.0);
      //point = p->xFormDev(point);
      p->drawText(5,-5,QString::fromStdString(text));
      p->restore();
    }
  };

  struct MyWidget{
    struct MyQWidget : public QWidget{

      MyWidget *mywidget;
      MyQWidget(QWidget *parent, MyWidget *mywidget) : QWidget(parent),mywidget(mywidget){
      }

      void mousePressEvent ( QMouseEvent * event ){
        if(mywidget->mousePress(event->x(),event->y()))
          event->accept();
      }
      void mouseMoveEvent ( QMouseEvent * event ){
        if(mywidget->mouseMove(event->x(),event->y()))
          event->accept();
      }
      void mouseReleaseEvent ( QMouseEvent * event ){
        if(mywidget->mouseRelease(event->x(),event->y()))
          event->accept();
      }

      void paintEvent ( QPaintEvent * ev ){
        QPainter qp(this);
        MyPainter p(&qp);
        mywidget->repaint(&p);
      }

      virtual void resizeEvent ( QResizeEvent * event ){
        mywidget->resized();
      }
    };

    MyQWidget w;

    MyWidget(void *parent)
      : w(static_cast<QWidget*>(parent),this)
    {}

    void update(int x1,int y1,int x2,int y2){
      w.update(x1,y1,x2-x1,y2-y1);      
    }
    void update(const MyRect &r){
      update(r.x1,r.y1,r.x2,r.y2);
    }

    void update(){
      w.update();
    }

    int width(){return w.width();}
    int height(){return w.height();}

    bool isVisible(){return w.isVisible();}
    bool isEnabled(){
      //printf("w is %s\n",w.isEnabled()?"enabled":"disabled");
      return w.isEnabled();
    }

    virtual void resized() = 0;

    //virtual void repaint(MyPainter *p){}
    virtual void repaint(MyPainter *p) = 0;

    virtual bool mousePress(int x, int y) = 0;

    virtual bool mouseMove(int x, int y) = 0;

    virtual bool mouseRelease(int x, int y) = 0;
  };

  struct MyTimer{

    struct MyQTimer : public QTimer{
      MyTimer *mytimer;

      MyQTimer(MyTimer *mytimer) : mytimer(mytimer) {}

      void timerEvent(QTimerEvent * e){ // virtual method from QTimer
        mytimer->timer();
      }
    };

    MyQTimer qtimer;

    MyTimer() : qtimer(this) {}

    virtual void timer() = 0;

    void startTimer(int interval_in_milliseconds){
      qtimer.setInterval(interval_in_milliseconds);
      qtimer.start();
    }
  };

} // namespace cvs



#include "../audio/system_compressor_wrapper_proc.h"


#ifdef COMPILING_RADIUM

// I'm pretty sure it's not necessary to test for plugin!=NULL in these three functons.
// Normally, I'm 100% sure it's safe, but these functions can also be called from a timer.
//
static void set_compressor_parameter(struct Patch *patch, int num,float value){
  SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
  if(plugin!=NULL)
    PLUGIN_set_effect_value(plugin, 0, plugin->type->num_effects+EFFNUM_COMP_RATIO+num, value, PLUGIN_NONSTORED_TYPE, PLUGIN_STORE_VALUE);
}

#else // COMPILING_RADIUM

static void set_compressor_parameter(struct Patch *patch, int num,float value){
  SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
  if(plugin!=NULL)
    COMPRESSOR_set_parameter(plugin->compressor,num,value);
}

#endif // #else COMPILING_RADIUM

static float get_compressor_parameter(struct Patch *patch, int num){
  SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
  if(plugin!=NULL)
    return COMPRESSOR_get_parameter(plugin->compressor,num);
  else
    return 0.0f;
}

static float get_graph_value(struct Patch *patch, int num){
  SoundPlugin *plugin = (SoundPlugin*)patch->patchdata;
  if(plugin!=NULL)
    return COMPRESSOR_get_graph_value(plugin->compressor,num);
  else
    return 0.0;
}


//#ifdef COMPILING_RADIUM
//namespace radium_comp{ // got an error trying to use anonymous namespace here.
  //#endif


#include "compressor_vertical_sliders.cpp"


static double OS_get_double_from_string(const char *s){
  QLocale::setDefault(QLocale::C);
  QString string(s);
  return string.toDouble();
}

static float read_float(FILE *file){
  char temp[512] = {0};

  fgets(temp,500,file);

  return OS_get_double_from_string(temp);
}


class Compressor_widget : public QWidget, public Ui::Compressor_widget{
  Q_OBJECT

 public:
  bool initing;
  cvs::Comp *comp;
  struct Patch *_patch;

 Compressor_widget(struct Patch *patch, QWidget *parent=NULL)
    : QWidget(parent)
    , _patch(patch)
  {
    initing = true;
    setupUi(this);

    QWidget *parent_for_comp = this;
    comp = new cvs::Comp(patch,parent_for_comp);

#ifdef COMPILING_RADIUM
    comp->w.setEnabled(false);
#else
    attack_slider->setEnabled(true);
    release_slider->setEnabled(true);
    enable_checkbox->setVisible(false);
#endif
    verticalLayout->insertWidget(1,&comp->w);

#ifdef COMPILING_RADIUM
    // Enable MyQSlider and MyQCheckBox to take care of undo/redo.
    {
      SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
      attack_slider->_patch = patch;
      attack_slider->_effect_num = plugin->type->num_effects+EFFNUM_COMP_ATTACK;
      release_slider->_patch = patch;
      release_slider->_effect_num = plugin->type->num_effects+EFFNUM_COMP_RELEASE;
      enable_checkbox->_patch = patch;
      enable_checkbox->_effect_num = plugin->type->num_effects+EFFNUM_COMP_ONOFF;
    }
#endif

    update_gui();
    initing = false;
  }

  float get_exp_value(double val, double max_val, double y1, double y2){
    return scale_double(exp(val/max_val),
                        exp(0.0),expf(1.0),
                        y1,y2
                        );
  }

  float get_exp_inverted_value(double y, double max_val, double y1, double y2){
    return max_val * log(scale_double(y,y1,y2,exp(0.0),exp(1.0)));
  }

  void update_gui(){
    comp->set_gui_parameters();
    attack_slider->setValue(get_exp_inverted_value(get_compressor_parameter(_patch, COMP_EFF_ATTACK),1000,0,max_attack_release));
    release_slider->setValue(get_exp_inverted_value(get_compressor_parameter(_patch, COMP_EFF_RELEASE),1000,0,max_attack_release));

    SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
    enable_checkbox->setChecked(plugin->comp.is_on);

    //paint_all=true;
    //updateBackgroundImage();
    //comp->update();
    update();
  }

  void load(QString filename, bool complain_if_file_not_found = true){
    FILE *file = fopen(filename.toUtf8().constData(),"r");

    if(file==NULL){
      if(complain_if_file_not_found){
        QMessageBox msgBox;
        msgBox.setText("Could not open file \""+filename+"\".");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
      }
      return;
    }
    
    set_compressor_parameter(_patch, COMP_EFF_RATIO,read_float(file)); // ratio
    set_compressor_parameter(_patch, COMP_EFF_THRESHOLD,read_float(file)); // threshold
    set_compressor_parameter(_patch, COMP_EFF_ATTACK,read_float(file)); // attack
    set_compressor_parameter(_patch, COMP_EFF_RELEASE,read_float(file)); // release
    //set_compressor_parameter(INPUT_VOLUME,read_float(file)); // input volume (currently not used)
    read_float(file); // input volume (currently not used)
    set_compressor_parameter(_patch, COMP_EFF_OUTPUT_VOLUME,read_float(file)); // output volume

    int x = read_float(file);
    int y = read_float(file);
    int width = read_float(file);
    int height = read_float(file);

    if(width>0 && height>0){
      move(x,y);
      resize(width,height);
    }

    fclose(file);

    comp->background_image_must_be_updated=true;
    
    update_gui();
  }

  void save(QString filename){
    FILE *file = fopen(filename.toUtf8().constData(),"w");

    if(file==NULL){
      QMessageBox msgBox;
      msgBox.setText("Could not save file.");
      msgBox.setStandardButtons(QMessageBox::Ok);
      msgBox.setDefaultButton(QMessageBox::Ok);
      msgBox.exec();
      return;
    }

    fprintf(file,"%f\n%f\n%f\n%f\n%f\n%f\n",
            get_compressor_parameter(_patch, COMP_EFF_RATIO),
            get_compressor_parameter(_patch, COMP_EFF_THRESHOLD),
            get_compressor_parameter(_patch, COMP_EFF_ATTACK),
            get_compressor_parameter(_patch, COMP_EFF_RELEASE),
            //get_compressor_parameter(INPUT_VOLUME),
            0.0f, // input volume (in dB)
            get_compressor_parameter(_patch, COMP_EFF_OUTPUT_VOLUME)
            );

    fprintf(file,"%d\n%d\n%d\n%d\n",
            pos().x(),pos().y(),
            width(),height()
            );

    fclose(file);
  }

public slots:

void on_enable_checkbox_toggled(bool val){
#ifdef COMPILING_RADIUM
  SoundPlugin *plugin = (SoundPlugin*)_patch->patchdata;
  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);

  attack_slider->setEnabled(val);
  release_slider->setEnabled(val);
  comp->background_image_must_be_updated=true;
  comp->w.setEnabled(val);
#endif
}

  void on_attack_slider_valueChanged(int val){
    float attack = get_exp_value(val,1000,0,max_attack_release);
    set_compressor_parameter(_patch, COMP_EFF_ATTACK,attack);
    char temp[512];
    sprintf(temp,"Attack: %.2fms",attack);
    SLIDERPAINTER_set_string(attack_slider->_painter, temp);
  }

  void on_release_slider_valueChanged(int val){
    float release = get_exp_value(val,1000,0,max_attack_release);
    set_compressor_parameter(_patch, COMP_EFF_RELEASE,release);
    char temp[512];
    sprintf(temp,"Release: %.2fms",release);
    SLIDERPAINTER_set_string(release_slider->_painter, temp);
  }

  void on_bypass_toggled(bool val){
    printf("bypass: %d\n",(int)val);
    set_compressor_parameter(_patch, COMP_EFF_BYPASS,val==true?1.0f:0.0f);
  }

  void on_load_button_pressed(void){
    printf("load pressed\n");

    QString filename = QFileDialog::getOpenFileName(this, "Load Effect configuration", "", "Radium Compressor Configuration (*.rcc)");

    if(filename=="")
      return;

    load(filename);
  }

  void on_save_button_pressed(void){
    printf("save pressed\n");

    QString filename = QFileDialog::getSaveFileName(this, "Save Effect configuration", "", "Radium Compressor Configuration (*.rcc)");

    if(filename=="")
      return;

    save(filename);
  }
};

  //#ifdef COMPILING_RADIUM
  //} // radium_comp namespace
  //#endif