summaryrefslogtreecommitdiff
path: root/muse2/muse/mixer/meter.cpp
blob: aae380f35fbc649c730fb433927d9daebe10d882 (plain)
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
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: meter.cpp,v 1.4.2.2 2009/05/03 04:14:00 terminator356 Exp $
//
//  (C) Copyright 2000 Werner Schweer (ws@seh.de)
//=========================================================

#include <stdio.h>
#include <cmath>
#include <qpainter.h>
//Added by qt3to4:
#include <QResizeEvent>
#include <QMouseEvent>
#include <QFrame>

#include "meter.h"
#include "gconfig.h"
#include "fastlog.h"

//---------------------------------------------------------
//   Meter
//---------------------------------------------------------

Meter::Meter(QWidget* parent, MeterType type)
   : QFrame(parent) //Qt::WNoAutoErase
      {
      mtype = type;
      overflow    = false;
      val         = 0.0;
      maxVal      = 0.0;
      minScale    = mtype == DBMeter ? config.minMeter : 0.0;      // min value in dB or int
      maxScale    = mtype == DBMeter ? 10.0 : 127.0;
      yellowScale = -10;
      redScale    = 0;
      setLineWidth(0);
      setMidLineWidth(0);
      }

//---------------------------------------------------------
//   setVal
//---------------------------------------------------------

//void Meter::setVal(int v, int max, bool ovl)
void Meter::setVal(double v, double max, bool ovl)
      {
      overflow = ovl;
      bool ud = false;
      
      if(mtype == DBMeter)
      {
        double minScaleLin = pow(10.0, minScale/20.0);
        if((v >= minScaleLin && val != v) || val >= minScaleLin)
        {
          val = v;
          ud = true;
        }
      }
      else
      {
        if(val != v)
        {
          val = v;
          ud = true;
        }
      }  
      
      if(maxVal != max)
      {
        maxVal = max;
        ud = true;
      }
      
      if(ud)
        update();
    }
//---------------------------------------------------------
//   resetPeaks
//    reset peak and overflow indicator
//---------------------------------------------------------

void Meter::resetPeaks()
      {
      maxVal   = val;
      overflow = val > 0.0;
      update();
      }

//---------------------------------------------------------
//   setRange
//---------------------------------------------------------

void Meter::setRange(double min, double max)
      {
      minScale = min;
      maxScale = max;
      drawVU(width(), height());
      update();
      }

//---------------------------------------------------------
//   paintEvent
//---------------------------------------------------------

void Meter::paintEvent(QPaintEvent*)
      {
      QPainter p;
      p.begin(this);
      double range = maxScale - minScale;

      int fw = frameWidth();
      int h  = height() - 2* fw;
      int yv;
      if(mtype == DBMeter) 
        yv = val == 0 ? h : int(((maxScale - (fast_log10(val) * 20.0)) * h)/range);
      else
        yv = val == 0 ? h : int(((maxScale - val) * h)/range);
      
      bitBlt(this, fw, fw,    &bgPm, 0, 0,  -1, yv, true); //   CopyROP, true); ddskrjo
      bitBlt(this, fw, fw+yv, &fgPm, 0, yv, -1, h-yv, true); //CopyROP, true); ddskrjo

      int ymax;
      if(mtype == DBMeter) 
        ymax = maxVal == 0 ? 0 : int(((maxScale - (fast_log10(maxVal) * 20.0)) * h)/range);
      else
        ymax = maxVal == 0 ? 0 : int(((maxScale - maxVal) * h)/range);
      p.setPen(Qt::white);
      p.drawLine(0, ymax, width()-2*fw, ymax);
      }

//---------------------------------------------------------
//   drawVU
//---------------------------------------------------------

void Meter::drawVU(int w, int h)
      {
      double range = maxScale - minScale;
      int fw = frameWidth();
      w -= 2*fw;
      h -= 2*fw;

      bgPm = QPixmap(QSize(w, h));
      fgPm = QPixmap(QSize(w, h));

      QPainter p1(&fgPm);
      QPainter p2(&bgPm);
      
      if(mtype == LinMeter)
      {
        p1.fillRect(0, 0, w, h, QBrush(0x00ff00));  // green
        p2.fillRect(0, 0, w, h, QBrush(0x007000));  // green
      }  
      else
      {
        int y1 = int((maxScale - redScale) * h / range);
        int y2 = int((maxScale - yellowScale) * h / range);
        int y3 = h;
        p1.fillRect(0, 0,  w, y1,    QBrush(0xff0000));  // red
        p1.fillRect(0, y1, w, y2-y1, QBrush(0xffff00));  // yellow
        p1.fillRect(0, y2, w, y3-y2, QBrush(0x00ff00));  // green
  
        p2.fillRect(0, 0,  w, y1,    QBrush(0x8e0000));  // red
        p2.fillRect(0, y1, w, y2-y1, QBrush(0x8e8e00));  // yellow
        p2.fillRect(0, y2, w, y3-y2, QBrush(0x007000));  // green
      }
    }

//---------------------------------------------------------
//   resizeEvent
//---------------------------------------------------------

void Meter::resizeEvent(QResizeEvent* ev)
      {
      int h  = ev->size().height();
      int w  = ev->size().width();
      drawVU(w, h);
    }

//---------------------------------------------------------
//   mousePressEvent
//---------------------------------------------------------

void Meter::mousePressEvent(QMouseEvent*)
      {
      emit mousePress();
      }