-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.cpp
More file actions
324 lines (262 loc) · 6.55 KB
/
player.cpp
File metadata and controls
324 lines (262 loc) · 6.55 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
319
320
321
322
323
324
#include "player.h"
bool endOfPlayback;
bool pauseAllowed;
Player* Player::player = NULL;
Player::Player(QObject *parent) :
QObject(parent)
{
if (!BASS_Init(-1, 44100, 0, NULL, NULL))
qDebug() << "Cannot initialize device";
t = new QTimer(this);
t->start(100);
connect(t, SIGNAL(timeout()), this, SLOT(signalUpdate()));
connect(t, SIGNAL(timeout()), this, SLOT(checkEndPlayback()));
//endOfMusic = true;
isPlaying = false;
isShuffling = false;
isChangingSong = false;
isLocked = false;
endOfPlayback = false;
pauseAllowed = false;
isRepeating = false;
volume = 1.0;
playlist = NULL;
emit(posChanged(0));
}
Player::~Player()
{
if (t != NULL)
delete t;
}
void Player::changeToPlaylist(Playlist *_playlist, bool playFirstSong, bool masterReset)
{
if (!masterReset)
removeCurrentPlaylist();
playlist = _playlist;
connect(playlist, SIGNAL(changeCurrentSong(int)), this, SLOT(changeToSong(int)));
connect(playlist, SIGNAL(nextPlaylist()), this, SIGNAL(nextPlaylist()));
connect(playlist, SIGNAL(prevPlaylist()), this, SIGNAL(prevPlaylist()));
if (playFirstSong)
playlist->playFirstSong(isShuffling);
else
playlist->playLastSong(isShuffling);
}
void Player::changeToSong(int songNum)
{
isChangingSong = true;
QString filename;
filename = Manager::master.Get(songNum);
BASS_ChannelRemoveSync(channel, BASS_SYNC_END);
BASS_ChannelStop(channel);
BASS_StreamFree(channel);
channel = BASS_StreamCreateFile(false, reinterpret_cast<const wchar_t*>(filename.constData()), 0, 0, NULL);
BASS_ChannelSetAttribute(channel, BASS_ATTRIB_VOL, volume);
BASS_ChannelSetSync(channel, BASS_SYNC_END, 0, &EndOfPlayback, 0);
QString title, artist, album;
TagLib::MPEG::File f( reinterpret_cast<const wchar_t*>(filename.constData()) );
if (!f.isValid())
return;
title = TStringToQString(f.tag()->title());
artist = TStringToQString(f.tag()->artist());
album = TStringToQString(f.tag()->album());
Manager::CheckSongInfo(title, artist, album, filename);
emit songTitle(title);
emit songArtist(artist);
emit songAlbum(album);
TagLib::ID3v2::Tag *tag = f.ID3v2Tag();
TagLib::ID3v2::FrameList framelist = tag->frameList("APIC");
if (framelist.isEmpty())
{
QPixmap pixMap(":/resources/cover.png");
emit songCover(pixMap);
}
else
{
TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(framelist.front());
if (pic)
{
QImage coverArt;
coverArt.loadFromData((const uchar *)pic->picture().data(), pic->picture().size());
QPixmap pixMap = QPixmap::fromImage(coverArt);
emit songCover(pixMap);
}
else
{
QPixmap pixMap(":/resources/cover.png");
emit songCover(pixMap);
}
}
emit songLength(BASS_ChannelBytes2Seconds(channel, BASS_ChannelGetLength(channel, BASS_POS_BYTE)));
BASS_ChannelSetPosition(channel, 0, BASS_POS_BYTE);
signalUpdate();
if (isPlaying)
{
BASS_ChannelPlay(channel, false);
}
else
{
BASS_ChannelPause(channel);
}
isChangingSong = false;
}
void Player::setShuffle(bool state)
{
if (state)
{
isShuffling = true;
if (playlist)
playlist->reShuffle(true);
}
else
{
isShuffling = false;
}
}
void Player::setRepeat(bool state)
{
if (state)
{
isRepeating = true;
}
else
{
isRepeating = false;
}
}
void Player::setLock(bool state)
{
if (state)
{
isLocked = true;
}
else
{
isLocked = false;
}
}
void Player::setVolume(float vol)
{
if (vol < 0 || vol > 1)
return;
volume = vol;
if (BASS_ChannelIsActive(channel) == BASS_ACTIVE_STOPPED)
return;
BASS_ChannelSlideAttribute(
channel,
BASS_ATTRIB_VOL,
volume,
500
);
}
float Player::getVolume()
{
float vol;
BASS_ChannelGetAttribute(
channel,
BASS_ATTRIB_VOL,
&vol
);
return vol;
}
Playlist *Player::getPlaylist()
{
return playlist;
}
void Player::checkEndPlayback()
{
if (endOfPlayback)
{
if (isRepeating)
{
setPosition(0);
BASS_ChannelPlay(channel, false);
}
else
{
this->nextSong();
}
endOfPlayback = false;
}
}
void Player::removeCurrentPlaylist()
{
if (playlist != NULL && playlist->getInit())
{
playlist->disconnect();
}
}
bool Player::getShuffle()
{
return isShuffling;
}
void CALLBACK PauseAfterFadeOut(HSYNC handle, DWORD channel, DWORD data, void *user)
{
if (pauseAllowed)
{
BASS_ChannelPause(channel);
}
}
void CALLBACK EndOfPlayback(HSYNC handle, DWORD channel, DWORD data, void *user)
{
endOfPlayback = true;
}
void Player::play()
{
pauseAllowed = false;
//Check is channel is Active
//If no then start the playlist, if yes then resume the song
if (BASS_ChannelIsActive(channel) == BASS_ACTIVE_STOPPED)
{
BASS_ChannelStop(channel);
}
if (!BASS_ChannelPlay(channel, false))
qDebug() << "Error resuming";
else
{
isPlaying = true;
}
emit changePlaying(isPlaying);
//Fade in music after starting or resuming
BASS_ChannelSetAttribute(channel, BASS_ATTRIB_VOL, 0);
BASS_ChannelSlideAttribute(channel, BASS_ATTRIB_VOL, volume, 500);
}
void Player::pause()
{
pauseAllowed = true;
isPlaying = false;
emit changePlaying(isPlaying);
//Fade out music
BASS_ChannelSlideAttribute(channel, BASS_ATTRIB_VOL, 0.f, 500);
//After fade out call function to pause
BASS_ChannelSetSync(channel, BASS_SYNC_SLIDE | BASS_SYNC_ONETIME, 0.f, &PauseAfterFadeOut, 0);
//playing = false;
}
void Player::nextSong()
{
if (playlist)
{
BASS_ChannelStop(channel);
playlist->nextSong(isShuffling, isLocked);
}
}
void Player::prevSong()
{
if (playlist)
{
BASS_ChannelStop(channel);
playlist->prevSong(isShuffling, isLocked);
}
}
bool Player::getPlaying()
{
return isPlaying;
}
void Player::setPosition(int cur)
{
if (isChangingSong == false)
BASS_ChannelSetPosition(channel, BASS_ChannelSeconds2Bytes(channel, cur), BASS_POS_BYTE);
}
void Player::signalUpdate()
{
emit posChanged(BASS_ChannelBytes2Seconds(channel, BASS_ChannelGetPosition(channel, BASS_POS_BYTE)));
}