Class thl::dsp::granular::GrainEngine#

class GrainEngine#

A granular synthesiser over a sample: a pre-allocated pool of windowed grains triggered at the Density rate, each with jittered size, pitch and pan.

Where each grain starts is decided by the HeadPolicy of the HeadMode the caller asks for; the render maths never branches on it.

Sources

render() takes a span of alternative, equal-length sources (e.g. a pitch::PitchBank) and the index to trigger new grains from. A grain keeps reading the source it started on; switching retriggers at once.

Visualisation

With a GrainVisualizer set, grain starts, updates (rate-limited) and ends are reported.

Real-Time Safety

prepare() and set_visualizer() are setup calls. render() and the schedule calls are real-time safe; nothing allocates after construction.

Public Functions

GrainEngine()#
GrainEngine(const GrainEngine&) = delete#
GrainEngine &operator=(const GrainEngine&) = delete#
void prepare(double sample_rate, size_t num_channels)#
inline void set_visualizer(GrainVisualizer *visualizer)#

Report to visualizer (nullptr: report nothing).

Must outlive the engine’s use of it.

inline void seed(uint32_t value)#

Reseed the jitter generator (reproducible grain streams).

void reset_schedule(HeadMode mode)#

Restart the trigger clock and the mode’s head (note-on, mode switch).

void deactivate_all()#

Silence every grain.

inline bool finished(HeadMode mode) const#

One pass (loop off) is over: the head reached End, nothing triggers any more and every grain has ended.

reset_schedule() clears it.

bool any_grain_active() const#
void render(std::span<const sampler::SampleView> sources, size_t source, const GrainParams &params, HeadMode mode, float *const *out, size_t num_channels, size_t num_frames, size_t elapsed_samples)#

Render one block, overwriting all num_channels of out (channels past the prepared count are written silent).

Parameters:

elapsed_samples – Samples since note-on: drives the temperature ramp (position temperature eases in over the first second).