forked from Greedysky/QtSpek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspek-spectrogram.cc
290 lines (260 loc) · 7.49 KB
/
spek-spectrogram.cc
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
#include "spek-audio.h"
#include "spek-fft.h"
#include "spek-ruler.h"
#include "spek-spectrogram.h"
#include <cmath>
#include <QPainter>
#include <QDateTime>
#include <QApplication>
enum
{
MIN_RANGE = -140,
MAX_RANGE = 0,
URANGE = 0,
LRANGE = -120,
FFT_BITS = 11,
MIN_FFT_BITS = 8,
MAX_FFT_BITS = 14,
LPAD = 60,
TPAD = 60,
RPAD = 90,
BPAD = 40,
GAP = 10,
RULER = 10,
};
// Forward declarations.
static QString trim(const QString& s, int length, bool trim_end);
static int bits_to_bands(int bits);
SpekSpectrogram::SpekSpectrogram(QWidget *parent) :
QWidget(parent),
audio(new Audio()), // TODO: refactor
fft(new FFT()),
pipeline(nullptr),
streams(0),
stream(0),
channels(0),
channel(0),
window_function(WINDOW_DEFAULT),
duration(0.0),
sample_rate(0),
palette(PALETTE_DEFAULT),
prev_width(-1),
fft_bits(FFT_BITS),
urange(URANGE),
lrange(LRANGE)
{
this->create_palette();
}
SpekSpectrogram::~SpekSpectrogram()
{
this->stop();
}
void SpekSpectrogram::open(const QString &path)
{
this->path = path;
this->stream = 0;
this->channel = 0;
start();
}
void SpekSpectrogram::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter p(this);
paint(&p);
}
void SpekSpectrogram::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
start();
}
static QString time_formatter(int unit)
{
// TODO: i18n
return QTime(0, 0).addSecs(unit).toString("mm:ss");
}
static QString freq_formatter(int unit)
{
return QString("%1 kHz").arg(unit / 1000);
}
static QString density_formatter(int unit)
{
return QString("%1 dB").arg(-unit);
}
void SpekSpectrogram::paint(QPainter *dc)
{
int w = width();
int h = height();
// Initialise.
dc->setBrush(Qt::black);
dc->drawRect(this->rect());
dc->setPen(Qt::white);
// Border around the spectrogram.
dc->drawRect(LPAD - 1, TPAD - 1, w - LPAD - RPAD + 2, h - TPAD - BPAD + 2);
if(this->image.width() > 1 && this->image.height() > 1 &&
w - LPAD - RPAD > 0 && h - TPAD - BPAD > 0) {
// Draw the spectrogram.
dc->drawImage(LPAD, TPAD, this->image.scaled(w - LPAD - RPAD, h - TPAD - BPAD));
// File name.
dc->drawText(
LPAD,
TPAD - 2 * GAP,
trim(this->path, w - LPAD - RPAD, false)
);
if(this->duration) {
// Time ruler.
int time_factors[] = {1, 2, 5, 10, 20, 30, 1*60, 2*60, 5*60, 10*60, 20*60, 30*60, 0};
SpekRuler time_ruler(
LPAD,
h - BPAD,
SpekRuler::BOTTOM,
// TODO: i18n
"00:00",
time_factors,
0,
(int)this->duration,
1.5,
(w - LPAD - RPAD) / this->duration,
0.0,
time_formatter
);
time_ruler.draw(*dc);
}
if(this->sample_rate) {
// Frequency ruler.
int freq = this->sample_rate / 2;
int freq_factors[] = {1000, 2000, 5000, 10000, 20000, 0};
SpekRuler freq_ruler(
LPAD,
TPAD,
SpekRuler::LEFT,
// TRANSLATORS: keep "00" unchanged, it's used to calc the text width
"00 kHz",
freq_factors,
0,
freq,
3.0,
(h - TPAD - BPAD) / (double)freq,
0.0,
freq_formatter
);
freq_ruler.draw(*dc);
}
}
// The palette.
if(h - TPAD - BPAD > 0) {
dc->drawImage(w - RPAD + GAP, TPAD, this->palette_image.scaled(RULER, h - TPAD - BPAD + 1));
int density_factors[] = {1, 2, 5, 10, 20, 50, 0};
SpekRuler density_ruler(
w - RPAD + GAP + RULER,
TPAD,
SpekRuler::RIGHT,
// TRANSLATORS: keep "-00" unchanged, it's used to calc the text width
"-00 dB",
density_factors,
-this->urange,
-this->lrange,
3.0,
(h - TPAD - BPAD) / (double)(this->lrange - this->urange),
h - TPAD - BPAD,
density_formatter
);
density_ruler.draw(*dc);
}
}
static void pipeline_cb(int bands, int sample, float *values, void *cb_data)
{
SpekSpectrogram *spek = static_cast<SpekSpectrogram*>(cb_data);
if (sample == -1) {
// spek->stop();
return;
}
// TODO: check image size, quit if wrong.
const double range = spek->getURange() - spek->getLRange();
for(int y = 0; y < bands; y++) {
const double value = fmin(spek->getURange(), fmax(spek->getLRange(), values[y]));
const double level = (value - spek->getLRange()) / range;
const uint32_t color = spek_palette(spek->getPalette(), level);
spek->getPaintImage()->setPixel(sample, bands - y - 1, color);
}
spek->update();
}
void SpekSpectrogram::start()
{
if(this->path.isEmpty()) {
return;
}
this->stop();
// The number of samples is the number of pixels available for the image.
// The number of bands is fixed, FFT results are very different for
// different values but we need some consistency.
int samples = width() - LPAD - RPAD;
if(samples > 0) {
this->image = QImage(samples, bits_to_bands(this->fft_bits), QImage::Format_RGB32);
this->pipeline = spek_pipeline_open(
this->audio->open(std::string(this->path.toUtf8().data()), this->stream),
this->fft->create(this->fft_bits),
this->stream,
this->channel,
this->window_function,
samples,
pipeline_cb,
this
);
spek_pipeline_start(this->pipeline);
// // TODO: extract conversion into a utility function.
this->streams = spek_pipeline_streams(this->pipeline);
this->channels = spek_pipeline_channels(this->pipeline);
this->duration = spek_pipeline_duration(this->pipeline);
this->sample_rate = spek_pipeline_sample_rate(this->pipeline);
}
}
void SpekSpectrogram::stop()
{
if(this->pipeline) {
spek_pipeline_close(this->pipeline);
this->pipeline = nullptr;
}
}
void SpekSpectrogram::create_palette()
{
this->palette_image = QImage(RULER, bits_to_bands(this->fft_bits), QImage::Format_RGB32);
for(int y = 0; y < bits_to_bands(this->fft_bits); y++) {
uint32_t color = spek_palette(this->palette, y / (double)bits_to_bands(this->fft_bits));
for(int j =0; j < RULER; ++j) {
this->palette_image.setPixel(
j,
bits_to_bands(this->fft_bits) - y - 1,
color
);
}
}
}
static QString trim(const QString& s, int length, bool trim_end)
{
if(length <= 0) {
return QString();
}
// Check if the entire string fits.
QFontMetrics f(QApplication::font());
int w = f.width(s);
if(w <= length) {
return s;
}
// Binary search FTW!
QString fix("...");
int i = 0;
int k = s.length();
while (k - i > 1) {
int j = (i + k) / 2;
w = f.width(trim_end ? s.mid(0, j) + fix : fix + s.mid(j));
if(trim_end != (w > length)) {
i = j;
} else {
k = j;
}
}
return trim_end ? s.mid(0, i) + fix : fix + s.mid(k);
}
static int bits_to_bands(int bits) {
return (1 << (bits - 1)) + 1;
}