Sayonara Player
PlayManager.h
1 /* PlayManager.h */
2 
3 /* Copyright (C) 2011-2016 Lucio Carreras
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef PLAY_MANAGER_H
22 #define PLAY_MANAGER_H
23 
24 #include "Helper/globals.h"
25 #include "Helper/MetaData/MetaData.h"
26 #include "Helper/Settings/SayonaraClass.h"
27 
28 template<typename T, int N_ITEMS>
29 class RingBuffer {
30 
31  private:
32  int _cur_idx;
33  int _n_items;
34  T _data[N_ITEMS];
35 
36  public:
37  RingBuffer(){
38  clear();
39  }
40 
41  void clear(){
42  _cur_idx = 0;
43  _n_items = 0;
44  }
45 
46  void insert(const T& item){
47  _data[_cur_idx] = item;
48  _cur_idx = (_cur_idx + 1) % N_ITEMS;
49  _n_items = std::min(N_ITEMS, _n_items + 1);
50  }
51 
52  bool has_item(const T& item) const {
53  for(int i=0; i<_n_items; i++){
54  if(_data[i] == item){
55  return true;
56  }
57  }
58 
59  return false;
60  }
61 };
62 
67 class PlayManager : public QObject, protected SayonaraClass
68 {
69 
70  Q_OBJECT
71 
72  SINGLETON_QOBJECT(PlayManager)
73 
74  public:
75 
79  enum class PlayState : quint8 {
80  Playing=0,
81  Paused,
82  Stopped
83  };
84 
85 
86 signals:
87 
92 
96  void sig_next();
97 
101  void sig_previous();
102 
106  void sig_stopped();
107 
112  void sig_seeked_rel(double percent);
113 
118  void sig_seeked_rel_ms(qint64 ms);
119 
124  void sig_seeked_abs_ms(quint64 ms);
125 
130  void sig_position_changed_ms(quint64 ms);
131 
136  void sig_track_changed(const MetaData& md);
137 
142  void sig_track_idx_changed(int idx);
143 
148  void sig_playlist_changed(int len);
149 
154  void sig_duration_changed(quint64 ms);
155 
159  void sig_playlist_finished();
160 
167  void sig_record(bool b);
168 
173  void sig_buffer(int b);
174 
179  void sig_volume_changed(int vol);
180 
181 
186  void sig_mute_changed(bool b);
187 
188  void sig_md_changed(const MetaData& md);
189 
190 
191  void sig_duration_changed(qint64 ms);
192 
193 
194 public slots:
198  void play();
199 
203  void play_pause();
204 
208  void pause();
209 
213  void previous();
214 
218  void next();
219 
223  void stop();
224 
231  void record(bool b);
232 
237  void seek_rel(double percent);
238 
243  void seek_abs_ms(quint64 ms);
244 
249  void seek_rel_ms(qint64 ms);
250 
255  void set_position_ms(quint64 ms);
256 
261  void change_track(const MetaData& md, int playlist_idx);
262 
263 
268  void duration_changed(quint64 duration_ms);
269 
273  void set_track_ready();
274 
279  void buffering(int progress);
280 
284  void volume_up();
285 
289  void volume_down();
290 
295  void set_volume(int vol);
296 
301  void set_mute(bool b);
302 
303 
304  void change_metadata(const MetaData& md);
305 
306 
307 
308  void change_duration(qint64 ms);
309 
310 public:
315  PlayState get_play_state() const;
316 
321  quint64 get_cur_position_ms() const;
322 
327  quint64 get_init_position_ms() const;
328 
333  quint64 get_duration_ms() const;
334 
339  MetaData get_cur_track() const;
340 
345  int get_volume() const;
346 
347 
352  bool get_mute() const;
353 
354 
355 private:
356  RingBuffer<QString, 3> _ring_buffer;
357  quint64 _position_ms;
358  int _cur_idx;
359  quint64 _initial_position_ms;
360  PlayState _playstate;
361  MetaData _md;
362 
363 };
364 
365 
366 
367 #endif
368 
quint64 get_duration_ms() const
get duration of track
void set_mute(bool b)
mute/unmute
void volume_down()
decrease volume by 5
bool get_mute() const
query mute status
void play()
Start playing if there's a track.
The SayonaraClass class provides access to Settings and notifications.
Definition: SayonaraClass.h:31
void sig_previous()
previous track was triggered
void sig_playlist_changed(int len)
playlist has changed
Definition: MetaData.h:49
PlayState
Current Playing state.
Definition: PlayManager.h:79
quint64 get_cur_position_ms() const
get current position in milliseconds
void set_position_ms(quint64 ms)
set current position of track
void sig_seeked_rel_ms(qint64 ms)
relative seeking was triggered
quint64 get_init_position_ms() const
get position in milliseconds where track will start
void sig_track_idx_changed(int idx)
track has changed
void seek_rel_ms(qint64 ms)
seek_rel_ms
void change_track(const MetaData &md, int playlist_idx)
change current track
void sig_position_changed_ms(quint64 ms)
position in track has changed
void sig_volume_changed(int vol)
emitted when volume has changed
void sig_buffer(int b)
emitted when currently in buffering state
void sig_next()
next track was triggered
void sig_track_changed(const MetaData &md)
track has changed
Global handler for current playback state (Singleton)
Definition: PlayManager.h:67
void record(bool b)
request recording (see also sig_record(bool b))
void sig_record(bool b)
recording is requested
void duration_changed(quint64 duration_ms)
change duration of track
void sig_playlist_finished()
playlist has finished
void sig_stopped()
stop was triggered
PlayState get_play_state() const
get current play state
void sig_seeked_abs_ms(quint64 ms)
absolute seeking was triggered
int get_volume() const
get current volume
void sig_seeked_rel(double percent)
relative seeking was triggered
void set_volume(int vol)
set volume
void buffering(int progress)
notifiy, that track is in buffering state currently
void next()
change to next track
void set_track_ready()
notify, that track is ready for playback
void seek_rel(double percent)
seek relative
void volume_up()
increase volume by 5
void play_pause()
toggle play/pause
void pause()
pause track, if currently playing
void seek_abs_ms(quint64 ms)
seek absolute
Definition: PlayManager.h:29
void previous()
change to previous track
void sig_duration_changed(quint64 ms)
duration of track has changed
void sig_playstate_changed(PlayManager::PlayState)
emitted, when PlayManager::PlayState was changed
void sig_mute_changed(bool b)
emitted when mute state has changed
void stop()
stop playback
MetaData get_cur_track() const
get current track