This repository was archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrograph.cpp
More file actions
211 lines (202 loc) · 6.49 KB
/
spectrograph.cpp
File metadata and controls
211 lines (202 loc) · 6.49 KB
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
#include "spectrograph.h"
#include <iostream>
#include <QtCore/qmath.h>
#include <QBrush>
#include <QPen>
#include <fstream>
#include <QGradient>
//#include <QDebug>
#include <QResizeEvent>
#include <QTimerEvent>
#include <QPainterPath>
#include <QEasingCurve>
Spectrograph::Spectrograph(QWidget *parent) :
AbstractSpectrograph(parent){
// at each 15ms, the timer clicks and timerEvent is called
startTimer(15);
// however 256 samples may be delivered to this widget, only
// 64 are to be displayed becaus of speed limitations ;)
NUM_BANDS = 16;
//
spectrum.resize(NUM_BANDS);
decay.resize(NUM_BANDS);
gravity.resize(NUM_BANDS);
delay[0].resize(NUM_BANDS);
delay[1].resize(NUM_BANDS);
for(int i=0; i<NUM_BANDS; i++){
decay[i]=0;
gravity[i]=1;
spectrum[i]=1;
delay[0][i]=1;
delay[1][i]=1;
}
leftLevel = rightLevel = 1;
gradient = QLinearGradient(rect().topLeft(), rect().bottomRight());
gradientBrush = QBrush(gradient);
gradientBrush.setStyle(Qt::SolidPattern);
backgroundBrush.setColor(Qt::white);
backgroundBrush.setStyle(Qt::SolidPattern);
// transparentBrush.setColor(Qt::transparent);
pen.setColor(qRgb(0,0,0));
decayBrush.setColor(QColor(255,0,0,255));
decayBrush.setStyle(Qt::SolidPattern);
// barWidth = MIN_BARWIDTH;
barWidth = 1;
barSpacing = 1;
}
void Spectrograph::resizeEvent(QResizeEvent *e){
e->accept();
gradient = QLinearGradient(rect().topLeft(), rect().bottomRight());
gradient.setColorAt(1, Qt::black);
gradient.setColorAt(0, Qt::white);
gradientBrush = QBrush(gradient);
barWidth = (float)width()/NUM_BANDS;
//qDebug() << "barwidth = " << barWidth;
widgetHeight = height();
repaint();
}
void Spectrograph::loadLevels(double left, double right){
if(leftLevel < 5*width()/2*left)
leftLevel = 5*width()/2*left;
if(rightLevel < 5*width()/2*right)
rightLevel = 5*width()/2*right;
}
void Spectrograph::paintEvent(QPaintEvent *e){
QPainter p(this);
QPen pen;
QVector<QPointF> vec;
QPainterPath path;
e->accept();
QEasingCurve curve;
// gradient.setColorAt(0, Qt::black);
// gradient.setColorAt(1, Qt::white);
// p.setBrush(gradient);
p.setBrush(gradientBrush);
p.drawRect(rect());
// gradient.setColorAt(0, Qt::white);
// gradient.setColorAt(1, Qt::black);
p.setRenderHint(QPainter::Antialiasing);
pen.setStyle(Qt::SolidLine);
pen.setColor(Qt::black);
pen.setWidth(1);
p.setPen(pen);/*
curve.addTCBSegment(QPointF(0,altura-spectrum[0]),0,0,0);
for(int i=1; i<NUM_BANDAS;i++){
p1x = i*barWidth;
p1y = altura-spectrum[i];
p.setBrush(Qt::white);
// p.setBrush(gradient);
curve.addTCBSegment(QPointF(p1x,p1y),0,0,0);
p1y = (float)altura -decay[i];
p.setBrush(decayBrush);
p.drawRect(QRectF(QPointF(p1x,p1y),QPointF(p2x,p1y+3)));
}
pen.setColor(Qt::white);
pen.setWidth(2);
p.setPen(pen);
p.setBrush(Qt::NoBrush);
vec=curve.toCubicSpline();
qDebug() << vec[0].rx();
p.drawLines(vec);*/
/*for(int i=0; i<NUM_BANDS;i++){
p1x = i*barWidth;
p2x = p1x+barWidth;
p1y = widgetHeight-spectrum[i];
// p.setBrush(Qt::white);
gradient = QLinearGradient(widgetHeight);
p.setBrush(gradient);
p.drawRect(QRectF(QPointF(p1x,p1y),QPointF(p2x,widgetHeight)));
p1y = (float)widgetHeight -decay[i];
p.setBrush(decayBrush);
p.drawEllipse(QPointF(p1x+barWidth/2,p1y),3,3);
p.drawRect(QRectF(QPointF(p1x,p1y),QPointF(p2x,p1y+3)));
}*/
// aqui eh desenhada as linhas do spectrum a partir de um ponto central
// do painel
QPoint *centerPoint = new QPoint(this->width()/2,this->height()/2);
//float flo = p.pen().width();
for(int i=0; i<NUM_BANDS;i++){
float p1x, p1y, p2x, p2y, teta;
p1x = centerPoint->rx();
p1y = centerPoint->ry();
teta = fi + i * (360*3.1415/180.0) / spectrum.length();
p2x = p1x + spectrum[i] * cos(teta)/3;
p2y = p1y + spectrum[i] * sin(teta)/3;
p.setPen(QPen(QColor(0,0,spectrum[i]/5),spectrum[i]/8));
p.drawLine(QPointF(p1x,p1y),QPointF(p2x,p2y));
}
//p.setBrush();
p.drawEllipse(QPointF(centerPoint->rx(),centerPoint->ry()),this->height()/8,this->height()/8);
/*p.setPen(QPen(Qt::black,flo));
//p.setBrush(Qt::black);
p.drawRect(0,height()-7,width(),7);
p.setBrush(Qt::red);
p.drawRoundedRect(width()/2-leftLevel,height()-6,leftLevel,6,3,3);
p.setBrush(Qt::blue);
p.drawRoundedRect(width()/2,height()-6,rightLevel,6,3,3);
p.setPen(pen);*/
// p.setBrush(transparentBrush);
// p.drawRect(rect());
}
void Spectrograph::timerEvent(QTimerEvent *e){
// accepts the event (boring messages are not displayed)
e->accept();
fi+=1*3.1415/180.0;
// processes all spectrum bands
for(int i=0; i<NUM_BANDS; i++){
// make the spectrum bar smaller
spectrum[i]-=delay[0][i];
// but not with negative size
if(spectrum[i] <0 ){
spectrum[i]=0;
}
// waits for a while while gravity forces start to act
// it simulates a small stop when the the bar reachs its
// maximum
//
// however, this only work for the decay bars that stay
// at the top of each spectrum bar
//
// gravity stop does not affect the spectrum bars
if(gravity[i] < 0.8){
// make decay bar lower
decay[i]-=delay[1][i];
// but not too lower
if(decay[i] < 0){
decay[i] = 0;
}
// increments decay bar reduction
delay[1][i]++;
}
delay[0][i]++;
// reduces gravity for that decay bar (not the spectrum bar)
gravity[i] = gravity[i]*0.98;
}
// reduces left and right audio mean level
// please note that this reduction is linear
if(leftLevel > 0)
leftLevel--;
if(rightLevel > 0)
rightLevel--;
// repaint the screen
repaint();
}
void Spectrograph::loadSamples(QVector<double> &_spectrum){
int incr, value;
incr = _spectrum.size()/NUM_BANDS;
newSpectrum=_spectrum;
for(int i=0; i<NUM_BANDS;i++){
value = ceil(newSpectrum[i*incr]*height());
if(value > spectrum[i]){
spectrum[i] = value;
delay[0][i] = 0;
delay[1][i] = 0;
}
if(value > decay[i]){
decay[i] = value;
gravity[i] = 1;
delay[1][i] = 0;
}
}
repaint();
}