36#include <initializer_list>
38#include <memory_resource>
78 static_assert(
sizeof(size_t) >=
sizeof(uint64_t));
79 static_assert(std::convertible_to<uint64_t, size_t>);
82 struct hash<
JPL::SourceLayoutKey>
91 struct hash<
JPL::PanningCacheKey>
96 static_assert(
sizeof(key.
Handle) == 4);
134 template<
class VBAPTraits>
139 template<
class Key,
class T>
186 JPL_INLINE
bool AddSourceTargets(
PanEffectHandle source, std::span<const ChannelMap> targetChannelMaps);
187 JPL_INLINE
bool AddSourceTargets(
PanEffectHandle source, std::initializer_list<const ChannelMap> targetChannelMaps);
272 template<
class VBAPTraits>
275 if (!targetChannelMap.IsValid())
278 auto& panner = mPanners[targetChannelMap];
279 if (!panner.IsInitialized())
281 if (!panner.Initialize(targetChannelMap))
283 mPanners.erase(targetChannelMap);
290 template<
class VBAPTraits>
293 auto it = mPanners.find(targetChannelMap);
294 return it != mPanners.end() ? &(it->second) :
nullptr;
297 template<
class VBAPTraits>
299 -> std::shared_ptr<const SourceLayout>
301 if (!layout.SourceMap.IsValid())
304 auto& sourceLayout = mInitializedSourceLayouts[layout];
311 const auto* panner = CreatePannerFor(layout.TargetMap);
313 if (!panner || !panner->InitializeSourceLayout(layout.SourceMap, *sourceLayout))
315 JPL_ERROR_TAG(
"PanningService",
"Failed to initialize SourceLayout");
317 mInitializedSourceLayouts.erase(layout);
321 if (source.IsValid() && sourceLayout)
322 mSourceLayouts[source] = sourceLayout;
327 template<
class VBAPTraits>
330 auto sourceLayout = mInitializedSourceLayouts.find(layout);
331 if (sourceLayout != mInitializedSourceLayouts.end())
332 return sourceLayout->second;
337 template<
class VBAPTraits>
340 std::pmr::vector<float>& outChannelGains)
const
346 std::ranges::fill(outChannelGains, 0.0f);
351 template<
class VBAPTraits>
354 if (!targetChannelMap.
IsValid() || !source.
IsValid() || !mSourceLayouts.contains(source))
361 auto& channelGains = mPanningCache[
364 .TargetChannelMap = targetChannelMap
367 channelGains.resize(mSourceLayouts[source]->ChannelGroups.size() * targetChannelMap.
GetNumChannels());
368 std::ranges::fill(channelGains, 0.0f);
373 template<
class VBAPTraits>
375 -> std::span<const float>
377 auto cache = mPanningCache.find(
380 .TargetChannelMap = targetChannelMap
383 if (cache != mPanningCache.end())
384 return cache->second;
389 template<
class VBAPTraits>
403 std::shared_ptr<const SourceLayout> panningData =
404 CreatePanningDataFor(
407 .TargetMap = targetMap
412 ReleasePanningEffect(newHandle);
427 ReleasePanningEffect(newHandle);
433 template<
class VBAPTraits>
436 return mSourceLayouts.erase(source)
437 + mPanningParams.erase(source)
438 + mPanningCache.erase_if([source](
const std::pair<
const PanningCacheKey, std::pmr::vector<float>>& pair)
440 return pair.first.Handle == source;
444 template<
class VBAPTraits>
450 if (!mSourceLayouts.contains(source))
452 JPL_ERROR_TAG(
"PanningService",
"AddSoruceTargets failed because source handle wasn't initialized before adding source targets. "
453 "This is likely because the provided source handle was released previously and is no longer valid.");
457 if (targetChannelMaps.empty())
460 bool hasAnyTargetInitialized =
false;
462 for (ChannelMap channelMap : targetChannelMaps)
464 hasAnyTargetInitialized |= CreateChannelGainsFor(channelMap, source);
467 return hasAnyTargetInitialized;
470 template<
class VBAPTraits>
471 JPL_INLINE
bool PanningService<VBAPTraits>::AddSourceTargets(
PanEffectHandle source, std::initializer_list<const ChannelMap> targetChannelMaps)
473 return AddSourceTargets(source, std::span(targetChannelMaps));
479 template<CVec3 Vec3Type>
485 static const Vec3Type fallbackDir = Normalized(Vec3Type(0, 1, -
static_cast<Float
>(5e-2)));
489 static_cast<Float
>(1.0))
493 SetY(sanitizedDirection, std::copysign(
GetY(sanitizedDirection),
GetY(sourceDirection)));
494 return sanitizedDirection;
498 template<
class VBAPTraits>
507 for (
auto&& [key, gains] : mPanningCache)
509 if (key.Handle == source && !gains.empty())
511 JPL_ASSERT(mSourceLayouts.contains(key.Handle));
512 JPL_ASSERT(mPanners.contains(key.TargetChannelMap));
514 const auto& sourceLayout = mSourceLayouts[key.Handle];
515 const auto& params = mPanningParams[key.Handle];
520 switch (spatialziationType)
533 .Focus = params.Focus,
534 .Spread = params.Spread,
536 mPanners[key.TargetChannelMap].ProcessVBAPData(*sourceLayout, updateData, gains);
545 .Focus = params.Focus,
546 .Spread = params.Spread
550 mPanners[key.TargetChannelMap].ProcessVBAPData(*sourceLayout, updateData, gains);
563 template<
class VBAPTraits>
566 auto data = mSourceLayouts.find(source);
567 return data == mSourceLayouts.end() ? nullptr : data->second;
570 template<
class VBAPTraits>
576 mPanningParams[effect] = parameters;
580 template<
class VBAPTraits>
586 mPanningParams[effect].
Spread = spread;
#define JPL_ASSERT(inExpression,...)
Main assert macro, usage: JPL_ASSERT(condition, message) or JPL_ASSERT(condition)
Definition ErrorReporting.h:80
#define JPL_ERROR_TAG(tag, message)
Definition ErrorReporting.h:128
Definition ChannelMap.h:154
constexpr uint32 GetNumChannels() const noexcept
Definition ChannelMap.h:166
constexpr bool IsValid() const noexcept
Definition ChannelMap.h:164
constexpr uint32 GetChannelMask() const
Definition ChannelMap.h:198
Definition PanningService.h:136
JPL_INLINE std::shared_ptr< const SourceLayout > GetPanningDataFor(SourceLayoutKey layout) const
Definition PanningService.h:328
JPL_INLINE const PannerType * CreatePannerFor(ChannelMap targetChannelMap)
Definition PanningService.h:273
JPL_INLINE bool CreateChannelGainsFor(ChannelMap targetChannelMap, ChannelMap sourceChannelMap, std::pmr::vector< float > &outChannelGains) const
Definition PanningService.h:338
JPL_INLINE bool CreateChannelGainsFor(ChannelMap targetChannelMap, PanEffectHandle source)
Definition PanningService.h:352
JPL_INLINE bool EvaluateDirection(PanEffectHandle source, const Position< Vec3Type > &position, ESpatializationType spatialziationType)
Definition PanningService.h:499
typename PannerType::SourceLayoutType SourceLayout
Definition PanningService.h:146
typename PannerType::PanUpdateData PanData
Definition PanningService.h:147
typename VBAPTraits::Vec3Type Vec3Type
Definition PanningService.h:142
JPL_INLINE std::span< const float > GetChannelGainsFor(PanEffectHandle source, ChannelMap targetChannelMap) const
Definition PanningService.h:374
JPL_INLINE bool SetPanningEffectSpread(PanEffectHandle effect, float spread)
Definition PanningService.h:581
PanningService & operator=(const PanningService &)=delete
VBAPTraits PannerTraits
Definition PanningService.h:143
JPL_INLINE bool SetPanningEffectParameters(PanEffectHandle effect, const PanEffectParameters ¶meters)
Definition PanningService.h:571
typename PannerType::PanUpdateDataWithOrientation PanDataWithOrientation
Definition PanningService.h:148
JPL_INLINE const PannerType * GetPannerFor(ChannelMap targetChannelMap) const
Definition PanningService.h:291
JPL_INLINE PanEffectHandle InitializePanningEffect(const PanEffectInitParameters &initParameters)
High level API.
Definition PanningService.h:390
PanningService(const PanningService &)=delete
JPL_INLINE std::shared_ptr< const SourceLayout > GetSourceLayoutFor(PanEffectHandle source) const
Definition PanningService.h:564
JPL_INLINE std::shared_ptr< const SourceLayout > CreatePanningDataFor(SourceLayoutKey layout, PanEffectHandle source={})
Definition PanningService.h:298
JPL_INLINE bool ReleasePanningEffect(PanEffectHandle source)
Definition PanningService.h:434
FlatMapWithAllocator< Key, T, std::pmr::polymorphic_allocator > FlatMapType
Definition PanningService.h:140
Definition PannerBase.h:227
typename PanType::SourceLayout SourceLayoutType
Definition PannerBase.h:303
JPL_INLINE Vec3Type SanitizeSourceDir(const Vec3Type sourceDirection)
Definition PanningService.h:480
JPL_INLINE constexpr bool IsNearlyEqual(T a, T b, T tolerance=JPL_FLOAT_EPS_V< T >) noexcept
Definition Math.h:152
JPL_INLINE constexpr auto Abs(const T &value) noexcept
Standard abs is not constexpr in C++20.
Definition Math.h:87
Definition AcousticMaterial.h:36
std::remove_cvref_t< decltype(Vec3Access< Vec3Type >::GetX(std::declval< Vec3Type >()))> Vec3FloatType
Definition Vec3Traits.h:33
@ None
Definition DistanceAttenuation.h:34
JPL_INLINE void reset_pmr_shared(std::shared_ptr< T > &sharedPtr, Y *ptr)
Definition Memory.h:221
JPL_INLINE void SetY(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:40
JPL_INLINE std::shared_ptr< T > make_pmr_shared(Args &&... args)
std::make_shared wrapper using our global memory resource
Definition Memory.h:206
typename StandardPanner::PanUpdateDataWithOrientation StandardPanDataWithOrientation
Definition PanningService.h:45
std::pmr::memory_resource * GetDefaultMemoryResource() noexcept
Definition Memory.h:42
ESpatializationType
Determines spatialized source channel oritentation.
Definition PanningService.h:107
JPL_INLINE constexpr bool operator==(const Vec2 &A, const Vec2 &B) noexcept
Definition MinimalVec2.h:59
IDType< PanningServiceIDTag > PanEffectHandle
Definition PanningService.h:48
JPL_INLINE auto GetY(const Vec3Type &v) noexcept
Definition Vec3Traits.h:36
typename StandardPanner::SourceLayoutType StandardSourceLayout
Definition PanningService.h:43
typename StandardPanner::PanUpdateData StandardPanData
Definition PanningService.h:44
VBAPanner2D<> StandardPanner2D
Alias for 2D panner that doesn't customize any traits.
Definition VBAPanning2D.h:59
Definition ChannelMap.h:272
static constexpr IDType New() noexcept
Definition IDType.h:44
constexpr bool IsValid() const noexcept
Definition IDType.h:46
Definition PanningService.h:122
PanEffectParameters EffectParameters
Definition PanningService.h:127
ChannelMap SourceChannelMap
Definition PanningService.h:123
std::span< const ChannelMap > TargetChannelMaps
Definition PanningService.h:124
Definition PanningService.h:116
float Spread
Definition PanningService.h:118
float Focus
Definition PanningService.h:117
Definition PanningService.h:51
JPL_INLINE constexpr bool operator==(const PanningCacheKey &other) const
Definition PanningService.h:55
ChannelMap TargetChannelMap
Definition PanningService.h:53
PanEffectHandle Handle
Definition PanningService.h:52
Definition PanningService.h:47
Location and orientation in one place.
Definition Position.h:80
OrientationData< Vec3Type > Orientation
Definition Position.h:82
Vec3Type Location
Definition Position.h:81
Definition PanningService.h:62
ChannelMap SourceMap
Definition PanningService.h:63
ChannelMap TargetMap
Definition PanningService.h:64
Definition PannerBase.h:247
Parameters to update VBAP data for a source.
Definition PannerBase.h:238
size_t operator()(const JPL::PanningCacheKey &key) const
Definition PanningService.h:93
size_t operator()(const JPL::SourceLayoutKey &key) const
Definition PanningService.h:84