From d0eafb0124a39eeda6c00595a943ce9811d589c4 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 10 Apr 2011 20:50:54 +0200 Subject: major audio rewrite --- pacman-c++/audioplayer.cpp | 187 --------------------------------------------- 1 file changed, 187 deletions(-) delete mode 100644 pacman-c++/audioplayer.cpp (limited to 'pacman-c++/audioplayer.cpp') 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 @@ -#include "audioplayer.h" -#include -#include -#include -#include -#include -#include -#include -#include - -class Sleeper - : public QThread -{ -public: - static void sleep(unsigned long secs) - { - QThread::sleep(secs); - } - - static void msleep(unsigned long msecs) - { - QThread::msleep(msecs); - } - - static void usleep(unsigned long usecs) - { - QThread::usleep(usecs); - } -}; - -// the universe's only audio player -AudioPlayer *AudioPlayer::m_instance = NULL; - -AudioPlayer::AudioPlayer() - : m_working(false) -{ -#ifndef SERVER - m_player = new Phonon::MediaObject(this); - connect(m_player, SIGNAL(finished()), this, SLOT(finished_p())); - connect(m_player, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish_p())); - - m_output = new Phonon::AudioOutput(Phonon::MusicCategory, m_player); - connect(m_output, SIGNAL(mutedChanged(bool)), this, SLOT(mutedChanged_p(bool))); - - Phonon::createPath(m_player, m_output); - - preload(); - test(); -#endif // SERVER -} - -AudioPlayer *AudioPlayer::self() -{ - if (!m_instance) - m_instance = new AudioPlayer(); - return m_instance; -} - -bool AudioPlayer::isWorking() const -{ - return m_working; -} - -void AudioPlayer::stop() -{ - if (!isWorking()) - return; - m_player->stop(); -} - -void AudioPlayer::setMuted(bool mute) -{ - if (!isWorking()) - return; - m_output->setMuted(mute); -} - -bool AudioPlayer::isMuted() const -{ - if (!isWorking()) - return true; - return m_output->isMuted(); -} - -void AudioPlayer::play(AudioPlayer::Sound sound) -{ - if (!isWorking()) - { - emit finished_p(); - return; - } - - m_player->stop(); - m_player->setCurrentSource(Phonon::MediaSource(m_sounds[sound])); - m_player->play(); -} - -void AudioPlayer::clear() -{ - if (!isWorking()) - return; - m_player->clear(); -} - -void AudioPlayer::enqueue(AudioPlayer::Sound sound) -{ - if (!isWorking()) - return; - m_player->enqueue(Phonon::MediaSource(m_sounds[sound])); -} - -void AudioPlayer::clearQueue() const -{ - if (!isWorking()) - return; - m_player->clearQueue(); -} - -Phonon::State AudioPlayer::state() -{ - if (!isWorking()) - return Phonon::ErrorState; - return m_player->state(); -} - -void AudioPlayer::preload() -{ - m_sounds.clear(); - QDir sounds(":/sound"); - for(unsigned i = 1; i <= sounds.count(); ++i) - m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), m_player)); -} - -/* this is a simple hack to check if phonon can actually play sounds.. */ -void AudioPlayer::test() -{ - m_player->stop(); - connect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); - m_output->setVolume(0); - m_player->setCurrentSource(Phonon::MediaSource(m_sounds[AudioPlayer::WakaWaka])); - m_player->play(); - - QTimer timer; - timer.setSingleShot(true); - connect(&timer, SIGNAL(timeout()), this, SLOT(testFinished())); - timer.start(500); - while(timer.isActive()) - { - qApp->processEvents(); - Sleeper::msleep(1); - } -} - -void AudioPlayer::finished_p() -{ - emit finished(); -} - -void AudioPlayer::aboutToFinish_p() -{ - emit aboutToFinish(); -} - -void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State /* oldstate */) -{ - if (newstate != Phonon::ErrorState) - { - m_working = true; - m_output->setVolume(1); - qDebug() << "Sound is working for you!"; - } - disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); - m_player->stop(); -} - -void AudioPlayer::testFinished() -{ - if (!m_working) - qDebug() << "There's no sound for you :("; - disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); -} - - -void AudioPlayer::mutedChanged_p(bool muted) -{ - emit mutedChanged(muted); -} -- cgit v1.2.3