-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
projectmvisualisation.cpp
422 lines (335 loc) · 11.3 KB
/
projectmvisualisation.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
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <string>
#include <memory>
#ifdef HAVE_PROJECTM4
# include <projectM-4/types.h>
# include <projectM-4/core.h>
# include <projectM-4/parameters.h>
# include <projectM-4/playlist_types.h>
# include <projectM-4/playlist_core.h>
# include <projectM-4/playlist_items.h>
# include <projectM-4/render_opengl.h>
# include <projectM-4/audio.h>
#else
# include <libprojectM/projectM.hpp>
#endif
#include <QCoreApplication>
#include <QGraphicsScene>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QFileInfo>
#include <QPainter>
#include <QRandomGenerator>
#include <QMessageBox>
#include <QTimerEvent>
#include "core/logging.h"
#include "core/settings.h"
#include "projectmvisualisation.h"
#include "projectmpresetmodel.h"
#include "visualisationcontainer.h"
ProjectMVisualisation::ProjectMVisualisation(VisualisationContainer *container)
: QGraphicsScene(container),
container_(container),
preset_model_(nullptr),
#ifdef HAVE_PROJECTM4
projectm_instance_(nullptr),
projectm_playlist_instance_(nullptr),
#endif
mode_(Mode::Random),
duration_(15),
texture_size_(512),
pixel_ratio_(container->devicePixelRatio()) {
QObject::connect(this, &QGraphicsScene::sceneRectChanged, this, &ProjectMVisualisation::SceneRectChanged);
#ifndef HAVE_PROJECTM4
for (int i = 0; i < TOTAL_RATING_TYPES; ++i) {
default_rating_list_.push_back(3);
}
#endif
}
ProjectMVisualisation::~ProjectMVisualisation() {
#ifdef HAVE_PROJECTM4
if (projectm_playlist_instance_) {
projectm_playlist_destroy(projectm_playlist_instance_);
}
if (projectm_instance_) {
projectm_destroy(projectm_instance_);
}
#endif
}
void ProjectMVisualisation::InitProjectM() {
// Find the projectM presets
QStringList data_paths = QStringList() << QStringLiteral("/usr/share")
<< QStringLiteral("/usr/local/share")
<< QLatin1String(CMAKE_INSTALL_PREFIX) + QLatin1String("/share");
const QStringList xdg_data_dirs = QString::fromUtf8(qgetenv("XDG_DATA_DIRS")).split(QLatin1Char(':'));
for (const QString &xdg_data_dir : xdg_data_dirs) {
if (!data_paths.contains(xdg_data_dir)) {
data_paths.append(xdg_data_dir);
}
}
#if defined(Q_OS_WIN32)
data_paths.prepend(QCoreApplication::applicationDirPath());
#endif
const QStringList projectm_paths = QStringList() << QStringLiteral("projectM/presets")
<< QStringLiteral("projectm-presets");
QStringList preset_paths;
QString preset_path;
for (const QString &data_path : std::as_const(data_paths)) {
for (const QString &projectm_path : projectm_paths) {
const QString path = data_path + QLatin1Char('/') + projectm_path;
if (!QFileInfo::exists(path) || QDir(path).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot).isEmpty()) {
preset_paths << path;
continue;
}
preset_path = path;
break;
}
}
// Create projectM settings
#ifdef HAVE_PROJECTM4
Q_ASSERT(projectm_instance_ == nullptr);
Q_ASSERT(projectm_playlist_instance_ == nullptr);
projectm_instance_ = projectm_create();
projectm_set_preset_duration(projectm_instance_, duration_);
projectm_set_mesh_size(projectm_instance_, 32, 24);
projectm_set_fps(projectm_instance_, 35);
projectm_set_window_size(projectm_instance_, 512, 512);
projectm_playlist_instance_ = projectm_playlist_create(projectm_instance_);
#else
projectM::Settings s;
s.presetURL = preset_path.toStdString();
s.meshX = 32;
s.meshY = 24;
s.textureSize = texture_size_;
s.fps = 35;
s.windowWidth = 512;
s.windowHeight = 512;
s.smoothPresetDuration = 5;
s.presetDuration = duration_;
s.shuffleEnabled = true;
s.softCutRatingsEnabled = false;
s.easterEgg = 0;
projectm_ = std::make_unique<projectM>(s);
#endif
Q_ASSERT(preset_model_ == nullptr);
preset_model_ = new ProjectMPresetModel(this, this);
Load();
// Start at a random preset.
#ifdef HAVE_PROJECTM4
const QByteArray _preset_path = preset_path.toUtf8();
projectm_load_preset_data(projectm_instance_, _preset_path.constData(), true);
#else
if (projectm_->getPlaylistSize() > 0) {
const int selection = QRandomGenerator::global()->bounded(projectm_->getPlaylistSize());
projectm_->selectPreset(selection, true);
}
#endif
if (preset_path.isEmpty()) {
qWarning("ProjectM presets could not be found, search path was:\n %s", preset_paths.join(QLatin1String("\n ")).toLocal8Bit().constData());
QMessageBox::warning(nullptr, tr("Missing projectM presets"), tr("Strawberry could not load any projectM visualisations. Check that you have installed Clementine properly."));
}
}
void ProjectMVisualisation::drawBackground(QPainter *p, const QRectF &rect) {
Q_UNUSED(rect);
p->beginNativePainting();
#ifdef HAVE_PROJECTM4
if (!projectm_instance_) {
#else
if (!projectm_) {
#endif
InitProjectM();
}
#ifdef HAVE_PROJECTM4
projectm_opengl_render_frame(projectm_instance_);
#else
projectm_->projectM_resetGL(sceneRect().width() * pixel_ratio_, sceneRect().height() * pixel_ratio_);
projectm_->renderFrame();
#endif
p->endNativePainting();
}
void ProjectMVisualisation::SceneRectChanged(const QRectF &rect) {
// NOTE: This should be updated on a QScreen dpi change signal.
// Accessing the QScreen becomes a lot easier in Qt 5.14 with QWidget::screen().
pixel_ratio_ = container_->devicePixelRatio();
#ifndef HAVE_PROJECTM4
if (projectm_) {
projectm_->projectM_resetGL(rect.width() * pixel_ratio_, rect.height() * pixel_ratio_);
}
#endif
}
void ProjectMVisualisation::SetTextureSize(const int size) {
texture_size_ = size;
#ifndef HAVE_PROJECTM4
if (projectm_) projectm_->changeTextureSize(texture_size_);
#endif
}
void ProjectMVisualisation::SetDuration(const int seconds) {
duration_ = seconds;
#ifndef HAVE_PROJECTM4
if (projectm_) projectm_->changePresetDuration(duration_);
#endif
Save();
}
void ProjectMVisualisation::ConsumeBuffer(GstBuffer *buffer, const int pipeline_id, const QString &format) {
Q_UNUSED(pipeline_id);
Q_UNUSED(format);
GstMapInfo map;
gst_buffer_map(buffer, &map, GST_MAP_READ);
#ifdef HAVE_PROJECTM4
if (projectm_instance_) {
const unsigned int samples_per_channel = static_cast<short>(map.size) / sizeof(short) / 2;
const int16_t *data = reinterpret_cast<short*>(map.data);
projectm_pcm_add_int16(projectm_instance_, data, samples_per_channel, PROJECTM_STEREO);
}
#else
if (projectm_) {
const short samples_per_channel = static_cast<short>(map.size) / sizeof(short) / 2;
const short *data = reinterpret_cast<short*>(map.data);
projectm_->pcm()->addPCM16Data(data, samples_per_channel);
}
#endif
gst_buffer_unmap(buffer, &map);
gst_buffer_unref(buffer);
}
void ProjectMVisualisation::SetSelected(const QStringList &paths, bool selected) {
for (const QString &path : paths) {
const int index = IndexOfPreset(path);
if (selected && index == -1) {
#ifdef HAVE_PROJECTM4
projectm_playlist_add_preset(projectm_playlist_instance_, path.toUtf8().constData(), true);
#else
projectm_->addPresetURL(path.toStdString(), std::string(), default_rating_list_);
#endif
}
else if (!selected && index != -1) {
#ifdef HAVE_PROJECTM4
projectm_playlist_remove_preset(projectm_playlist_instance_, index);
#else
projectm_->removePreset(index);
#endif
}
}
Save();
}
void ProjectMVisualisation::ClearSelected() {
#ifdef HAVE_PROJECTM4
projectm_playlist_clear(projectm_playlist_instance_);
#else
projectm_->clearPlaylist();
#endif
Save();
}
int ProjectMVisualisation::IndexOfPreset(const QString &path) const {
#ifdef HAVE_PROJECTM4
char **playlist_items = projectm_playlist_items(projectm_playlist_instance_, 0, 1000);
for (int i = 0; playlist_items[i] != nullptr; ++i) {
char *playlist_item = projectm_playlist_item(projectm_playlist_instance_, i);
if (playlist_item == path.toUtf8().constData()) {
return i;
}
}
#else
for (uint i = 0; i < projectm_->getPlaylistSize(); ++i) {
if (QString::fromStdString(projectm_->getPresetURL(i)) == path) return static_cast<int>(i);
}
#endif
return -1;
}
void ProjectMVisualisation::Load() {
Settings s;
s.beginGroup(QLatin1String(VisualisationContainer::kSettingsGroup));
mode_ = Mode(s.value("mode", 0).toInt());
duration_ = s.value("duration", duration_).toInt();
s.endGroup();
#ifdef HAVE_PROJECTM4
projectm_playlist_clear(projectm_playlist_instance_);
#else
projectm_->changePresetDuration(duration_);
projectm_->clearPlaylist();
#endif
switch (mode_) {
case Mode::Random:{
for (int i = 0; i < preset_model_->all_presets_.count(); ++i) {
#ifdef HAVE_PROJECTM4
projectm_playlist_add_preset(projectm_playlist_instance_, preset_model_->all_presets_[i].path_.toUtf8().constData(), true);
#else
projectm_->addPresetURL(preset_model_->all_presets_[i].path_.toStdString(), std::string(), default_rating_list_);
#endif
preset_model_->all_presets_[i].selected_ = true;
}
break;
}
case Mode::FromList:{
const QStringList paths = s.value("preset_paths").toStringList();
for (const QString &path : paths) {
#ifdef HAVE_PROJECTM4
projectm_playlist_add_preset(projectm_playlist_instance_, path.toUtf8().constData(), true);
#else
projectm_->addPresetURL(path.toStdString(), std::string(), default_rating_list_);
#endif
preset_model_->MarkSelected(path, true);
}
}
}
}
void ProjectMVisualisation::Save() {
QStringList paths;
for (const ProjectMPresetModel::Preset &preset : preset_model_->all_presets_) {
if (preset.selected_) paths << preset.path_;
}
Settings s;
s.beginGroup(VisualisationContainer::kSettingsGroup);
s.setValue("preset_paths", paths);
s.setValue("mode", static_cast<int>(mode_));
s.setValue("duration", duration_);
s.endGroup();
}
void ProjectMVisualisation::SetMode(const Mode mode) {
mode_ = mode;
Save();
}
QString ProjectMVisualisation::preset_url() const {
#ifdef HAVE_PROJECTM4
return QString();
#else
return QString::fromStdString(projectm_->settings().presetURL);
#endif
}
void ProjectMVisualisation::SetImmediatePreset(const QString &path) {
int index = IndexOfPreset(path);
if (index == -1) {
#ifndef HAVE_PROJECTM4
index = projectm_->addPresetURL(path.toStdString(), std::string(), default_rating_list_);
#endif
}
#ifndef HAVE_PROJECTM4
projectm_->selectPreset(index, true);
#endif
}
void ProjectMVisualisation::Lock(const bool lock) {
#ifndef HAVE_PROJECTM4
projectm_->setPresetLock(lock);
#endif
if (!lock) Load();
}