-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlistitem.cpp
462 lines (393 loc) · 10.9 KB
/
playlistitem.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "playlistitem.h"
#include <taglib/fileref.h>
#include <taglib/tstring.h>
#include <taglib/tag.h>
#include "arfariusapplication.h"
#include "avexception.h"
#include "avsplitter.h"
#include "avhistogram.h"
#include "avspectrum.h"
#include "avfile.h"
#include <QCryptographicHash>
#include <QStandardPaths>
#include <QStorageInfo>
#include <QtConcurrent>
#include <QDataStream>
#include <QTextCodec>
#include <QFileInfo>
#include <QPainter>
#include <QImage>
#include <QDebug>
#include <QFile>
/*
* Helpers
*/
QString formatTime(int time)
{
int h,m,s;
s = time % 60;
m = time / 60 % 60;
h = time / 60 / 60;
if (h > 0)
return QString("%1:%2:%3").arg(h, 2).arg(m, 2, 10, QChar('0')).arg(s, 2, 10, QChar('0'));
else
return QString("%1:%2").arg(m, 2).arg(s, 2, 10, QChar('0'));
}
QString toQstring(TagLib::String str) {
if (str.isLatin1()) {
QTextCodec* codec = QTextCodec::codecForName("cp1251");
return codec->toUnicode(str.toCString());
} else {
return QString::fromUtf8(str.toCString(true));
}
}
TagLib::String toString(QString str) {
return TagLib::String(str.toStdString(), TagLib::String::UTF8);
}
/*
* PlayListItem
*/
PlayListItem::PlayListItem(QUrl s) :
QObject(), source(s), artist(), title(), album(), duration(-1), busy(false)
{
}
PlayListItem::~PlayListItem()
{
}
bool PlayListItem::isBusy() {
return busy;
}
bool PlayListItem::isValid()
{
try {
AVFile f;
f.open(getUrlString().toLocal8Bit().constData());
duration = f.getDurationInSeconds();
} catch (AVException &e) {
qDebug() << "PlayListItem::isValid() says NOO to "<< source << "because:" << e.what();
return false;
}
if (source.isLocalFile()) {
readTags();
}
return true;
}
bool PlayListItem::isLocalFile()
{
return source.isLocalFile();
}
QUrl PlayListItem::getUrl() {
return source;
}
void PlayListItem::setUrl(QUrl url) {
source = url;
}
QString PlayListItem::getUrlHash()
{
return QCryptographicHash::hash(getUrlStringLocal().toLocal8Bit(), QCryptographicHash::Sha3_512).toHex();
}
QString PlayListItem::getUrlString() {
QString url;
if (source.isLocalFile()) {
QStorageInfo storage_info(source.toLocalFile());
auto fs_type = storage_info.fileSystemType();
qDebug() << this << "getUrlString(): underlying fs is" << fs_type;
if (fs_type == "smbfs") {
qDebug() << this << "getUrlString(): using async io and caching";
url = QString("async:cache:%1").arg(source.toLocalFile());
} else {
url = source.toLocalFile();
}
} else {
url = source.toString();
}
return url;
}
QString PlayListItem::getUrlStringLocal() {
return source.toLocalFile();
}
int PlayListItem::getColumnsCount()
{
return 4;
}
QString PlayListItem::getColumnName(int col)
{
switch (col) {
case 0:
return "Artist";
case 1:
return "Title";
case 2:
return "Album";
case 3:
return "Time";
default:
return "UNKNOWN";
}
}
QString PlayListItem::getColumn(int col)
{
if (artist.isEmpty() && title.isEmpty()) {
}
switch (col) {
case 0:
return getArtist();
case 1:
return getTitle();
case 2:
return getAlbum();
case 3:
return getFormattedDuration();
default:
return "UNKNOWN";
}
}
QString PlayListItem::getArtist()
{
return artist;
}
QString PlayListItem::getTitle()
{
if (source.isLocalFile() ) {
if (artist.isEmpty() && title.isEmpty() && album.isEmpty()) {
QFileInfo fileInf(source.toLocalFile());
return fileInf.completeBaseName();;
} else {
return title;
}
} else {
return source.toString();
}
}
QString PlayListItem::getAlbum()
{
return album;
}
QString PlayListItem::getFormattedDuration()
{
if (source.isLocalFile()) {
return formatTime(duration);
} else {
return "??:??";
}
}
void PlayListItem::setColumn(int col, QString value)
{
bool w = true;
switch (col) {
case 0:
artist = value;
break;
case 1:
title = value;
break;
case 2:
album = value;
break;
default:
w = false;
break;
}
if (w)
writeTags();
}
bool PlayListItem::hasHistogram()
{
QFile data_file(getHistogramDataPath());
if (data_file.size())
return true;
return false;
}
void PlayListItem::ensureHistogram()
{
if (!hasHistogram()) {
busy = true;
QtConcurrent::run(
arfariusApp->getAnalyzeThreadPool(),
[this]{ analyze(); busy = false; }
);
}
}
QString PlayListItem::getHistogramDataPath()
{
return QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/histogram/" + getUrlHash();
}
QImage * PlayListItem::getHistogrammImage(size_t width, size_t height)
{
qDebug() << this << "generating histogram image";
if (!isLocalFile())
return nullptr;
QFile data_file(getHistogramDataPath());
if (!data_file.open(QIODevice::ReadOnly)) {
qDebug() << this << "Unable to open histogram data file";
return nullptr;
}
qDebug() << this << "Requested image Width:" << width << "Height" << height;
size_t blocks = data_file.size() / 7 / sizeof(float);
if (!blocks) {
return nullptr;
} else if (blocks < width) {
width = blocks;
} else {
size_t t_blocks = blocks;
while (t_blocks > width*2) {
t_blocks /= 2;
}
width = t_blocks;
}
qDebug() << this << "Final image Width:" << width << "Height" << height;
size_t blocks_per_pixel = blocks / width;
QDataStream data(&data_file);
data.setFloatingPointPrecision(QDataStream::SinglePrecision);
QImage *pic = new QImage(width, height, QImage::Format_ARGB32);
pic->fill(QColor(0,0,0,0));
QPainter painter(pic);
size_t x=0, p=0;
float hh = height/2;
float pos_peak, neg_peak, pos_rms, neg_rms, r, g, b;
float pos_peak_avg=0, neg_peak_avg=0, pos_rms_avg=0, neg_rms_avg=0, r_avg=0, g_avg=0, b_avg=0;
while (true) {
if (data.atEnd()) {
qDebug() << this << "Unexpected end" << data_file.pos() << p << blocks_per_pixel;
break;
}
data >> pos_peak >> neg_peak >> pos_rms >> neg_rms;
data >> r >> g >> b;
// Gain
r = r*0.5;
b = b*5.0;
// find maximum
float maximum = r;
if (g > maximum) {
maximum = g;
}
if (b > maximum) {
maximum = b;
}
// Rescaling
if (maximum) {
r = r / maximum * 180 + 30;
g = g / maximum * 180 + 30;
b = b / maximum * 180 + 30;
} else {
r = g = b = 0;
}
// Summ data
pos_peak_avg += pos_peak; neg_peak_avg += neg_peak;
pos_rms_avg += pos_rms; neg_rms_avg += neg_rms;
r_avg += r; g_avg += g; b_avg += b;
// Commit data to image
if (++p == blocks_per_pixel) {
pos_peak_avg /= blocks_per_pixel; neg_peak_avg /= blocks_per_pixel;
pos_rms_avg /= blocks_per_pixel; neg_rms_avg /= blocks_per_pixel;
r_avg /= blocks_per_pixel; g_avg /= blocks_per_pixel; b_avg /= blocks_per_pixel;
// Peaks
painter.setPen(QColor(r_avg/2, g_avg/2, b_avg/2));
painter.drawLine(
x, hh + pos_peak_avg * hh,
x, hh - neg_peak_avg * hh
);
// RMS
painter.setPen(QColor(r_avg, g_avg, b_avg));
painter.drawLine(
x, hh + pos_rms_avg * hh,
x, hh - neg_rms_avg * hh
);
// Break the circle
if (++x == width)
break;
// zero
p = 0;
pos_peak_avg = neg_peak_avg = pos_rms_avg = neg_rms_avg = r_avg = g_avg = b_avg = 0;
}
}
return pic;
}
void PlayListItem::readTags()
{
qDebug() << this << "reading tags";
TagLib::FileRef f(getUrlStringLocal().toLocal8Bit().constData());
TagLib::Tag *t = f.tag();
if (t && !t->isEmpty() && (!t->artist().isEmpty() && !t->title().isEmpty())) {
artist = toQstring(t->artist());
title = toQstring(t->title());
album = toQstring(t->album());
}
}
void PlayListItem::writeTags()
{
qDebug() << this << "writing tags";
TagLib::FileRef f(getUrlStringLocal().toLocal8Bit().constData());
if (!f.isNull()) {
TagLib::Tag *t = f.tag();
t->setArtist(toString(artist));
t->setTitle(toString(title));
t->setAlbum(toString(album));
f.save();
} else {
qDebug() << this << "can not write tags into" << getUrlStringLocal();
}
}
void PlayListItem::analyze()
{
qDebug() << this << "analyzing";
try {
if (!isLocalFile())
return;
QFile data_file(getHistogramDataPath());
if (!data_file.open(QIODevice::WriteOnly)) {
qDebug() << this << "Unable to open histogram data file for write:" << getHistogramDataPath();
return;
}
QDataStream data(&data_file);
data.setFloatingPointPrecision(QDataStream::SinglePrecision);
size_t h_count=0, s_count=0;
AVFile file;
AVSplitter splitter;
AVHistogram histogram(8192);
AVSpectrum spectrum(8192, AVSpectrum::BlackmanHarris);
histogram.dataCallback = [&](float p_p, float n_p, float p_r, float n_r) {
data << p_p << n_p << p_r << n_r;
h_count++;
};
spectrum.dataCallback = [&](float r, float g, float b) {
data << r << g << b;
s_count++;
};
file.connectOutput(&splitter);
splitter.connectOutput(&histogram);
splitter.connectOutput(&spectrum);
file.setChannels(1);
file.setSamplerate(22050);
file.open(getUrlString().toLocal8Bit().constData());
file.decode();
Q_ASSERT(h_count == s_count);
data_file.close();
qDebug() << this << "histogram ready";
} catch (AVException e) {
qDebug() << this << "failed to create histogram" << e.what();
}
emit histogramUpdated();
}
AVFile * PlayListItem::getAVFile()
{
AVFile *file = nullptr;
try {
file = new AVFile;
file->open(getUrlString().toLocal8Bit().constData());
} catch(AVException e) {
qDebug() << this << "Failed to open file because of" << e.what();
delete file; file = nullptr;
}
return file;
}
bool PlayListItem::ensurePath()
{
QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/histogram/";
QDir dir(path);
if (!dir.exists()) {
qDebug() << "Creating histogram storage" << path;
QDir::root().mkpath(path);
}
return true;
}