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.)
if (collision.idA.isNull() || collision.idB.isNull()) {
MyAvatar* myAvatar = getMyAvatar();
const QString& collisionSoundURL = myAvatar->getCollisionSoundURL();
if (!collisionSoundURL.isEmpty()) {
auto collisionSound = myAvatar->getCollisionSound();
if (collisionSound) {
const float velocityChange = glm::length(collision.velocityChange);
const float MIN_AVATAR_COLLISION_ACCELERATION = 0.01f;
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.
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);
return;
}

View file

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

View file

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

View file

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