[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 <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,169 +47,139 @@
QT_USE_NAMESPACE
class StaticSoundBufferAL : public QSoundBufferPrivateAL
{
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");
}
}
void bindToSource(ALuint alSource)
{
Q_ASSERT(m_alBuffer != 0);
alSourcei(alSource, AL_BUFFER, m_alBuffer);
}
void unbindFromSource(ALuint alSource)
{
alSourcei(alSource, AL_BUFFER, 0);
}
//called in application
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
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;
}
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 (!QAudioEnginePrivate::checkNoError("fill buffer")) {
decoderError();
return;
}
m_sample->release();
m_sample = 0;
m_isReady = true;
emit ready();
}
void decoderError()
{
qWarning() << "loading [" << m_url << "] failed";
disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError()));
disconnect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady()));
m_sample->release();
m_sample = 0;
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)
{
}
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
}
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)
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;
}
}
bool StaticSoundBufferAL::isReady() const
{
return m_isReady;
}
void StaticSoundBufferAL::bindToSource(ALuint alSource)
{
Q_ASSERT(m_alBuffer != 0);
alSourcei(alSource, AL_BUFFER, m_alBuffer);
}
void StaticSoundBufferAL::unbindFromSource(ALuint alSource)
{
alSourcei(alSource, AL_BUFFER, 0);
}
void StaticSoundBufferAL::sampleReady()
{
#ifdef DEBUG_AUDIOENGINE
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;
}
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 (!QAudioEnginePrivate::checkNoError("fill buffer")) {
decoderError();
return;
}
m_sample->release();
m_sample = 0;
m_isReady = true;
emit ready();
}
void StaticSoundBufferAL::decoderError()
{
qWarning() << "loading [" << m_url << "] failed";
disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError()));
disconnect(m_sample, SIGNAL(ready()), this, SLOT(sampleReady()));
m_sample->release();
m_sample = 0;
emit error();
}
/////////////////////////////////////////////////////////////////
QAudioEnginePrivate::QAudioEnginePrivate(QObject *parent)
: QObject(parent)
@@ -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"

View File

@@ -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