diff --git a/libraries/script-engine/src/SoundEffect.cpp b/libraries/script-engine/src/SoundEffect.cpp new file mode 100644 index 0000000000..6833bb1f31 --- /dev/null +++ b/libraries/script-engine/src/SoundEffect.cpp @@ -0,0 +1,39 @@ + +#include "SoundEffect.h" + +#include +#include + +SoundEffect::~SoundEffect() { + if (_sound) { + _sound->deleteLater(); + } + if (_injector) { + // stop will cause the AudioInjector to delete itself. + _injector->stop(); + } +} + +QUrl SoundEffect::getSource() const { + return _url; +} + +void SoundEffect::setSource(QUrl url) { + _url = url; + _sound = DependencyManager::get()->getSound(_url); +} + +void SoundEffect::play(QVariant position) { + AudioInjectorOptions options; + options.position = vec3FromVariant(position); + options.localOnly = true; + if (_injector) { + _injector->setOptions(options); + _injector->restart(); + } else { + QByteArray samples = _sound->getByteArray(); + _injector = AudioInjector::playSound(samples, options); + } +} + +#include "SoundEffect.moc" diff --git a/libraries/script-engine/src/SoundEffect.h b/libraries/script-engine/src/SoundEffect.h new file mode 100644 index 0000000000..5d2a5095c1 --- /dev/null +++ b/libraries/script-engine/src/SoundEffect.h @@ -0,0 +1,39 @@ +// +// Created by Anthony J. Thibault on 2017-01-30 +// Copyright 2013-2017 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_SoundEffect_h +#define hifi_SoundEffect_h + +#include +#include + +#include + +class AudioInjector; + +// SoundEffect object, exposed to qml only, not interface JavaScript. +// This is used to play spatial sound effects on tablets/web entities from within QML. + +class SoundEffect : public QQuickItem { + Q_OBJECT + Q_PROPERTY(QUrl source READ getSource WRITE setSource) +public: + + virtual ~SoundEffect(); + + QUrl getSource() const; + void setSource(QUrl url); + + Q_INVOKABLE void play(QVariant position); +protected: + QUrl _url; + SharedSoundPointer _sound; + AudioInjector* _injector { nullptr }; +}; + +#endif // hifi_SoundEffect_h diff --git a/libraries/script-engine/src/TabletScriptingInterface.cpp b/libraries/script-engine/src/TabletScriptingInterface.cpp index 0e829b6953..d73cb980f6 100644 --- a/libraries/script-engine/src/TabletScriptingInterface.cpp +++ b/libraries/script-engine/src/TabletScriptingInterface.cpp @@ -1,5 +1,5 @@ // -// Created by Bradley Austin Davis on 2016-06-16 +// Created by Anthony J. Thibault on 2016-12-12 // Copyright 2013-2016 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. @@ -10,13 +10,13 @@ #include -#include #include #include #include #include "ScriptEngineLogging.h" #include "DependencyManager.h" #include "OffscreenUi.h" +#include "SoundEffect.h" TabletScriptingInterface::TabletScriptingInterface() { qmlRegisterType("Hifi", 1, 0, "SoundEffect"); @@ -416,47 +416,5 @@ void TabletButtonProxy::editProperties(QVariantMap properties) { } } -// -// SoundEffect -// - -SoundEffect::~SoundEffect() { - if (_sound) { - _sound->deleteLater(); - } - if (_injector) { - // stop will cause the AudioInjector to delete itself. - _injector->stop(); - } -} - -QUrl SoundEffect::getSource() const { - return _url; -} - -void SoundEffect::setSource(QUrl url) { - _url = url; - _sound = DependencyManager::get()->getSound(_url); -} - -void SoundEffect::play(QVariant position) { - auto tsi = DependencyManager::get(); - if (tsi) { - TabletProxy* tablet = qobject_cast(tsi->getTablet("com.highfidelity.interface.tablet.system")); - if (tablet) { - AudioInjectorOptions options; - options.position = vec3FromVariant(position); - options.localOnly = true; - if (_injector) { - _injector->setOptions(options); - _injector->restart(); - } else { - QByteArray samples = _sound->getByteArray(); - _injector = AudioInjector::playSound(samples, options); - } - } - } -} - #include "TabletScriptingInterface.moc" diff --git a/libraries/script-engine/src/TabletScriptingInterface.h b/libraries/script-engine/src/TabletScriptingInterface.h index 4afb3cad0e..b0b2d00e0f 100644 --- a/libraries/script-engine/src/TabletScriptingInterface.h +++ b/libraries/script-engine/src/TabletScriptingInterface.h @@ -23,11 +23,9 @@ #include #include -#include class TabletProxy; class TabletButtonProxy; -class AudioInjector; /**jsdoc * @namespace Tablet @@ -201,24 +199,4 @@ protected: QVariantMap _properties; }; - -// Exposed to qml only, not java script -class SoundEffect : public QQuickItem { - Q_OBJECT - Q_PROPERTY(QUrl source READ getSource WRITE setSource) -public: - - virtual ~SoundEffect(); - - QUrl getSource() const; - void setSource(QUrl url); - - Q_INVOKABLE void play(QVariant position); -protected: - QUrl _url; - SharedSoundPointer _sound; - AudioInjector* _injector { nullptr }; -}; - - #endif // hifi_TabletScriptingInterface_h