-
Notifications
You must be signed in to change notification settings - Fork 20
/
track.cpp
209 lines (155 loc) · 4.83 KB
/
track.cpp
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
#include "track.h"
#include <QAudioDecoder>
Track::Track(QWidget *parent)
: QCustomPlot(parent)
, decoder(new QAudioDecoder(this))
{
wavePlot = addGraph();
QBrush FillBrush(QColor(100,100,100));
this->setBackground(FillBrush);
QPen ThePen(QColor(127,255,0));
wavePlot->setPen(ThePen);
wavePlot->setBrush(FillBrush);
yAxis->setVisible(false);
xAxis->setVisible(false);
// add independent layer for playrect and labels so we don't replot the entire thing every time
addLayer("Playing");
setCurrentLayer("Playing");
layer("Playing")->setMode(QCPLayer::LayerMode::lmBuffered);
PlayRect = new QCPItemRect(this);
PlayRect->topLeft->setType(QCPItemPosition::ptViewportRatio);
PlayRect->bottomRight->setType(QCPItemPosition::ptViewportRatio);
QPen RectPen(QColor(255,255,255,150));
QBrush RectBrush(QColor(200,200,200,75));
RectPen.setWidth(3);
PlayRect->topLeft->setCoords(0,0);
PlayRect->bottomRight->setCoords(1,1);
PlayRect->setPen(RectPen);
PlayRect->setBrush(RectBrush);
timGenericTick = new QTimer(this);
timGenericTick->setInterval(10);
timGenericTick->setSingleShot(false);
timEndTick = new QTimer(this);
timEndTick->setInterval(1000);
timEndTick->setSingleShot(false);
connect(timGenericTick,&QTimer::timeout,this,&Track::TimerTick);
connect(timEndTick,&QTimer::timeout,this,&Track::EndSlide);
SecsTxt = new QCPItemText(this);
SecsTxt->setPositionAlignment(Qt::AlignTop|Qt::AlignLeft);
SecsTxt->position->setType(QCPItemPosition::ptViewportRatio);
SecsTxt->position->setCoords(0.02, 0.05);
SecsTxt->setText("Ready");
SecsTxt->setFont(QFont(font().family(), 10));
SecsTxt->setColor(QColor(255,255,255));
SecsTxt->setClipToAxisRect(false);
DoSlide = false;
//wavePlot->setPen(ThePen);
}
Track::~Track()
{
delete decoder;
// wavePlot delete auto ?
}
void Track::setSource(const QAudioBuffer &inbuffer)
{
buffer = inbuffer;
setBuffer();
startPlaying(((float)buffer.duration()) / 1e+6);
}
void Track::setBuffer()
{
samples.clear();
qreal peak = getPeakValue(buffer.format());
const float *data = buffer.constData<float>();
int count = buffer.sampleCount();
for (int i=0; i<count; i += 2){
double val = ((double)data[i])/peak;
samples.append(val);
}
}
void Track::plot()
{
QVector<double> x(samples.size());
for (int i=0; i<x.size(); i++)
x[i] = i;
wavePlot->addData(x, samples);
yAxis->setRange(QCPRange(-1.0, 1.0));
xAxis->setRange(QCPRange(0, samples.size()));
replot();
}
void Track::startPlaying(float TimeInSecs)
{
//TickAdd = 1.f/( TimeInSecs / 0.025f );
TotSecs = TimeInSecs;
timGenericTick->start();
timEndTick->start((int)(TimeInSecs * 1000));
}
void Track::TimerTick()
{
if (!DoSlide)
return;
float RemSecs = ((float)timEndTick->remainingTime()) / 1000.f;
float CurrentPos = TotSecs - RemSecs;
TickSet = CurrentPos/TotSecs;
PlayRect->topLeft->setCoords(TickSet,0);
SetTimeLabel(CurrentPos,TotSecs);
layer("Playing")->replot();
}
void Track::EndSlide()
{
timGenericTick->stop();
timEndTick->stop();
PlayRect->topLeft->setCoords(1,0);
SetTimeLabel(TotSecs,TotSecs);
layer("Playing")->replot();
}
void Track::SetTimeLabel(float Cur, float Remaining)
{
SecsTxt->setText(QString::number(Cur,'f',1) + " / " + QString::number(Remaining,'f',1) + " (sec)");
}
/**
* https://stackoverflow.com/questions/46947668/draw-waveform-from-raw-data-using-qaudioprobe
* @brief Track::getPeakValue
* @param format
* @return The peak value
*/
qreal Track::getPeakValue(const QAudioFormat &format)
{
qreal ret(0);
if (format.isValid()){
switch (format.sampleType()) {
case QAudioFormat::Unknown:
break;
case QAudioFormat::Float:
if (format.sampleSize() != 32) // other sample formats are not supported
ret = 0;
else
ret = 1.00003;
break;
case QAudioFormat::SignedInt:
if (format.sampleSize() == 32)
#ifdef Q_OS_WIN
ret = INT_MAX;
#endif
#ifdef Q_OS_UNIX
ret = SHRT_MAX;
#endif
else if (format.sampleSize() == 16)
ret = SHRT_MAX;
else if (format.sampleSize() == 8)
ret = CHAR_MAX;
break;
case QAudioFormat::UnSignedInt:
if (format.sampleSize() == 32)
ret = UINT_MAX;
else if (format.sampleSize() == 16)
ret = USHRT_MAX;
else if (format.sampleSize() == 8)
ret = UCHAR_MAX;
break;
default:
break;
}
}
return ret;
}