diff options
author | Konstantin Ritt <ritt.ks@gmail.com> | 2015-04-11 23:35:49 +0400 |
---|---|---|
committer | Yoann Lopes <yoann.lopes@theqtcompany.com> | 2015-04-13 10:02:06 +0000 |
commit | b987b7087e5144135d150cd6e5aec359365f700f (patch) | |
tree | d1848b5fe53faa5c524fec682278688c13a88a11 /src/imports/audioengine | |
parent | fe1046794da6e547148bae9d384fafbb60e80304 (diff) |
[AudioEngine] Minor code clean-up
Move StaticSoundBufferAL declaration to .h file and replace
inherits("StaticSoundBufferAL") check with qobject_cast.
Change-Id: Icedc2796cf31b3a92335112a77cac24a5a34ac15
Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
Diffstat (limited to 'src/imports/audioengine')
-rw-r--r-- | src/imports/audioengine/qaudioengine_openal_p.cpp | 251 | ||||
-rw-r--r-- | src/imports/audioengine/qaudioengine_openal_p.h | 39 |
2 files changed, 146 insertions, 144 deletions
diff --git a/src/imports/audioengine/qaudioengine_openal_p.cpp b/src/imports/audioengine/qaudioengine_openal_p.cpp index 0a34a6b03..c71695ad0 100644 --- a/src/imports/audioengine/qaudioengine_openal_p.cpp +++ b/src/imports/audioengine/qaudioengine_openal_p.cpp @@ -31,16 +31,15 @@ ** ****************************************************************************/ +#include "qaudioengine_openal_p.h" + +#include <QtCore/QMutex> #include <QtCore/QThread> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkAccessManager> -#include <QtCore/QUrl> -#include <QtCore/QThread> -#include <QtCore/QMutex> #include "qsamplecache_p.h" -#include "qaudioengine_openal_p.h" #include "qdebug.h" @@ -48,166 +47,136 @@ QT_USE_NAMESPACE -class StaticSoundBufferAL : public QSoundBufferPrivateAL +QSoundBufferPrivateAL::QSoundBufferPrivateAL(QObject *parent) + : QSoundBuffer(parent) { - Q_OBJECT -public: - StaticSoundBufferAL(QObject *parent, const QUrl& url, QSampleCache *sampleLoader) - : QSoundBufferPrivateAL(parent) - , m_ref(1) - , m_url(url) - , m_alBuffer(0) - , m_isReady(false) - , m_sample(0) - , m_sampleLoader(sampleLoader) - { -#ifdef DEBUG_AUDIOENGINE - qDebug() << "creating new StaticSoundBufferOpenAL"; -#endif - } - - void load() - { - if (m_sample) - return; - m_sample = m_sampleLoader->requestSample(m_url); - connect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); - connect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); - switch (m_sample->state()) { - case QSample::Ready: - sampleReady(); - break; - case QSample::Error: - decoderError(); - break; - default: - break; - } - } +} - ~StaticSoundBufferAL() - { - if (m_sample) - m_sample->release(); - if (m_alBuffer != 0) { - alGetError(); // clear error - alDeleteBuffers(1, &m_alBuffer); - QAudioEnginePrivate::checkNoError("delete buffer"); - } - } +StaticSoundBufferAL::StaticSoundBufferAL(QObject *parent, const QUrl &url, QSampleCache *sampleLoader) + : QSoundBufferPrivateAL(parent), + m_ref(1), + m_url(url), + m_alBuffer(0), + m_isReady(false), + m_sample(0), + m_sampleLoader(sampleLoader) +{ +#ifdef DEBUG_AUDIOENGINE + qDebug() << "creating new StaticSoundBufferOpenAL"; +#endif +} - void bindToSource(ALuint alSource) - { - Q_ASSERT(m_alBuffer != 0); - alSourcei(alSource, AL_BUFFER, m_alBuffer); - } +StaticSoundBufferAL::~StaticSoundBufferAL() +{ + if (m_sample) + m_sample->release(); - void unbindFromSource(ALuint alSource) - { - alSourcei(alSource, AL_BUFFER, 0); + if (m_alBuffer != 0) { + alGetError(); // clear error + alDeleteBuffers(1, &m_alBuffer); + QAudioEnginePrivate::checkNoError("delete buffer"); } +} - //called in application - bool isReady() const - { - return m_isReady; +void StaticSoundBufferAL::load() +{ + if (m_sample) + return; + m_sample = m_sampleLoader->requestSample(m_url); + connect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); + connect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); + switch (m_sample->state()) { + case QSample::Ready: + sampleReady(); + break; + case QSample::Error: + decoderError(); + break; + default: + break; } +} - long addRef() - { - return ++m_ref; - } +bool StaticSoundBufferAL::isReady() const +{ + return m_isReady; +} - long release() - { - return --m_ref; - } +void StaticSoundBufferAL::bindToSource(ALuint alSource) +{ + Q_ASSERT(m_alBuffer != 0); + alSourcei(alSource, AL_BUFFER, m_alBuffer); +} - long refCount() const - { - return m_ref; - } +void StaticSoundBufferAL::unbindFromSource(ALuint alSource) +{ + alSourcei(alSource, AL_BUFFER, 0); +} -public Q_SLOTS: - void sampleReady() - { +void StaticSoundBufferAL::sampleReady() +{ #ifdef DEBUG_AUDIOENGINE - qDebug() << "StaticSoundBufferOpenAL:sample[" << m_url << "] loaded"; + qDebug() << "StaticSoundBufferOpenAL:sample[" << m_url << "] loaded"; #endif - disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); - disconnect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); - - if (m_sample->data().size() > 1024 * 1024 * 4) { - qWarning() << "source [" << m_url << "] size too large!"; - decoderError(); - return; - } + disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); + disconnect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); - if (m_sample->format().channelCount() > 2) { - qWarning() << "source [" << m_url << "] channel > 2!"; - decoderError(); - return; - } - - ALenum alFormat = 0; - if (m_sample->format().sampleSize() == 8) { - alFormat = m_sample->format().channelCount() == 1 ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8; - } else if (m_sample->format().sampleSize() == 16) { - alFormat = m_sample->format().channelCount() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; - } else { - qWarning() << "source [" << m_url << "] invalid sample size:" - << m_sample->format().sampleSize() << "(should be 8 or 16)"; - decoderError(); - return; - } - - alGenBuffers(1, &m_alBuffer); - if (!QAudioEnginePrivate::checkNoError("create buffer")) { - decoderError(); - return; - } - alBufferData(m_alBuffer, alFormat, m_sample->data().data(), - m_sample->data().size(), m_sample->format().sampleRate()); + if (m_sample->data().size() > 1024 * 1024 * 4) { + qWarning() << "source [" << m_url << "] size too large!"; + decoderError(); + return; + } - if (!QAudioEnginePrivate::checkNoError("fill buffer")) { - decoderError(); - return; - } + if (m_sample->format().channelCount() > 2) { + qWarning() << "source [" << m_url << "] channel > 2!"; + decoderError(); + return; + } - m_sample->release(); - m_sample = 0; + ALenum alFormat = 0; + if (m_sample->format().sampleSize() == 8) { + alFormat = m_sample->format().channelCount() == 1 ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8; + } else if (m_sample->format().sampleSize() == 16) { + alFormat = m_sample->format().channelCount() == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; + } else { + qWarning() << "source [" << m_url << "] invalid sample size:" + << m_sample->format().sampleSize() << "(should be 8 or 16)"; + decoderError(); + return; + } - m_isReady = true; - emit ready(); + alGenBuffers(1, &m_alBuffer); + if (!QAudioEnginePrivate::checkNoError("create buffer")) { + decoderError(); + return; + } + alBufferData(m_alBuffer, alFormat, m_sample->data().data(), + m_sample->data().size(), m_sample->format().sampleRate()); + if (!QAudioEnginePrivate::checkNoError("fill buffer")) { + decoderError(); + return; } - void decoderError() - { - qWarning() << "loading [" << m_url << "] failed"; + m_sample->release(); + m_sample = 0; - disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); - disconnect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); + m_isReady = true; + emit ready(); +} - m_sample->release(); - m_sample = 0; +void StaticSoundBufferAL::decoderError() +{ + qWarning() << "loading [" << m_url << "] failed"; - emit error(); - } + disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError())); + disconnect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady())); -private: - long m_ref; - QUrl m_url; - ALuint m_alBuffer; - bool m_isReady; - QSample *m_sample; - QSampleCache *m_sampleLoader; -}; + m_sample->release(); + m_sample = 0; -QSoundBufferPrivateAL::QSoundBufferPrivateAL(QObject *parent) - : QSoundBuffer(parent) -{ + emit error(); } @@ -334,8 +303,7 @@ void QAudioEnginePrivate::releaseSoundBuffer(QSoundBuffer *buffer) #ifdef DEBUG_AUDIOENGINE qDebug() << "QAudioEnginePrivate: recycle sound buffer"; #endif - if (buffer->inherits("StaticSoundBufferAL")) { - StaticSoundBufferAL *staticBuffer = static_cast<StaticSoundBufferAL*>(buffer); + if (StaticSoundBufferAL *staticBuffer = qobject_cast<StaticSoundBufferAL *>(buffer)) { //decrement the reference count, still kept in memory for reuse staticBuffer->release(); //TODO implement some resource recycle strategy @@ -450,6 +418,3 @@ void QAudioEnginePrivate::updateSoundSources() m_updateTimer.stop(); } } - -#include "qaudioengine_openal_p.moc" -//#include "moc_qaudioengine_openal_p.cpp" diff --git a/src/imports/audioengine/qaudioengine_openal_p.h b/src/imports/audioengine/qaudioengine_openal_p.h index 5a940d05b..9b3a7abea 100644 --- a/src/imports/audioengine/qaudioengine_openal_p.h +++ b/src/imports/audioengine/qaudioengine_openal_p.h @@ -38,6 +38,7 @@ #include <QList> #include <QMap> #include <QTimer> +#include <QUrl> #if defined(HEADER_OPENAL_PREFIX) #include <OpenAL/al.h> @@ -52,6 +53,9 @@ QT_BEGIN_NAMESPACE +class QSample; +class QSampleCache; + class QSoundBufferPrivateAL : public QSoundBuffer { Q_OBJECT @@ -61,6 +65,39 @@ public: virtual void unbindFromSource(ALuint alSource) = 0; }; + +class StaticSoundBufferAL : public QSoundBufferPrivateAL +{ + Q_OBJECT + +public: + StaticSoundBufferAL(QObject *parent, const QUrl &url, QSampleCache *sampleLoader); + ~StaticSoundBufferAL(); + + void load() Q_DECL_OVERRIDE; + bool isReady() const Q_DECL_OVERRIDE; + + void bindToSource(ALuint alSource) Q_DECL_OVERRIDE; + void unbindFromSource(ALuint alSource) Q_DECL_OVERRIDE; + + inline long addRef() { return ++m_ref; } + inline long release() { return --m_ref; } + inline long refCount() const { return m_ref; } + +public Q_SLOTS: + void sampleReady(); + void decoderError(); + +private: + long m_ref; + QUrl m_url; + ALuint m_alBuffer; + bool m_isReady; + QSample *m_sample; + QSampleCache *m_sampleLoader; +}; + + class QSoundSourcePrivate : public QSoundSource { Q_OBJECT @@ -113,7 +150,7 @@ private: qreal m_coneOuterGain; }; -class QSampleCache; + class QAudioEnginePrivate : public QObject { Q_OBJECT |