October 18, 2026
Step 4: Playing One Grain
Turn a short sample slice into a small, pitched sound.
So far, Drizel can load audio and has the small tools a grain needs: a window to shape its edges and interpolation to read between samples. Step 4 puts those pieces together. We will render one short slice at a chosen pitch, without a scheduler or a cloud of overlapping grains yet.
Keeping it to one grain makes the path through the engine easier to understand: choose a source position, read the sample, apply a window, and add the result to the output.
A grain has a small set of controls
A single grain can be described with a few values. The start position says where to read in the loaded sound. The duration says how many output frames it lasts. The pitch changes how quickly the read position moves through the source. The window gives the grain its fade in and fade out.
One grain, from source to output
Pitch is a read-speed change
A digital sample does not contain a note that can be shifted independently of its playback. To change pitch in a simple sampler, we change how far the source read position advances for each output frame. A larger step moves through the source faster, making the sound higher and shorter; a smaller step makes it lower and longer.
If pitch is expressed in semitones relative to the original sample, convert it to a playback ratio:
const double pitchRatio = std::pow(2.0, semitones / 12.0);Zero semitones gives a ratio of 1, so the source is read at its original rate. Twelve semitones gives 2, which advances twice as far per output frame and raises the pitch by an octave. Minus twelve gives 0.5, half the read speed and an octave down.
The complete source-position increment also accounts for the sample rates. When the loaded sound and output device use different rates, the ratio is:
const double sourceStep = (sourceSampleRate / outputSampleRate) * pitchRatio;For example, reading a 48 kHz file on a 48 kHz output at the original pitch advances one source sample per output frame. Reading it one octave up advances two source samples per frame.
Render the grain frame by frame
For each output frame, the renderer uses the current source position to get an interpolated sample, multiplies it by the window value, then advances the source position. This is the core loop in pseudocode:
double sourcePosition = grainStart;
const double sourceStep = sampleRateRatio * pitchRatio;
for (int frame = 0; frame < grainLength; ++frame) {
const double t = grainLength == 1
? 0.5
: static_cast<double>(frame) / (grainLength - 1);
const float sample = readInterpolated(sourcePosition);
output[frame] += sample * window(t);
sourcePosition += sourceStep;
}The interpolation and window functions are the ones introduced in Step 3. The grain length here is measured in output frames; the source position can move by a fractional amount, which is why the reader needs interpolation.
Why multiply by a window?
A grain is a short excerpt, so its first and last samples may not be near zero. Cutting it off abruptly can create a discontinuity and an audible click. A Hann or triangle window brings the gain from zero up and back down, making the edges gentler.
In the loop, the window is a gain applied to each sample. It does not move the read position or change the source audio; it controls how much each point contributes. At the start and end, the gain is zero. In the middle, it is strongest.
A few boundaries to handle
A renderer also needs to be deliberate about edges. The requested grain length must be positive; a one-frame grain needs a defined window value instead of dividing by zero when calculating t. As pitch increases, the read position can reach the end of the source before the output grain finishes, so the implementation must stop or safely handle reads outside the available audio.
These are not special cases to hide. They define the limits of the grain and make the result predictable. For a first version, it is useful to choose a valid start position and duration, keep the whole read inside the sample, and test pitches above and below the original rate.
Listen for the pieces
Test the grain at its original pitch first. Then try one octave up and one octave down. A clean result should have a clear beginning and ending, no edge clicks, and a pitch change that matches the read-speed ratio. If it clicks, inspect the window and the sample boundaries. If it sounds rough while changing pitch, inspect the interpolated reads.
One grain is not yet the continuous, evolving sound people associate with granular synthesis. But this is the smallest complete unit of that sound: a chosen piece of audio, read at a chosen rate, shaped into a short event.
Step 4 summary
Step 4 combines sample reading, pitch, and windowing to play one grain. Pitch changes the rate at which the source position advances; interpolation makes fractional reads possible; and the window smooths the grain’s edges. With this unit working, the next step can focus on scheduling many grains and overlapping them into a texture.