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
|
#ifndef AUDIO_H
#define AUDIO_H
#include "audiointerface.h"
#include <QObject>
#include <QList>
#include <QFile>
#include <QTimer>
namespace Sound
{
enum Type
{
Intro = 0,
WakaWaka,
EatingFruit,
EatingGhost,
Die,
Ambient
};
const unsigned int length[] = {
4310, 2090, 570, 570, 1720, 1990
};
};
/* --------------------------------------------------------------- */
class NoopAudioPlayer
: public AudioPlayer
{
Q_OBJECT
friend class AudioManager;
public:
virtual bool isWorking() const;
virtual void setMuted(bool mute = true);
virtual bool isMuted() const;
virtual void play();
virtual bool isPlaying();
virtual void pause();
virtual bool isPaused();
virtual void stop();
virtual bool isStopped();
virtual void clear();
virtual void clearQueue();
virtual void setPrefinishMark(qint32 msecToEnd);
virtual void seek(qint64 time);
protected:
NoopAudioPlayer(QObject *parent = 0);
virtual void setWorking(bool working = true);
virtual void test(QFile *sound, unsigned int length);
virtual void play(QFile *sound, unsigned int length);
virtual void setSource(QFile *sound, unsigned int length);
virtual void enqueue(QFile *sound, unsigned int length);
protected slots:
virtual void prefinishMarkReached_ex(qint32 mark);
protected:
bool m_working;
bool m_muted;
bool m_playing;
bool m_paused;
};
/* --------------------------------------------------------------- */
class FakeAudioPlayer
: public NoopAudioPlayer
{
Q_OBJECT
friend class AudioManager;
protected:
FakeAudioPlayer(QObject *parent = 0);
virtual void play(QFile *sound, unsigned int length);
protected slots:
void finished_ex();
protected:
QTimer m_timer;
};
/* --------------------------------------------------------------- */
class GaplessAudioPlayer
: public QObject
{
Q_OBJECT
public:
GaplessAudioPlayer(Sound::Type sound, qint32 mark, QObject *parent = 0);
bool isWorking() const;
void setMuted(bool mute = true);
bool isMuted() const;
void play();
void pause();
protected slots:
void startPlayer1();
void startPlayer2();
protected:
bool m_working;
bool m_paused;
Sound::Type m_sound;
AudioPlayer *m_player1;
AudioPlayer *m_player2;
};
/* --------------------------------------------------------------- */
class AudioManager
: public QObject
{
Q_OBJECT
friend class GaplessAudioPlayer;
public:
AudioManager();
static AudioManager *self();
static bool isWorking();
void setMuted(bool mute = true);
bool isMuted() const;
void pause();
void stop();
void clear();
void clearQueue() const;
AudioPlayer *audioPlayer();
void registerAudioPlayer(AudioPlayer *player);
void unregisterAudioPlayer(AudioPlayer *player);
void play(Sound::Type sound, AudioPlayer *player = NULL);
void setSource(Sound::Type sound, AudioPlayer *player = NULL);
void enqueue(Sound::Type sound, AudioPlayer *player = NULL);
signals:
void mutedChanged(bool muted);
private slots:
void unregisterAudioPlayer_helper(QObject *player);
protected:
void preload();
bool tryLoadAudioPlugin(const QString &libraryname);
AudioPlayer *createAudioPlayer(QObject *parent = 0);
QFile *sound(Sound::Type sound);
unsigned int length(Sound::Type sound);
private:
bool m_muted;
static bool m_working;
static AudioManager *m_instance;
QList<QFile *> m_sounds;
QList<AudioPlayer *> m_players;
AudioPlayerFactory *m_factory;
};
#endif // AUDIO_H
|