diff options
| author | manuel <manuel@mausz.at> | 2011-04-10 20:50:54 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-10 20:50:54 +0200 |
| commit | d0eafb0124a39eeda6c00595a943ce9811d589c4 (patch) | |
| tree | b39bea35df3a719c7707950988d180a1516e677d | |
| parent | 5f7d86446c8d7c8e03ce02f6188ee8dede4a6975 (diff) | |
| download | foop-d0eafb0124a39eeda6c00595a943ce9811d589c4.tar.gz foop-d0eafb0124a39eeda6c00595a943ce9811d589c4.tar.bz2 foop-d0eafb0124a39eeda6c00595a943ce9811d589c4.zip | |
major audio rewrite
| -rw-r--r-- | pacman-c++/actor.cpp | 43 | ||||
| -rw-r--r-- | pacman-c++/actor.h | 5 | ||||
| -rw-r--r-- | pacman-c++/audio.cpp | 262 | ||||
| -rw-r--r-- | pacman-c++/audio.h | 108 | ||||
| -rw-r--r-- | pacman-c++/audioplayer.cpp | 187 | ||||
| -rw-r--r-- | pacman-c++/audioplayer.h | 65 | ||||
| -rw-r--r-- | pacman-c++/client.cpp | 12 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 2 | ||||
| -rw-r--r-- | pacman-c++/pacman.pro | 4 | ||||
| -rw-r--r-- | pacman-c++/pacman.server.pro | 4 |
10 files changed, 403 insertions, 289 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index bd50a3c..8184c6f 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #include "actor.h" | 1 | #include "actor.h" |
| 2 | #include "animationmanager.h" | 2 | #include "animationmanager.h" |
| 3 | #include "audioplayer.h" | ||
| 4 | #include <QtCore/QPropertyAnimation> | 3 | #include <QtCore/QPropertyAnimation> |
| 5 | #include <QtCore/QVariantAnimation> | 4 | #include <QtCore/QVariantAnimation> |
| 6 | #include <QDebug> | 5 | #include <QDebug> |
| @@ -12,7 +11,7 @@ static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal | |||
| 12 | 11 | ||
| 13 | Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | 12 | Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) |
| 14 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local), | 13 | : PixmapItem(parent), m_color(color), m_direction(Actor::None), m_local(local), |
| 15 | m_roundPoints(0), m_gamePoints(0) | 14 | m_player(NULL), m_roundPoints(0), m_gamePoints(0) |
| 16 | { | 15 | { |
| 17 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); | 16 | m_pix = ":/" + QString("actor%1").arg((m_color >> 1) + 1); |
| 18 | /* DON'T set any pixmap here. we've a pixmap in the animation | 17 | /* DON'T set any pixmap here. we've a pixmap in the animation |
| @@ -50,8 +49,16 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | |||
| 50 | m_eating.append(setupEatingAnimation(Actor::Up)); | 49 | m_eating.append(setupEatingAnimation(Actor::Up)); |
| 51 | m_eating.append(setupEatingAnimation(Actor::Down)); | 50 | m_eating.append(setupEatingAnimation(Actor::Down)); |
| 52 | 51 | ||
| 53 | /* setup player */ | 52 | /* setup sound */ |
| 54 | connect(AudioPlayer::self(), SIGNAL(aboutToFinish()), this, SLOT(enqueue())); | 53 | if (local) |
| 54 | { | ||
| 55 | m_player = new AudioPlayer(this); | ||
| 56 | if (m_player->isWorking()) | ||
| 57 | { | ||
| 58 | m_player->setLoop(Sound::WakaWaka); | ||
| 59 | AudioManager::self()->registerAudioPlayer(m_player); | ||
| 60 | } | ||
| 61 | } | ||
| 55 | 62 | ||
| 56 | /* make the picture showing the current direction visible */ | 63 | /* make the picture showing the current direction visible */ |
| 57 | m_images[m_direction]->setVisible(true); | 64 | m_images[m_direction]->setVisible(true); |
| @@ -159,26 +166,22 @@ void Actor::move(Actor::Movement direction) | |||
| 159 | if (direction == Actor::None) | 166 | if (direction == Actor::None) |
| 160 | m_images[m_direction]->setVisible(true); | 167 | m_images[m_direction]->setVisible(true); |
| 161 | else | 168 | else |
| 162 | { | ||
| 163 | m_eating[direction]->start(); | 169 | m_eating[direction]->start(); |
| 164 | } | ||
| 165 | m_direction = direction; | ||
| 166 | } | 170 | } |
| 167 | 171 | ||
| 168 | if (direction != Actor::None) | 172 | if (direction != Actor::None) |
| 169 | { | 173 | { |
| 170 | if (m_local && AudioPlayer::self()->state() != Phonon::PlayingState) | 174 | if (m_local && m_player->isWorking() && m_player->state() != Phonon::PlayingState) |
| 171 | { | 175 | m_player->play(); |
| 172 | AudioPlayer::self()->clear(); | ||
| 173 | AudioPlayer::self()->play(AudioPlayer::WakaWaka); | ||
| 174 | AudioPlayer::self()->enqueue(AudioPlayer::WakaWaka); | ||
| 175 | } | ||
| 176 | m_moving->start(); | 176 | m_moving->start(); |
| 177 | } | 177 | } |
| 178 | else | 178 | else if (direction != m_direction) |
| 179 | { | 179 | { |
| 180 | AudioPlayer::self()->stop(); | 180 | if (m_local && m_player->isWorking() && m_player->state() != Phonon::PausedState) |
| 181 | m_player->pause(); | ||
| 181 | } | 182 | } |
| 183 | |||
| 184 | m_direction = direction; | ||
| 182 | } | 185 | } |
| 183 | 186 | ||
| 184 | bool Actor::isMoving() | 187 | bool Actor::isMoving() |
| @@ -186,22 +189,16 @@ bool Actor::isMoving() | |||
| 186 | return (m_moving->state() == QAbstractAnimation::Running); | 189 | return (m_moving->state() == QAbstractAnimation::Running); |
| 187 | } | 190 | } |
| 188 | 191 | ||
| 189 | void Actor::enqueue() | ||
| 190 | { | ||
| 191 | if (isMoving()) | ||
| 192 | AudioPlayer::self()->enqueue(AudioPlayer::WakaWaka); | ||
| 193 | } | ||
| 194 | |||
| 195 | void Actor::die() | 192 | void Actor::die() |
| 196 | { | 193 | { |
| 197 | if (!m_local) | 194 | if (!m_local) |
| 198 | return; | 195 | return; |
| 199 | AudioPlayer::self()->play(AudioPlayer::Die); | 196 | AudioManager::self()->play(Sound::Die); |
| 200 | } | 197 | } |
| 201 | 198 | ||
| 202 | void Actor::eatingCherry() | 199 | void Actor::eatingCherry() |
| 203 | { | 200 | { |
| 204 | if (!m_local) | 201 | if (!m_local) |
| 205 | return; | 202 | return; |
| 206 | AudioPlayer::self()->play(AudioPlayer::EatingCherry); | 203 | AudioManager::self()->play(Sound::EatingCherry); |
| 207 | } | 204 | } |
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 965d240..78b7c01 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | #include "pixmapitem.h" | 4 | #include "pixmapitem.h" |
| 5 | #include "constants.h" | 5 | #include "constants.h" |
| 6 | #include "audio.h" | ||
| 6 | #include <QtCore/QSequentialAnimationGroup> | 7 | #include <QtCore/QSequentialAnimationGroup> |
| 7 | #include <QtCore/QParallelAnimationGroup> | 8 | #include <QtCore/QParallelAnimationGroup> |
| 8 | #include <QList> | 9 | #include <QList> |
| @@ -39,15 +40,13 @@ public: | |||
| 39 | void addRoundPoints(unsigned int amount) { m_roundPoints += amount; } | 40 | void addRoundPoints(unsigned int amount) { m_roundPoints += amount; } |
| 40 | void finishRound() { m_gamePoints += m_roundPoints; m_roundPoints = 0; } | 41 | void finishRound() { m_gamePoints += m_roundPoints; m_roundPoints = 0; } |
| 41 | 42 | ||
| 42 | private slots: | ||
| 43 | void enqueue(); | ||
| 44 | |||
| 45 | private: | 43 | private: |
| 46 | QPixmap m_pix; | 44 | QPixmap m_pix; |
| 47 | Color::Color m_color; | 45 | Color::Color m_color; |
| 48 | Movement m_direction; | 46 | Movement m_direction; |
| 49 | PixmapItem m_icon; | 47 | PixmapItem m_icon; |
| 50 | bool m_local; | 48 | bool m_local; |
| 49 | AudioPlayer *m_player; | ||
| 51 | 50 | ||
| 52 | unsigned int m_roundPoints, m_gamePoints; | 51 | unsigned int m_roundPoints, m_gamePoints; |
| 53 | 52 | ||
diff --git a/pacman-c++/audio.cpp b/pacman-c++/audio.cpp new file mode 100644 index 0000000..0692cd1 --- /dev/null +++ b/pacman-c++/audio.cpp | |||
| @@ -0,0 +1,262 @@ | |||
| 1 | #include "audio.h" | ||
| 2 | #include <phonon/AudioOutput> | ||
| 3 | #include <QCoreApplication> | ||
| 4 | #include <QTimer> | ||
| 5 | #include <QFile> | ||
| 6 | #include <QDir> | ||
| 7 | #include <QDebug> | ||
| 8 | |||
| 9 | /* the universe's only audio manager */ | ||
| 10 | AudioManager *AudioManager::m_instance = NULL; | ||
| 11 | bool AudioManager::m_working = false; | ||
| 12 | |||
| 13 | AudioManager::AudioManager() | ||
| 14 | : m_muted(true) | ||
| 15 | { | ||
| 16 | #ifndef SERVER | ||
| 17 | preload(); | ||
| 18 | |||
| 19 | AudioPlayer *firstplayer = new AudioPlayer(this); | ||
| 20 | firstplayer->test(m_sounds[Sound::WakaWaka]); | ||
| 21 | m_working = firstplayer->m_working; | ||
| 22 | m_players.append(firstplayer); | ||
| 23 | |||
| 24 | m_muted = false; | ||
| 25 | #endif // SERVER | ||
| 26 | } | ||
| 27 | |||
| 28 | AudioManager *AudioManager::self() | ||
| 29 | { | ||
| 30 | if (m_instance == NULL) | ||
| 31 | m_instance = new AudioManager(); | ||
| 32 | return m_instance; | ||
| 33 | } | ||
| 34 | |||
| 35 | bool AudioManager::isWorking() const | ||
| 36 | { | ||
| 37 | return m_working; | ||
| 38 | } | ||
| 39 | |||
| 40 | void AudioManager::setMuted(bool mute) | ||
| 41 | { | ||
| 42 | if (!isWorking()) | ||
| 43 | return; | ||
| 44 | |||
| 45 | if (mute == m_muted) | ||
| 46 | return; | ||
| 47 | |||
| 48 | qDebug() << "mute"; | ||
| 49 | for(int i = 0; i < m_players.count(); ++i) | ||
| 50 | m_players.at(i)->setMuted(mute); | ||
| 51 | m_muted = mute; | ||
| 52 | emit mutedChanged(mute); | ||
| 53 | } | ||
| 54 | |||
| 55 | bool AudioManager::isMuted() const | ||
| 56 | { | ||
| 57 | return m_muted; | ||
| 58 | } | ||
| 59 | |||
| 60 | void AudioManager::pause() | ||
| 61 | { | ||
| 62 | if (!isWorking()) | ||
| 63 | return; | ||
| 64 | qDebug() << "pause"; | ||
| 65 | for(int i = 0; i < m_players.count(); ++i) | ||
| 66 | m_players.at(i)->pause(); | ||
| 67 | } | ||
| 68 | |||
| 69 | void AudioManager::stop() | ||
| 70 | { | ||
| 71 | if (!isWorking()) | ||
| 72 | return; | ||
| 73 | qDebug() << "stop"; | ||
| 74 | for(int i = 0; i < m_players.count(); ++i) | ||
| 75 | m_players.at(i)->stop(); | ||
| 76 | } | ||
| 77 | |||
| 78 | void AudioManager::clear() | ||
| 79 | { | ||
| 80 | if (!isWorking()) | ||
| 81 | return; | ||
| 82 | qDebug() << "clear"; | ||
| 83 | for(int i = 0; i < m_players.count(); ++i) | ||
| 84 | m_players.at(i)->clear(); | ||
| 85 | } | ||
| 86 | |||
| 87 | void AudioManager::clearQueue() const | ||
| 88 | { | ||
| 89 | if (!isWorking()) | ||
| 90 | return; | ||
| 91 | for(int i = 0; i < m_players.count(); ++i) | ||
| 92 | m_players.at(i)->clearQueue(); | ||
| 93 | } | ||
| 94 | |||
| 95 | AudioPlayer *AudioManager::audioPlayer() | ||
| 96 | { | ||
| 97 | return m_players.at(0); | ||
| 98 | } | ||
| 99 | |||
| 100 | void AudioManager::play(Sound::Type sound) | ||
| 101 | { | ||
| 102 | if (!isWorking()) | ||
| 103 | { | ||
| 104 | emit audioPlayer()->finished(); | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | qDebug() << "play"; | ||
| 109 | AudioPlayer *player = audioPlayer(); | ||
| 110 | player->setCurrentSource(Phonon::MediaSource(m_sounds[sound])); | ||
| 111 | player->play(); | ||
| 112 | } | ||
| 113 | |||
| 114 | void AudioManager::enqueue(Sound::Type sound) | ||
| 115 | { | ||
| 116 | if (!isWorking()) | ||
| 117 | return; | ||
| 118 | qDebug() << "manager enqueue"; | ||
| 119 | audioPlayer()->enqueue(Phonon::MediaSource(m_sounds[sound])); | ||
| 120 | } | ||
| 121 | |||
| 122 | void AudioManager::registerAudioPlayer(AudioPlayer *player) | ||
| 123 | { | ||
| 124 | player->setMuted(m_muted); | ||
| 125 | connect(player, SIGNAL(destroyed(QObject *)), this, SLOT(unregisterAudioPlayer_helper(QObject *))); | ||
| 126 | m_players.append(player); | ||
| 127 | } | ||
| 128 | |||
| 129 | void AudioManager::unregisterAudioPlayer(AudioPlayer *player) | ||
| 130 | { | ||
| 131 | disconnect(player, SIGNAL(destroyed(QObject *)), this, SLOT(unregisterAudioPlayer_helper(QObject *))); | ||
| 132 | m_players.removeAll(player); | ||
| 133 | } | ||
| 134 | |||
| 135 | void AudioManager::unregisterAudioPlayer_helper(QObject *player) | ||
| 136 | { | ||
| 137 | unregisterAudioPlayer(static_cast<AudioPlayer *>(player)); | ||
| 138 | } | ||
| 139 | |||
| 140 | void AudioManager::preload() | ||
| 141 | { | ||
| 142 | m_sounds.clear(); | ||
| 143 | QDir sounds(":/sound"); | ||
| 144 | for(unsigned i = 1; i <= sounds.count(); ++i) | ||
| 145 | m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), this)); | ||
| 146 | } | ||
| 147 | |||
| 148 | QFile *AudioManager::sound(Sound::Type sound) | ||
| 149 | { | ||
| 150 | if (!isWorking()) | ||
| 151 | return NULL; | ||
| 152 | return m_sounds.at(sound); | ||
| 153 | } | ||
| 154 | |||
| 155 | /* --------------------------------------------------------------- */ | ||
| 156 | |||
| 157 | AudioPlayer::AudioPlayer(QObject *parent) | ||
| 158 | : Phonon::MediaObject(parent) | ||
| 159 | { | ||
| 160 | m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); | ||
| 161 | Phonon::createPath(this, m_output); | ||
| 162 | m_working = AudioManager::m_working; | ||
| 163 | } | ||
| 164 | |||
| 165 | bool AudioPlayer::isWorking() const | ||
| 166 | { | ||
| 167 | return m_working; | ||
| 168 | } | ||
| 169 | |||
| 170 | void AudioPlayer::setMuted(bool mute) | ||
| 171 | { | ||
| 172 | m_output->setMuted(mute); | ||
| 173 | } | ||
| 174 | |||
| 175 | bool AudioPlayer::isMuted() const | ||
| 176 | { | ||
| 177 | return m_output->isMuted(); | ||
| 178 | } | ||
| 179 | |||
| 180 | void AudioPlayer::setLoop(QFile *sound) | ||
| 181 | { | ||
| 182 | if (!isWorking()) | ||
| 183 | return; | ||
| 184 | |||
| 185 | if (sound == NULL) | ||
| 186 | { | ||
| 187 | disconnect(this, SIGNAL(aboutToFinish()), this, SLOT(loopEnqueue())); | ||
| 188 | return; | ||
| 189 | } | ||
| 190 | |||
| 191 | m_loopsound = sound; | ||
| 192 | connect(this, SIGNAL(aboutToFinish()), this, SLOT(loopEnqueue())); | ||
| 193 | setCurrentSource(Phonon::MediaSource(m_loopsound)); | ||
| 194 | enqueue(Phonon::MediaSource(m_loopsound)); | ||
| 195 | } | ||
| 196 | |||
| 197 | void AudioPlayer::setLoop(Sound::Type sound) | ||
| 198 | { | ||
| 199 | setLoop(AudioManager::self()->sound(sound)); | ||
| 200 | } | ||
| 201 | |||
| 202 | /* this is a simple hack to check if phonon can actually play sounds.. */ | ||
| 203 | void AudioPlayer::test(QFile *testsound) | ||
| 204 | { | ||
| 205 | stop(); | ||
| 206 | m_output->setVolume(0); | ||
| 207 | setCurrentSource(Phonon::MediaSource(testsound)); | ||
| 208 | connect(this, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(stateChanged_ex(Phonon::State,Phonon::State))); | ||
| 209 | play(); | ||
| 210 | |||
| 211 | QTimer timer; | ||
| 212 | timer.setSingleShot(true); | ||
| 213 | connect(&timer, SIGNAL(timeout()), this, SLOT(testFinished())); | ||
| 214 | timer.start(500); | ||
| 215 | while(timer.isActive()) | ||
| 216 | { | ||
| 217 | qApp->processEvents(); | ||
| 218 | Sleeper::msleep(1); | ||
| 219 | } | ||
| 220 | clear(); | ||
| 221 | } | ||
| 222 | |||
| 223 | void AudioPlayer::stateChanged_ex(Phonon::State newstate, Phonon::State /* oldstate */) | ||
| 224 | { | ||
| 225 | if (newstate != Phonon::ErrorState) | ||
| 226 | { | ||
| 227 | m_working = true; | ||
| 228 | m_output->setVolume(1); | ||
| 229 | qDebug() << "Sound is working for you!"; | ||
| 230 | } | ||
| 231 | disconnect(this, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_ex(Phonon::State, Phonon::State))); | ||
| 232 | stop(); | ||
| 233 | } | ||
| 234 | |||
| 235 | void AudioPlayer::testFinished() | ||
| 236 | { | ||
| 237 | if (!m_working) | ||
| 238 | qDebug() << "There's no sound for you :("; | ||
| 239 | disconnect(this, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_ex(Phonon::State, Phonon::State))); | ||
| 240 | } | ||
| 241 | |||
| 242 | void AudioPlayer::loopEnqueue() | ||
| 243 | { | ||
| 244 | enqueue(Phonon::MediaSource(m_loopsound)); | ||
| 245 | } | ||
| 246 | |||
| 247 | /* --------------------------------------------------------------- */ | ||
| 248 | |||
| 249 | void AudioPlayer::Sleeper::sleep(unsigned long secs) | ||
| 250 | { | ||
| 251 | QThread::sleep(secs); | ||
| 252 | } | ||
| 253 | |||
| 254 | void AudioPlayer::Sleeper::msleep(unsigned long msecs) | ||
| 255 | { | ||
| 256 | QThread::msleep(msecs); | ||
| 257 | } | ||
| 258 | |||
| 259 | void AudioPlayer::Sleeper::usleep(unsigned long usecs) | ||
| 260 | { | ||
| 261 | QThread::usleep(usecs); | ||
| 262 | } | ||
diff --git a/pacman-c++/audio.h b/pacman-c++/audio.h new file mode 100644 index 0000000..bea7fb8 --- /dev/null +++ b/pacman-c++/audio.h | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #ifndef AUDIO_H | ||
| 2 | #define AUDIO_H | ||
| 3 | |||
| 4 | #include <QObject> | ||
| 5 | #include <QList> | ||
| 6 | #include <QFile> | ||
| 7 | #include <QThread> | ||
| 8 | #include <phonon/MediaObject> | ||
| 9 | |||
| 10 | namespace Phonon | ||
| 11 | { | ||
| 12 | class AudioOutput; | ||
| 13 | } | ||
| 14 | |||
| 15 | namespace Sound | ||
| 16 | { | ||
| 17 | enum Type | ||
| 18 | { | ||
| 19 | Intro = 0, | ||
| 20 | WakaWaka, | ||
| 21 | EatingCherry, | ||
| 22 | Die | ||
| 23 | }; | ||
| 24 | }; | ||
| 25 | |||
| 26 | class AudioPlayer | ||
| 27 | : public Phonon::MediaObject | ||
| 28 | { | ||
| 29 | Q_OBJECT | ||
| 30 | friend class AudioManager; | ||
| 31 | |||
| 32 | private: | ||
| 33 | class Sleeper | ||
| 34 | : public QThread | ||
| 35 | { | ||
| 36 | public: | ||
| 37 | static void sleep(unsigned long secs); | ||
| 38 | static void msleep(unsigned long msecs); | ||
| 39 | static void usleep(unsigned long usecs); | ||
| 40 | }; | ||
| 41 | |||
| 42 | public: | ||
| 43 | AudioPlayer(QObject *parent = 0); | ||
| 44 | bool isWorking() const; | ||
| 45 | void setMuted(bool mute = true); | ||
| 46 | bool isMuted() const; | ||
| 47 | void setLoop(QFile *sound); | ||
| 48 | void setLoop(Sound::Type sound); | ||
| 49 | |||
| 50 | private: | ||
| 51 | void test(QFile *testsound); | ||
| 52 | |||
| 53 | public slots: | ||
| 54 | void loopEnqueue(); | ||
| 55 | |||
| 56 | private slots: | ||
| 57 | void testFinished(); | ||
| 58 | void stateChanged_ex(Phonon::State newstate, Phonon::State oldstate); | ||
| 59 | |||
| 60 | private: | ||
| 61 | bool m_working; | ||
| 62 | QFile *m_loopsound; | ||
| 63 | Phonon::AudioOutput *m_output; | ||
| 64 | }; | ||
| 65 | |||
| 66 | class AudioManager | ||
| 67 | : public QObject | ||
| 68 | { | ||
| 69 | Q_OBJECT | ||
| 70 | friend class AudioPlayer; | ||
| 71 | |||
| 72 | public: | ||
| 73 | AudioManager(); | ||
| 74 | static AudioManager *self(); | ||
| 75 | bool isWorking() const; | ||
| 76 | void setMuted(bool mute = true); | ||
| 77 | bool isMuted() const; | ||
| 78 | void pause(); | ||
| 79 | void stop(); | ||
| 80 | void clear(); | ||
| 81 | void clearQueue() const; | ||
| 82 | |||
| 83 | AudioPlayer *audioPlayer(); | ||
| 84 | void play(Sound::Type sound); | ||
| 85 | void enqueue(Sound::Type sound); | ||
| 86 | |||
| 87 | void registerAudioPlayer(AudioPlayer *player); | ||
| 88 | void unregisterAudioPlayer(AudioPlayer *player); | ||
| 89 | |||
| 90 | signals: | ||
| 91 | void mutedChanged(bool muted); | ||
| 92 | |||
| 93 | private slots: | ||
| 94 | void unregisterAudioPlayer_helper(QObject *player); | ||
| 95 | |||
| 96 | private: | ||
| 97 | void preload(); | ||
| 98 | QFile *sound(Sound::Type sound); | ||
| 99 | |||
| 100 | private: | ||
| 101 | bool m_muted; | ||
| 102 | static bool m_working; | ||
| 103 | static AudioManager *m_instance; | ||
| 104 | QList<QFile *> m_sounds; | ||
| 105 | QList<AudioPlayer *> m_players; | ||
| 106 | }; | ||
| 107 | |||
| 108 | #endif // AUDIOPLAYER_H | ||
diff --git a/pacman-c++/audioplayer.cpp b/pacman-c++/audioplayer.cpp deleted file mode 100644 index c360a23..0000000 --- a/pacman-c++/audioplayer.cpp +++ /dev/null | |||
| @@ -1,187 +0,0 @@ | |||
| 1 | #include "audioplayer.h" | ||
| 2 | #include <phonon/AudioOutput> | ||
| 3 | #include <phonon/MediaObject> | ||
| 4 | #include <QCoreApplication> | ||
| 5 | #include <QTimer> | ||
| 6 | #include <QThread> | ||
| 7 | #include <QFile> | ||
| 8 | #include <QDir> | ||
| 9 | #include <QDebug> | ||
| 10 | |||
| 11 | class Sleeper | ||
| 12 | : public QThread | ||
| 13 | { | ||
| 14 | public: | ||
| 15 | static void sleep(unsigned long secs) | ||
| 16 | { | ||
| 17 | QThread::sleep(secs); | ||
| 18 | } | ||
| 19 | |||
| 20 | static void msleep(unsigned long msecs) | ||
| 21 | { | ||
| 22 | QThread::msleep(msecs); | ||
| 23 | } | ||
| 24 | |||
| 25 | static void usleep(unsigned long usecs) | ||
| 26 | { | ||
| 27 | QThread::usleep(usecs); | ||
| 28 | } | ||
| 29 | }; | ||
| 30 | |||
| 31 | // the universe's only audio player | ||
| 32 | AudioPlayer *AudioPlayer::m_instance = NULL; | ||
| 33 | |||
| 34 | AudioPlayer::AudioPlayer() | ||
| 35 | : m_working(false) | ||
| 36 | { | ||
| 37 | #ifndef SERVER | ||
| 38 | m_player = new Phonon::MediaObject(this); | ||
| 39 | connect(m_player, SIGNAL(finished()), this, SLOT(finished_p())); | ||
| 40 | connect(m_player, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish_p())); | ||
| 41 | |||
| 42 | m_output = new Phonon::AudioOutput(Phonon::MusicCategory, m_player); | ||
| 43 | connect(m_output, SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged_p(bool))); | ||
| 44 | |||
| 45 | Phonon::createPath(m_player, m_output); | ||
| 46 | |||
| 47 | preload(); | ||
| 48 | test(); | ||
| 49 | #endif // SERVER | ||
| 50 | } | ||
| 51 | |||
| 52 | AudioPlayer *AudioPlayer::self() | ||
| 53 | { | ||
| 54 | if (!m_instance) | ||
| 55 | m_instance = new AudioPlayer(); | ||
| 56 | return m_instance; | ||
| 57 | } | ||
| 58 | |||
| 59 | bool AudioPlayer::isWorking() const | ||
| 60 | { | ||
| 61 | return m_working; | ||
| 62 | } | ||
| 63 | |||
| 64 | void AudioPlayer::stop() | ||
| 65 | { | ||
| 66 | if (!isWorking()) | ||
| 67 | return; | ||
| 68 | m_player->stop(); | ||
| 69 | } | ||
| 70 | |||
| 71 | void AudioPlayer::setMuted(bool mute) | ||
| 72 | { | ||
| 73 | if (!isWorking()) | ||
| 74 | return; | ||
| 75 | m_output->setMuted(mute); | ||
| 76 | } | ||
| 77 | |||
| 78 | bool AudioPlayer::isMuted() const | ||
| 79 | { | ||
| 80 | if (!isWorking()) | ||
| 81 | return true; | ||
| 82 | return m_output->isMuted(); | ||
| 83 | } | ||
| 84 | |||
| 85 | void AudioPlayer::play(AudioPlayer::Sound sound) | ||
| 86 | { | ||
| 87 | if (!isWorking()) | ||
| 88 | { | ||
| 89 | emit finished_p(); | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | |||
| 93 | m_player->stop(); | ||
| 94 | m_player->setCurrentSource(Phonon::MediaSource(m_sounds[sound])); | ||
| 95 | m_player->play(); | ||
| 96 | } | ||
| 97 | |||
| 98 | void AudioPlayer::clear() | ||
| 99 | { | ||
| 100 | if (!isWorking()) | ||
| 101 | return; | ||
| 102 | m_player->clear(); | ||
| 103 | } | ||
| 104 | |||
| 105 | void AudioPlayer::enqueue(AudioPlayer::Sound sound) | ||
| 106 | { | ||
| 107 | if (!isWorking()) | ||
| 108 | return; | ||
| 109 | m_player->enqueue(Phonon::MediaSource(m_sounds[sound])); | ||
| 110 | } | ||
| 111 | |||
| 112 | void AudioPlayer::clearQueue() const | ||
| 113 | { | ||
| 114 | if (!isWorking()) | ||
| 115 | return; | ||
| 116 | m_player->clearQueue(); | ||
| 117 | } | ||
| 118 | |||
| 119 | Phonon::State AudioPlayer::state() | ||
| 120 | { | ||
| 121 | if (!isWorking()) | ||
| 122 | return Phonon::ErrorState; | ||
| 123 | return m_player->state(); | ||
| 124 | } | ||
| 125 | |||
| 126 | void AudioPlayer::preload() | ||
| 127 | { | ||
| 128 | m_sounds.clear(); | ||
| 129 | QDir sounds(":/sound"); | ||
| 130 | for(unsigned i = 1; i <= sounds.count(); ++i) | ||
| 131 | m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), m_player)); | ||
| 132 | } | ||
| 133 | |||
| 134 | /* this is a simple hack to check if phonon can actually play sounds.. */ | ||
| 135 | void AudioPlayer::test() | ||
| 136 | { | ||
| 137 | m_player->stop(); | ||
| 138 | connect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 139 | m_output->setVolume(0); | ||
| 140 | m_player->setCurrentSource(Phonon::MediaSource(m_sounds[AudioPlayer::WakaWaka])); | ||
| 141 | m_player->play(); | ||
| 142 | |||
| 143 | QTimer timer; | ||
| 144 | timer.setSingleShot(true); | ||
| 145 | connect(&timer, SIGNAL(timeout()), this, SLOT(testFinished())); | ||
| 146 | timer.start(500); | ||
| 147 | while(timer.isActive()) | ||
| 148 | { | ||
| 149 | qApp->processEvents(); | ||
| 150 | Sleeper::msleep(1); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | void AudioPlayer::finished_p() | ||
| 155 | { | ||
| 156 | emit finished(); | ||
| 157 | } | ||
| 158 | |||
| 159 | void AudioPlayer::aboutToFinish_p() | ||
| 160 | { | ||
| 161 | emit aboutToFinish(); | ||
| 162 | } | ||
| 163 | |||
| 164 | void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State /* oldstate */) | ||
| 165 | { | ||
| 166 | if (newstate != Phonon::ErrorState) | ||
| 167 | { | ||
| 168 | m_working = true; | ||
| 169 | m_output->setVolume(1); | ||
| 170 | qDebug() << "Sound is working for you!"; | ||
| 171 | } | ||
| 172 | disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 173 | m_player->stop(); | ||
| 174 | } | ||
| 175 | |||
| 176 | void AudioPlayer::testFinished() | ||
| 177 | { | ||
| 178 | if (!m_working) | ||
| 179 | qDebug() << "There's no sound for you :("; | ||
| 180 | disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | void AudioPlayer::mutedChanged_p(bool muted) | ||
| 185 | { | ||
| 186 | emit mutedChanged(muted); | ||
| 187 | } | ||
diff --git a/pacman-c++/audioplayer.h b/pacman-c++/audioplayer.h deleted file mode 100644 index 474b6ca..0000000 --- a/pacman-c++/audioplayer.h +++ /dev/null | |||
| @@ -1,65 +0,0 @@ | |||
| 1 | #ifndef AUDIOPLAYER_H | ||
| 2 | #define AUDIOPLAYER_H | ||
| 3 | |||
| 4 | #include <QObject> | ||
| 5 | #include <QList> | ||
| 6 | #include <QFile> | ||
| 7 | #include <phonon/phononnamespace.h> | ||
| 8 | |||
| 9 | namespace Phonon | ||
| 10 | { | ||
| 11 | class MediaObject; | ||
| 12 | class AudioOutput; | ||
| 13 | } | ||
| 14 | |||
| 15 | class AudioPlayer | ||
| 16 | : public QObject | ||
| 17 | { | ||
| 18 | Q_OBJECT | ||
| 19 | |||
| 20 | public: | ||
| 21 | enum Sound { | ||
| 22 | Intro = 0, | ||
| 23 | WakaWaka, | ||
| 24 | EatingCherry, | ||
| 25 | Die | ||
| 26 | }; | ||
| 27 | |||
| 28 | public: | ||
| 29 | AudioPlayer(); | ||
| 30 | static AudioPlayer *self(); | ||
| 31 | bool isWorking() const; | ||
| 32 | void stop(); | ||
| 33 | void setMuted(bool mute = true); | ||
| 34 | bool isMuted() const; | ||
| 35 | void play(Sound sound); | ||
| 36 | void clear(); | ||
| 37 | void enqueue(Sound sound); | ||
| 38 | void clearQueue() const; | ||
| 39 | Phonon::State state(); | ||
| 40 | |||
| 41 | signals: | ||
| 42 | void finished(); | ||
| 43 | void aboutToFinish(); | ||
| 44 | void mutedChanged(bool muted); | ||
| 45 | |||
| 46 | private: | ||
| 47 | void test(); | ||
| 48 | void preload(); | ||
| 49 | |||
| 50 | private slots: | ||
| 51 | void finished_p(); | ||
| 52 | void aboutToFinish_p(); | ||
| 53 | void stateChanged_p(Phonon::State newstate, Phonon::State oldstate); | ||
| 54 | void mutedChanged_p(bool muted); | ||
| 55 | void testFinished(); | ||
| 56 | |||
| 57 | private: | ||
| 58 | bool m_working; | ||
| 59 | Phonon::MediaObject *m_player; | ||
| 60 | Phonon::AudioOutput *m_output; | ||
| 61 | static AudioPlayer *m_instance; | ||
| 62 | QList<QFile *> m_sounds; | ||
| 63 | }; | ||
| 64 | |||
| 65 | #endif // AUDIOPLAYER_H | ||
diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index c35510a..394b446 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | #include "client.h" | 1 | #include "client.h" |
| 2 | #include "clicklabel.h" | 2 | #include "clicklabel.h" |
| 3 | #include "audioplayer.h" | 3 | #include "audio.h" |
| 4 | #include "pacman.pb.h" | 4 | #include "pacman.pb.h" |
| 5 | 5 | ||
| 6 | Client::Client() | 6 | Client::Client() |
| @@ -25,9 +25,9 @@ void Client::createMenu() | |||
| 25 | toggleSound->setFixedHeight(16); | 25 | toggleSound->setFixedHeight(16); |
| 26 | toggleSound->setAlignment(Qt::AlignBottom); | 26 | toggleSound->setAlignment(Qt::AlignBottom); |
| 27 | 27 | ||
| 28 | bool sound = AudioPlayer::self()->isWorking(); | 28 | bool sound = AudioManager::self()->isWorking(); |
| 29 | bool muted = !(sound && m_settings->value("muted", false).toBool()); | 29 | bool muted = !(sound && m_settings->value("muted", false).toBool()); |
| 30 | AudioPlayer::self()->setMuted(muted); | 30 | AudioManager::self()->setMuted(muted); |
| 31 | 31 | ||
| 32 | QImage img(muted ? ":/soundoff" : ":/soundon"); | 32 | QImage img(muted ? ":/soundoff" : ":/soundon"); |
| 33 | img.setColor(1, menuBar()->palette().color( | 33 | img.setColor(1, menuBar()->palette().color( |
| @@ -38,7 +38,7 @@ void Client::createMenu() | |||
| 38 | if (sound) | 38 | if (sound) |
| 39 | { | 39 | { |
| 40 | connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); | 40 | connect(toggleSound, SIGNAL(clicked()), this, SLOT(toggleSound())); |
| 41 | connect(AudioPlayer::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool))); | 41 | connect(AudioManager::self(), SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged(bool))); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | menuBar()->setCornerWidget(toggleSound); | 44 | menuBar()->setCornerWidget(toggleSound); |
| @@ -46,9 +46,9 @@ void Client::createMenu() | |||
| 46 | 46 | ||
| 47 | void Client::toggleSound() const | 47 | void Client::toggleSound() const |
| 48 | { | 48 | { |
| 49 | if (!AudioPlayer::self()->isWorking()) | 49 | if (!AudioManager::self()->isWorking()) |
| 50 | return; | 50 | return; |
| 51 | AudioPlayer::self()->setMuted(!AudioPlayer::self()->isMuted()); | 51 | AudioManager::self()->setMuted(!AudioManager::self()->isMuted()); |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | void Client::mutedChanged(bool muted) const | 54 | void Client::mutedChanged(bool muted) const |
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index c21e81d..ebd0f02 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | #include "actor.h" | 2 | #include "actor.h" |
| 3 | #include "block.h" | 3 | #include "block.h" |
| 4 | #include "constants.h" | 4 | #include "constants.h" |
| 5 | #include "audioplayer.h" | 5 | #include "audio.h" |
| 6 | #include "util.h" | 6 | #include "util.h" |
| 7 | #include "pacman.pb.h" | 7 | #include "pacman.pb.h" |
| 8 | 8 | ||
diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro index a346f20..45592c5 100644 --- a/pacman-c++/pacman.pro +++ b/pacman-c++/pacman.pro | |||
| @@ -11,7 +11,7 @@ SOURCES += pixmapitem.cpp \ | |||
| 11 | bonuspoint.cpp \ | 11 | bonuspoint.cpp \ |
| 12 | mainwidget.cpp \ | 12 | mainwidget.cpp \ |
| 13 | point.cpp \ | 13 | point.cpp \ |
| 14 | audioplayer.cpp \ | 14 | audio.cpp \ |
| 15 | clicklabel.cpp \ | 15 | clicklabel.cpp \ |
| 16 | sceneholder.cpp \ | 16 | sceneholder.cpp \ |
| 17 | util.cpp | 17 | util.cpp |
| @@ -24,7 +24,7 @@ HEADERS += pixmapitem.h \ | |||
| 24 | mainwidget.h \ | 24 | mainwidget.h \ |
| 25 | constants.h \ | 25 | constants.h \ |
| 26 | point.h \ | 26 | point.h \ |
| 27 | audioplayer.h \ | 27 | audio.h \ |
| 28 | clicklabel.h \ | 28 | clicklabel.h \ |
| 29 | sceneholder.h \ | 29 | sceneholder.h \ |
| 30 | util.h | 30 | util.h |
diff --git a/pacman-c++/pacman.server.pro b/pacman-c++/pacman.server.pro index 631bc50..4284286 100644 --- a/pacman-c++/pacman.server.pro +++ b/pacman-c++/pacman.server.pro | |||
| @@ -14,7 +14,7 @@ SOURCES += pixmapitem.cpp \ | |||
| 14 | bonuspoint.cpp \ | 14 | bonuspoint.cpp \ |
| 15 | mainwidget.cpp \ | 15 | mainwidget.cpp \ |
| 16 | point.cpp \ | 16 | point.cpp \ |
| 17 | audioplayer.cpp \ | 17 | audio.cpp \ |
| 18 | sceneholder.cpp \ | 18 | sceneholder.cpp \ |
| 19 | util.cpp \ | 19 | util.cpp \ |
| 20 | clicklabel.cpp | 20 | clicklabel.cpp |
| @@ -27,7 +27,7 @@ HEADERS += pixmapitem.h \ | |||
| 27 | mainwidget.h \ | 27 | mainwidget.h \ |
| 28 | constants.h \ | 28 | constants.h \ |
| 29 | point.h \ | 29 | point.h \ |
| 30 | audioplayer.h \ | 30 | audio.h \ |
| 31 | sceneholder.h \ | 31 | sceneholder.h \ |
| 32 | util.h \ | 32 | util.h \ |
| 33 | clicklabel.h | 33 | clicklabel.h |
