-
Notifications
You must be signed in to change notification settings - Fork 1
/
scopewidget.h
377 lines (308 loc) · 8.94 KB
/
scopewidget.h
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
/*
* Copyright (C) 2020 - 2023 Judd Niemann - All Rights Reserved.
* You may use, distribute and modify this code under the
* terms of the GNU Lesser General Public License, version 2.1
*
* You should have received a copy of GNU Lesser General Public License v2.1
* with this file. If not, please refer to: https://github.com/jniemann66/sndscope.git
*/
#ifndef SCOPEWIDGET_H
#define SCOPEWIDGET_H
#include <memory>
#include <QWidget>
#include <QPixmap>
#include <QLabel>
#include <QTimer>
#include <QElapsedTimer>
#include <QTime>
#include <QColor>
#include <QPainter>
#include <QResizeEvent>
#include <QDebug>
#include <QPixmap>
#include <QHBoxLayout>
#include <QAudioDeviceInfo>
#include <QThread>
#include <sndfile.hh>
#include "plotmode.h"
#include "sweepparameters.h"
#include "audiocontroller.h"
#include "upsampler.h"
#include "plotter.h"
#ifdef SNDSCOPE_BLEND2D
#include "blimagewrapper.h"
#endif
// ScopeDisplay : this is the Oscilloscope's screen
// it owns a QPixmap as an image buffer, which is accessed via getPixmap()
class ScopeDisplay : public QWidget
{
Q_OBJECT
public:
#ifdef SNDSCOPE_BLEND2D
ScopeDisplay(QWidget* parent = nullptr) : QWidget(parent), pixmap(800, 640), blImageWrapper(std::make_unique<BLImageWrapper>(800, 640))
#else
ScopeDisplay(QWidget* parent = nullptr) : QWidget(parent), pixmap(800, 640)
#endif
{
setAutoFillBackground(false);
resizeCooldownTimer.setSingleShot(true);
resizeCooldownTimer.setInterval(50);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
// actual resizing of pixmap is only done after waiting for resize events to settle-down
connect(&resizeCooldownTimer, &QTimer::timeout, this, [this]{
if (pixmap.height() != height()) {
const int h = height();
const int w = aspectRatio.first * h / aspectRatio.second;
qDebug().noquote() << QStringLiteral("adjusting pixmap resolution to %1x%2").arg(w).arg(h);
#ifdef SNDSCOPE_BLEND2D
blImageWrapper = std::make_unique<BLImageWrapper>(w, h);
#else
pixmap = pixmap.scaled(w, h);
#endif
calcGraticule();
emit pixmapResolutionChanged(pixmap.size());
}
});
}
// getters
#ifdef SNDSCOPE_BLEND2D
BLImageWrapper*getBlImageWrapper() const
{
return blImageWrapper.get();
}
#else
QPixmap* getPixmap()
{
return &pixmap;
}
#endif
bool getAllowPixmapResolutionChange() const
{
return allowPixmapResolutionChange;
}
QPair<int, int> getAspectRatio() const
{
return aspectRatio;
}
QPair<int, int> getDivisions() const
{
return divisions;
}
// setters
void setAllowPixmapResolutionChange(bool value)
{
allowPixmapResolutionChange = value;
}
void setAspectRatio(const QPair<int, int> &newAspectRatio)
{
aspectRatio = newAspectRatio;
}
void setDivisions(const QPair<int, int> &newDivisions)
{
divisions = newDivisions;
}
bool getShowGraticule() const
{
return showGraticule;
}
void setShowGraticule(bool newShowGraticule)
{
showGraticule = newShowGraticule;
}
signals:
void pixmapResolutionChanged(const QSizeF& size);
protected:
QSize sizeHint() const override
{
return pixmap.size();
}
void paintEvent(QPaintEvent *event) override
{
Q_UNUSED(event)
QPainter p(this);
p.setRenderHint(QPainter::TextAntialiasing, false);
#ifdef SNDSCOPE_BLEND2D
p.drawImage(0, 0, *blImageWrapper->getQImage());
#else
if(size() == pixmap.size()) {
p.drawPixmap(0, 0, pixmap);
} else {
p.drawPixmap(0, 0, pixmap.scaled(size()));
}
#endif
if(showGraticule) {
p.setRenderHint(QPainter::Antialiasing, true);
QPen graticulePen(graticuleColor, 1.5);
p.setPen(graticulePen);
p.drawLines(graticuleLines);
}
}
void resizeEvent(QResizeEvent *event) override
{
const int h = event->size().height();
const int w = aspectRatio.first * h / aspectRatio.second;
setFixedWidth(w);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
updateGeometry();
if(allowPixmapResolutionChange) {
resizeCooldownTimer.start();
}
calcGraticule();
}
void calcGraticule()
{
const double w = width() - 0.5;
const double h = height() - 0.5;
const double cx = 0.5 * w;
const double cy = 0.5 * h;
const double divx = w / divisions.first;
const double divy = h / divisions.second;
graticuleLines.clear();
double gx = 0.5;
double gy = 0.5;
constexpr double tl = 4.0;
const double txd = 0.2 * divx;
const double tyd = 0.2 * divy;
const double ty0 = cy - tl;
const double ty1 = cy + tl;
const double tx0 = cx - tl;
const double tx1 = cx + tl;
for(int i = 0; i <= divisions.first; i++) {
graticuleLines.append(QPointF{gx, 0});
graticuleLines.append(QPointF{gx, h});
double tx = gx + txd;
for(int j = 1; j <= 4; j++) {
graticuleLines.append(QPointF{tx, ty0});
graticuleLines.append(QPointF{tx, ty1});
tx += txd;
}
gx += divx;
}
for(int i = 0; i <= divisions.second; i++) {
graticuleLines.append(QPointF{0, gy});
graticuleLines.append(QPointF{w, gy});
double ty = gy + tyd;
for(int j = 1; j <= 4; j++) {
graticuleLines.append(QPointF{tx0, ty});
graticuleLines.append(QPointF{tx1, ty});
ty += tyd;
}
gy += divy;
}
}
private:
QColor graticuleColor{112, 112, 112, 160};
QPair<int, int> divisions{10, 8};
QPair<int, int> aspectRatio{5, 4};
QTimer resizeCooldownTimer;
QPixmap pixmap;
#ifdef SNDSCOPE_BLEND2D
std::unique_ptr<BLImageWrapper> blImageWrapper;
#endif
bool allowPixmapResolutionChange{true};
QVector<QPointF> graticuleLines;
bool showGraticule{true};
};
// ScopeWidget : the heart of the Oscilloscope
class ScopeWidget : public QWidget
{
Q_OBJECT
friend class Plotter;
static constexpr int upsampleFactor = 4;
QThread renderThread;
public:
explicit ScopeWidget(QWidget *parent = nullptr);
~ScopeWidget();
QPair<bool, QString> loadSoundFile(const QString &filename);
// getters
Plotmode getPlotmode() const;
int getLengthMilliseconds() const;
bool getPaused() const;
int64_t getTotalFrames() const;
double getBrightness() const;
double getFocus() const;
QColor getPhosphorColor() const;
double getPersistence() const;
bool getMultiColor() const;
QColor getBackgroundColor() const;
SweepParameters getSweepParameters() const;
QAudioDeviceInfo getOutputDeviceInfo() const;
bool getShowTrigger() const;
bool getconnectSamples() const;
// setters
void setPaused(bool value);
void setBrightness(double value);
void setFocus(double value);
void setPersistence(double time_ms);
void setPhosporColors(const QVector<QColor> &colors);
void setBackgroundColor(const QColor &value);
void setOutputDevice(const QAudioDeviceInfo &newOutputDeviceInfo);
void setShowTrigger(bool val);
void setconnectSamples(bool val);
void plotTest();
bool getUpsampling() const;
public slots:
void returnToStart();
void gotoPosition(int64_t milliSeconds);
void wipeScreen();
void setAudioVolume(qreal linearVolume);
void setSweepParameters(const SweepParameters &newSweepParameters);
void setPlotmode(Plotmode newPlotmode);
void setUpsampling(bool val);
signals:
void loadedFile();
void renderedFrame(int positionMilliseconds);
void outputVolume(qreal linearVol);
protected:
private:
ScopeDisplay* scopeDisplay{nullptr};
AudioController *audioController{nullptr};
Plotter *plotter{nullptr};
QIODevice* pushOut{nullptr};
QHBoxLayout *screenLayout{nullptr};
std::unique_ptr<SndfileHandle> sndfile;
QAudioFormat audioFormat;
QAudioDeviceInfo outputDeviceInfo;
UpSampler<float, float, upsampleFactor> upsampler;
// audio buffers
QVector<float> rawinputBuffer; // interleaved
QVector<QVector<float>> inputBuffers; // de-interleaved
// timing
QTimer plotTimer;
QTimer screenUpdateTimer;
QElapsedTimer elapsedTimer;
Plotmode plotMode{XY};
bool showTrigger{false};
SweepParameters sweepParameters;
bool upsampling{false};
int numInputChannels{0};
int audioFramesPerMs{0};
double msPerAudioFrame{0.0};
// audio frame accounting
int64_t startFrame{0ll}; // start position for playback
int64_t currentFrame{0ll}; // start position of next read
int64_t framesRead{0ll}; // number of audioframes last read from file
int64_t framesAvailable{011}; // number of audioframes currently stored in input buffers; after upsampling
int64_t expectedFrames{0ll}; // number of audioframes expected per plotTimer timeout
int64_t maxFramesToRead{0ll}; // limit of how many audioframes can fit in buffer
int64_t totalFrames{0ll}; // total number of audioframes in sound file
bool fileLoaded{false};
bool paused{true};
// crt properties
qreal brightness{80.0};
qreal focus{80.0};
qreal persistence{32.0};
QColor backgroundColor{0, 0, 0, 255};
int beamAlpha; // todo: try AlphaF (qreal)
// plot dimensions
qreal cx;
qreal cy;
qreal w;
qreal h;
// private functions
void readInput();
void calcBeamAlpha();
void drawTrigger(QPainter *painter);
void makeTestPlot();
};
#endif // SCOPEWIDGET_H