diff options
author | Justin McPherson <justin.mcpherson@nokia.com> | 2010-03-08 03:45:33 (GMT) |
---|---|---|
committer | Justin McPherson <justin.mcpherson@nokia.com> | 2010-03-08 05:53:08 (GMT) |
commit | 2e501c4d45dddba3db2d8817a27380615c1ce8fd (patch) | |
tree | 89598b65b64a0425868e8335e794ba380330fab9 | |
parent | a8b5200403742272811f095c0710193a27eed466 (diff) | |
download | Qt-2e501c4d45dddba3db2d8817a27380615c1ce8fd.zip Qt-2e501c4d45dddba3db2d8817a27380615c1ce8fd.tar.gz Qt-2e501c4d45dddba3db2d8817a27380615c1ce8fd.tar.bz2 |
Refactor QSoundEffect.
Reviewed-by: Dmytro Poplavskiy
-rw-r--r-- | src/multimedia/effects/qsoundeffect.cpp | 76 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_p.h | 14 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_pulse_p.cpp | 183 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_pulse_p.h | 39 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_qmedia_p.cpp | 99 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_qmedia_p.h | 34 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_qsound_p.cpp | 153 | ||||
-rw-r--r-- | src/multimedia/effects/qsoundeffect_qsound_p.h | 55 | ||||
-rw-r--r-- | src/multimedia/effects/wavedecoder_p.h | 4 |
9 files changed, 217 insertions, 440 deletions
diff --git a/src/multimedia/effects/qsoundeffect.cpp b/src/multimedia/effects/qsoundeffect.cpp index 2694023..3ccca6e 100644 --- a/src/multimedia/effects/qsoundeffect.cpp +++ b/src/multimedia/effects/qsoundeffect.cpp @@ -39,9 +39,6 @@ ** ****************************************************************************/ -#include "qmediacontent.h" -#include "qmediaplayer.h" - #include "qsoundeffect_p.h" #if defined(QT_MULTIMEDIA_PULSEAUDIO) @@ -139,17 +136,12 @@ QT_BEGIN_NAMESPACE */ QSoundEffect::QSoundEffect(QObject *parent) : - QObject(parent), - m_loopCount(1), - m_vol(100), - m_muted(false), - m_runningCount(0) + QObject(parent) { d = new QSoundEffectPrivate(this); - connect(d, SIGNAL(volumeChanged(int)), SIGNAL(volumeChanged())); - connect(d, SIGNAL(mutedChanged(bool)), SIGNAL(mutedChanged())); - connect(d, SIGNAL(durationChanged(qint64)), SIGNAL(durationChanged())); - connect(d, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(repeat())); + connect(d, SIGNAL(volumeChanged()), SIGNAL(volumeChanged())); + connect(d, SIGNAL(mutedChanged()), SIGNAL(mutedChanged())); + connect(d, SIGNAL(durationChanged()), SIGNAL(durationChanged())); } QSoundEffect::~QSoundEffect() @@ -159,97 +151,69 @@ QSoundEffect::~QSoundEffect() QUrl QSoundEffect::source() const { - return d != 0 ? d->media().canonicalUrl() : QUrl(); + return d->source(); } void QSoundEffect::setSource(const QUrl &url) { - if (d != 0 && d->media().canonicalUrl() == url) + if (d->source() == url) return; - d->setVolume(m_vol); - d->setMuted(m_muted); - d->setMedia(url); - - if (url.isEmpty()) - return; + d->setSource(url); emit sourceChanged(); } int QSoundEffect::loopCount() const { - return m_loopCount; + return d->loopCount(); } void QSoundEffect::setLoopCount(int loopCount) { - if (m_loopCount == loopCount) + if (d->loopCount() == loopCount) return; - m_loopCount = loopCount; + d->setLoopCount(loopCount); emit loopCountChanged(); } int QSoundEffect::volume() const { - return d != 0 ? d->volume() : m_vol; + return d->volume(); } void QSoundEffect::setVolume(int volume) { - if (m_vol == volume) + if (d->volume() == volume) return; - m_vol = volume; - if (d != 0) - d->setVolume(volume); - else - emit volumeChanged(); + d->setVolume(volume); + emit volumeChanged(); } bool QSoundEffect::isMuted() const { - return d != 0 ? d->isMuted() : m_muted; + return d->isMuted(); } void QSoundEffect::setMuted(bool muted) { - if (m_muted == muted) + if (d->isMuted() == muted) return; - m_muted = muted; - if (d != 0) - d->setMuted(muted); - else - emit mutedChanged(); + d->setMuted(muted); + emit mutedChanged(); } int QSoundEffect::duration() const { - return d != 0 ? d->duration() : 0; + return d->duration(); } void QSoundEffect::play() { - m_runningCount = 0; - - if (d != 0) - d->play(); -} - -void QSoundEffect::stop() -{ - if (d != 0) - d->stop(); -} - -void QSoundEffect::repeat() -{ - if (d->state() == QMediaPlayer::StoppedState) { - if (++m_runningCount < m_loopCount) - d->play(); - } + d->play(); } QT_END_NAMESPACE diff --git a/src/multimedia/effects/qsoundeffect_p.h b/src/multimedia/effects/qsoundeffect_p.h index c5554bf..29cb16c 100644 --- a/src/multimedia/effects/qsoundeffect_p.h +++ b/src/multimedia/effects/qsoundeffect_p.h @@ -90,28 +90,18 @@ public: int duration() const; -signals: +Q_SIGNALS: void sourceChanged(); void loopCountChanged(); void volumeChanged(); void mutedChanged(); void durationChanged(); -public slots: +public Q_SLOTS: void play(); - void stop(); - -private slots: - void repeat(); private: Q_DISABLE_COPY(QSoundEffect) - - int m_loopCount; - int m_vol; - bool m_muted; - int m_runningCount; - QSoundEffectPrivate* d; }; diff --git a/src/multimedia/effects/qsoundeffect_pulse_p.cpp b/src/multimedia/effects/qsoundeffect_pulse_p.cpp index d99bf4b..aa5a14d 100644 --- a/src/multimedia/effects/qsoundeffect_pulse_p.cpp +++ b/src/multimedia/effects/qsoundeffect_pulse_p.cpp @@ -55,10 +55,6 @@ #include <QtNetwork> #include <QTime> -#include "qmediacontent.h" -#include "qmediaplayer.h" -#include "qsoundeffect_p.h" - #include "wavedecoder_p.h" #include "qsoundeffect_pulse_p.h" @@ -240,11 +236,12 @@ QSoundEffectPrivate::QSoundEffectPrivate(QObject* parent): QObject(parent), m_muted(false), m_playQueued(false), - m_vol(100), + m_sampleLoaded(false), + m_volume(100), m_duration(0), m_dataUploaded(0), - m_state(QMediaPlayer::StoppedState), - m_status(QMediaPlayer::NoMedia), + m_loopCount(1), + m_runningCount(0), m_reply(0), m_stream(0), m_networkAccessManager(0) @@ -257,76 +254,53 @@ QSoundEffectPrivate::~QSoundEffectPrivate() unloadSample(); } -qint64 QSoundEffectPrivate::duration() const +QUrl QSoundEffectPrivate::source() const { - return m_duration; + return m_source; } -int QSoundEffectPrivate::volume() const +void QSoundEffectPrivate::setSource(const QUrl &url) { - return m_vol; -} + if (url.isEmpty()) { + m_source = QUrl(); + unloadSample(); + return; + } -bool QSoundEffectPrivate::isMuted() const -{ - return m_muted; -} + m_source = url; -QMediaContent QSoundEffectPrivate::media() const -{ - return m_media; + if (m_networkAccessManager == 0) + m_networkAccessManager = new QNetworkAccessManager(this); + + m_stream = m_networkAccessManager->get(QNetworkRequest(m_source)); + + unloadSample(); + loadSample(); } -QMediaPlayer::State QSoundEffectPrivate::state() const +int QSoundEffectPrivate::loopCount() const { - return m_state; + return m_loopCount; } -QMediaPlayer::MediaStatus QSoundEffectPrivate::mediaStatus() const +void QSoundEffectPrivate::setLoopCount(int loopCount) { - return m_status; + m_loopCount = loopCount; } -void QSoundEffectPrivate::play() +int QSoundEffectPrivate::volume() const { - if (m_status == QMediaPlayer::LoadingMedia) { - m_playQueued = true; - return; - } - - if (m_status != QMediaPlayer::BufferedMedia || - m_state == QMediaPlayer::PlayingState) - return; - - pa_volume_t m_vol = PA_VOLUME_NORM; - - daemon()->lock(); -#if defined(Q_WS_MAEMO_5) - m_vol = PA_VOLUME_NORM/100*((daemon()->volume()+m_vol)/2); -#endif - pa_operation_unref( - pa_context_play_sample(daemon()->context(), - m_name.constData(), - 0, - m_vol, - play_callback, - this) - ); - daemon()->unlock(); - - m_playbackTime.start(); - - emit stateChanged(m_state = QMediaPlayer::PlayingState); + return m_volume; } -void QSoundEffectPrivate::stop() +void QSoundEffectPrivate::setVolume(int volume) { - emit stateChanged(m_state = QMediaPlayer::StoppedState); + m_volume = volume; } -void QSoundEffectPrivate::setVolume(int volume) +bool QSoundEffectPrivate::isMuted() const { - m_vol = volume; + return m_muted; } void QSoundEffectPrivate::setMuted(bool muted) @@ -334,34 +308,28 @@ void QSoundEffectPrivate::setMuted(bool muted) m_muted = muted; } -void QSoundEffectPrivate::setMedia(const QMediaContent &media) +qint64 QSoundEffectPrivate::duration() const { - if (media.isNull()) { - m_media = QMediaContent(); - unloadSample(); + return m_duration; +} + +void QSoundEffectPrivate::play() +{ + if (!m_sampleLoaded) { + m_playQueued = true; return; } - if (m_media == media) - return; - m_media = media; - if (m_networkAccessManager == 0) - m_networkAccessManager = new QNetworkAccessManager(this); - - m_stream = m_networkAccessManager->get(QNetworkRequest(m_media.canonicalUrl())); - - unloadSample(); - loadSample(); + m_runningCount += m_loopCount; - emit mediaChanged(m_media); + playSample(); } void QSoundEffectPrivate::decoderReady() { if (m_waveDecoder->size() >= PA_SCACHE_ENTRY_SIZE_MAX) { - m_status = QMediaPlayer::InvalidMedia; - emit mediaStatusChanged(m_status); - qWarning("QtPulseAudio: attempting to load to large a sample"); + m_waveDecoder->deleteLater(); + qWarning("QSoundEffect(pulseaudio): Attempting to load to large a sample"); return; } @@ -380,52 +348,65 @@ void QSoundEffectPrivate::decoderReady() void QSoundEffectPrivate::decoderError() { - emit mediaStatusChanged(m_status = QMediaPlayer::InvalidMedia); + qWarning("QSoundEffect(pulseaudio): Error decoding source"); } void QSoundEffectPrivate::checkPlayTime() { int elapsed = m_playbackTime.elapsed(); - if (elapsed >= m_duration) { - m_state = QMediaPlayer::StoppedState; - emit stateChanged(m_state); - } - else + if (elapsed < m_duration) startTimer(m_duration - elapsed); } void QSoundEffectPrivate::loadSample() { + m_sampleLoaded = false; + m_dataUploaded = 0; m_waveDecoder = new WaveDecoder(m_stream); connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady())); connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError())); - - m_status = QMediaPlayer::LoadingMedia; - emit mediaStatusChanged(m_status); } void QSoundEffectPrivate::unloadSample() { - if (m_status != QMediaPlayer::BufferedMedia) + if (!m_sampleLoaded) return; - m_status = QMediaPlayer::NoMedia; - daemon()->lock(); pa_context_remove_sample(daemon()->context(), m_name.constData(), NULL, NULL); daemon()->unlock(); m_duration = 0; m_dataUploaded = 0; + m_sampleLoaded = false; +} + +void QSoundEffectPrivate::playSample() +{ + pa_volume_t volume = PA_VOLUME_NORM; + + daemon()->lock(); +#ifdef Q_WS_MAEMO_5 + volume = PA_VOLUME_NORM / 100 * ((daemon()->volume() + m_volume) / 2); +#endif + pa_operation_unref( + pa_context_play_sample(daemon()->context(), + m_name.constData(), + 0, + volume, + play_callback, + this) + ); + daemon()->unlock(); + + m_playbackTime.start(); } void QSoundEffectPrivate::timerEvent(QTimerEvent *event) { - if (m_state == QMediaPlayer::PlayingState) { - m_state = QMediaPlayer::StoppedState; - emit stateChanged(m_state); - } + if (m_runningCount > 0) + playSample(); killTimer(event->timerId()); } @@ -456,15 +437,12 @@ void QSoundEffectPrivate::stream_write_callback(pa_stream *s, size_t length, voi pa_stream_finish_upload(s); self->m_duration = self->m_waveDecoder->duration(); - emit self->durationChanged(self->m_duration); - - self->m_status = QMediaPlayer::BufferedMedia; - emit self->mediaStatusChanged(self->m_status); + emit self->durationChanged(); self->m_waveDecoder->deleteLater(); - if (!self->m_media.isNull()) - self->m_stream->deleteLater(); + self->m_stream->deleteLater(); + self->m_sampleLoaded = true; if (self->m_playQueued) { self->m_playQueued = false; QMetaObject::invokeMethod(self, "play"); @@ -474,8 +452,6 @@ void QSoundEffectPrivate::stream_write_callback(pa_stream *s, size_t length, voi void QSoundEffectPrivate::stream_state_callback(pa_stream *s, void *userdata) { - QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata); - switch (pa_stream_get_state(s)) { case PA_STREAM_CREATING: case PA_STREAM_READY: @@ -484,8 +460,7 @@ void QSoundEffectPrivate::stream_state_callback(pa_stream *s, void *userdata) case PA_STREAM_FAILED: default: - self->m_status = QMediaPlayer::InvalidMedia; - emit self->mediaStatusChanged(self->m_status); + qWarning("QSoundEffect(pulseaudio): Error in pulse audio stream"); break; } } @@ -496,14 +471,10 @@ void QSoundEffectPrivate::play_callback(pa_context *c, int success, void *userda QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata); - if (success == 1) + if (success == 1) { + self->m_runningCount--; QMetaObject::invokeMethod(self, "checkPlayTime", Qt::QueuedConnection); - else { - self->m_state = QMediaPlayer::StoppedState; - emit self->stateChanged(self->m_state); } } QT_END_NAMESPACE - - diff --git a/src/multimedia/effects/qsoundeffect_pulse_p.h b/src/multimedia/effects/qsoundeffect_pulse_p.h index 247f8a3..7353e4f 100644 --- a/src/multimedia/effects/qsoundeffect_pulse_p.h +++ b/src/multimedia/effects/qsoundeffect_pulse_p.h @@ -77,30 +77,25 @@ public: explicit QSoundEffectPrivate(QObject* parent); ~QSoundEffectPrivate(); - qint64 duration() const; + QUrl source() const; + void setSource(const QUrl &url); + int loopCount() const; + void setLoopCount(int loopCount); int volume() const; + void setVolume(int volume); bool isMuted() const; - QMediaContent media() const; - QMediaPlayer::State state() const; - QMediaPlayer::MediaStatus mediaStatus() const; + void setMuted(bool muted); + qint64 duration() const; public Q_SLOTS: void play(); - void stop(); - void setVolume(int volume); - void setMuted(bool muted); - void setMedia(const QMediaContent &media); Q_SIGNALS: - void mediaChanged(const QMediaContent &media); - void mediaStatusChanged(QMediaPlayer::MediaStatus status); - void stateChanged(QMediaPlayer::State newState); - void durationChanged(qint64 duration); - void volumeChanged(int volume); - void mutedChanged(bool muted); - void error(QMediaPlayer::Error error); - -private slots: + void durationChanged(); + void volumeChanged(); + void mutedChanged(); + +private Q_SLOTS: void decoderReady(); void decoderError(); void checkPlayTime(); @@ -108,6 +103,7 @@ private slots: private: void loadSample(); void unloadSample(); + void playSample(); void timerEvent(QTimerEvent *event); @@ -117,14 +113,15 @@ private: bool m_muted; bool m_playQueued; - int m_vol; + bool m_sampleLoaded; + int m_volume; int m_duration; int m_dataUploaded; + int m_loopCount; + int m_runningCount; + QUrl m_source; QTime m_playbackTime; - QMediaPlayer::State m_state; - QMediaPlayer::MediaStatus m_status; QByteArray m_name; - QMediaContent m_media; QNetworkReply *m_reply; WaveDecoder *m_waveDecoder; QIODevice *m_stream; diff --git a/src/multimedia/effects/qsoundeffect_qmedia_p.cpp b/src/multimedia/effects/qsoundeffect_qmedia_p.cpp index 48fb257..e670a36 100644 --- a/src/multimedia/effects/qsoundeffect_qmedia_p.cpp +++ b/src/multimedia/effects/qsoundeffect_qmedia_p.cpp @@ -50,117 +50,90 @@ // We mean it. // -#include <QtCore/qcoreapplication.h> +#include "qsoundeffect_qmedia_p.h" -#include "qmediacontent.h" -#include "qmediaplayer.h" +#include <QtCore/qcoreapplication.h> -#include "qsoundeffect_p.h" -#include "qsoundeffect_qmedia_p.h" +#include <QtMultimedia/qmediacontent.h> +#include <QtMultimedia/qmediaplayer.h> QT_BEGIN_NAMESPACE QSoundEffectPrivate::QSoundEffectPrivate(QObject* parent): QObject(parent), - m_muted(false), - m_vol(100), + m_loopCount(1), + m_runningCount(0), m_player(0) { + m_player = new QMediaPlayer(this, QMediaPlayer::LowLatency); + connect(m_player, SIGNAL(volumeChanged(int)), SIGNAL(volumeChanged())); + connect(m_player, SIGNAL(mutedChanged(bool)), SIGNAL(mutedChanged())); + connect(m_player, SIGNAL(durationChanged(qint64)), SIGNAL(durationChanged())); + connect(m_player, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(stateChanged(QMediaPlayer::State))); } QSoundEffectPrivate::~QSoundEffectPrivate() { - if (m_player) delete m_player; } -qint64 QSoundEffectPrivate::duration() const +QUrl QSoundEffectPrivate::source() const { - if (m_player) return m_player->duration(); - - return 0; + return m_player->media().canonicalUrl(); } -int QSoundEffectPrivate::volume() const +void QSoundEffectPrivate::setSource(const QUrl &url) { - if (m_player) return m_player->volume(); - - return m_vol; + m_player->setMedia(url); } -bool QSoundEffectPrivate::isMuted() const +int QSoundEffectPrivate::loopCount() const { - if (m_player) return m_player->isMuted(); - - return m_muted; + return m_loopCount; } -QMediaContent QSoundEffectPrivate::media() const +void QSoundEffectPrivate::setLoopCount(int loopCount) { - if (m_player) return m_player->media(); - - return QMediaContent(); + m_loopCount = loopCount; } -QMediaPlayer::State QSoundEffectPrivate::state() const +int QSoundEffectPrivate::volume() const { - if (m_player) return m_player->state(); - - return QMediaPlayer::StoppedState; + return m_player->volume(); } -QMediaPlayer::MediaStatus QSoundEffectPrivate::mediaStatus() const +void QSoundEffectPrivate::setVolume(int volume) { - if (m_player) return m_player->mediaStatus(); - - return QMediaPlayer::UnknownMediaStatus; + m_player->setVolume(volume); } -void QSoundEffectPrivate::play() +bool QSoundEffectPrivate::isMuted() const { - if (m_player && !m_player->isMuted()) - m_player->play(); + return m_player->isMuted(); } -void QSoundEffectPrivate::stop() +void QSoundEffectPrivate::setMuted(bool muted) { - if (m_player) - m_player->stop(); + m_player->setMuted(muted); } -void QSoundEffectPrivate::setVolume(int volume) +int QSoundEffectPrivate::duration() const { - m_vol = volume; - - if (m_player) - m_player->setVolume(volume); + return m_player->duration(); } -void QSoundEffectPrivate::setMuted(bool muted) +void QSoundEffectPrivate::play() { - m_muted = muted; - - if (m_player) - m_player->setMuted(muted); + m_runningCount += m_loopCount; + m_player->play(); } -void QSoundEffectPrivate::setMedia(const QMediaContent &media) +void QSoundEffectPrivate::stateChanged(QMediaPlayer::State state) { - if (media.isNull()) - return; - - if (m_player == 0) { - m_player = new QMediaPlayer(this, QMediaPlayer::LowLatency); - m_player->setVolume(m_vol); - m_player->setMuted(m_muted); - - connect(m_player, SIGNAL(volumeChanged(int)), SIGNAL(volumeChanged(int))); - connect(m_player, SIGNAL(mutedChanged(bool)), SIGNAL(mutedChanged(bool))); - connect(m_player, SIGNAL(durationChanged(qint64)), SIGNAL(durationChanged(qint64))); - connect(m_player, SIGNAL(stateChanged(QMediaPlayer::State)), SIGNAL(stateChanged(QMediaPlayer::State))); + if (state == QMediaPlayer::StoppedState) { + if (--m_runningCount > 0) + m_player->play(); } - - m_player->setMedia(media.canonicalUrl()); } QT_END_NAMESPACE diff --git a/src/multimedia/effects/qsoundeffect_qmedia_p.h b/src/multimedia/effects/qsoundeffect_qmedia_p.h index 8267f79..73682ea 100644 --- a/src/multimedia/effects/qsoundeffect_qmedia_p.h +++ b/src/multimedia/effects/qsoundeffect_qmedia_p.h @@ -57,14 +57,12 @@ #include <QtCore/qobject.h> #include <QtCore/qurl.h> #include <QtMultimedia/qmediaplayer.h> -#include "qsoundeffect_p.h" QT_BEGIN_HEADER QT_BEGIN_NAMESPACE -class WaveDecoder; class QSoundEffectPrivate : public QObject { @@ -73,32 +71,30 @@ public: explicit QSoundEffectPrivate(QObject* parent); ~QSoundEffectPrivate(); - qint64 duration() const; + QUrl source() const; + void setSource(const QUrl &url); + int loopCount() const; + void setLoopCount(int loopCount); int volume() const; + void setVolume(int volume); bool isMuted() const; - QMediaContent media() const; - QMediaPlayer::State state() const; - QMediaPlayer::MediaStatus mediaStatus() const; + void setMuted(bool muted); + int duration() const; public Q_SLOTS: void play(); - void stop(); - void setVolume(int volume); - void setMuted(bool muted); - void setMedia(const QMediaContent &media); Q_SIGNALS: - void mediaChanged(const QMediaContent &media); - void mediaStatusChanged(QMediaPlayer::MediaStatus status); - void stateChanged(QMediaPlayer::State newState); - void durationChanged(qint64 duration); - void volumeChanged(int volume); - void mutedChanged(bool muted); - void error(QMediaPlayer::Error error); + void durationChanged(); + void volumeChanged(); + void mutedChanged(); + +private Q_SLOTS: + void stateChanged(QMediaPlayer::State); private: - bool m_muted; - int m_vol; + int m_loopCount; + int m_runningCount; QMediaPlayer *m_player; }; diff --git a/src/multimedia/effects/qsoundeffect_qsound_p.cpp b/src/multimedia/effects/qsoundeffect_qsound_p.cpp index df160a9..e6e889e 100644 --- a/src/multimedia/effects/qsoundeffect_qsound_p.cpp +++ b/src/multimedia/effects/qsoundeffect_qsound_p.cpp @@ -50,102 +50,72 @@ // We mean it. // +#include "qsoundeffect_qsound_p.h" + #include <QtCore/qcoreapplication.h> -#include <QtCore/qtimer.h> -#include <QtCore/qfile.h> #include <QtGui/qsound.h> -#include <QtMultimedia/qaudioformat.h> -#include <QDebug> -#include "qmediacontent.h" -#include "qmediaplayer.h" -#include "qsoundeffect_p.h" - -#include "wavedecoder_p.h" - -#include "qsoundeffect_qsound_p.h" QT_BEGIN_NAMESPACE QSoundEffectPrivate::QSoundEffectPrivate(QObject* parent): QObject(parent), - m_queued(false), m_muted(false), - m_state(QMediaPlayer::StoppedState), - m_status(QMediaPlayer::NoMedia), - m_file(0), + m_loopCount(1), + m_volume(100), m_sound(0) { - m_timer = new QTimer(this); - connect(m_timer,SIGNAL(timeout()),SLOT(checkPlayTime())); - m_media = QMediaContent(); } QSoundEffectPrivate::~QSoundEffectPrivate() { - if (m_sound) delete m_sound; - if (m_waveDecoder) delete m_waveDecoder; - m_file->close(); } -qint64 QSoundEffectPrivate::duration() const +QUrl QSoundEffectPrivate::source() const { - if (m_waveDecoder) - return m_waveDecoder->size(); - - return 0; + return m_source; } -int QSoundEffectPrivate::volume() const +void QSoundEffectPrivate::setSource(const QUrl &url) { - return 100; -} + if (url.isEmpty() || url.scheme() != QLatin1String("file")) { + m_source = QUrl(); + return; + } -bool QSoundEffectPrivate::isMuted() const -{ - return m_muted; -} + if (m_sound != 0) + delete m_sound; -QMediaContent QSoundEffectPrivate::media() const -{ - return m_media; + m_source = url; + m_sound = new QSound(m_source.toLocalFile(), this); + m_sound->setLoops(m_loopCount); } -QMediaPlayer::State QSoundEffectPrivate::state() const +int QSoundEffectPrivate::loopCount() const { - return m_state; + return m_loopCount; } -QMediaPlayer::MediaStatus QSoundEffectPrivate::mediaStatus() const +void QSoundEffectPrivate::setLoopCount(int lc) { - return m_status; + m_loopCount = lc; + if (m_sound) + m_sound->setLoops(lc); } -void QSoundEffectPrivate::play() +int QSoundEffectPrivate::volume() const { - if (m_sound && !m_muted) { - m_queued = false; - m_timer->start(20); - m_playbackTime.start(); - m_sound->play(); - emit stateChanged(m_state = QMediaPlayer::PlayingState); - } else if (m_status == QMediaPlayer::LoadingMedia) - m_queued = true; + return m_volume; } -void QSoundEffectPrivate::stop() +void QSoundEffectPrivate::setVolume(int v) { - m_timer->stop(); - - if (m_sound) { - m_sound->stop(); - emit stateChanged(m_state = QMediaPlayer::StoppedState); - } + m_volume = v; } -void QSoundEffectPrivate::setVolume(int volume) +bool QSoundEffectPrivate::isMuted() const { - Q_UNUSED(volume) + return m_muted; } void QSoundEffectPrivate::setMuted(bool muted) @@ -153,73 +123,14 @@ void QSoundEffectPrivate::setMuted(bool muted) m_muted = muted; } -void QSoundEffectPrivate::setMedia(const QMediaContent &media) -{ - m_queued = false; - - if (media.isNull() || media.canonicalUrl().scheme() != QLatin1String("file")) { - m_media = QMediaContent(); - return; - } - if (m_media == media) - return; - - m_media = media; - m_file = new QFile(m_media.canonicalUrl().toLocalFile()); - m_file->open(QIODevice::ReadOnly|QIODevice::Unbuffered); - - unloadSample(); - loadSample(); - - emit mediaChanged(m_media); -} - -void QSoundEffectPrivate::decoderReady() -{ - m_file->close(); - m_sound = new QSound(m_media.canonicalUrl().toLocalFile()); - emit mediaStatusChanged(m_status = QMediaPlayer::LoadedMedia); - - if (m_queued) - play(); -} - -void QSoundEffectPrivate::decoderError() -{ - m_file->close(); - emit mediaStatusChanged(m_status = QMediaPlayer::InvalidMedia); -} - -void QSoundEffectPrivate::checkPlayTime() +int QSoundEffectPrivate::duration() const { - if (m_sound->isFinished()) { - m_timer->stop(); - m_state = QMediaPlayer::StoppedState; - emit stateChanged(m_state); - } + return 1000; } -void QSoundEffectPrivate::loadSample() -{ - m_waveDecoder = new WaveDecoder(m_file); - connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady())); - connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError())); - - m_status = QMediaPlayer::LoadingMedia; - emit mediaStatusChanged(m_status); -} - -void QSoundEffectPrivate::unloadSample() +void QSoundEffectPrivate::play() { - if (m_sound == 0) - return; - - m_status = QMediaPlayer::NoMedia; - - if (m_sound) - delete m_sound; - - m_sound = 0; + m_sound->play(); } QT_END_NAMESPACE diff --git a/src/multimedia/effects/qsoundeffect_qsound_p.h b/src/multimedia/effects/qsoundeffect_qsound_p.h index 45c0888..377c8a7 100644 --- a/src/multimedia/effects/qsoundeffect_qsound_p.h +++ b/src/multimedia/effects/qsoundeffect_qsound_p.h @@ -55,20 +55,14 @@ #include <QtCore/qobject.h> -#include <QtCore/qdatetime.h> -#include <QtMultimedia/qmediaplayer.h> - -#include "qsoundeffect_p.h" +#include <QtCore/qurl.h> QT_BEGIN_HEADER QT_BEGIN_NAMESPACE -class QTimer; class QSound; -class QFile; -class WaveDecoder; class QSoundEffectPrivate : public QObject { @@ -77,49 +71,30 @@ public: explicit QSoundEffectPrivate(QObject* parent); ~QSoundEffectPrivate(); - qint64 duration() const; + QUrl source() const; + void setSource(const QUrl &url); + int loopCount() const; + void setLoopCount(int loopCount); int volume() const; + void setVolume(int volume); bool isMuted() const; - QMediaContent media() const; - QMediaPlayer::State state() const; - QMediaPlayer::MediaStatus mediaStatus() const; + void setMuted(bool muted); + int duration() const; public Q_SLOTS: void play(); - void stop(); - void setVolume(int volume); - void setMuted(bool muted); - void setMedia(const QMediaContent &media); Q_SIGNALS: - void mediaChanged(const QMediaContent &media); - void mediaStatusChanged(QMediaPlayer::MediaStatus status); - void stateChanged(QMediaPlayer::State newState); - void durationChanged(qint64 duration); - void volumeChanged(int volume); - void mutedChanged(bool muted); - void error(QMediaPlayer::Error error); - -private slots: - void decoderReady(); - void decoderError(); - void checkPlayTime(); + void durationChanged(); + void volumeChanged(); + void mutedChanged(); private: - void loadSample(); - void unloadSample(); - - bool m_queued; - bool m_muted; - QTime m_playbackTime; - QMediaPlayer::State m_state; - QMediaPlayer::MediaStatus m_status; - QFile *m_file; - QByteArray m_name; - QMediaContent m_media; - WaveDecoder *m_waveDecoder; + bool m_muted; + int m_loopCount; + int m_volume; QSound *m_sound; - QTimer *m_timer; + QUrl m_source; }; QT_END_NAMESPACE diff --git a/src/multimedia/effects/wavedecoder_p.h b/src/multimedia/effects/wavedecoder_p.h index fa1f77e..c1892bb 100644 --- a/src/multimedia/effects/wavedecoder_p.h +++ b/src/multimedia/effects/wavedecoder_p.h @@ -77,11 +77,11 @@ public: bool isSequential() const; qint64 bytesAvailable() const; -signals: +Q_SIGNALS: void formatKnown(); void invalidFormat(); -private slots: +private Q_SLOTS: void handleData(); private: |