-
Notifications
You must be signed in to change notification settings - Fork 12
/
audiosourcefile.cpp
246 lines (210 loc) · 6.6 KB
/
audiosourcefile.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
#include <QTime>
#include <QFileDialog>
#include "audiosourcefile.h"
#include "util.h"
//#define DISPLAY_BUFFERING
AudioSourceFile::AudioSourceFile(QObject *parent, PlaylistModel *playlistModel)
: AudioSource{parent}
{
m_player = new MediaPlayer(this);
connect(m_player, &MediaPlayer::durationChanged, this, &AudioSourceFile::durationChanged);
connect(m_player, &MediaPlayer::positionChanged, this, &AudioSourceFile::positionChanged);
connect(m_player, QOverload<>::of(&MediaPlayer::metaDataChanged), this,
&AudioSourceFile::handleMetaDataChanged);
connect(m_player, &MediaPlayer::mediaStatusChanged, this, &AudioSourceFile::handleMediaStatusChanged);
connect(m_player, &MediaPlayer::bufferProgressChanged, this, &AudioSourceFile::handleBufferingProgress);
connect(m_player, &MediaPlayer::errorChanged, this, &AudioSourceFile::handleMediaError);
m_playlistModel = playlistModel;
m_playlist = m_playlistModel->playlist();
connect(m_playlist, &QMediaPlaylist::currentIndexChanged, this,
&AudioSourceFile::handlePlaylistPositionChanged);
connect(m_playlist, &QMediaPlaylist::mediaAboutToBeRemoved, this,
&AudioSourceFile::handlePlaylistMediaRemoved);
connect(m_player, &MediaPlayer::playbackStateChanged, this, &AudioSourceFile::playbackStateChanged);
// Emit data for spectrum analyzer
connect(m_player, &MediaPlayer::newData, this, &AudioSourceFile::handleSpectrumData);
}
void AudioSourceFile::activate()
{
emit playbackStateChanged(m_player->playbackState());
emit positionChanged(m_player->position());
emit metadataChanged(m_player->metaData());
emit durationChanged(m_player->duration());
emit eqEnabledChanged(false);
emit plEnabledChanged(true);
emit shuffleEnabledChanged(shuffleEnabled);
emit repeatEnabledChanged(repeatEnabled);
}
void AudioSourceFile::deactivate()
{
// Stop everything
shouldBePlaying = false;
m_player->stop();
}
void AudioSourceFile::handlePl()
{
emit showPlaylistRequested();
emit plEnabledChanged(true);
}
void AudioSourceFile::handlePrevious()
{
// Go to previous track if we are within the first 5 seconds of playback
// Otherwise, seek to the beginning.
if (m_player->position() <= 5000) {
// If is first item in playlist, do nothing
// That's handled by QMediaPlaylist
m_playlist->previous();
} else {
m_player->setPosition(0);
}
}
void AudioSourceFile::handlePlay()
{
shouldBePlaying = true;
m_player->play();
}
void AudioSourceFile::handlePause()
{
shouldBePlaying = false;
m_player->pause();
}
void AudioSourceFile::handleStop()
{
shouldBePlaying = false;
m_player->stop();
}
void AudioSourceFile::handleNext()
{
// If is last item in playlist:
// If repeat enabled, go to first
// If not, do nothing
// That's handled by QMediaPlaylist
m_playlist->next();
}
void AudioSourceFile::handleOpen()
{
emit showPlaylistRequested();
}
void AudioSourceFile::handleShuffle()
{
shuffleEnabled = !shuffleEnabled;
m_playlist->setShuffle(shuffleEnabled);
emit shuffleEnabledChanged(shuffleEnabled);
}
void AudioSourceFile::handleRepeat()
{
repeatEnabled = !repeatEnabled;
QMediaPlaylist::PlaybackMode mode = QMediaPlaylist::PlaybackMode::Sequential;
if(repeatEnabled) mode = QMediaPlaylist::PlaybackMode::Loop;
m_playlist->setPlaybackMode(mode);
emit repeatEnabledChanged(repeatEnabled);
}
void AudioSourceFile::handleSeek(int mseconds)
{
m_player->setPosition(mseconds);
}
void AudioSourceFile::handleMetaDataChanged()
{
auto metaData = m_player->metaData();
emit metadataChanged(metaData);
}
void AudioSourceFile::handleMediaStatusChanged(MediaPlayer::MediaStatus status)
{
// handle status message
switch (status) {
case MediaPlayer::NoMedia:
case MediaPlayer::LoadedMedia:
setStatusInfo(QString());
break;
case MediaPlayer::LoadingMedia:
setStatusInfo(tr("Loading..."));
break;
case MediaPlayer::BufferingMedia:
case MediaPlayer::BufferedMedia:
#ifdef DISPLAY_BUFFERING
setStatusInfo(tr("Buffering %1%").arg(qRound(m_player->bufferProgress())));
#endif
break;
case MediaPlayer::StalledMedia:
setStatusInfo(tr("Stalled %1%").arg(qRound(m_player->bufferProgress())));
break;
case MediaPlayer::EndOfMedia:
handleNext();
break;
case MediaPlayer::InvalidMedia:
handleMediaError();
break;
}
}
void AudioSourceFile::handleBufferingProgress(float progress)
{
if (m_player->mediaStatus() == MediaPlayer::StalledMedia) {
setStatusInfo(tr("Stalled %1%").arg(qRound(progress)));
}
else {
#ifdef DISPLAY_BUFFERING
setStatusInfo(tr("Buffering %1%").arg(qRound(progress)));
#endif
}
}
void AudioSourceFile::handleMediaError()
{
if (m_player->error() == MediaPlayer::NoError)
return;
setStatusInfo(m_player->errorString());
qDebug() << m_player->errorString();
}
void AudioSourceFile::setStatusInfo(const QString &info)
{
m_statusInfo = info;
if (!m_statusInfo.isEmpty())
emit messageSet(QString("%1").arg(m_statusInfo), 1000);
else
emit messageClear();
}
void AudioSourceFile::handlePlaylistPositionChanged(int)
{
m_player->setSource(m_playlist->currentQueueMedia());
if (shouldBePlaying) {
m_player->play();
}
}
void AudioSourceFile::handlePlaylistMediaRemoved(int from, int to)
{
// Stop only if currently playing file was removed
int playingIndex = m_playlist->currentIndex();
if(playingIndex >= from && playingIndex <= to) {
shouldBePlaying = false;
m_player->stop();
m_player->clearSource();
}
}
void AudioSourceFile::jump(const QModelIndex &index)
{
if (index.isValid()) {
m_playlist->setCurrentIndex(index.row());
shouldBePlaying = true;
m_player->play();
}
}
void AudioSourceFile::addToPlaylist(const QList<QUrl> &urls)
{
const int previousMediaCount = m_playlist->mediaCount();
for (auto &url : urls) {
if (isPlaylist(url))
m_playlist->load(url);
else
m_playlist->addMedia(url);
}
if (m_playlist->mediaCount() > previousMediaCount) {
// Start playing only if not already playing
if(m_player->playbackState() == MediaPlayer::PlaybackState::StoppedState) {
auto index = m_playlistModel->index(previousMediaCount, 0);
jump(index);
}
}
}
void AudioSourceFile::handleSpectrumData(const QByteArray& data)
{
emit dataEmitted(data, m_player->format());
}