Fix avatars collision sounds

This commit is contained in:
Atlante45 2016-04-15 14:30:06 -07:00
parent ec50744aa1
commit 96ee33e80e
5 changed files with 26 additions and 20 deletions

View file

@ -309,8 +309,8 @@ void AvatarManager::handleCollisionEvents(const CollisionEvents& collisionEvents
// my avatar. (Other user machines will make a similar analysis and inject sound for their collisions.) // my avatar. (Other user machines will make a similar analysis and inject sound for their collisions.)
if (collision.idA.isNull() || collision.idB.isNull()) { if (collision.idA.isNull() || collision.idB.isNull()) {
MyAvatar* myAvatar = getMyAvatar(); MyAvatar* myAvatar = getMyAvatar();
const QString& collisionSoundURL = myAvatar->getCollisionSoundURL(); auto collisionSound = myAvatar->getCollisionSound();
if (!collisionSoundURL.isEmpty()) { if (collisionSound) {
const float velocityChange = glm::length(collision.velocityChange); const float velocityChange = glm::length(collision.velocityChange);
const float MIN_AVATAR_COLLISION_ACCELERATION = 0.01f; const float MIN_AVATAR_COLLISION_ACCELERATION = 0.01f;
const bool isSound = (collision.type == CONTACT_EVENT_TYPE_START) && (velocityChange > MIN_AVATAR_COLLISION_ACCELERATION); const bool isSound = (collision.type == CONTACT_EVENT_TYPE_START) && (velocityChange > MIN_AVATAR_COLLISION_ACCELERATION);
@ -327,7 +327,7 @@ void AvatarManager::handleCollisionEvents(const CollisionEvents& collisionEvents
// but most avatars are roughly the same size, so let's not be so fancy yet. // but most avatars are roughly the same size, so let's not be so fancy yet.
const float AVATAR_STRETCH_FACTOR = 1.0f; const float AVATAR_STRETCH_FACTOR = 1.0f;
AudioInjector::playSound(collisionSoundURL, energyFactorOfFull, AVATAR_STRETCH_FACTOR, myAvatar->getPosition()); AudioInjector::playSound(collisionSound, energyFactorOfFull, AVATAR_STRETCH_FACTOR, myAvatar->getPosition());
myAvatar->collisionWithEntity(collision); myAvatar->collisionWithEntity(collision);
return; return;
} }

View file

@ -23,7 +23,6 @@
#include <AccountManager.h> #include <AccountManager.h>
#include <AddressManager.h> #include <AddressManager.h>
#include <AudioClient.h> #include <AudioClient.h>
#include <DependencyManager.h>
#include <display-plugins/DisplayPlugin.h> #include <display-plugins/DisplayPlugin.h>
#include <FSTReader.h> #include <FSTReader.h>
#include <GeometryUtil.h> #include <GeometryUtil.h>
@ -32,6 +31,7 @@
#include <PathUtils.h> #include <PathUtils.h>
#include <PerfStat.h> #include <PerfStat.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include <SoundCache.h>
#include <TextRenderer3D.h> #include <TextRenderer3D.h>
#include <UserActivityLogger.h> #include <UserActivityLogger.h>
#include <AnimDebugDraw.h> #include <AnimDebugDraw.h>
@ -1232,12 +1232,21 @@ void MyAvatar::clearScriptableSettings() {
} }
void MyAvatar::setCollisionSoundURL(const QString& url) { void MyAvatar::setCollisionSoundURL(const QString& url) {
_collisionSoundURL = url; if (url != _collisionSoundURL) {
if (!url.isEmpty() && (url != _collisionSoundURL)) { _collisionSoundURL = url;
emit newCollisionSoundURL(QUrl(url)); _collisionSound = DependencyManager::get<SoundCache>()->getSound(_collisionSoundURL);
emit newCollisionSoundURL(QUrl(_collisionSoundURL));
} }
} }
SharedSoundPointer MyAvatar::getCollisionSound() {
if (!_collisionSound) {
_collisionSound = DependencyManager::get<SoundCache>()->getSound(_collisionSoundURL);
}
return _collisionSound;
}
void MyAvatar::attach(const QString& modelURL, const QString& jointName, void MyAvatar::attach(const QString& modelURL, const QString& jointName,
const glm::vec3& translation, const glm::quat& rotation, const glm::vec3& translation, const glm::quat& rotation,
float scale, bool isSoft, float scale, bool isSoft,

View file

@ -16,6 +16,7 @@
#include <SettingHandle.h> #include <SettingHandle.h>
#include <Rig.h> #include <Rig.h>
#include <Sound.h>
#include <controllers/Pose.h> #include <controllers/Pose.h>
@ -222,6 +223,8 @@ public:
const QString& getCollisionSoundURL() { return _collisionSoundURL; } const QString& getCollisionSoundURL() { return _collisionSoundURL; }
void setCollisionSoundURL(const QString& url); void setCollisionSoundURL(const QString& url);
SharedSoundPointer getCollisionSound();
void clearScriptableSettings(); void clearScriptableSettings();
float getBoomLength() const { return _boomLength; } float getBoomLength() const { return _boomLength; }
@ -362,6 +365,8 @@ private:
quint32 _motionBehaviors; quint32 _motionBehaviors;
QString _collisionSoundURL; QString _collisionSoundURL;
SharedSoundPointer _collisionSound;
MyCharacterController _characterController; MyCharacterController _characterController;
AvatarWeakPointer _lookAtTargetAvatar; AvatarWeakPointer _lookAtTargetAvatar;

View file

@ -365,17 +365,9 @@ void AudioInjector::stopAndDeleteLater() {
QMetaObject::invokeMethod(this, "deleteLater", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "deleteLater", Qt::QueuedConnection);
} }
AudioInjector* AudioInjector::playSound(const QString& soundUrl, const float volume, const float stretchFactor, const glm::vec3 position) { AudioInjector* AudioInjector::playSound(SharedSoundPointer sound, const float volume, const float stretchFactor, const glm::vec3 position) {
if (soundUrl.isEmpty()) { if (!sound || !sound->isReady()) {
return NULL; return nullptr;
}
auto soundCache = DependencyManager::get<SoundCache>();
if (soundCache.isNull()) {
return NULL;
}
SharedSoundPointer sound = soundCache->getSound(QUrl(soundUrl));
if (sound.isNull() || !sound->isReady()) {
return NULL;
} }
AudioInjectorOptions options; AudioInjectorOptions options;
@ -385,7 +377,7 @@ AudioInjector* AudioInjector::playSound(const QString& soundUrl, const float vol
QByteArray samples = sound->getByteArray(); QByteArray samples = sound->getByteArray();
if (stretchFactor == 1.0f) { if (stretchFactor == 1.0f) {
return playSoundAndDelete(samples, options, NULL); return playSoundAndDelete(samples, options, nullptr);
} }
const int standardRate = AudioConstants::SAMPLE_RATE; const int standardRate = AudioConstants::SAMPLE_RATE;

View file

@ -60,7 +60,7 @@ public:
static AudioInjector* playSoundAndDelete(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface); static AudioInjector* playSoundAndDelete(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface);
static AudioInjector* playSound(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface); static AudioInjector* playSound(const QByteArray& buffer, const AudioInjectorOptions options, AbstractAudioInterface* localInterface);
static AudioInjector* playSound(const QString& soundUrl, const float volume, const float stretchFactor, const glm::vec3 position); static AudioInjector* playSound(SharedSoundPointer sound, const float volume, const float stretchFactor, const glm::vec3 position);
public slots: public slots:
void restart(); void restart();