From 376be7b17eefdc29920ba7f4a85c3e069e68a691 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Fri, 12 Apr 2024 13:04:30 -0700 Subject: [PATCH] support registration point, improve locking --- libraries/entities/src/SoundEntityItem.cpp | 37 +++++++++++++--------- libraries/entities/src/SoundEntityItem.h | 4 +-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/libraries/entities/src/SoundEntityItem.cpp b/libraries/entities/src/SoundEntityItem.cpp index 57ce158ee0..201592dc1f 100644 --- a/libraries/entities/src/SoundEntityItem.cpp +++ b/libraries/entities/src/SoundEntityItem.cpp @@ -136,36 +136,42 @@ bool SoundEntityItem::shouldCreateSound(const EntityTreePointer& tree) const { } void SoundEntityItem::update(const quint64& now) { - withWriteLock([&] { - const auto tree = getTree(); - if (tree) { - _updateNeeded = false; + const auto tree = getTree(); + if (tree) { + _updateNeeded = false; + withWriteLock([&] { if (shouldCreateSound(tree)) { _sound = DependencyManager::get()->getSound(_url); } + }); + withReadLock([&] { if (_sound) { if (_sound->isLoaded()) { updateSound(true); } else { - connect(_sound.data(), &Resource::finished, this, [&] { updateSound(true); }); + connect(_sound.data(), &Resource::finished, this, [&] { + withReadLock([&] { + updateSound(true); + }); + }); } } - } - }); + }); + } } -void SoundEntityItem::setLocalPosition(const glm::vec3& value, bool tellPhysics) { - EntityItem::setLocalPosition(value, tellPhysics); - withWriteLock([&] { +void SoundEntityItem::locationChanged(bool tellPhysics, bool tellChildren) { + EntityItem::locationChanged(tellPhysics, tellChildren); + withReadLock([&] { updateSound(); }); } -void SoundEntityItem::setLocalOrientation(const glm::quat& value) { - EntityItem::setLocalOrientation(value); - withWriteLock([&] { +void SoundEntityItem::dimensionsChanged() { + EntityItem::dimensionsChanged(); + withReadLock([&] { updateSound(); }); } @@ -337,11 +343,12 @@ bool SoundEntityItem::restartSound() { } AudioInjectorOptions options; - options.position = getWorldPosition(); + const glm::quat orientation = getWorldOrientation(); + options.position = getWorldPosition() + orientation * (getScaledDimensions() * (ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint())); options.positionSet = _positional; options.volume = _volume; options.loop = _loop; - options.orientation = getWorldOrientation(); + options.orientation = orientation; options.localOnly = _localOnly || _sound->isAmbisonic(); // force localOnly when ambisonic options.secondOffset = _timeOffset; options.pitch = _pitch; diff --git a/libraries/entities/src/SoundEntityItem.h b/libraries/entities/src/SoundEntityItem.h index 5cbd4bb814..3d1815f557 100644 --- a/libraries/entities/src/SoundEntityItem.h +++ b/libraries/entities/src/SoundEntityItem.h @@ -51,8 +51,8 @@ public: virtual void update(const quint64& now) override; bool needsToCallUpdate() const override { return _updateNeeded; } - void setLocalPosition(const glm::vec3& value, bool tellPhysics = true) override; - void setLocalOrientation(const glm::quat& value) override; + void locationChanged(bool tellPhysics = true, bool tellChildren = true) override; + void dimensionsChanged() override; void setURL(const QString& value); QString getURL() const;