mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-07-12 15:14:35 +00:00
update openal
This commit is contained in:
parent
62f3b93ff9
commit
6721a6b021
287 changed files with 33851 additions and 27325 deletions
|
|
@ -22,75 +22,73 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iterator>
|
||||
#include <functional>
|
||||
#include <variant>
|
||||
|
||||
#include "alc/effects/base.h"
|
||||
#include "almalloc.h"
|
||||
#include "alnumbers.h"
|
||||
#include "alnumeric.h"
|
||||
#include "alspan.h"
|
||||
#include "core/ambidefs.h"
|
||||
#include "core/bufferline.h"
|
||||
#include "core/context.h"
|
||||
#include "core/devformat.h"
|
||||
#include "core/device.h"
|
||||
#include "core/effects/base.h"
|
||||
#include "core/effectslot.h"
|
||||
#include "core/filters/biquad.h"
|
||||
#include "core/mixer.h"
|
||||
#include "intrusive_ptr.h"
|
||||
#include "opthelpers.h"
|
||||
|
||||
struct BufferStorage;
|
||||
|
||||
namespace {
|
||||
|
||||
using uint = unsigned int;
|
||||
|
||||
#define MAX_UPDATE_SAMPLES 128
|
||||
struct SinFunc {
|
||||
static auto Get(uint index, float scale) noexcept(noexcept(std::sin(0.0f))) -> float
|
||||
{ return std::sin(static_cast<float>(index) * scale); }
|
||||
};
|
||||
|
||||
#define WAVEFORM_FRACBITS 24
|
||||
#define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
|
||||
#define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
|
||||
struct SawFunc {
|
||||
static constexpr auto Get(uint index, float scale) noexcept -> float
|
||||
{ return static_cast<float>(index)*scale - 1.0f; }
|
||||
};
|
||||
|
||||
inline float Sin(uint index)
|
||||
{
|
||||
constexpr float scale{al::numbers::pi_v<float>*2.0f / WAVEFORM_FRACONE};
|
||||
return std::sin(static_cast<float>(index) * scale);
|
||||
}
|
||||
struct SquareFunc {
|
||||
static constexpr auto Get(uint index, float scale) noexcept -> float
|
||||
{ return float(static_cast<float>(index)*scale < 0.5f)*2.0f - 1.0f; }
|
||||
};
|
||||
|
||||
inline float Saw(uint index)
|
||||
{ return static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f; }
|
||||
|
||||
inline float Square(uint index)
|
||||
{ return static_cast<float>(static_cast<int>((index>>(WAVEFORM_FRACBITS-2))&2) - 1); }
|
||||
|
||||
inline float One(uint) { return 1.0f; }
|
||||
|
||||
template<float (&func)(uint)>
|
||||
void Modulate(float *RESTRICT dst, uint index, const uint step, size_t todo)
|
||||
{
|
||||
for(size_t i{0u};i < todo;i++)
|
||||
{
|
||||
index += step;
|
||||
index &= WAVEFORM_FRACMASK;
|
||||
dst[i] = func(index);
|
||||
}
|
||||
}
|
||||
struct OneFunc {
|
||||
static constexpr auto Get(uint, float) noexcept -> float
|
||||
{ return 1.0f; }
|
||||
};
|
||||
|
||||
|
||||
struct ModulatorState final : public EffectState {
|
||||
void (*mGetSamples)(float*RESTRICT, uint, const uint, size_t){};
|
||||
std::variant<OneFunc,SinFunc,SawFunc,SquareFunc> mSampleGen;
|
||||
|
||||
uint mIndex{0};
|
||||
uint mStep{1};
|
||||
uint mRange{1};
|
||||
float mIndexScale{0.0f};
|
||||
|
||||
struct {
|
||||
alignas(16) FloatBufferLine mModSamples{};
|
||||
alignas(16) FloatBufferLine mBuffer{};
|
||||
|
||||
struct OutParams {
|
||||
uint mTargetChannel{InvalidChannelIndex};
|
||||
|
||||
BiquadFilter mFilter;
|
||||
|
||||
float mCurrentGain{};
|
||||
float mTargetGain{};
|
||||
} mChans[MaxAmbiChannels];
|
||||
};
|
||||
std::array<OutParams,MaxAmbiChannels> mChans;
|
||||
|
||||
|
||||
void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
|
||||
|
|
@ -98,8 +96,6 @@ struct ModulatorState final : public EffectState {
|
|||
const EffectTarget target) override;
|
||||
void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
|
||||
const al::span<FloatBufferLine> samplesOut) override;
|
||||
|
||||
DEF_NEWDEL(ModulatorState)
|
||||
};
|
||||
|
||||
void ModulatorState::deviceUpdate(const DeviceBase*, const BufferStorage*)
|
||||
|
|
@ -113,24 +109,54 @@ void ModulatorState::deviceUpdate(const DeviceBase*, const BufferStorage*)
|
|||
}
|
||||
|
||||
void ModulatorState::update(const ContextBase *context, const EffectSlot *slot,
|
||||
const EffectProps *props, const EffectTarget target)
|
||||
const EffectProps *props_, const EffectTarget target)
|
||||
{
|
||||
auto &props = std::get<ModulatorProps>(*props_);
|
||||
const DeviceBase *device{context->mDevice};
|
||||
|
||||
const float step{props->Modulator.Frequency / static_cast<float>(device->Frequency)};
|
||||
mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, float{WAVEFORM_FRACONE-1}));
|
||||
/* The effective frequency will be adjusted to have a whole number of
|
||||
* samples per cycle (at 48khz, that allows 8000, 6857.14, 6000, 5333.33,
|
||||
* 4800, etc). We could do better by using fixed-point stepping over a sin
|
||||
* function, with additive synthesis for the square and sawtooth waveforms,
|
||||
* but that may need a more efficient sin function since it needs to do
|
||||
* many iterations per sample.
|
||||
*/
|
||||
const float samplesPerCycle{props.Frequency > 0.0f
|
||||
? static_cast<float>(device->Frequency)/props.Frequency + 0.5f
|
||||
: 1.0f};
|
||||
const uint range{static_cast<uint>(std::clamp(samplesPerCycle, 1.0f,
|
||||
static_cast<float>(device->Frequency)))};
|
||||
mIndex = static_cast<uint>(uint64_t{mIndex} * range / mRange);
|
||||
mRange = range;
|
||||
|
||||
if(mStep == 0)
|
||||
mGetSamples = Modulate<One>;
|
||||
else if(props->Modulator.Waveform == ModulatorWaveform::Sinusoid)
|
||||
mGetSamples = Modulate<Sin>;
|
||||
else if(props->Modulator.Waveform == ModulatorWaveform::Sawtooth)
|
||||
mGetSamples = Modulate<Saw>;
|
||||
else /*if(props->Modulator.Waveform == ModulatorWaveform::Square)*/
|
||||
mGetSamples = Modulate<Square>;
|
||||
if(mRange == 1)
|
||||
{
|
||||
mIndexScale = 0.0f;
|
||||
mSampleGen.emplace<OneFunc>();
|
||||
}
|
||||
else if(props.Waveform == ModulatorWaveform::Sinusoid)
|
||||
{
|
||||
mIndexScale = al::numbers::pi_v<float>*2.0f / static_cast<float>(mRange);
|
||||
mSampleGen.emplace<SinFunc>();
|
||||
}
|
||||
else if(props.Waveform == ModulatorWaveform::Sawtooth)
|
||||
{
|
||||
mIndexScale = 2.0f / static_cast<float>(mRange-1);
|
||||
mSampleGen.emplace<SawFunc>();
|
||||
}
|
||||
else if(props.Waveform == ModulatorWaveform::Square)
|
||||
{
|
||||
/* For square wave, the range should be even (there should be an equal
|
||||
* number of high and low samples). An odd number of samples per cycle
|
||||
* would need a more complex value generator.
|
||||
*/
|
||||
mRange = (mRange+1) & ~1u;
|
||||
mIndexScale = 1.0f / static_cast<float>(mRange-1);
|
||||
mSampleGen.emplace<SquareFunc>();
|
||||
}
|
||||
|
||||
float f0norm{props->Modulator.HighPassCutoff / static_cast<float>(device->Frequency)};
|
||||
f0norm = clampf(f0norm, 1.0f/512.0f, 0.49f);
|
||||
float f0norm{props.HighPassCutoff / static_cast<float>(device->Frequency)};
|
||||
f0norm = std::clamp(f0norm, 1.0f/512.0f, 0.49f);
|
||||
/* Bandwidth value is constant in octaves. */
|
||||
mChans[0].mFilter.setParamsFromBandwidth(BiquadType::HighPass, f0norm, 1.0f, 0.75f);
|
||||
for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
|
||||
|
|
@ -147,34 +173,41 @@ void ModulatorState::update(const ContextBase *context, const EffectSlot *slot,
|
|||
|
||||
void ModulatorState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
|
||||
{
|
||||
for(size_t base{0u};base < samplesToDo;)
|
||||
ASSUME(samplesToDo > 0);
|
||||
|
||||
std::visit([this,samplesToDo](auto&& type)
|
||||
{
|
||||
alignas(16) float modsamples[MAX_UPDATE_SAMPLES];
|
||||
const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
|
||||
const uint range{mRange};
|
||||
const float scale{mIndexScale};
|
||||
uint index{mIndex};
|
||||
|
||||
mGetSamples(modsamples, mIndex, mStep, td);
|
||||
mIndex += static_cast<uint>(mStep * td);
|
||||
mIndex &= WAVEFORM_FRACMASK;
|
||||
ASSUME(range > 1);
|
||||
|
||||
auto chandata = std::begin(mChans);
|
||||
for(const auto &input : samplesIn)
|
||||
for(size_t i{0};i < samplesToDo;)
|
||||
{
|
||||
const size_t outidx{chandata->mTargetChannel};
|
||||
if(outidx != InvalidChannelIndex)
|
||||
{
|
||||
alignas(16) float temps[MAX_UPDATE_SAMPLES];
|
||||
|
||||
chandata->mFilter.process({&input[base], td}, temps);
|
||||
for(size_t i{0u};i < td;i++)
|
||||
temps[i] *= modsamples[i];
|
||||
|
||||
MixSamples({temps, td}, samplesOut[outidx].data()+base, chandata->mCurrentGain,
|
||||
chandata->mTargetGain, samplesToDo-base);
|
||||
}
|
||||
++chandata;
|
||||
size_t rem{std::min(samplesToDo-i, size_t{range-index})};
|
||||
do {
|
||||
mModSamples[i++] = type.Get(index++, scale);
|
||||
} while(--rem);
|
||||
if(index == range)
|
||||
index = 0;
|
||||
}
|
||||
mIndex = index;
|
||||
}, mSampleGen);
|
||||
|
||||
base += td;
|
||||
auto chandata = mChans.begin();
|
||||
for(const auto &input : samplesIn)
|
||||
{
|
||||
if(const size_t outidx{chandata->mTargetChannel}; outidx != InvalidChannelIndex)
|
||||
{
|
||||
chandata->mFilter.process(al::span{input}.first(samplesToDo), mBuffer);
|
||||
std::transform(mBuffer.cbegin(), mBuffer.cbegin()+samplesToDo, mModSamples.cbegin(),
|
||||
mBuffer.begin(), std::multiplies<>{});
|
||||
|
||||
MixSamples(al::span{mBuffer}.first(samplesToDo), samplesOut[outidx],
|
||||
chandata->mCurrentGain, chandata->mTargetGain, std::min(samplesToDo, 64_uz));
|
||||
}
|
||||
++chandata;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue