Sustain Hold

Overview

example/sustain_hold/sustain_hold.cpp is an interactive, monophonic infinite sustain effect: play a note, press SPACE, and the note simply refuses to die: a clean, faithful continuation of the sound at the moment you froze it, held for as long as you like, released on the next SPACE.

SPACE : engage the hold / release it
q     : quit (Ctrl-C works too; every exit path fades out cleanly)

The example is a compact tour of Q’s building blocks (ring buffers with interpolated taps, grains, the correlation lag search, window and ramp generators, envelope followers, and a compressor), composed into something musically useful. It measures self-similarity, not pitch: there is no pitch detector, so the same mechanism extends to polyphonic material.

Sustain Hold Signal Flow
Figure 1. Sustain hold signal flow
  • The ring buffer is read through hermite interpolation.

  • The two grains are 16 periods wide, 8 periods apart, and overlap-add.

  • norm matches each grain position to the freshest material from a table of RMS levels.

  • The compressor has an AR side-chain and a clip.

  • The dashed path is the dry signal, handed over to the grains through a complementary ramp.

  • The envelope marks the most recent attack, so the grains stay clear of it.

  • best_lag finds the period, P, by normalized cross-correlation, to a fraction of a sample.

  • The scheduler places the grains on the period grid, ping-ponging through a recent region.

  • SPACE engages the hold and releases it.

How It Works

While idle, the input plays through dry and is recorded into a fractional_ring_buffer (read through hermite sample_interpolation in the audio path). A peak envelope follower tracks the input and remembers where the most recent attack happened. On SPACE:

  1. Measure the period. Best Lag finds the lag at which the most recent samples best match themselves: an exhaustive normalized cross-correlation search with sub-sample (parabolic) refinement. On a low E it locks within a few thousandths of a sample, with similarities of 0.99+ when the note has settled.

  2. Re-emit the recent past as grains. Two Grain objects play Hann-windowed slices 16 periods wide at hops of exactly 8 periods. Width and hop are whole multiples of the measured period, so overlapping grains are phase-aligned and the windows sum to one (constant overlap-add): no comb filtering, no amplitude ripple.

  3. Vary the material from the recording. The grain anchor ping-pongs through a small recent region (up to 32 periods) of the buffer on the period grid, advancing one hop per spawn. Consecutive grains therefore read contiguous material, and the forward leg is the original recording playing back. The variation heard is the string’s own recorded evolution; nothing synthetic is modulated.

  4. Normalize. Each anchor position is RMS-matched to the freshest material (computed once, at engage), so traversing into earlier (louder) material varies timbre, not loudness. The hold sustains at a constant level.

  5. Hand over without a click. The dry signal exits through a half-Hann down-ramp that is the exact complement of the grains' overlap-add ramp-in: constant voltage across the transition between two phase-aligned signals. After a release, the dry stays muted until the envelope follower detects a new attack. Releasing ends in silence instead of replaying the old note’s remainder.

    The click-free engage handover
    Figure 2. Engage: the dry down-ramp and the grain ramp-in are exact complements, so their sum never moves
  6. Even out the level. A compressor (−12 dB threshold, 1:8, ×2 makeup) keeps the held sustain clearly audible. Its side-chain uses an attack/release envelope follower, and a soft clip catches the brief overshoots an AR side-chain admits.

Design Notes

The example converged through listening tests, and the failures were as instructive as the result:

  • Never capture across an attack. A loop window containing the pick transient replays it every cycle, and that is heard as an echo. The envelope follower’s attack position bounds the capture region (and sampling late in the decay, where the timbre has settled, sounds best, hence the startup tip).

  • Everything on the period grid. Anchor moves, hops, and crossfade offsets must be whole multiples of the period. Unquantized offsets comb immediately; period-quantized ones never do.

  • Synthetic modulation reads as a machine. Random-walk and LFO anchor movement, at any rate, sounded mechanical. Contiguous traversal of real material did not.

  • Transitions need complementary constant-voltage ramps. Hard-cutting the dry feed at the hold point clicks, even at moderate signal levels.

  • Respect the audio callback budget. The engage-time analysis (correlation search, normalization table) runs on integer buffer reads (interpolated reads are reserved for the audio path), and nothing in the callback ever blocks; reports are handed to the main thread through an atomic flag.

Running

The example is self-contained: it loads its own audio/Low E.wav via a path baked in at build time, so it runs from any directory:

cmake -B build
cmake --build build --target example_sustain_hold
build/example/sustain_hold/example_sustain_hold

Components Used

Component Role

Fractional Ring Buffer

The recording buffer; hermite sample_interpolation for sub-sample audio-path reads

Grain

Windowed, rate-1 read taps: the overlap-add voices

Best Lag

The correlator: period via self-similarity, sub-sample

Peak Envelope Follower

Attack tracking (capture keep-out, dry return)

AR Envelope Follower

Compressor side-chain

Compressor, Cubic Clip

Output leveling

Hann Window Generator (downward ramp)

Dry handover, release and master fades

Audio File

wav_reader: the sample played into it

Audio Stream

The processing loop this synth derives from


Previous: Grain Freeze | Next: Whammy Bar