Moved SoundEffect into a separate compilation unit.

Also, fixed license header in TabletScriptingInterface, to include proper author and date.
This commit is contained in:
Anthony J. Thibault 2017-01-30 12:45:37 -08:00
parent 116eef0a78
commit 0542945110
4 changed files with 80 additions and 66 deletions

View file

@ -0,0 +1,39 @@
#include "SoundEffect.h"
#include <RegisteredMetaTypes.h>
#include <AudioInjector.h>
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<SoundCache>()->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"

View file

@ -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 <QObject>
#include <QQuickItem>
#include <SoundCache.h>
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

View file

@ -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 <QtCore/QThread>
#include <AudioInjector.h>
#include <AccountManager.h>
#include <PathUtils.h>
#include <RegisteredMetaTypes.h>
#include "ScriptEngineLogging.h"
#include "DependencyManager.h"
#include "OffscreenUi.h"
#include "SoundEffect.h"
TabletScriptingInterface::TabletScriptingInterface() {
qmlRegisterType<SoundEffect>("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<SoundCache>()->getSound(_url);
}
void SoundEffect::play(QVariant position) {
auto tsi = DependencyManager::get<TabletScriptingInterface>();
if (tsi) {
TabletProxy* tablet = qobject_cast<TabletProxy*>(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"

View file

@ -23,11 +23,9 @@
#include <glm/gtx/quaternion.hpp>
#include <DependencyManager.h>
#include <SoundCache.h>
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