[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>
This commit is contained in:
Konstantin Ritt
2015-04-11 23:35:49 +04:00
committed by Yoann Lopes
parent fe1046794d
commit b987b7087e
2 changed files with 169 additions and 167 deletions

View File

@@ -31,16 +31,15 @@
** **
****************************************************************************/ ****************************************************************************/
#include "qaudioengine_openal_p.h"
#include <QtCore/QMutex>
#include <QtCore/QThread> #include <QtCore/QThread>
#include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkAccessManager>
#include <QtCore/QUrl>
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include "qsamplecache_p.h" #include "qsamplecache_p.h"
#include "qaudioengine_openal_p.h"
#include "qdebug.h" #include "qdebug.h"
@@ -48,25 +47,39 @@
QT_USE_NAMESPACE 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) StaticSoundBufferAL::StaticSoundBufferAL(QObject *parent, const QUrl &url, QSampleCache *sampleLoader)
, m_ref(1) : QSoundBufferPrivateAL(parent),
, m_url(url) m_ref(1),
, m_alBuffer(0) m_url(url),
, m_isReady(false) m_alBuffer(0),
, m_sample(0) m_isReady(false),
, m_sampleLoader(sampleLoader) m_sample(0),
m_sampleLoader(sampleLoader)
{ {
#ifdef DEBUG_AUDIOENGINE #ifdef DEBUG_AUDIOENGINE
qDebug() << "creating new StaticSoundBufferOpenAL"; qDebug() << "creating new StaticSoundBufferOpenAL";
#endif #endif
} }
void load() StaticSoundBufferAL::~StaticSoundBufferAL()
{
if (m_sample)
m_sample->release();
if (m_alBuffer != 0) {
alGetError(); // clear error
alDeleteBuffers(1, &m_alBuffer);
QAudioEnginePrivate::checkNoError("delete buffer");
}
}
void StaticSoundBufferAL::load()
{ {
if (m_sample) if (m_sample)
return; return;
@@ -85,52 +98,23 @@ public:
} }
} }
~StaticSoundBufferAL() bool StaticSoundBufferAL::isReady() const
{ {
if (m_sample) return m_isReady;
m_sample->release();
if (m_alBuffer != 0) {
alGetError(); // clear error
alDeleteBuffers(1, &m_alBuffer);
QAudioEnginePrivate::checkNoError("delete buffer");
}
} }
void bindToSource(ALuint alSource) void StaticSoundBufferAL::bindToSource(ALuint alSource)
{ {
Q_ASSERT(m_alBuffer != 0); Q_ASSERT(m_alBuffer != 0);
alSourcei(alSource, AL_BUFFER, m_alBuffer); alSourcei(alSource, AL_BUFFER, m_alBuffer);
} }
void unbindFromSource(ALuint alSource) void StaticSoundBufferAL::unbindFromSource(ALuint alSource)
{ {
alSourcei(alSource, AL_BUFFER, 0); alSourcei(alSource, AL_BUFFER, 0);
} }
//called in application void StaticSoundBufferAL::sampleReady()
bool isReady() const
{
return m_isReady;
}
long addRef()
{
return ++m_ref;
}
long release()
{
return --m_ref;
}
long refCount() const
{
return m_ref;
}
public Q_SLOTS:
void sampleReady()
{ {
#ifdef DEBUG_AUDIOENGINE #ifdef DEBUG_AUDIOENGINE
qDebug() << "StaticSoundBufferOpenAL:sample[" << m_url << "] loaded"; qDebug() << "StaticSoundBufferOpenAL:sample[" << m_url << "] loaded";
@@ -170,7 +154,6 @@ public Q_SLOTS:
} }
alBufferData(m_alBuffer, alFormat, m_sample->data().data(), alBufferData(m_alBuffer, alFormat, m_sample->data().data(),
m_sample->data().size(), m_sample->format().sampleRate()); m_sample->data().size(), m_sample->format().sampleRate());
if (!QAudioEnginePrivate::checkNoError("fill buffer")) { if (!QAudioEnginePrivate::checkNoError("fill buffer")) {
decoderError(); decoderError();
return; return;
@@ -183,7 +166,7 @@ public Q_SLOTS:
emit ready(); emit ready();
} }
void decoderError() void StaticSoundBufferAL::decoderError()
{ {
qWarning() << "loading [" << m_url << "] failed"; qWarning() << "loading [" << m_url << "] failed";
@@ -196,20 +179,6 @@ public Q_SLOTS:
emit error(); emit error();
} }
private:
long m_ref;
QUrl m_url;
ALuint m_alBuffer;
bool m_isReady;
QSample *m_sample;
QSampleCache *m_sampleLoader;
};
QSoundBufferPrivateAL::QSoundBufferPrivateAL(QObject *parent)
: QSoundBuffer(parent)
{
}
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
QAudioEnginePrivate::QAudioEnginePrivate(QObject *parent) QAudioEnginePrivate::QAudioEnginePrivate(QObject *parent)
@@ -334,8 +303,7 @@ void QAudioEnginePrivate::releaseSoundBuffer(QSoundBuffer *buffer)
#ifdef DEBUG_AUDIOENGINE #ifdef DEBUG_AUDIOENGINE
qDebug() << "QAudioEnginePrivate: recycle sound buffer"; qDebug() << "QAudioEnginePrivate: recycle sound buffer";
#endif #endif
if (buffer->inherits("StaticSoundBufferAL")) { if (StaticSoundBufferAL *staticBuffer = qobject_cast<StaticSoundBufferAL *>(buffer)) {
StaticSoundBufferAL *staticBuffer = static_cast<StaticSoundBufferAL*>(buffer);
//decrement the reference count, still kept in memory for reuse //decrement the reference count, still kept in memory for reuse
staticBuffer->release(); staticBuffer->release();
//TODO implement some resource recycle strategy //TODO implement some resource recycle strategy
@@ -450,6 +418,3 @@ void QAudioEnginePrivate::updateSoundSources()
m_updateTimer.stop(); m_updateTimer.stop();
} }
} }
#include "qaudioengine_openal_p.moc"
//#include "moc_qaudioengine_openal_p.cpp"

View File

@@ -38,6 +38,7 @@
#include <QList> #include <QList>
#include <QMap> #include <QMap>
#include <QTimer> #include <QTimer>
#include <QUrl>
#if defined(HEADER_OPENAL_PREFIX) #if defined(HEADER_OPENAL_PREFIX)
#include <OpenAL/al.h> #include <OpenAL/al.h>
@@ -52,6 +53,9 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QSample;
class QSampleCache;
class QSoundBufferPrivateAL : public QSoundBuffer class QSoundBufferPrivateAL : public QSoundBuffer
{ {
Q_OBJECT Q_OBJECT
@@ -61,6 +65,39 @@ public:
virtual void unbindFromSource(ALuint alSource) = 0; 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 class QSoundSourcePrivate : public QSoundSource
{ {
Q_OBJECT Q_OBJECT
@@ -113,7 +150,7 @@ private:
qreal m_coneOuterGain; qreal m_coneOuterGain;
}; };
class QSampleCache;
class QAudioEnginePrivate : public QObject class QAudioEnginePrivate : public QObject
{ {
Q_OBJECT Q_OBJECT