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 #include <algorithm>
29 
30 template<typename T, int N_ITEMS>
31 class RingBuffer {
32 
33  private:
34  int _cur_idx;
35  int _n_items;
36  T _data[N_ITEMS];
37 
38  public:
39  RingBuffer(){
40  clear();
41  }
42 
43  void clear(){
44  _cur_idx = 0;
45  _n_items = 0;
46  }
47 
48  void insert(const T& item){
49  _data[_cur_idx] = item;
50  _cur_idx = (_cur_idx + 1) % N_ITEMS;
51  _n_items = std::min(N_ITEMS, _n_items + 1);
52  }
53 
54  bool has_item(const T& item) const {
55  for(int i=0; i<_n_items; i++){
56  if(_data[i] == item){
57  return true;
58  }
59  }
60 
61  return false;
62  }
63 
64  int count() const
65  {
66  return _n_items;
67  }
68 
69  bool is_empty() const
70  {
71  return (_n_items == 0);
72  }
73 };
74 
79 class PlayManager : public QObject, protected SayonaraClass
80 {
81 
82  Q_OBJECT
83 
84  SINGLETON_QOBJECT(PlayManager)
85 
86  public:
87 
91  enum class PlayState : quint8 {
92  Playing=0,
93  Paused,
94  Stopped
95  };
96 
97 
98 signals:
99 
104  void sig_www_track_finished(const MetaData& old_md);
105 
109  void sig_playstate_changed(PlayManager::PlayState);
110 
114  void sig_next();
115 
119  void sig_previous();
120 
124  void sig_stopped();
125 
130  void sig_seeked_rel(double percent);
131 
136  void sig_seeked_rel_ms(qint64 ms);
137 
142  void sig_seeked_abs_ms(quint64 ms);
143 
148  void sig_position_changed_ms(quint64 ms);
149 
154  void sig_track_changed(const MetaData& md);
155 
160  void sig_track_idx_changed(int idx);
161 
166  void sig_playlist_changed(int len);
167 
172  void sig_duration_changed(quint64 ms);
173 
177  void sig_playlist_finished();
178 
185  void sig_record(bool b);
186 
191  void sig_buffer(int b);
192 
197  void sig_volume_changed(int vol);
198 
199 
204  void sig_mute_changed(bool b);
205 
206  void sig_md_changed(const MetaData& md);
207 
208 
209  void sig_duration_changed(qint64 ms);
210 
211 
212 public slots:
216  void play();
217 
221  void play_pause();
222 
226  void pause();
227 
231  void previous();
232 
236  void next();
237 
241  void stop();
242 
249  void record(bool b);
250 
255  void seek_rel(double percent);
256 
261  void seek_abs_ms(quint64 ms);
262 
267  void seek_rel_ms(qint64 ms);
268 
275  void set_position_ms(quint64 ms);
276 
281  void change_track(const MetaData& md, int playlist_idx);
282 
283 
288  void duration_changed(quint64 duration_ms);
289 
293  void set_track_ready();
294 
299  void buffering(int progress);
300 
304  void volume_up();
305 
309  void volume_down();
310 
315  void set_volume(int vol);
316 
321  void set_mute(bool b);
322 
323 
324  void change_metadata(const MetaData& md);
325 
326 
327 
328  void change_duration(qint64 ms);
329 
330 public:
335  PlayState get_play_state() const;
336 
341  quint64 get_cur_position_ms() const;
342 
347  quint64 get_init_position_ms() const;
348 
353  quint64 get_duration_ms() const;
354 
359  MetaData get_cur_track() const;
360 
365  int get_volume() const;
366 
367 
372  bool get_mute() const;
373 
374 
375 private:
376  RingBuffer<QString, 3> _ring_buffer;
377  quint64 _position_ms;
378  int _cur_idx;
379  quint64 _initial_position_ms;
380  PlayState _playstate;
381  MetaData _md;
382 
383 };
384 
385 
386 
387 #endif
388 
The SayonaraClass class provides access to Settings and notifications.
Definition: SayonaraClass.h:31
Definition: MetaData.h:49
PlayState
Current Playing state.
Definition: PlayManager.h:91
Global handler for current playback state (Singleton)
Definition: PlayManager.h:79
Definition: PlayManager.h:31