2021-04-27 15:26:42 +00:00
|
|
|
#ifndef CORE_VOICE_H
|
|
|
|
#define CORE_VOICE_H
|
2021-04-24 18:45:50 +00:00
|
|
|
|
2019-10-06 04:23:31 +00:00
|
|
|
#include <array>
|
2020-12-16 07:39:17 +00:00
|
|
|
#include <atomic>
|
2021-04-24 18:45:50 +00:00
|
|
|
#include <memory>
|
2021-04-27 15:26:42 +00:00
|
|
|
#include <stddef.h>
|
2021-04-27 15:04:09 +00:00
|
|
|
#include <string>
|
2019-10-06 04:23:31 +00:00
|
|
|
|
2021-04-24 18:45:50 +00:00
|
|
|
#include "albyte.h"
|
2020-03-22 18:34:37 +00:00
|
|
|
#include "almalloc.h"
|
2021-04-27 15:04:09 +00:00
|
|
|
#include "aloptional.h"
|
2019-10-02 23:53:23 +00:00
|
|
|
#include "alspan.h"
|
2021-04-27 15:26:42 +00:00
|
|
|
#include "bufferline.h"
|
2020-08-28 07:09:46 +00:00
|
|
|
#include "buffer_storage.h"
|
2021-04-27 15:26:42 +00:00
|
|
|
#include "devformat.h"
|
|
|
|
#include "filters/biquad.h"
|
|
|
|
#include "filters/nfc.h"
|
|
|
|
#include "filters/splitter.h"
|
|
|
|
#include "mixer/defs.h"
|
|
|
|
#include "mixer/hrtfdefs.h"
|
|
|
|
#include "resampler_limits.h"
|
|
|
|
#include "uhjfilter.h"
|
2020-12-16 07:39:17 +00:00
|
|
|
#include "vector.h"
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-04-24 15:28:13 +00:00
|
|
|
struct ContextBase;
|
|
|
|
struct DeviceBase;
|
2020-11-07 16:36:49 +00:00
|
|
|
struct EffectSlot;
|
2020-12-27 18:09:39 +00:00
|
|
|
enum class DistanceModel : unsigned char;
|
2019-10-03 02:13:07 +00:00
|
|
|
|
2020-11-20 09:37:19 +00:00
|
|
|
using uint = unsigned int;
|
|
|
|
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-04-27 15:04:09 +00:00
|
|
|
#define MAX_SENDS 6
|
|
|
|
|
|
|
|
|
2020-05-21 16:10:32 +00:00
|
|
|
enum class SpatializeMode : unsigned char {
|
2020-11-20 09:37:19 +00:00
|
|
|
Off,
|
|
|
|
On,
|
|
|
|
Auto
|
2019-10-02 23:53:23 +00:00
|
|
|
};
|
|
|
|
|
2019-12-28 19:33:19 +00:00
|
|
|
enum class DirectMode : unsigned char {
|
2020-11-20 09:37:19 +00:00
|
|
|
Off,
|
|
|
|
DropMismatch,
|
|
|
|
RemixMismatch
|
2019-12-28 19:33:19 +00:00
|
|
|
};
|
|
|
|
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-03-31 16:37:30 +00:00
|
|
|
/* Maximum number of extra source samples that may need to be loaded, for
|
|
|
|
* resampling or conversion purposes.
|
|
|
|
*/
|
|
|
|
constexpr uint MaxPostVoiceLoad{MaxResamplerEdge + UhjDecoder::sFilterDelay};
|
|
|
|
|
|
|
|
|
2019-10-02 23:53:23 +00:00
|
|
|
enum {
|
|
|
|
AF_None = 0,
|
|
|
|
AF_LowPass = 1,
|
|
|
|
AF_HighPass = 2,
|
|
|
|
AF_BandPass = AF_LowPass | AF_HighPass
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct DirectParams {
|
|
|
|
BiquadFilter LowPass;
|
|
|
|
BiquadFilter HighPass;
|
|
|
|
|
|
|
|
NfcFilter NFCtrlFilter;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
HrtfFilter Old;
|
|
|
|
HrtfFilter Target;
|
2020-12-26 04:52:18 +00:00
|
|
|
alignas(16) std::array<float,HrtfHistoryLength> History;
|
2019-10-02 23:53:23 +00:00
|
|
|
} Hrtf;
|
|
|
|
|
|
|
|
struct {
|
2019-10-06 04:23:31 +00:00
|
|
|
std::array<float,MAX_OUTPUT_CHANNELS> Current;
|
|
|
|
std::array<float,MAX_OUTPUT_CHANNELS> Target;
|
2019-10-02 23:53:23 +00:00
|
|
|
} Gains;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SendParams {
|
|
|
|
BiquadFilter LowPass;
|
|
|
|
BiquadFilter HighPass;
|
|
|
|
|
|
|
|
struct {
|
2019-10-06 04:23:31 +00:00
|
|
|
std::array<float,MAX_OUTPUT_CHANNELS> Current;
|
|
|
|
std::array<float,MAX_OUTPUT_CHANNELS> Target;
|
2019-10-02 23:53:23 +00:00
|
|
|
} Gains;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-25 17:08:49 +00:00
|
|
|
struct VoiceBufferItem {
|
|
|
|
std::atomic<VoiceBufferItem*> mNext{nullptr};
|
|
|
|
|
|
|
|
CallbackType mCallback{nullptr};
|
|
|
|
void *mUserData{nullptr};
|
|
|
|
|
|
|
|
uint mSampleLen{0u};
|
|
|
|
uint mLoopStart{0u};
|
|
|
|
uint mLoopEnd{0u};
|
|
|
|
|
|
|
|
al::byte *mSamples{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
struct VoiceProps {
|
2019-10-02 23:53:23 +00:00
|
|
|
float Pitch;
|
|
|
|
float Gain;
|
|
|
|
float OuterGain;
|
|
|
|
float MinGain;
|
|
|
|
float MaxGain;
|
|
|
|
float InnerAngle;
|
|
|
|
float OuterAngle;
|
|
|
|
float RefDistance;
|
|
|
|
float MaxDistance;
|
|
|
|
float RolloffFactor;
|
|
|
|
std::array<float,3> Position;
|
|
|
|
std::array<float,3> Velocity;
|
|
|
|
std::array<float,3> Direction;
|
|
|
|
std::array<float,3> OrientAt;
|
|
|
|
std::array<float,3> OrientUp;
|
|
|
|
bool HeadRelative;
|
|
|
|
DistanceModel mDistanceModel;
|
|
|
|
Resampler mResampler;
|
2019-12-28 19:33:19 +00:00
|
|
|
DirectMode DirectChannels;
|
2019-10-02 23:53:23 +00:00
|
|
|
SpatializeMode mSpatializeMode;
|
|
|
|
|
|
|
|
bool DryGainHFAuto;
|
|
|
|
bool WetGainAuto;
|
|
|
|
bool WetGainHFAuto;
|
2019-10-03 00:07:23 +00:00
|
|
|
float OuterGainHF;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
|
|
|
float AirAbsorptionFactor;
|
|
|
|
float RoomRolloffFactor;
|
|
|
|
float DopplerFactor;
|
|
|
|
|
|
|
|
std::array<float,2> StereoPan;
|
|
|
|
|
|
|
|
float Radius;
|
|
|
|
|
|
|
|
/** Direct filter and auxiliary send info. */
|
|
|
|
struct {
|
|
|
|
float Gain;
|
|
|
|
float GainHF;
|
|
|
|
float HFReference;
|
|
|
|
float GainLF;
|
|
|
|
float LFReference;
|
|
|
|
} Direct;
|
|
|
|
struct SendData {
|
2020-11-07 16:36:49 +00:00
|
|
|
EffectSlot *Slot;
|
2019-10-02 23:53:23 +00:00
|
|
|
float Gain;
|
|
|
|
float GainHF;
|
|
|
|
float HFReference;
|
|
|
|
float GainLF;
|
|
|
|
float LFReference;
|
|
|
|
} Send[MAX_SENDS];
|
|
|
|
};
|
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
struct VoicePropsItem : public VoiceProps {
|
|
|
|
std::atomic<VoicePropsItem*> next{nullptr};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
DEF_NEWDEL(VoicePropsItem)
|
2019-10-02 23:53:23 +00:00
|
|
|
};
|
|
|
|
|
2020-11-20 09:37:19 +00:00
|
|
|
constexpr uint VoiceIsStatic{ 1u<<0};
|
|
|
|
constexpr uint VoiceIsCallback{ 1u<<1};
|
|
|
|
constexpr uint VoiceIsAmbisonic{ 1u<<2}; /* Needs HF scaling for ambisonic upsampling. */
|
|
|
|
constexpr uint VoiceCallbackStopped{1u<<3};
|
|
|
|
constexpr uint VoiceIsFading{ 1u<<4}; /* Use gain stepping for smooth transitions. */
|
|
|
|
constexpr uint VoiceHasHrtf{ 1u<<5};
|
|
|
|
constexpr uint VoiceHasNfc{ 1u<<6};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
struct Voice {
|
2019-10-02 23:53:23 +00:00
|
|
|
enum State {
|
2020-03-04 18:09:44 +00:00
|
|
|
Stopped,
|
|
|
|
Playing,
|
|
|
|
Stopping,
|
|
|
|
Pending
|
2019-10-02 23:53:23 +00:00
|
|
|
};
|
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
std::atomic<VoicePropsItem*> mUpdate{nullptr};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
VoiceProps mProps;
|
2020-02-21 03:18:07 +00:00
|
|
|
|
2020-11-20 09:37:19 +00:00
|
|
|
std::atomic<uint> mSourceID{0u};
|
2019-10-02 23:53:23 +00:00
|
|
|
std::atomic<State> mPlayState{Stopped};
|
2020-02-26 09:48:59 +00:00
|
|
|
std::atomic<bool> mPendingChange{false};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Source offset in samples, relative to the currently playing buffer, NOT
|
|
|
|
* the whole queue.
|
|
|
|
*/
|
2020-11-20 09:37:19 +00:00
|
|
|
std::atomic<uint> mPosition;
|
2019-10-02 23:53:23 +00:00
|
|
|
/** Fractional (fixed-point) offset to the next sample. */
|
2020-11-20 09:37:19 +00:00
|
|
|
std::atomic<uint> mPositionFrac;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
|
|
|
/* Current buffer queue item being played. */
|
2021-01-25 17:08:49 +00:00
|
|
|
std::atomic<VoiceBufferItem*> mCurrentBuffer;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
|
|
|
/* Buffer queue item to loop to at end of queue (will be NULL for non-
|
|
|
|
* looping voices).
|
|
|
|
*/
|
2021-01-25 17:08:49 +00:00
|
|
|
std::atomic<VoiceBufferItem*> mLoopBuffer;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
|
|
|
/* Properties for the attached buffer(s). */
|
|
|
|
FmtChannels mFmtChannels;
|
2021-01-24 17:29:56 +00:00
|
|
|
FmtType mFmtType;
|
2020-11-20 09:37:19 +00:00
|
|
|
uint mFrequency;
|
2021-06-21 14:36:47 +00:00
|
|
|
uint mNumChannels;
|
2021-03-31 16:37:30 +00:00
|
|
|
uint mFrameSize;
|
2019-12-02 20:50:18 +00:00
|
|
|
AmbiLayout mAmbiLayout;
|
2020-08-28 07:09:46 +00:00
|
|
|
AmbiScaling mAmbiScaling;
|
2020-11-20 09:37:19 +00:00
|
|
|
uint mAmbiOrder;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-03-31 16:37:30 +00:00
|
|
|
std::unique_ptr<UhjDecoder> mDecoder;
|
|
|
|
|
2019-10-02 23:53:23 +00:00
|
|
|
/** Current target parameters used for mixing. */
|
2020-11-20 09:37:19 +00:00
|
|
|
uint mStep{0};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
|
|
|
ResamplerFunc mResampler;
|
|
|
|
|
|
|
|
InterpState mResampleState;
|
|
|
|
|
2020-11-20 09:37:19 +00:00
|
|
|
uint mFlags{};
|
|
|
|
uint mNumCallbackSamples{0};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2019-10-05 23:11:38 +00:00
|
|
|
struct TargetData {
|
2019-10-02 23:53:23 +00:00
|
|
|
int FilterType;
|
|
|
|
al::span<FloatBufferLine> Buffer;
|
|
|
|
};
|
2019-10-05 23:11:38 +00:00
|
|
|
TargetData mDirect;
|
|
|
|
std::array<TargetData,MAX_SENDS> mSend;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-03-20 02:02:30 +00:00
|
|
|
/* The first MaxResamplerPadding/2 elements are the sample history from the
|
|
|
|
* previous mix, with an additional MaxResamplerPadding/2 elements that are
|
|
|
|
* now current (which may be overwritten if the buffer data is still
|
|
|
|
* available).
|
|
|
|
*/
|
2021-06-21 16:04:33 +00:00
|
|
|
using HistoryLine = std::array<float,MaxResamplerPadding>;
|
|
|
|
al::vector<HistoryLine,16> mPrevSamples{2};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-03-20 02:02:30 +00:00
|
|
|
struct ChannelData {
|
2019-10-03 00:07:23 +00:00
|
|
|
float mAmbiScale;
|
2019-10-02 23:53:23 +00:00
|
|
|
BandSplitter mAmbiSplitter;
|
|
|
|
|
|
|
|
DirectParams mDryParams;
|
|
|
|
std::array<SendParams,MAX_SENDS> mWetParams;
|
|
|
|
};
|
2020-03-26 04:06:24 +00:00
|
|
|
al::vector<ChannelData> mChans{2};
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
Voice() = default;
|
|
|
|
~Voice() { delete mUpdate.exchange(nullptr, std::memory_order_acq_rel); }
|
2021-01-25 17:08:49 +00:00
|
|
|
|
|
|
|
Voice(const Voice&) = delete;
|
2020-03-29 00:20:38 +00:00
|
|
|
Voice& operator=(const Voice&) = delete;
|
2019-10-02 23:53:23 +00:00
|
|
|
|
2021-04-24 15:28:13 +00:00
|
|
|
void mix(const State vstate, ContextBase *Context, const uint SamplesToDo);
|
2020-03-22 18:34:37 +00:00
|
|
|
|
2021-04-24 15:28:13 +00:00
|
|
|
void prepare(DeviceBase *device);
|
2021-03-30 14:04:06 +00:00
|
|
|
|
2021-04-27 15:26:42 +00:00
|
|
|
static void InitMixer(al::optional<std::string> resampler);
|
|
|
|
|
2020-03-29 00:20:38 +00:00
|
|
|
DEF_NEWDEL(Voice)
|
2019-10-02 23:53:23 +00:00
|
|
|
};
|
|
|
|
|
2020-12-04 07:09:23 +00:00
|
|
|
extern Resampler ResamplerDefault;
|
|
|
|
|
2021-04-27 15:26:42 +00:00
|
|
|
#endif /* CORE_VOICE_H */
|