2021-05-14 10:15:42 +08:00
|
|
|
#ifndef CORE_CONTEXT_H
|
|
|
|
#define CORE_CONTEXT_H
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
2023-02-04 15:03:54 +08:00
|
|
|
#include <bitset>
|
2021-05-14 10:15:42 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "almalloc.h"
|
|
|
|
#include "alspan.h"
|
2023-02-04 15:03:54 +08:00
|
|
|
#include "async_event.h"
|
2021-05-14 10:15:42 +08:00
|
|
|
#include "atomic.h"
|
2022-04-25 12:02:45 +08:00
|
|
|
#include "bufferline.h"
|
2021-05-14 10:15:42 +08:00
|
|
|
#include "threads.h"
|
|
|
|
#include "vecmat.h"
|
|
|
|
#include "vector.h"
|
|
|
|
|
|
|
|
struct DeviceBase;
|
|
|
|
struct EffectSlot;
|
|
|
|
struct EffectSlotProps;
|
|
|
|
struct RingBuffer;
|
|
|
|
struct Voice;
|
|
|
|
struct VoiceChange;
|
|
|
|
struct VoicePropsItem;
|
|
|
|
|
|
|
|
using uint = unsigned int;
|
|
|
|
|
|
|
|
|
|
|
|
constexpr float SpeedOfSoundMetersPerSec{343.3f};
|
|
|
|
|
2022-04-25 12:02:45 +08:00
|
|
|
constexpr float AirAbsorbGainHF{0.99426f}; /* -0.05dB */
|
|
|
|
|
2021-05-14 10:15:42 +08:00
|
|
|
enum class DistanceModel : unsigned char {
|
|
|
|
Disable,
|
|
|
|
Inverse, InverseClamped,
|
|
|
|
Linear, LinearClamped,
|
|
|
|
Exponent, ExponentClamped,
|
|
|
|
|
|
|
|
Default = InverseClamped
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ContextProps {
|
2022-04-25 12:02:45 +08:00
|
|
|
std::array<float,3> Position;
|
|
|
|
std::array<float,3> Velocity;
|
|
|
|
std::array<float,3> OrientAt;
|
|
|
|
std::array<float,3> OrientUp;
|
|
|
|
float Gain;
|
|
|
|
float MetersPerUnit;
|
|
|
|
float AirAbsorptionGainHF;
|
|
|
|
|
2021-05-14 10:15:42 +08:00
|
|
|
float DopplerFactor;
|
|
|
|
float DopplerVelocity;
|
|
|
|
float SpeedOfSound;
|
|
|
|
bool SourceDistanceModel;
|
|
|
|
DistanceModel mDistanceModel;
|
|
|
|
|
|
|
|
std::atomic<ContextProps*> next;
|
|
|
|
|
|
|
|
DEF_NEWDEL(ContextProps)
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ContextParams {
|
|
|
|
/* Pointer to the most recent property values that are awaiting an update. */
|
|
|
|
std::atomic<ContextProps*> ContextUpdate{nullptr};
|
|
|
|
|
2022-04-25 12:02:45 +08:00
|
|
|
alu::Vector Position{};
|
2021-05-14 10:15:42 +08:00
|
|
|
alu::Matrix Matrix{alu::Matrix::Identity()};
|
|
|
|
alu::Vector Velocity{};
|
|
|
|
|
|
|
|
float Gain{1.0f};
|
|
|
|
float MetersPerUnit{1.0f};
|
2022-04-25 12:02:45 +08:00
|
|
|
float AirAbsorptionGainHF{AirAbsorbGainHF};
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
float DopplerFactor{1.0f};
|
2022-04-25 12:02:45 +08:00
|
|
|
float SpeedOfSound{SpeedOfSoundMetersPerSec}; /* in units per sec! */
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
bool SourceDistanceModel{false};
|
|
|
|
DistanceModel mDistanceModel{};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ContextBase {
|
|
|
|
DeviceBase *const mDevice;
|
|
|
|
|
|
|
|
/* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
|
|
|
|
* indicates if updates are currently happening).
|
|
|
|
*/
|
|
|
|
RefCount mUpdateCount{0u};
|
|
|
|
std::atomic<bool> mHoldUpdates{false};
|
|
|
|
std::atomic<bool> mStopVoicesOnDisconnect{true};
|
|
|
|
|
|
|
|
float mGainBoost{1.0f};
|
|
|
|
|
|
|
|
/* Linked lists of unused property containers, free to use for future
|
|
|
|
* updates.
|
|
|
|
*/
|
|
|
|
std::atomic<ContextProps*> mFreeContextProps{nullptr};
|
|
|
|
std::atomic<VoicePropsItem*> mFreeVoiceProps{nullptr};
|
|
|
|
std::atomic<EffectSlotProps*> mFreeEffectslotProps{nullptr};
|
|
|
|
|
|
|
|
/* The voice change tail is the beginning of the "free" elements, up to and
|
|
|
|
* *excluding* the current. If tail==current, there's no free elements and
|
|
|
|
* new ones need to be allocated. The current voice change is the element
|
|
|
|
* last processed, and any after are pending.
|
|
|
|
*/
|
|
|
|
VoiceChange *mVoiceChangeTail{};
|
|
|
|
std::atomic<VoiceChange*> mCurrentVoiceChange{};
|
|
|
|
|
2022-04-25 12:02:45 +08:00
|
|
|
void allocVoiceChanges();
|
|
|
|
void allocVoiceProps();
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
ContextParams mParams;
|
|
|
|
|
|
|
|
using VoiceArray = al::FlexArray<Voice*>;
|
|
|
|
std::atomic<VoiceArray*> mVoices{};
|
|
|
|
std::atomic<size_t> mActiveVoiceCount{};
|
|
|
|
|
|
|
|
void allocVoices(size_t addcount);
|
|
|
|
al::span<Voice*> getVoicesSpan() const noexcept
|
|
|
|
{
|
|
|
|
return {mVoices.load(std::memory_order_relaxed)->data(),
|
|
|
|
mActiveVoiceCount.load(std::memory_order_relaxed)};
|
|
|
|
}
|
|
|
|
al::span<Voice*> getVoicesSpanAcquired() const noexcept
|
|
|
|
{
|
|
|
|
return {mVoices.load(std::memory_order_acquire)->data(),
|
|
|
|
mActiveVoiceCount.load(std::memory_order_acquire)};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using EffectSlotArray = al::FlexArray<EffectSlot*>;
|
|
|
|
std::atomic<EffectSlotArray*> mActiveAuxSlots{nullptr};
|
|
|
|
|
|
|
|
std::thread mEventThread;
|
|
|
|
al::semaphore mEventSem;
|
|
|
|
std::unique_ptr<RingBuffer> mAsyncEvents;
|
2023-02-04 15:03:54 +08:00
|
|
|
using AsyncEventBitset = std::bitset<AsyncEvent::UserEventCount>;
|
|
|
|
std::atomic<AsyncEventBitset> mEnabledEvts{0u};
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
/* Asynchronous voice change actions are processed as a linked list of
|
|
|
|
* VoiceChange objects by the mixer, which is atomically appended to.
|
|
|
|
* However, to avoid allocating each object individually, they're allocated
|
|
|
|
* in clusters that are stored in a vector for easy automatic cleanup.
|
|
|
|
*/
|
|
|
|
using VoiceChangeCluster = std::unique_ptr<VoiceChange[]>;
|
|
|
|
al::vector<VoiceChangeCluster> mVoiceChangeClusters;
|
|
|
|
|
|
|
|
using VoiceCluster = std::unique_ptr<Voice[]>;
|
|
|
|
al::vector<VoiceCluster> mVoiceClusters;
|
|
|
|
|
2022-04-25 12:02:45 +08:00
|
|
|
using VoicePropsCluster = std::unique_ptr<VoicePropsItem[]>;
|
|
|
|
al::vector<VoicePropsCluster> mVoicePropClusters;
|
|
|
|
|
2021-05-14 10:15:42 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
static constexpr size_t EffectSlotClusterSize{4};
|
|
|
|
EffectSlot *getEffectSlot();
|
|
|
|
|
|
|
|
using EffectSlotCluster = std::unique_ptr<EffectSlot[]>;
|
|
|
|
al::vector<EffectSlotCluster> mEffectSlotClusters;
|
|
|
|
|
|
|
|
|
2021-05-14 10:15:42 +08:00
|
|
|
ContextBase(DeviceBase *device);
|
|
|
|
ContextBase(const ContextBase&) = delete;
|
|
|
|
ContextBase& operator=(const ContextBase&) = delete;
|
|
|
|
~ContextBase();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CORE_CONTEXT_H */
|