diff options
Diffstat (limited to 'pacman-c++/audioplayer.cpp')
| -rw-r--r-- | pacman-c++/audioplayer.cpp | 187 |
1 files changed, 0 insertions, 187 deletions
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 | } | ||
