Improved random mode
[someplayer] / src / player / player.h
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #ifndef PLAYER_H
21 #define PLAYER_H
22
23 #include <QObject>
24 #include "../someplayer.h"
25 #include "../track.h"
26 #include "../trackmetainformation.h"
27 #include "../playlist.h"
28 #include <phonon/MediaObject>
29 #include <phonon/AudioOutput>
30 #include <phonon/VolumeSlider>
31 #include <phonon/Effect>
32 #include <phonon/Path>
33 #include <QStack>
34 #include <QQueue>
35
36 // represents player
37
38 using SomePlayer::DataObjects::Track;
39 using SomePlayer::DataObjects::TrackMetadata;
40 using SomePlayer::DataObjects::Playlist;
41 using SomePlayer::Storage::Config;
42
43 namespace SomePlayer {
44         namespace Playback {
45
46                 enum PlayerState { PLAYER_STOPPED, PLAYER_PLAYING, PLAYER_PAUSED, PLAYER_LOADING, PLAYER_DONE, PLAYER_ERROR };
47
48                 class Randomizer {
49                 public:
50                         void setPlaylist(QList<int>);
51                         int next();
52                 private:
53                         QList<int> _playlist;
54                         QList<int> _rand;
55                         void _shuffle();
56                         int _current;
57                 };
58
59                 class Player : public QObject
60                 {
61                         Q_OBJECT
62                 public:
63                         explicit Player(QObject *parent = 0);
64
65                         bool random() {return _random;}
66                         bool repeat() {return _repeat;}
67                         int volume() {return (int)(_output->volume()*100 + 0.5);}
68                         Phonon::MediaObject* mediaObject() {return _player;}
69                         bool equalizerEnabled() {return _equalizer_enabled;}
70                         bool equalizerAvailable() {return _equalizer != NULL;}
71
72                 signals:
73                         void stateChanged (PlayerState);
74                         void trackChanged (Track);
75                         void tick (int, int); // played | all (seconds)
76                         void trackDone(Track);
77
78                 public slots:
79                         void setTrackId(int id);
80                         void enqueue(int id);
81                         void toggle();
82                         void play();
83                         void stop();
84                         void next();
85                         void prev();
86                         void setPlaylist(Playlist);
87                         void toggleRandom();
88                         void toggleRepeat();
89                         void seek(int);
90                         void setVolume(int);
91                         void enableEqualizer();
92                         void disableEqualizer();
93                         void setEqualizerValue(int band, double value);
94                         void equalizerValue(int band, double *);
95                 private slots:
96                         void _stateChanged(Phonon::State, Phonon::State);
97                         void _tick(qint64);
98                 private:
99                         Randomizer _randomizer;
100                         int _current;
101                         Track _track; // current track (workaround)
102                         bool _random;
103                         bool _repeat;
104                         bool _equalizer_enabled;
105                         QStack<int> _history;
106                         QQueue<int> _queue;
107                         QStack<int> _prev_history;
108                         Playlist _playlist;
109                         Phonon::MediaObject *_player;
110                         Phonon::AudioOutput *_output;
111                         Phonon::Path _path;
112                         Phonon::Effect *_equalizer;
113                         PlayerState _state;
114                         Config _config;
115
116                         void _set_source();
117
118                 };
119         };
120 };
121
122 #endif // PLAYER_H