mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 17:41:12 +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"
|
#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) :
|
AudioInjector::AudioInjector(QObject* parent) :
|
||||||
QObject(parent)
|
QObject(parent)
|
||||||
{
|
{
|
||||||
|
|
|
@ -30,8 +30,6 @@ class AbstractAudioInterface;
|
||||||
|
|
||||||
class AudioInjector : public QObject {
|
class AudioInjector : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(bool isPlaying READ isPlaying)
|
|
||||||
Q_PROPERTY(float loudness READ getLoudness)
|
|
||||||
public:
|
public:
|
||||||
AudioInjector(QObject* parent);
|
AudioInjector(QObject* parent);
|
||||||
AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions);
|
AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions);
|
||||||
|
@ -49,6 +47,8 @@ public slots:
|
||||||
void injectAudio();
|
void injectAudio();
|
||||||
void restart();
|
void restart();
|
||||||
void stop();
|
void stop();
|
||||||
|
void stopAndDeleteLater();
|
||||||
|
|
||||||
void setOptions(AudioInjectorOptions& options) { _options = options; }
|
void setOptions(AudioInjectorOptions& options) { _options = options; }
|
||||||
void setCurrentSendPosition(int currentSendPosition) { _currentSendPosition = currentSendPosition; }
|
void setCurrentSendPosition(int currentSendPosition) { _currentSendPosition = currentSendPosition; }
|
||||||
float getLoudness() const { return _loudness; }
|
float getLoudness() const { return _loudness; }
|
||||||
|
@ -57,8 +57,6 @@ public slots:
|
||||||
signals:
|
signals:
|
||||||
void finished();
|
void finished();
|
||||||
|
|
||||||
private slots:
|
|
||||||
void stopAndDeleteLater();
|
|
||||||
private:
|
private:
|
||||||
void injectToMixer();
|
void injectToMixer();
|
||||||
void injectLocally();
|
void injectLocally();
|
||||||
|
@ -74,13 +72,7 @@ private:
|
||||||
int _currentSendPosition = 0;
|
int _currentSendPosition = 0;
|
||||||
AbstractAudioInterface* _localAudioInterface = NULL;
|
AbstractAudioInterface* _localAudioInterface = NULL;
|
||||||
AudioInjectorLocalBuffer* _localBuffer = 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
|
#endif // hifi_AudioInjector_h
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "ScriptAudioInjector.h"
|
||||||
|
|
||||||
#include "AudioScriptingInterface.h"
|
#include "AudioScriptingInterface.h"
|
||||||
|
|
||||||
void registerAudioMetaTypes(QScriptEngine* engine) {
|
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;
|
AudioInjector* injector = NULL;
|
||||||
QMetaObject::invokeMethod(this, "invokedPlaySound", Qt::BlockingQueuedConnection,
|
QMetaObject::invokeMethod(this, "invokedPlaySound", Qt::BlockingQueuedConnection,
|
||||||
Q_RETURN_ARG(AudioInjector*, injector),
|
Q_RETURN_ARG(AudioInjector*, injector),
|
||||||
Q_ARG(Sound*, sound), Q_ARG(const AudioInjectorOptions&, injectorOptions));
|
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) {
|
AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const AudioInjectorOptions& injectorOptions) {
|
||||||
|
@ -43,6 +49,7 @@ AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const Aud
|
||||||
optionsCopy.stereo = sound->isStereo();
|
optionsCopy.stereo = sound->isStereo();
|
||||||
|
|
||||||
QThread* injectorThread = new QThread();
|
QThread* injectorThread = new QThread();
|
||||||
|
qDebug() << "the injector thread is" << injectorThread;
|
||||||
injectorThread->setObjectName("Audio Injector Thread");
|
injectorThread->setObjectName("Audio Injector Thread");
|
||||||
|
|
||||||
AudioInjector* injector = new AudioInjector(sound, optionsCopy);
|
AudioInjector* injector = new AudioInjector(sound, optionsCopy);
|
||||||
|
@ -50,6 +57,8 @@ AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const Aud
|
||||||
|
|
||||||
injector->moveToThread(injectorThread);
|
injector->moveToThread(injectorThread);
|
||||||
|
|
||||||
|
qDebug() << "the injector is" << injector;
|
||||||
|
|
||||||
// start injecting when the injector thread starts
|
// start injecting when the injector thread starts
|
||||||
connect(injectorThread, &QThread::started, injector, &AudioInjector::injectAudio);
|
connect(injectorThread, &QThread::started, injector, &AudioInjector::injectAudio);
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
#include <AudioInjector.h>
|
#include <AudioInjector.h>
|
||||||
#include <Sound.h>
|
#include <Sound.h>
|
||||||
|
|
||||||
|
class ScriptAudioInjector;
|
||||||
|
|
||||||
class AudioScriptingInterface : public QObject {
|
class AudioScriptingInterface : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
@ -25,7 +27,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// this method is protected to stop C++ callers from calling, but invokable from script
|
// 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:
|
signals:
|
||||||
void mutedByMixer();
|
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 <AudioConstants.h>
|
||||||
#include <AudioEffectOptions.h>
|
#include <AudioEffectOptions.h>
|
||||||
#include <AudioInjector.h>
|
|
||||||
#include <AvatarData.h>
|
#include <AvatarData.h>
|
||||||
#include <Bitstream.h>
|
#include <Bitstream.h>
|
||||||
#include <CollisionInfo.h>
|
#include <CollisionInfo.h>
|
||||||
|
@ -35,6 +34,7 @@
|
||||||
#include "DataViewClass.h"
|
#include "DataViewClass.h"
|
||||||
#include "EventTypes.h"
|
#include "EventTypes.h"
|
||||||
#include "MenuItemProperties.h"
|
#include "MenuItemProperties.h"
|
||||||
|
#include "ScriptAudioInjector.h"
|
||||||
#include "ScriptEngine.h"
|
#include "ScriptEngine.h"
|
||||||
#include "TypedArrays.h"
|
#include "TypedArrays.h"
|
||||||
#include "XMLHttpRequestClass.h"
|
#include "XMLHttpRequestClass.h"
|
||||||
|
|
Loading…
Reference in a new issue