Grain

Overview

A grain is a windowed read tap over a ring buffer, playing at rate 1. It is spawned at a fixed delay, possibly fractional, and for the next width samples it outputs window × buffer[delay], then goes inactive. The grain is implemented by a class template named grain.

A grain
Figure 1. A grain: a Hann window over a slice of the buffer, and the windowed output it emits

The delay stays constant for the grain’s lifetime. The buffer indexes newest first, so a constant delay advances through the signal at the write rate: playback at rate 1, with no per-sample position arithmetic.

A grain does not resample. It plays its slice of the buffer at the original rate, so its output keeps the original spectral content. Re-pitching, time stretching, freezing and sustaining come from the scheduler: when grains are spawned and where they are anchored. This preserves the formants in pitch-synchronous overlap-add (PSOLA): the scheduler changes the spacing of the grains, and their content stays as it is.

Hann windows at half-width hops sum to one (constant overlap-add), so two alternating grains reconstruct a signal. Granular, OLA, WSOLA and PSOLA scheduling build on this.

Constant overlap-add
Figure 2. Constant overlap-add: Hann windows at half-width hops sum to one

The window is a template parameter: any generator constructible from (duration, sps), with config(duration, sps, bool) and operator()(). hann_gen is the default; blackman_gen and hamming_gen also qualify. Buffer reads go through the buffer’s sample_interpolation policy; hermite is recommended when grains are anchored at sub-sample positions.

Include

#include <q/synth/grain.hpp>

Declaration

template <typename Window = hann_gen>
class grain
{
public:

   explicit       grain(float sps);

                  template <typename Buffer>
   float          operator()(Buffer const& buf);

   void           spawn(float delay, std::size_t width);

   bool           active() const;
   float          delay() const;
   std::size_t    remaining() const;
};

Expressions

Notation

W

A window generator type, e.g. hann_gen.

g_type

A grain<W> type.

g, a, b

Objects of type grain<W>.

buf

A ring buffer indexable with the grain’s delay, where index 0 refers to the newest element (e.g. fractional_ring_buffer).

d

Floating point value for the delay in samples, possibly fractional.

w

Object of type std::size_t: the grain width in samples.

sps

Floating point value for samples per second.

Type Construction

Expression Semantics

grain<>

Instantiate a grain type with the default hann_gen window.

grain<W>

Instantiate a grain type given W (window generator type).

Constructors and Assignment

Expression Semantics

g_type(sps)

Construct an inactive grain given sps (samples per second).

g_type(g)

Copy construct from grain<W> g.

a = b

Assign b to a.

C++ brace initialization may also be used.

Function Call

Expression Semantics Return Type

g(buf)

Get the next windowed sample, window × buf[d], and advance the grain. Returns 0 when inactive.

float

Call g(buf) exactly once per buffer push: the rate 1 playback comes from the constant delay riding the buffer’s newest-first indexing.
The delay must stay in the buffer’s valid index range for its interpolation type over the grain’s whole life (see sample_interpolation): [1, size-3] for hermite.

Operation

Expression Semantics Return Type

g.spawn(d, w)

Start the grain, reading at the constant delay d under a window spanning w samples.

void

Accessors

Expression Semantics Return Type

g.active()

Get whether the grain is active.

bool

g.delay()

Get the grain’s delay.

float

g.remaining()

Get the number of samples left before the grain goes inactive.

std::size_t

Example

Delay the input samples in by 1000 samples into the output samples out, at sps samples per second, through two grains alternating at half-width hops:

#include <q/utility/fractional_ring_buffer.hpp>  // For fractional_ring_buffer

namespace q = cycfi::q;

using buffer_type = q::fractional_ring_buffer<
   float, std::vector<float>, double, q::sample_interpolation::hermite>;

auto buf = buffer_type{4096};
auto a = q::grain<>{sps};                                   (1)
auto b = q::grain<>{sps};

for (std::size_t i = 0; i != in.size(); ++i)
{
   buf.push(in[i]);
   if (!a.active())
      a.spawn(1000.0f, 512);                                (2)
   if (i >= 256 && !b.active())
      b.spawn(1000.0f, 512);                                (3)
   out[i] = a(buf) + b(buf);                                (4)
}
1 Construct two grains with the default Hann window.
2 Respawn grain a every 512 samples, reading 1000 samples back.
3 Respawn grain b every 512 samples, starting 256 samples after a.
4 Sum the two grains. Their windows overlap by half and sum to one.