[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:
committed by
Yoann Lopes
parent
fe1046794d
commit
b987b7087e
@@ -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,26 +47,40 @@
|
|||||||
|
|
||||||
QT_USE_NAMESPACE
|
QT_USE_NAMESPACE
|
||||||
|
|
||||||
class StaticSoundBufferAL : public QSoundBufferPrivateAL
|
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)
|
||||||
{
|
{
|
||||||
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
|
#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;
|
||||||
m_sample = m_sampleLoader->requestSample(m_url);
|
m_sample = m_sampleLoader->requestSample(m_url);
|
||||||
@@ -83,55 +96,26 @@ public:
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~StaticSoundBufferAL()
|
bool StaticSoundBufferAL::isReady() const
|
||||||
{
|
{
|
||||||
if (m_sample)
|
return m_isReady;
|
||||||
m_sample->release();
|
}
|
||||||
|
|
||||||
if (m_alBuffer != 0) {
|
void StaticSoundBufferAL::bindToSource(ALuint alSource)
|
||||||
alGetError(); // clear error
|
{
|
||||||
alDeleteBuffers(1, &m_alBuffer);
|
|
||||||
QAudioEnginePrivate::checkNoError("delete buffer");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void 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";
|
||||||
#endif
|
#endif
|
||||||
@@ -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;
|
||||||
@@ -181,10 +164,10 @@ public Q_SLOTS:
|
|||||||
|
|
||||||
m_isReady = true;
|
m_isReady = true;
|
||||||
emit ready();
|
emit ready();
|
||||||
}
|
}
|
||||||
|
|
||||||
void decoderError()
|
void StaticSoundBufferAL::decoderError()
|
||||||
{
|
{
|
||||||
qWarning() << "loading [" << m_url << "] failed";
|
qWarning() << "loading [" << m_url << "] failed";
|
||||||
|
|
||||||
disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError()));
|
disconnect(m_sample, SIGNAL(error()), this, SLOT(decoderError()));
|
||||||
@@ -194,20 +177,6 @@ public Q_SLOTS:
|
|||||||
m_sample = 0;
|
m_sample = 0;
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -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"
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user