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 /pacman-c++/audio.cpp | |
| parent | 5f7d86446c8d7c8e03ce02f6188ee8dede4a6975 (diff) | |
| download | foop-d0eafb0124a39eeda6c00595a943ce9811d589c4.tar.gz foop-d0eafb0124a39eeda6c00595a943ce9811d589c4.tar.bz2 foop-d0eafb0124a39eeda6c00595a943ce9811d589c4.zip | |
major audio rewrite
Diffstat (limited to 'pacman-c++/audio.cpp')
| -rw-r--r-- | pacman-c++/audio.cpp | 262 |
1 files changed, 262 insertions, 0 deletions
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 | } | ||
