Moved audio asset processing to a separate thread

This commit is contained in:
seefo 2017-05-25 16:10:53 -07:00
parent 967148e3bf
commit 32b5ac5020
2 changed files with 51 additions and 15 deletions

View file

@ -13,6 +13,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <QRunnable>
#include <QThreadPool>
#include <QDataStream> #include <QDataStream>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkRequest>
@ -53,9 +55,25 @@ Sound::Sound(const QUrl& url, bool isStereo, bool isAmbisonic) :
} }
void Sound::downloadFinished(const QByteArray& data) { void Sound::downloadFinished(const QByteArray& data) {
SoundProcessor* soundProcessor = new SoundProcessor(_url, data, this);
QThreadPool::globalInstance()->start(soundProcessor);
}
void Sound::setReady(bool isReady) {
if (isReady) {
finishedLoading(true);
_isReady = true;
emit ready();
qCDebug(audio) << "Setting ready state for audio asset" << _url;
}
}
void SoundProcessor::run() {
// replace our byte array with the downloaded data // replace our byte array with the downloaded data
QByteArray rawAudioByteArray = QByteArray(data); QByteArray rawAudioByteArray = QByteArray(_data);
QString fileName = getURL().fileName().toLower(); QString fileName = _url.fileName().toLower();
//Sound* sound = dynamic_cast<Sound*>(_sound);
static const QString WAV_EXTENSION = ".wav"; static const QString WAV_EXTENSION = ".wav";
static const QString RAW_EXTENSION = ".raw"; static const QString RAW_EXTENSION = ".raw";
@ -63,28 +81,25 @@ void Sound::downloadFinished(const QByteArray& data) {
QByteArray outputAudioByteArray; QByteArray outputAudioByteArray;
int sampleRate = interpretAsWav(rawAudioByteArray, outputAudioByteArray); int sampleRate = _sound->interpretAsWav(rawAudioByteArray, outputAudioByteArray);
if (sampleRate != 0) { if (sampleRate != 0) {
downSample(outputAudioByteArray, sampleRate); _sound->downSample(outputAudioByteArray, sampleRate);
} }
} else if (fileName.endsWith(RAW_EXTENSION)) { } else if (fileName.endsWith(RAW_EXTENSION)) {
// check if this was a stereo raw file // check if this was a stereo raw file
// since it's raw the only way for us to know that is if the file was called .stereo.raw // since it's raw the only way for us to know that is if the file was called .stereo.raw
if (fileName.toLower().endsWith("stereo.raw")) { if (fileName.toLower().endsWith("stereo.raw")) {
_isStereo = true; _sound->setStereo(true);
qCDebug(audio) << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << getURL() << "as stereo audio file."; qCDebug(audio) << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << _url << "as stereo audio file.";
} }
// Process as 48khz RAW file // Process as 48khz RAW file
downSample(rawAudioByteArray, 48000); _sound->downSample(rawAudioByteArray, 48000);
} else { } else {
qCDebug(audio) << "Unknown sound file type"; qCDebug(audio) << "Unknown sound file type";
} }
finishedLoading(true); _sound->setReady(true);
_isReady = true;
emit ready();
} }
void Sound::downSample(const QByteArray& rawAudioByteArray, int sampleRate) { void Sound::downSample(const QByteArray& rawAudioByteArray, int sampleRate) {

View file

@ -12,6 +12,7 @@
#ifndef hifi_Sound_h #ifndef hifi_Sound_h
#define hifi_Sound_h #define hifi_Sound_h
#include <QRunnable>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkReply>
#include <QtScript/qscriptengine.h> #include <QtScript/qscriptengine.h>
@ -28,10 +29,15 @@ public:
bool isAmbisonic() const { return _isAmbisonic; } bool isAmbisonic() const { return _isAmbisonic; }
bool isReady() const { return _isReady; } bool isReady() const { return _isReady; }
float getDuration() const { return _duration; } float getDuration() const { return _duration; }
const QByteArray& getByteArray() const { return _byteArray; } const QByteArray& getByteArray() const { return _byteArray; }
void setStereo(bool stereo) { _isStereo = stereo; }
void setReady(bool ready);
void downSample(const QByteArray& rawAudioByteArray, int sampleRate);
int interpretAsWav(const QByteArray& inputAudioByteArray, QByteArray& outputAudioByteArray);
signals: signals:
void ready(); void ready();
@ -42,12 +48,27 @@ private:
bool _isReady; bool _isReady;
float _duration; // In seconds float _duration; // In seconds
void downSample(const QByteArray& rawAudioByteArray, int sampleRate);
int interpretAsWav(const QByteArray& inputAudioByteArray, QByteArray& outputAudioByteArray);
virtual void downloadFinished(const QByteArray& data) override; virtual void downloadFinished(const QByteArray& data) override;
}; };
class SoundProcessor : public QObject, public QRunnable {
Q_OBJECT
public:
SoundProcessor(const QUrl& url, const QByteArray& data, Sound* sound)
: _url(url), _data(data), _sound(sound)
{
}
virtual void run() override;
private:
QUrl _url;
QByteArray _data;
Sound* _sound;
};
typedef QSharedPointer<Sound> SharedSoundPointer; typedef QSharedPointer<Sound> SharedSoundPointer;
class SoundScriptingInterface : public QObject { class SoundScriptingInterface : public QObject {