mirror of
https://github.com/lubosz/overte.git
synced 2025-04-24 07:34:02 +02:00
use a wrapper on audio injector for script ownership
This commit is contained in:
parent
449719bc7c
commit
79674fb94f
7 changed files with 105 additions and 25 deletions
|
@ -22,17 +22,6 @@
|
|||
|
||||
#include "AudioInjector.h"
|
||||
|
||||
QScriptValue injectorToScriptValue(QScriptEngine* engine, AudioInjector* const& in) {
|
||||
// when the script goes down we want to cleanup the injector
|
||||
QObject::connect(engine, &QScriptEngine::destroyed, in, &AudioInjector::stopAndDeleteLater);
|
||||
|
||||
return engine->newQObject(in);
|
||||
}
|
||||
|
||||
void injectorFromScriptValue(const QScriptValue& object, AudioInjector*& out) {
|
||||
out = qobject_cast<AudioInjector*>(object.toQObject());
|
||||
}
|
||||
|
||||
AudioInjector::AudioInjector(QObject* parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
|
|
|
@ -30,8 +30,6 @@ class AbstractAudioInterface;
|
|||
|
||||
class AudioInjector : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool isPlaying READ isPlaying)
|
||||
Q_PROPERTY(float loudness READ getLoudness)
|
||||
public:
|
||||
AudioInjector(QObject* parent);
|
||||
AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions);
|
||||
|
@ -49,6 +47,8 @@ public slots:
|
|||
void injectAudio();
|
||||
void restart();
|
||||
void stop();
|
||||
void stopAndDeleteLater();
|
||||
|
||||
void setOptions(AudioInjectorOptions& options) { _options = options; }
|
||||
void setCurrentSendPosition(int currentSendPosition) { _currentSendPosition = currentSendPosition; }
|
||||
float getLoudness() const { return _loudness; }
|
||||
|
@ -57,8 +57,6 @@ public slots:
|
|||
signals:
|
||||
void finished();
|
||||
|
||||
private slots:
|
||||
void stopAndDeleteLater();
|
||||
private:
|
||||
void injectToMixer();
|
||||
void injectLocally();
|
||||
|
@ -74,13 +72,7 @@ private:
|
|||
int _currentSendPosition = 0;
|
||||
AbstractAudioInterface* _localAudioInterface = NULL;
|
||||
AudioInjectorLocalBuffer* _localBuffer = NULL;
|
||||
|
||||
friend QScriptValue injectorToScriptValue(QScriptEngine* engine, AudioInjector* const& in);
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(AudioInjector*)
|
||||
|
||||
QScriptValue injectorToScriptValue(QScriptEngine* engine, AudioInjector* const& in);
|
||||
void injectorFromScriptValue(const QScriptValue& object, AudioInjector*& out);
|
||||
|
||||
#endif // hifi_AudioInjector_h
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include "ScriptAudioInjector.h"
|
||||
|
||||
#include "AudioScriptingInterface.h"
|
||||
|
||||
void registerAudioMetaTypes(QScriptEngine* engine) {
|
||||
|
@ -28,12 +30,16 @@ AudioScriptingInterface::AudioScriptingInterface() :
|
|||
|
||||
}
|
||||
|
||||
AudioInjector* AudioScriptingInterface::playSound(Sound* sound, const AudioInjectorOptions& injectorOptions) {
|
||||
ScriptAudioInjector* AudioScriptingInterface::playSound(Sound* sound, const AudioInjectorOptions& injectorOptions) {
|
||||
AudioInjector* injector = NULL;
|
||||
QMetaObject::invokeMethod(this, "invokedPlaySound", Qt::BlockingQueuedConnection,
|
||||
Q_RETURN_ARG(AudioInjector*, injector),
|
||||
Q_ARG(Sound*, sound), Q_ARG(const AudioInjectorOptions&, injectorOptions));
|
||||
return injector;
|
||||
if (injector) {
|
||||
return new ScriptAudioInjector(injector);
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const AudioInjectorOptions& injectorOptions) {
|
||||
|
@ -43,6 +49,7 @@ AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const Aud
|
|||
optionsCopy.stereo = sound->isStereo();
|
||||
|
||||
QThread* injectorThread = new QThread();
|
||||
qDebug() << "the injector thread is" << injectorThread;
|
||||
injectorThread->setObjectName("Audio Injector Thread");
|
||||
|
||||
AudioInjector* injector = new AudioInjector(sound, optionsCopy);
|
||||
|
@ -50,6 +57,8 @@ AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const Aud
|
|||
|
||||
injector->moveToThread(injectorThread);
|
||||
|
||||
qDebug() << "the injector is" << injector;
|
||||
|
||||
// start injecting when the injector thread starts
|
||||
connect(injectorThread, &QThread::started, injector, &AudioInjector::injectAudio);
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
#include <AudioInjector.h>
|
||||
#include <Sound.h>
|
||||
|
||||
class ScriptAudioInjector;
|
||||
|
||||
class AudioScriptingInterface : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -25,7 +27,7 @@ public:
|
|||
|
||||
protected:
|
||||
// this method is protected to stop C++ callers from calling, but invokable from script
|
||||
Q_INVOKABLE AudioInjector* playSound(Sound* sound, const AudioInjectorOptions& injectorOptions = AudioInjectorOptions());
|
||||
Q_INVOKABLE ScriptAudioInjector* playSound(Sound* sound, const AudioInjectorOptions& injectorOptions = AudioInjectorOptions());
|
||||
|
||||
signals:
|
||||
void mutedByMixer();
|
||||
|
|
36
libraries/script-engine/src/ScriptAudioInjector.cpp
Normal file
36
libraries/script-engine/src/ScriptAudioInjector.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
//
|
||||
// ScriptAudioInjector.cpp
|
||||
// libraries/script-engine/src
|
||||
//
|
||||
// Created by Stephen Birarda on 2015-02-11.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include "ScriptAudioInjector.h"
|
||||
|
||||
QScriptValue injectorToScriptValue(QScriptEngine* engine, ScriptAudioInjector* const& in) {
|
||||
// when the script goes down we want to cleanup the injector
|
||||
QObject::connect(engine, &QScriptEngine::destroyed, in, &ScriptAudioInjector::stopInjectorImmediately);
|
||||
|
||||
return engine->newQObject(in, QScriptEngine::ScriptOwnership);
|
||||
}
|
||||
|
||||
void injectorFromScriptValue(const QScriptValue& object, ScriptAudioInjector*& out) {
|
||||
out = qobject_cast<ScriptAudioInjector*>(object.toQObject());
|
||||
}
|
||||
|
||||
ScriptAudioInjector::ScriptAudioInjector(AudioInjector* injector) :
|
||||
_injector(injector)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ScriptAudioInjector::~ScriptAudioInjector() {
|
||||
}
|
||||
|
||||
void ScriptAudioInjector::stopInjectorImmediately() {
|
||||
_injector->stopAndDeleteLater();
|
||||
}
|
52
libraries/script-engine/src/ScriptAudioInjector.h
Normal file
52
libraries/script-engine/src/ScriptAudioInjector.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
//
|
||||
// ScriptAudioInjector.h
|
||||
// libraries/script-engine/src
|
||||
//
|
||||
// Created by Stephen Birarda on 2015-02-11.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_ScriptAudioInjector_h
|
||||
#define hifi_ScriptAudioInjector_h
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
#include <AudioInjector.h>
|
||||
|
||||
class ScriptAudioInjector : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(bool isPlaying READ isPlaying)
|
||||
Q_PROPERTY(float loudness READ getLoudness)
|
||||
public:
|
||||
ScriptAudioInjector(AudioInjector* injector);
|
||||
~ScriptAudioInjector();
|
||||
public slots:
|
||||
void restart() { _injector->restart(); }
|
||||
void stop() { _injector->stop(); }
|
||||
|
||||
void setOptions(AudioInjectorOptions& options) { _injector->setOptions(options); }
|
||||
|
||||
float getLoudness() const { return _injector->getLoudness(); }
|
||||
bool isPlaying() const { return _injector->isPlaying(); }
|
||||
|
||||
signals:
|
||||
void finished();
|
||||
|
||||
protected slots:
|
||||
void stopInjectorImmediately();
|
||||
private:
|
||||
AudioInjector* _injector;
|
||||
|
||||
friend QScriptValue injectorToScriptValue(QScriptEngine* engine, ScriptAudioInjector* const& in);
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(ScriptAudioInjector*)
|
||||
|
||||
QScriptValue injectorToScriptValue(QScriptEngine* engine, ScriptAudioInjector* const& in);
|
||||
void injectorFromScriptValue(const QScriptValue& object, ScriptAudioInjector*& out);
|
||||
|
||||
#endif // hifi_ScriptAudioInjector_h
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
#include <AudioConstants.h>
|
||||
#include <AudioEffectOptions.h>
|
||||
#include <AudioInjector.h>
|
||||
#include <AvatarData.h>
|
||||
#include <Bitstream.h>
|
||||
#include <CollisionInfo.h>
|
||||
|
@ -35,6 +34,7 @@
|
|||
#include "DataViewClass.h"
|
||||
#include "EventTypes.h"
|
||||
#include "MenuItemProperties.h"
|
||||
#include "ScriptAudioInjector.h"
|
||||
#include "ScriptEngine.h"
|
||||
#include "TypedArrays.h"
|
||||
#include "XMLHttpRequestClass.h"
|
||||
|
|
Loading…
Reference in a new issue