VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Classes | Public Member Functions | Protected Member Functions | Protected Attributes
makVrv::DtProceduralSoundLayer Class Reference

One pool of randomly-triggered WAV clips that can be mixed into a DtProceduralSound.

Each DtProceduralSoundLayer owns a collection of pre-loaded mono float32 PCM clips. On each call to mix(), the layer:

Multiple layers mix into the same scratch buffer to produce rich ambient audio from a single OpenAL voice (one per DtProceduralSound).

Classes

struct  DtActiveEvent
 
struct  DtClipData
 

Public Member Functions

 DtProceduralSoundLayer ()
 
virtual ~DtProceduralSoundLayer ()
 
 DtProceduralSoundLayer (const DtProceduralSoundLayer &)=delete
 
DtProceduralSoundLayer & operator= (const DtProceduralSoundLayer &)=delete
 
 DtProceduralSoundLayer (DtProceduralSoundLayer &&)=default
 
DtProceduralSoundLayer & operator= (DtProceduralSoundLayer &&)=default
 
virtual bool addClip (const std::string &wavFile, float weight=1.0f, float decibelsDb=0.0f)
 
virtual int addClipsFrom (const DtProceduralSoundLayer &src)
 
virtual void setDensity (float eventsPerSecond)
 
virtual float density () const
 
virtual void setFixedParallelEvents (int count)
 
virtual int fixedParallelEvents () const
 
virtual float weightedMeanClipDurationSeconds () const
 
virtual void setGainRange (float minGain, float maxGain)
 
virtual void setPitchRange (float minPitch, float maxPitch)
 
virtual void setMasterGain (float g)
 
virtual float masterGain () const
 
virtual void resetPlayback ()
 
virtual void triggerEvent (unsigned int outputSampleRate, float gain=1.0f, float pitchFactor=1.0f)
 
virtual void setBehavior (DtProceduralSoundBehavior *behavior)
 
virtual DtProceduralSoundBehavior * behavior () const
 
virtual void setProximityFactor (float factor)
 
virtual int clipCount () const
 
virtual int activeEventCount () const
 
virtual std::vector< float > activeEventProgresses () const
 
virtual float timeUntilNextEvent () const
 
virtual float minGain () const
 
virtual float maxGain () const
 
virtual float minPitch () const
 
virtual float maxPitch () const
 
virtual int lastPickedClipIndex () const
 
virtual std::string clipBasename (int index) const
 
virtual float behaviorCurrentLinearGain () const
 
virtual bool behaviorIsActive () const
 
virtual void mix (std::vector< float > &accumBuffer, int numSamples, float dt, unsigned int sampleRate, float densityScale)
 

Protected Member Functions

virtual void rebuildWeightTable ()
 
virtual void recomputeDensityFromFixed ()
 
virtual int pickClip ()
 
virtual float randomFloat (float minVal, float maxVal)
 
virtual float scheduleNextInterval (float effectiveDensity)
 
virtual void fireEvent (unsigned int outputSampleRate, float startOffset=0.0f, float startFadeGain=1.0f, float fadeRate=0.0f)
 
DtProceduralSoundBehaviorContext buildContext (float dt) const
 

Protected Attributes

std::vector< DtClipData > myClips
 
std::vector< float > myCumulativeWeights
 
std::vector< DtActiveEvent > myActiveEvents
 
float myDensity = 1.0f
 
int myFixedParallelEvents = 0
 
float myWeightedMeanDurationSecs = 0.0f
 
float myMinGain = 0.2f
 
float myMaxGain = 1.0f
 
float myMinPitch = 1.0f
 
float myMaxPitch = 1.0f
 
float myMasterGain = 1.0f
 
int myLastPickedClipIndex = -1
 
float myEventTimer = 0.0f
 
DtProceduralSoundBehavior * myBehavior = nullptr
 
float myProximityFactor = 0.0f
 
std::mt19937 myRng
 

Constructor & Destructor Documentation

makVrv::DtProceduralSoundLayer::DtProceduralSoundLayer ( )

Default constructor.

Note
Layers are normally created via DtProceduralSound::addLayer() rather than constructed directly.
virtual makVrv::DtProceduralSoundLayer::~DtProceduralSoundLayer ( )
virtual

Destructor. Releases all loaded PCM clip data.

makVrv::DtProceduralSoundLayer::DtProceduralSoundLayer ( const DtProceduralSoundLayer &  )
delete

Not copyable.

makVrv::DtProceduralSoundLayer::DtProceduralSoundLayer ( DtProceduralSoundLayer &&  )
default

Move-constructible; required so std::map can value-construct it.

Member Function Documentation

DtProceduralSoundLayer& makVrv::DtProceduralSoundLayer::operator= ( const DtProceduralSoundLayer &  )
delete

Not assignable.

DtProceduralSoundLayer& makVrv::DtProceduralSoundLayer::operator= ( DtProceduralSoundLayer &&  )
default

Move-assignable.

virtual bool makVrv::DtProceduralSoundLayer::addClip ( const std::string &  wavFile,
float  weight = 1.0f,
float  decibelsDb = 0.0f 
)
virtual

Load a WAV clip and add it to this layer's pool.

The file is decoded to mono float32 PCM (normalized to [-1.0f, 1.0f]) and held in memory. Supported source formats: 8-bit unsigned PCM, 16-bit signed PCM, and 32-bit float PCM. All clips in one layer should share the same sample rate; a warning is emitted for mismatches (the clip is still added).

Parameters
wavFileFully-resolved filesystem path to a .wav file.
weightRelative selection probability (default 1.0).
decibelsDbAbsolute recorded reference level of this clip in dB; applied per-event as 10^(dB/20) linear gain at fireEvent time, stacking with the layer's per-event random gain and tier master gain. Default 0.0.
Returns
true on success, false if the file failed to load.

Referenced by makVrv::DtForestSoundCreator::create().

virtual int makVrv::DtProceduralSoundLayer::addClipsFrom ( const DtProceduralSoundLayer &  src)
virtual

Copy all clips from src into this layer's pool.

Used when a tier source is a ProceduralSound registry key: the caller creates a temporary DtProceduralSound via the factory, then transfers its PCM data into this layer with addClipsFrom(). The PCM float arrays are copied; the source layer's settings (density, gain, pitch, behavior) are NOT transferred.

Parameters
srcSource layer to copy clips from.
Returns
Number of clips copied (0 if src has none).
virtual void makVrv::DtProceduralSoundLayer::setDensity ( float  eventsPerSecond)
virtual

Average number of sound events to trigger per second (Poisson rate). e.g. 1.5 ~ one event every 0.67 s; 40 = heavy-rain density. Calling this clears any previously set fixedParallelEvents.

virtual float makVrv::DtProceduralSoundLayer::density ( ) const
virtual

Current average events-per-second (Poisson rate).

Returns
Events per second.
virtual void makVrv::DtProceduralSoundLayer::setFixedParallelEvents ( int  count)
virtual

Set the exact number of clip instances that must be playing at all times. After each PCM chunk is advanced any finished events are immediately replaced, keeping the count fixed at count.

On first activation (cold fill) instances are staggered evenly across the clip duration so they don't start in phase.

This also derives a density used for Little's Law book-keeping: density = count / weightedMeanClipDuration.

Pass 0 to clear and revert to direct density (Poisson) mode.

Referenced by makVrv::DtForestSoundCreator::create().

virtual int makVrv::DtProceduralSoundLayer::fixedParallelEvents ( ) const
virtual

Current fixed parallel event count, or 0 if Poisson mode.

Returns
The count passed to setFixedParallelEvents(), or 0.
virtual float makVrv::DtProceduralSoundLayer::weightedMeanClipDurationSeconds ( ) const
virtual

Weighted-mean clip duration in seconds (read-only diagnostic). Equal to sum(weight_i * duration_i) / sum(weight_i) across all loaded clips. Returns 0 when no clips are loaded.

virtual void makVrv::DtProceduralSoundLayer::setGainRange ( float  minGain,
float  maxGain 
)
virtual

Linear gain range for randomly triggered events [0.0, 1.0]. Quieter events simulate sources further from the listener.

Referenced by makVrv::DtForestSoundCreator::create().

virtual void makVrv::DtProceduralSoundLayer::setPitchRange ( float  minPitch,
float  maxPitch 
)
virtual

Pitch ratio range for randomly triggered events. 1.0 = original pitch. e.g. [0.92, 1.08] for natural variation. Pitch is implemented as a fractional sample-rate ratio (nearest-neighbor resampling); no FFT or high-quality resampler is used.

Referenced by makVrv::DtForestSoundCreator::create().

virtual void makVrv::DtProceduralSoundLayer::setMasterGain ( float  g)
virtual

Set the per-layer master gain multiplier.

Applied per-event in fireEvent() as a linear multiplier on top of the layer's per-event random gain and the per-clip dB->linear multiplier. Used by tiered behaviors (Rain, Wind, Visibility) to carry the tier's authored master gain into each fired event.

Parameters
gLinear master gain (1.0 = no change; default is 1.0).
virtual float makVrv::DtProceduralSoundLayer::masterGain ( ) const
virtual

Return the per-layer master gain multiplier (default 1.0).

virtual void makVrv::DtProceduralSoundLayer::resetPlayback ( )
virtual

Reset in-flight playback state so the next mix() starts from a clean cold fill. Called automatically by DtProceduralSound::stop(); you rarely need to call this directly.

virtual void makVrv::DtProceduralSoundLayer::triggerEvent ( unsigned int  outputSampleRate,
float  gain = 1.0f,
float  pitchFactor = 1.0f 
)
virtual

Immediately inject a single clip event, bypassing the Poisson scheduler.

Picks a clip at random (weighted) and starts it at the given gain and pitch. Intended for externally-triggered one-shot sounds (e.g. ballistic proximity). Does nothing if no clips are loaded.

The pitch ratio written to the event is computed as: pitchRatio = (clipSampleRate / outputSampleRate) * pitchFactor so that the nearest-neighbor resampler in mix() plays the clip at its native speed when pitchFactor == 1.0, regardless of the clip's original sample rate.

Parameters
outputSampleRateSample rate of the output stream in Hz (must be > 0).
gainLinear amplitude scale for this event [0.0, 1.0].
pitchFactorMultiplier applied on top of the native pitch (1.0 = no shift).
virtual void makVrv::DtProceduralSoundLayer::setBehavior ( DtProceduralSoundBehavior *  behavior)
virtual

Attach a behavior to this layer.

Non-owning: the layer never deletes the behavior pointer. The owner (DtProceduralSoundPlayer or the behavior itself) is responsible for lifetime management. Pass nullptr to remove any attached behavior.

Parameters
behaviorBehavior to attach, or nullptr to detach.
virtual DtProceduralSoundBehavior* makVrv::DtProceduralSoundLayer::behavior ( ) const
virtual

Return the currently attached behavior, or nullptr.

virtual void makVrv::DtProceduralSoundLayer::setProximityFactor ( float  factor)
virtual

Update the current proximity factor for use in the behavior context.

Called by DtProceduralSoundPlayer before each tick. The value is forwarded into the DtProceduralSoundBehaviorContext passed to all audio-thread callbacks.

Parameters
factorFade factor in [0.0, 1.0]; 1.0 = fully inside the feature polygon.
virtual int makVrv::DtProceduralSoundLayer::clipCount ( ) const
virtual

Number of WAV clips loaded into this layer's pool.

virtual int makVrv::DtProceduralSoundLayer::activeEventCount ( ) const
virtual

Number of events currently playing (advancing their sample offset). Non-zero means audio is actively being mixed right now.

virtual std::vector<float> makVrv::DtProceduralSoundLayer::activeEventProgresses ( ) const
virtual

Playback progress [0.0, 1.0] for each currently active event. Sorted by ascending progress so the display is stable. Empty when no events are active. Allocation is kept small - only called when the ImGui overlay is visible.

virtual float makVrv::DtProceduralSoundLayer::timeUntilNextEvent ( ) const
virtual

Seconds until the next event is scheduled to fire. Counts down each mix() call; resets to a new random interval on each firing.

virtual float makVrv::DtProceduralSoundLayer::minGain ( ) const
virtual

Gain range accessors.

virtual float makVrv::DtProceduralSoundLayer::maxGain ( ) const
virtual
virtual float makVrv::DtProceduralSoundLayer::minPitch ( ) const
virtual

Pitch ratio range accessors (1.0 = original pitch).

virtual float makVrv::DtProceduralSoundLayer::maxPitch ( ) const
virtual
virtual int makVrv::DtProceduralSoundLayer::lastPickedClipIndex ( ) const
virtual

Index of the most recently picked clip, or -1 before the first event.

virtual std::string makVrv::DtProceduralSoundLayer::clipBasename ( int  index) const
virtual

Basename of the clip at index as recorded at load time. Returns an empty string if index is out of range.

virtual float makVrv::DtProceduralSoundLayer::behaviorCurrentLinearGain ( ) const
virtual

Return the linear gain [0, 1] that the layer behavior's continuous gain curve currently applies at this moment. Returns 1.0 if no behavior is attached. Intended for debug display only; the authoritative value is computed inside mix() on the main thread.

Returns
Linear amplitude gain in [0, infinity); typically [0, 1].
virtual bool makVrv::DtProceduralSoundLayer::behaviorIsActive ( ) const
virtual

Return whether the layer behavior currently considers this layer active. Returns true if no behavior is attached.

virtual void makVrv::DtProceduralSoundLayer::mix ( std::vector< float > &  accumBuffer,
int  numSamples,
float  dt,
unsigned int  sampleRate,
float  densityScale 
)
virtual

Mix this layer's active events into a float accumulation buffer.

Parameters
accumBufferOutput buffer of numSamples float values. Caller must zero-fill before the first mix() call per chunk.
numSamplesNumber of output samples to produce.
dtElapsed time in seconds for this chunk (used for Poisson scheduling).
sampleRateOutput sample rate in Hz.
densityScaleRuntime density multiplier from DtProceduralSoundscapeChannel.
Warning
This method must be called from the simulation/main thread that owns the DtProceduralSound. It must never be called concurrently with mix() on the same layer from a different thread.
virtual void makVrv::DtProceduralSoundLayer::rebuildWeightTable ( )
protectedvirtual
virtual void makVrv::DtProceduralSoundLayer::recomputeDensityFromFixed ( )
protectedvirtual
virtual int makVrv::DtProceduralSoundLayer::pickClip ( )
protectedvirtual
virtual float makVrv::DtProceduralSoundLayer::randomFloat ( float  minVal,
float  maxVal 
)
protectedvirtual
virtual float makVrv::DtProceduralSoundLayer::scheduleNextInterval ( float  effectiveDensity)
protectedvirtual
virtual void makVrv::DtProceduralSoundLayer::fireEvent ( unsigned int  outputSampleRate,
float  startOffset = 0.0f,
float  startFadeGain = 1.0f,
float  fadeRate = 0.0f 
)
protectedvirtual
Parameters
startOffsetFractional sample position to begin playback from. 0.0 = clip start (normal). Used by fixed-parallel mode to stagger the initial fill evenly across the clip duration.
startFadeGainInitial value of the crossfade envelope (default 1.0 = full volume). Pass 0.0 for a fade-in event so it starts silent and ramps up.
fadeRatePer-sample additive change applied to fadeGain each output sample. Pass a positive value to fade in; 0.0 for no envelope (default).
DtProceduralSoundBehaviorContext makVrv::DtProceduralSoundLayer::buildContext ( float  dt) const
protected

Build a behavior context from current layer state.

Parameters
dtElapsed time in seconds for this chunk (0.0 for non-scheduling calls).

Member Data Documentation

std::vector<DtClipData> makVrv::DtProceduralSoundLayer::myClips
protected
std::vector<float> makVrv::DtProceduralSoundLayer::myCumulativeWeights
protected

for O(log n) weighted selection

std::vector<DtActiveEvent> makVrv::DtProceduralSoundLayer::myActiveEvents
protected
float makVrv::DtProceduralSoundLayer::myDensity = 1.0f
protected
int makVrv::DtProceduralSoundLayer::myFixedParallelEvents = 0
protected

0 = Poisson mode

float makVrv::DtProceduralSoundLayer::myWeightedMeanDurationSecs = 0.0f
protected

recomputed in rebuildWeightTable()

float makVrv::DtProceduralSoundLayer::myMinGain = 0.2f
protected
float makVrv::DtProceduralSoundLayer::myMaxGain = 1.0f
protected
float makVrv::DtProceduralSoundLayer::myMinPitch = 1.0f
protected
float makVrv::DtProceduralSoundLayer::myMaxPitch = 1.0f
protected
float makVrv::DtProceduralSoundLayer::myMasterGain = 1.0f
protected

Per-layer master gain multiplier applied per-event in fireEvent(). Default 1.0. Composes multiplicatively with the layer's per-event random gain, the per-clip dB->linear multiplier, and (per-sample) the behavior's continuous gain curve.

int makVrv::DtProceduralSoundLayer::myLastPickedClipIndex = -1
protected

Index of the most recently picked clip (-1 before the first event).

float makVrv::DtProceduralSoundLayer::myEventTimer = 0.0f
protected

Seconds remaining until the next event fires. Initialised to 0 so the first event fires on the first mix() call.

DtProceduralSoundBehavior* makVrv::DtProceduralSoundLayer::myBehavior = nullptr
protected

Optional behavior hook. Non-owning pointer; never deleted by this class.

float makVrv::DtProceduralSoundLayer::myProximityFactor = 0.0f
protected

Proximity factor in [0.0, 1.0] set by DtProceduralSoundPlayer each frame.

std::mt19937 makVrv::DtProceduralSoundLayer::myRng
protected

The documentation for this class was generated from the following file:

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)