Sampling — Time Stretching & Pitch Shifting

Our sampler exposed a limitation: when we change the speed of a buffer, pitch and duration change together. Time-stretching techniques try to break that relationship.

The question: Can we make a recording last twice as long without dropping it an octave — or move it up an octave without making it half as long?

1. Three different operations

Resampling
Change read speed.

Pitch and duration move together.
Time stretching
Change duration while trying to preserve pitch.
Pitch shifting
Change pitch while trying to preserve duration.
Resampling at 2× → +12 semitones AND half the duration

That coupling is exactly what we heard when key-spanning the C2 sample. To separate time from pitch we need to do more than simply move one read head faster or slower.

2. Start with chunks

Imagine cutting the recording into short overlapping windows. We can reposition those pieces in time, repeat them, omit them, and blend their edges.

The waveform is illustrative. The blocks below show the basic idea: output windows can be placed closer together or farther apart than their source positions.

3. Hear a simple browser time stretcher

This is deliberately a teaching algorithm, not Ableton Warp. Load audio, choose a stretch amount and window size, then render it using short windowed grains with overlap/add.

Original duration—
Target duration—
Rendered duration—
AlgorithmWindowed grains
Listen for artefacts. Large windows may smear or repeat obvious pieces; very small windows can become grainy. With no overlap, boundaries become much more obvious. The “best” settings depend on the material.

4. Why overlap and windows?

If we simply chop audio and butt the pieces together, discontinuities at the joins can produce clicks. A window fades each piece in and out, and overlapping neighbouring pieces lets them crossfade.

grain × window → overlap with neighbours → add together
This is another familiar DSP operation: multiplication. The grain is multiplied by an amplitude window, then overlapping grains are added together.

5. Families of time-stretching algorithms

Overlap/Add
Window short chunks and overlap them at new positions. Simple and useful for understanding the principle.
WSOLA
Search around expected positions for waveform sections that align well before overlapping them. This can reduce obvious discontinuities.
Phase Vocoder
Analyse short windows with the FFT/STFT, manipulate their progression through time, then reconstruct the signal.
Granular
Work with many very short grains whose position, rate, envelope and overlap can be controlled independently.

Commercial time-stretching systems may combine or extend these ideas with transient detection, formant handling, source-specific processing and other techniques.

6. Pitch shifting from time stretching

One conceptual route to independent pitch shifting is surprisingly neat:

RESAMPLE TO CHANGE PITCH → TIME-STRETCH BACK TO THE ORIGINAL DURATION

For example, resampling upward by an octave makes the sound twice as fast and half as long. If a time stretcher then expands that result back to the original duration, the pitch change can remain while much of the duration change is removed.

Pitch and time are no longer forced to move together. The quality now depends on the reconstruction algorithm and the source material.

7. From correction to synthesis

Make the chunks smaller and smaller: 100 ms → 50 ms → 20 ms → 10 ms. At some point we stop thinking only about repairing or stretching a recording and start treating the tiny fragments themselves as musical material.

BUFFER → WINDOWS → TINY GRAINS → POSITION / DENSITY / PITCH / ENVELOPE → NEW SOUND
Next: Granular Synthesis — instead of hiding the grains, we make them the instrument.