forked from MUME/MMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicManager.cpp
More file actions
318 lines (284 loc) · 10.5 KB
/
Copy pathMusicManager.cpp
File metadata and controls
318 lines (284 loc) · 10.5 KB
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
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 The MMapper Authors
#include "MusicManager.h"
#include "../configuration/configuration.h"
#include "../global/ConfigConsts-Computed.h"
#include "MediaLibrary.h"
#ifndef MMAPPER_NO_AUDIO
#include <QAudioDevice>
#include <QAudioOutput>
#include <QMediaPlayer>
#include <QUrl>
#endif
#include <memory>
#include <QDir>
#include <QFileInfo>
#include <QTemporaryFile>
#include <QTimer>
MusicManager::MusicManager(MediaLibrary &library, QObject *const parent)
: QObject(parent)
, m_library(library)
{
#ifndef MMAPPER_NO_AUDIO
m_cachedPositions.setMaxCost(10);
m_wasmFiles.setMaxCost(3);
for (int i = 0; i < 2; ++i) {
m_channels[i].player = new QMediaPlayer(this);
m_channels[i].audioOutput = new QAudioOutput(this);
m_channels[i].player->setAudioOutput(m_channels[i].audioOutput);
m_channels[i].player->setLoops(QMediaPlayer::Infinite);
connect(m_channels[i].player,
&QMediaPlayer::mediaStatusChanged,
this,
[this, i](QMediaPlayer::MediaStatus status) {
if (status == QMediaPlayer::LoadedMedia || status == QMediaPlayer::BufferedMedia
|| status == QMediaPlayer::BufferingMedia) {
applyPendingPosition(i);
}
});
connect(m_channels[i].player, &QMediaPlayer::seekableChanged, this, [this, i](bool seekable) {
if (seekable) {
applyPendingPosition(i);
}
});
}
m_fadeStep = static_cast<float>(FADE_INTERVAL_MS) / CROSSFADE_DURATION_MS;
m_fadeTimer = new QTimer(this);
m_fadeTimer->setInterval(FADE_INTERVAL_MS);
connect(&m_library, &MediaLibrary::sig_mediaChanged, this, &MusicManager::slot_onMediaChanged);
connect(m_fadeTimer, &QTimer::timeout, this, [this]() {
bool changed = false;
for (int i = 0; i < 2; ++i) {
float target = 0.0f;
if (!m_fadingToSilence && i == m_activeChannel) {
target = 1.0f;
}
if (m_channels[i].fadeVolume < target) {
m_channels[i].fadeVolume = std::min(target, m_channels[i].fadeVolume + m_fadeStep);
changed = true;
} else if (m_channels[i].fadeVolume > target) {
m_channels[i].fadeVolume = std::max(target, m_channels[i].fadeVolume - m_fadeStep);
changed = true;
}
updateChannelVolume(i);
}
if (!changed) {
m_fadeTimer->stop();
int inactive = (m_activeChannel + 1) % 2;
if (m_channels[inactive].fadeVolume <= 0.0f) {
if (!m_channels[inactive].file.isEmpty()) {
if (m_channels[inactive].player->playbackState() == QMediaPlayer::PlayingState) {
m_cachedPositions.insert(m_channels[inactive].file,
new qint64(
m_channels[inactive].player->position()));
}
m_channels[inactive].player->stop();
m_channels[inactive].file.clear();
}
}
if (m_fadingToSilence && m_channels[m_activeChannel].fadeVolume <= 0.0f) {
if (m_channels[m_activeChannel].player->playbackState()
== QMediaPlayer::PlayingState) {
m_cachedPositions.insert(m_channels[m_activeChannel].file,
new qint64(
m_channels[m_activeChannel].player->position()));
}
m_channels[m_activeChannel].player->stop();
m_channels[m_activeChannel].file.clear();
}
}
});
#endif
}
MusicManager::~MusicManager()
{
shutdown();
}
void MusicManager::shutdown()
{
#ifndef MMAPPER_NO_AUDIO
if (m_fadeTimer) {
m_fadeTimer->stop();
}
for (int i = 0; i < 2; ++i) {
auto &player = deref(m_channels[i].player);
player.stop();
player.setSource(QUrl());
m_channels[i].file.clear();
}
#endif
}
void MusicManager::playMusic(MAYBE_UNUSED const QString &musicFile)
{
#ifndef MMAPPER_NO_AUDIO
if (musicFile.isEmpty() || NO_AUDIO) {
stopMusic();
return;
}
auto playMusic2 = [this, musicFile](const QUrl &url) {
const int ch = prepareChannel(musicFile);
if (ch < 0) {
return;
}
activateChannel(ch, musicFile, url);
};
if constexpr (CURRENT_PLATFORM == PlatformEnum::Wasm) {
if (QTemporaryFile *tempFile = m_wasmFiles.object(musicFile)) {
playMusic2(QUrl::fromLocalFile(tempFile->fileName()));
return;
}
m_library.fetchAsync(musicFile, [this, musicFile, playMusic2](const QByteArray &data) {
if (data.isEmpty()) {
qWarning() << "MusicManager: received empty data for" << musicFile
<< "- skipping playback";
return;
}
auto tempFile = std::make_unique<QTemporaryFile>(QDir::tempPath() + "/mmapper_XXXXXX."
+ QFileInfo(musicFile).suffix());
if (!tempFile->open()) {
qWarning() << "MusicManager: failed to create temp file for" << musicFile;
return;
}
const qint64 bytesWritten = tempFile->write(data);
if (bytesWritten != static_cast<qint64>(data.size())) {
qWarning() << "MusicManager: failed to write temp file for" << musicFile
<< "- expected" << data.size() << "bytes, wrote" << bytesWritten;
return;
}
tempFile->close();
const QString fileName = tempFile->fileName();
m_wasmFiles.insert(musicFile, tempFile.release());
playMusic2(QUrl::fromLocalFile(fileName));
});
return;
}
playMusic2(musicFile.startsWith(":/") ? QUrl(QStringLiteral("qrc") + musicFile)
: QUrl::fromLocalFile(musicFile));
#endif
}
#ifndef MMAPPER_NO_AUDIO
int MusicManager::prepareChannel(const QString &musicFile)
{
if (musicFile == m_channels[m_activeChannel].file) {
if constexpr (CURRENT_PLATFORM == PlatformEnum::Wasm) {
// Browser does not let music loop on Wasm, so we have to restart it manually with user input
if (m_channels[m_activeChannel].player->playbackState() != QMediaPlayer::PlayingState) {
const auto &cfg = getConfig().audio;
if (cfg.isUnlocked() && cfg.getMusicVolume() > 0) {
m_channels[m_activeChannel].player->setPosition(0);
m_channels[m_activeChannel].player->play();
}
return -1;
}
}
startFade(false);
return -1;
}
const int inactiveChannel = (m_activeChannel + 1) % 2;
if (musicFile == m_channels[inactiveChannel].file) {
m_activeChannel = inactiveChannel;
startFade(false);
return -1;
}
// Save position of the current track before crossfading away from it
if (!m_channels[m_activeChannel].file.isEmpty()
&& m_channels[m_activeChannel].player->playbackState() == QMediaPlayer::PlayingState) {
m_cachedPositions.insert(m_channels[m_activeChannel].file,
new qint64(m_channels[m_activeChannel].player->position()));
}
return inactiveChannel;
}
void MusicManager::activateChannel(const int channelIndex,
const QString &musicFile,
const QUrl &source)
{
m_activeChannel = channelIndex;
auto &ch = m_channels[m_activeChannel];
ch.file = musicFile;
ch.fadeVolume = 0.0f;
if (qint64 *pos = m_cachedPositions.object(musicFile)) {
ch.pendingPosition = *pos;
} else {
ch.pendingPosition = -1;
}
ch.player->setSource(source);
const auto &cfg = getConfig().audio;
if (cfg.isUnlocked() && cfg.getMusicVolume() > 0) {
ch.player->play();
applyPendingPosition(m_activeChannel);
}
startFade(false);
}
#endif
void MusicManager::stopMusic()
{
#ifndef MMAPPER_NO_AUDIO
startFade(true);
#endif
}
void MusicManager::updateVolumes()
{
#ifndef MMAPPER_NO_AUDIO
for (int i = 0; i < 2; ++i) {
updateChannelVolume(i);
}
auto &cfg = getConfig().audio;
float masterVol = static_cast<float>(cfg.getMusicVolume()) / 100.0f;
bool canPlay = cfg.isUnlocked() && masterVol > 0;
auto &active = m_channels[m_activeChannel];
if (canPlay && active.player->playbackState() == QMediaPlayer::StoppedState
&& !active.file.isEmpty()) {
if (qint64 *pos = m_cachedPositions.object(active.file)) {
active.pendingPosition = *pos;
}
active.player->play();
applyPendingPosition(m_activeChannel);
} else if (masterVol <= 0 && active.player->playbackState() == QMediaPlayer::PlayingState) {
if (!active.file.isEmpty()) {
m_cachedPositions.insert(active.file, new qint64(active.player->position()));
}
active.player->stop();
}
#endif
}
void MusicManager::slot_onMediaChanged()
{
#ifndef MMAPPER_NO_AUDIO
m_cachedPositions.clear();
QString currentFile = m_channels[m_activeChannel].file;
if (!currentFile.isEmpty()) {
m_channels[m_activeChannel].file.clear();
playMusic(currentFile);
}
#endif
}
#ifndef MMAPPER_NO_AUDIO
void MusicManager::updateOutputDevice(const QAudioDevice &device)
{
for (int i = 0; i < 2; ++i) {
if (m_channels[i].audioOutput->device() != device) {
m_channels[i].audioOutput->setDevice(device);
}
}
}
void MusicManager::applyPendingPosition(int channelIndex)
{
auto &ch = m_channels[channelIndex];
if (ch.pendingPosition != -1 && ch.player->isSeekable()) {
ch.player->setPosition(ch.pendingPosition);
ch.pendingPosition = -1;
}
}
void MusicManager::updateChannelVolume(int channelIndex)
{
float masterVol = static_cast<float>(getConfig().audio.getMusicVolume()) / 100.0f;
m_channels[channelIndex].audioOutput->setVolume(masterVol * m_channels[channelIndex].fadeVolume);
}
void MusicManager::startFade(bool toSilence)
{
m_fadingToSilence = toSilence;
if (m_fadeTimer && !m_fadeTimer->isActive()) {
m_fadeTimer->start();
}
}
#endif