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  int count() const
63  {
64  return _n_items;
65  }
66 
67  bool is_empty() const
68  {
69  return (_n_items == 0);
70  }
71 };
72 
77 class PlayManager : public QObject, protected SayonaraClass
78 {
79 
80  Q_OBJECT
81 
82  SINGLETON_QOBJECT(PlayManager)
83 
84  public:
85 
89  enum class PlayState : quint8 {
90  Playing=0,
91  Paused,
92  Stopped
93  };
94 
95 
96 signals:
97 
102  void sig_www_track_finished(const MetaData& old_md);
103 
107  void sig_playstate_changed(PlayManager::PlayState);
108 
112  void sig_next();
113 
117  void sig_previous();
118 
122  void sig_stopped();
123 
128  void sig_seeked_rel(double percent);
129 
134  void sig_seeked_rel_ms(qint64 ms);
135 
140  void sig_seeked_abs_ms(quint64 ms);
141 
146  void sig_position_changed_ms(quint64 ms);
147 
152  void sig_track_changed(const MetaData& md);
153 
158  void sig_track_idx_changed(int idx);
159 
164  void sig_playlist_changed(int len);
165 
170  void sig_duration_changed(quint64 ms);
171 
175  void sig_playlist_finished();
176 
183  void sig_record(bool b);
184 
189  void sig_buffer(int b);
190 
195  void sig_volume_changed(int vol);
196 
197 
202  void sig_mute_changed(bool b);
203 
204  void sig_md_changed(const MetaData& md);
205 
206 
207  void sig_duration_changed(qint64 ms);
208 
209 
210 public slots:
214  void play();
215 
219  void play_pause();
220 
224  void pause();
225 
229  void previous();
230 
234  void next();
235 
239  void stop();
240 
247  void record(bool b);
248 
253  void seek_rel(double percent);
254 
259  void seek_abs_ms(quint64 ms);
260 
265  void seek_rel_ms(qint64 ms);
266 
273  void set_position_ms(quint64 ms);
274 
279  void change_track(const MetaData& md, int playlist_idx);
280 
281 
286  void duration_changed(quint64 duration_ms);
287 
291  void set_track_ready();
292 
297  void buffering(int progress);
298 
302  void volume_up();
303 
307  void volume_down();
308 
313  void set_volume(int vol);
314 
319  void set_mute(bool b);
320 
321 
322  void change_metadata(const MetaData& md);
323 
324 
325 
326  void change_duration(qint64 ms);
327 
328 public:
333  PlayState get_play_state() const;
334 
339  quint64 get_cur_position_ms() const;
340 
345  quint64 get_init_position_ms() const;
346 
351  quint64 get_duration_ms() const;
352 
357  MetaData get_cur_track() const;
358 
363  int get_volume() const;
364 
365 
370  bool get_mute() const;
371 
372 
373 private:
374  RingBuffer<QString, 3> _ring_buffer;
375  quint64 _position_ms;
376  int _cur_idx;
377  quint64 _initial_position_ms;
378  PlayState _playstate;
379  MetaData _md;
380 
381 };
382 
383 
384 
385 #endif
386 
The SayonaraClass class provides access to Settings and notifications.
Definition: SayonaraClass.h:31
Definition: MetaData.h:49
PlayState
Current Playing state.
Definition: PlayManager.h:89
Global handler for current playback state (Singleton)
Definition: PlayManager.h:77
Definition: PlayManager.h:29