2021-05-14 10:15:42 +08:00
|
|
|
#ifndef CORE_VOICE_H
|
|
|
|
#define CORE_VOICE_H
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
2022-04-25 12:02:45 +08:00
|
|
|
#include <bitset>
|
2023-02-04 15:03:54 +08:00
|
|
|
#include <chrono>
|
2021-05-14 10:15:42 +08:00
|
|
|
#include <memory>
|
2023-05-31 23:57:33 +08:00
|
|
|
#include <stddef.h>
|
2021-05-14 10:15:42 +08:00
|
|
|
#include <string>
|
|
|
|
|
2023-05-31 23:57:33 +08:00
|
|
|
#include "albyte.h"
|
2021-05-14 10:15:42 +08:00
|
|
|
#include "almalloc.h"
|
2023-05-31 23:57:33 +08:00
|
|
|
#include "aloptional.h"
|
2021-05-14 10:15:42 +08:00
|
|
|
#include "alspan.h"
|
|
|
|
#include "bufferline.h"
|
|
|
|
#include "buffer_storage.h"
|
|
|
|
#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"
|
|
|
|
#include "vector.h"
|
|
|
|
|
|
|
|
struct ContextBase;
|
|
|
|
struct DeviceBase;
|
|
|
|
struct EffectSlot;
|
|
|
|
enum class DistanceModel : unsigned char;
|
|
|
|
|
|
|
|
using uint = unsigned int;
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_SENDS 6
|
|
|
|
|
|
|
|
|
|
|
|
enum class SpatializeMode : unsigned char {
|
|
|
|
Off,
|
|
|
|
On,
|
|
|
|
Auto
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class DirectMode : unsigned char {
|
|
|
|
Off,
|
|
|
|
DropMismatch,
|
|
|
|
RemixMismatch
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-05-03 18:59:33 +08:00
|
|
|
constexpr uint MaxPitch{10};
|
2021-05-14 10:15:42 +08: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;
|
|
|
|
alignas(16) std::array<float,HrtfHistoryLength> History;
|
|
|
|
} Hrtf;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
std::array<float,MAX_OUTPUT_CHANNELS> Current;
|
|
|
|
std::array<float,MAX_OUTPUT_CHANNELS> Target;
|
|
|
|
} Gains;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SendParams {
|
|
|
|
BiquadFilter LowPass;
|
|
|
|
BiquadFilter HighPass;
|
|
|
|
|
|
|
|
struct {
|
2023-02-04 15:03:54 +08:00
|
|
|
std::array<float,MaxAmbiChannels> Current;
|
|
|
|
std::array<float,MaxAmbiChannels> Target;
|
2021-05-14 10:15:42 +08:00
|
|
|
} Gains;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct VoiceBufferItem {
|
|
|
|
std::atomic<VoiceBufferItem*> mNext{nullptr};
|
|
|
|
|
|
|
|
CallbackType mCallback{nullptr};
|
|
|
|
void *mUserData{nullptr};
|
|
|
|
|
2023-05-03 18:59:33 +08:00
|
|
|
uint mBlockAlign{0u};
|
2021-05-14 10:15:42 +08:00
|
|
|
uint mSampleLen{0u};
|
|
|
|
uint mLoopStart{0u};
|
|
|
|
uint mLoopEnd{0u};
|
|
|
|
|
2023-05-31 23:57:33 +08:00
|
|
|
al::byte *mSamples{nullptr};
|
2021-05-14 10:15:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct VoiceProps {
|
|
|
|
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;
|
|
|
|
DirectMode DirectChannels;
|
|
|
|
SpatializeMode mSpatializeMode;
|
|
|
|
|
|
|
|
bool DryGainHFAuto;
|
|
|
|
bool WetGainAuto;
|
|
|
|
bool WetGainHFAuto;
|
|
|
|
float OuterGainHF;
|
|
|
|
|
|
|
|
float AirAbsorptionFactor;
|
|
|
|
float RoomRolloffFactor;
|
|
|
|
float DopplerFactor;
|
|
|
|
|
|
|
|
std::array<float,2> StereoPan;
|
|
|
|
|
|
|
|
float Radius;
|
2022-04-25 12:02:45 +08:00
|
|
|
float EnhWidth;
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
/** Direct filter and auxiliary send info. */
|
|
|
|
struct {
|
|
|
|
float Gain;
|
|
|
|
float GainHF;
|
|
|
|
float HFReference;
|
|
|
|
float GainLF;
|
|
|
|
float LFReference;
|
|
|
|
} Direct;
|
|
|
|
struct SendData {
|
|
|
|
EffectSlot *Slot;
|
|
|
|
float Gain;
|
|
|
|
float GainHF;
|
|
|
|
float HFReference;
|
|
|
|
float GainLF;
|
|
|
|
float LFReference;
|
|
|
|
} Send[MAX_SENDS];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VoicePropsItem : public VoiceProps {
|
|
|
|
std::atomic<VoicePropsItem*> next{nullptr};
|
|
|
|
|
|
|
|
DEF_NEWDEL(VoicePropsItem)
|
|
|
|
};
|
|
|
|
|
2022-04-25 12:02:45 +08:00
|
|
|
enum : uint {
|
|
|
|
VoiceIsStatic,
|
|
|
|
VoiceIsCallback,
|
|
|
|
VoiceIsAmbisonic,
|
|
|
|
VoiceCallbackStopped,
|
|
|
|
VoiceIsFading,
|
|
|
|
VoiceHasHrtf,
|
|
|
|
VoiceHasNfc,
|
|
|
|
|
|
|
|
VoiceFlagCount
|
|
|
|
};
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
struct Voice {
|
|
|
|
enum State {
|
|
|
|
Stopped,
|
|
|
|
Playing,
|
|
|
|
Stopping,
|
|
|
|
Pending
|
|
|
|
};
|
|
|
|
|
|
|
|
std::atomic<VoicePropsItem*> mUpdate{nullptr};
|
|
|
|
|
|
|
|
VoiceProps mProps;
|
|
|
|
|
|
|
|
std::atomic<uint> mSourceID{0u};
|
|
|
|
std::atomic<State> mPlayState{Stopped};
|
|
|
|
std::atomic<bool> mPendingChange{false};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Source offset in samples, relative to the currently playing buffer, NOT
|
|
|
|
* the whole queue.
|
|
|
|
*/
|
2023-02-04 15:03:54 +08:00
|
|
|
std::atomic<int> mPosition;
|
2021-05-14 10:15:42 +08:00
|
|
|
/** Fractional (fixed-point) offset to the next sample. */
|
|
|
|
std::atomic<uint> mPositionFrac;
|
|
|
|
|
|
|
|
/* Current buffer queue item being played. */
|
|
|
|
std::atomic<VoiceBufferItem*> mCurrentBuffer;
|
|
|
|
|
|
|
|
/* Buffer queue item to loop to at end of queue (will be NULL for non-
|
|
|
|
* looping voices).
|
|
|
|
*/
|
|
|
|
std::atomic<VoiceBufferItem*> mLoopBuffer;
|
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
std::chrono::nanoseconds mStartTime{};
|
|
|
|
|
2021-05-14 10:15:42 +08:00
|
|
|
/* Properties for the attached buffer(s). */
|
|
|
|
FmtChannels mFmtChannels;
|
|
|
|
FmtType mFmtType;
|
|
|
|
uint mFrequency;
|
2022-04-25 12:02:45 +08:00
|
|
|
uint mFrameStep; /**< In steps of the sample type size. */
|
2023-05-03 18:59:33 +08:00
|
|
|
uint mBytesPerBlock; /**< Or for PCM formats, BytesPerFrame. */
|
|
|
|
uint mSamplesPerBlock; /**< Always 1 for PCM formats. */
|
2021-05-14 10:15:42 +08:00
|
|
|
AmbiLayout mAmbiLayout;
|
|
|
|
AmbiScaling mAmbiScaling;
|
|
|
|
uint mAmbiOrder;
|
|
|
|
|
2022-07-14 23:17:11 +08:00
|
|
|
std::unique_ptr<DecoderBase> mDecoder;
|
|
|
|
uint mDecoderPadding{};
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
/** Current target parameters used for mixing. */
|
|
|
|
uint mStep{0};
|
|
|
|
|
|
|
|
ResamplerFunc mResampler;
|
|
|
|
|
|
|
|
InterpState mResampleState;
|
|
|
|
|
2022-04-25 12:02:45 +08:00
|
|
|
std::bitset<VoiceFlagCount> mFlags{};
|
2023-05-03 18:59:33 +08:00
|
|
|
uint mNumCallbackBlocks{0};
|
|
|
|
uint mCallbackBlockBase{0};
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
struct TargetData {
|
|
|
|
int FilterType;
|
|
|
|
al::span<FloatBufferLine> Buffer;
|
|
|
|
};
|
|
|
|
TargetData mDirect;
|
|
|
|
std::array<TargetData,MAX_SENDS> mSend;
|
|
|
|
|
|
|
|
/* 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).
|
|
|
|
*/
|
2022-04-25 12:02:45 +08:00
|
|
|
using HistoryLine = std::array<float,MaxResamplerPadding>;
|
|
|
|
al::vector<HistoryLine,16> mPrevSamples{2};
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
struct ChannelData {
|
2022-04-25 12:02:45 +08:00
|
|
|
float mAmbiHFScale, mAmbiLFScale;
|
2021-05-14 10:15:42 +08:00
|
|
|
BandSplitter mAmbiSplitter;
|
|
|
|
|
|
|
|
DirectParams mDryParams;
|
|
|
|
std::array<SendParams,MAX_SENDS> mWetParams;
|
|
|
|
};
|
|
|
|
al::vector<ChannelData> mChans{2};
|
|
|
|
|
|
|
|
Voice() = default;
|
2022-04-25 12:02:45 +08:00
|
|
|
~Voice() = default;
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
Voice(const Voice&) = delete;
|
|
|
|
Voice& operator=(const Voice&) = delete;
|
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
void mix(const State vstate, ContextBase *Context, const std::chrono::nanoseconds deviceTime,
|
|
|
|
const uint SamplesToDo);
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
void prepare(DeviceBase *device);
|
|
|
|
|
2023-05-31 23:57:33 +08:00
|
|
|
static void InitMixer(al::optional<std::string> resampler);
|
2021-05-14 10:15:42 +08:00
|
|
|
|
|
|
|
DEF_NEWDEL(Voice)
|
|
|
|
};
|
|
|
|
|
|
|
|
extern Resampler ResamplerDefault;
|
|
|
|
|
|
|
|
#endif /* CORE_VOICE_H */
|