Samples and Grains

Lecture 8 · Sampling, granular synthesis, and the phase vocoder

Prof. Dr.-Ing. Sebastian J. Schlecht · Dr.-Ing. Maximilian Schäfer

Where we are

One question, asked of every method

Block Representation Generator
Filters and effects LTI and time-varying systems hand-designed
Classical synthesis samples, partials, modes, physical state hand-designed
DDSP the same structured models parameters learned
Reverberation a recursive dynamical system hand-designed, fittable
Diffusion a learned representation learned

 

The four questions

  1. How is the sound represented?
  2. What are the controllable parameters?
  3. What structure is built in?
  4. What is fixed by design, and what is learned?

Lecture 7 described sounds by their spectrum. This lecture starts from a recording and divides it into pieces of decreasing length. For each division we ask which parameters become controllable.

Listen

Five sounds from one recording

Guitar riff from lecture 7
original
four times slower, same pitch
frozen at 2.1 s
scrubbed back and forth
cloud in octaves and fifths

 

Question

All five sounds are built from pieces of the first recording. Which parameter changes from row to row?

Answer

The read position in the recording. It advances at normal speed, at a quarter of the speed, stops, oscillates, or jumps at random. Granular synthesis makes read position, speed and pitch independent parameters.

Three representations of a recording

Representation Unit Controls
the recording (sampling) a whole note, seconds long choice of note, loop points, playback rate
grains (granular synthesis) windowed segments of 10–200 ms position, density, duration, pitch, randomness
short-time Fourier transform (STFT) frames (phase vocoder) grains with a Fourier transform: magnitude and phase per bin time and pitch independently

From top to bottom the units get shorter and the representation gets more structure. More parameters can be changed before the sound is damaged.

Foundations

Samples and the continuous signal

In digital audio, a sample is one value of a sampled signal, taken every \(T = 1/f_s\) seconds: \[x[n] = x_c(nT)\]

In a sampler, a sample is a recorded sound such as a note or a drum hit, stored as the sequence \(x[0], \dots, x[L-1]\).

Reconstruction

If \(x_c\) is bandlimited to \(f_s/2\), its sample values determine it at every instant: \[x_c(t) = \sum_n x[n]\,\operatorname{sinc}\!\Big(\frac{t - nT}{T}\Big), \quad \operatorname{sinc}(u) = \frac{\sin \pi u}{\pi u}\]

 

Common form of the methods in this lecture

The methods evaluate \(x_c\) at new instants \(\tau_k(t)\), multiply by windows \(w_k(t)\), and add: \[y(t) = \sum_k w_k(t)\; x_c\big(\tau_k(t)\big)\] A sampler uses one read position and an amplitude envelope. Granular synthesis uses many read positions with short windows. The phase vocoder also modifies the Fourier transform of each windowed segment.

Playback with a read pointer

\[y[n] = x_c\big(\tau[n]\,T\big), \qquad \tau[n+1] = \tau[n] + r[n]\]

\(\tau[n]\) is the read position in samples of the recording, \(r[n]\) the playback rate.

Rate \(r[n]\) Result
\(1\) original playback
\(2^{s/12}\) transposed by \(s\) semitones, duration divided by \(r\)
\(0 < r < 1\) slower and lower
\(0\) constant output
\(-1\) reversed
periodic vibrato, scrubbing

 

A single pointer with \(r = 0\) outputs the constant value \(x_c(\tau T)\), which is silent. An audible freeze needs many short windowed segments around \(\tau\). These are the grains discussed later in this lecture.

Represent: the recording

A short history of sampling

Mellotron, 1963: one tape strip per key · 120years

 

Fairlight CMI, 1979 · 120years

 

  • 1963, Mellotron: 8 s of tape per key, no loops
  • 1979, Fairlight CMI: digital recording, pitch shifting, looping
  • 1980, Linn LM-1: a drum machine that plays recorded drums
  • 1984, Kurzweil K250: one recording every few keys, a realistic piano
  • today: tens of gigabytes per instrument

Limitation

A recording stores one performance, with one duration, one pitch and one playing style.

Segments of a recorded note

  • The attack lasts a few tens of milliseconds and contains hammer noise and fast-decaying partials. Listeners identify instruments much less reliably when the attack is removed (Saldanha & Corso 1964).
  • The decay has two slopes, here 11 dB/s and 1.9 dB/s. The double decay is caused by coupled piano strings (Weinreich 1977).

 

Segments in a sampler

Attack: played once as recorded. Sustain: a loop that repeats while the key is held. Release: an envelope after note-off, in piano libraries often with a recorded release noise.

Failure case: a loop that clicks

Two seconds of a piano note (A2) are stored, six seconds are played:

stored, 2 s
37 ms loop
455 ms loop, matched
real note

 

  • The 37 ms loop is 4.07 periods long; the waveform jumps 27 times per second.
  • The 455 ms loop length is chosen by correlation, with a 40 ms crossfade.
  • The loop is flattened, and the late decay of the note is applied afterwards.
  • The looped note keeps one fixed spectrum. The real note becomes duller over time.

Spectrum of a loop

Repeating the segment \(s[n] = x[a+n]\), \(n = 0, \dots, L-1\), gives the periodic signal \(y[n] = s[n \bmod L]\) with the Fourier series

\[y[n] = \frac{1}{L}\sum_{k=0}^{L-1} S[k]\, e^{j 2\pi k n / L}, \qquad S[k] = \sum_{n=0}^{L-1} s[n]\, e^{-j2\pi kn/L}\]

Its spectrum has lines at \(f = k\, f_s / L\) only. Consequences:

  1. The harmonics of a tone at \(f_0\) lie on these lines only if \(L = m\, f_s/f_0\) for a whole number \(m\) of periods.
  2. A discontinuity between \(s[L-1]\) and \(s[0]\) repeats every \(L\) samples and is heard as a buzz at \(f_s/L\).
  3. The spectrum does not change over time.

 

Example

\(f_0 = 110\) Hz at \(f_s = 44.1\) kHz: one period is 400.91 samples.

  • A one-period loop of \(L = 401\) samples is 0.39 cents flat.
  • The 37 ms loop has lines every 27 Hz. The line nearest to 110 Hz is at 108 Hz.

Choosing the loop length

The loop end should continue the waveform at the loop start. Compare \(W\) samples after the start with \(W\) samples after a candidate end:

\[\rho(L) = \frac{\sum_{n=0}^{W-1} x[a+n]\; x[a+L+n]}{\|\mathbf{x}_a\|\;\|\mathbf{x}_{a+L}\|}\]

For segments of equal energy, \[\|\mathbf{x}_a - \mathbf{x}_{a+L}\|^2 = \|\mathbf{x}_a\|^2 + \|\mathbf{x}_{a+L}\|^2 - 2\,\langle \mathbf{x}_a, \mathbf{x}_{a+L}\rangle\] is smallest where \(\rho(L)\) is largest.

 

Piano A2, \(a = 1\) s, \(W = 4096\). The waveform nearly repeats after each period, and the correlation peaks at these lengths; the first peak is at 9.07 ms (\(T_0 = 9.09\) ms). The 455 ms loop is the highest peak between 450 and 550 ms.

A loop of many periods keeps the slow beating between the strings. A loop of one period is a single-cycle wavetable (lecture 7).

Crossfade laws

Two segments with equal power \(\sigma^2\) and correlation coefficient \(\rho\) are mixed: \[y = g_1 x_1 + g_2 x_2\] \[\mathbb{E}\big[y^2\big] = \sigma^2\big(g_1^2 + g_2^2 + 2\rho\, g_1 g_2\big)\]

  • For \(\rho = 1\) the power is constant if \(g_1 + g_2 = 1\) (equal gain).
  • For \(\rho = 0\) the power is constant if \(g_1^2 + g_2^2 = 1\) (equal power).

 

Loop junction

The two segments at the piano loop junction have \(\rho = 0.79\). At the midpoint of the fade, equal power raises the level by 2.5 dB and equal gain lowers it by 0.5 dB. The loop example uses equal gain.

Resampling scales time and frequency

Playing a recording \(r\) times faster is a time scaling: \[y(t) = x_c(rt) \;\;\Longleftrightarrow\;\; Y(f) = \frac{1}{|r|}\,X_c\!\Big(\frac{f}{r}\Big)\]

Derivation: substitute \(u = rt\) in \(\int x_c(rt)\,e^{-j2\pi f t}\,dt = \frac{1}{r}\int x_c(u)\, e^{-j2\pi (f/r) u}\,du\).

All frequencies are multiplied by \(r\): the fundamental, the harmonics and the spectral envelope. The duration is divided by \(r\). Samplers therefore store a separate recording every few semitones.

 

Speech, \(r = 2^{5/12} = 1.335\)

\(f_0\) 202 → 269 Hz; envelope peak 900 Hz → 1.2 kHz; duration 1.86 s → 1.39 s.

original
a fourth up

Interpolation error

For \(r \neq 1\) the read position falls between stored samples, and the value there is interpolated. Example: a 64-sample sawtooth table with 8 harmonics, played one semitone above A4 at \(f_s = 22.05\) kHz. The read pointer advances by 1.35 table values per output sample.

Drop-sample (order 0): the pointer is rounded down, so about one table value in three is skipped.

 

Linear (order 1): straight lines between neighbouring values.

The red lines mark the 8 harmonics. All other lines are aliased interpolation error.

Interpolation as filtering

Interpolation convolves the sample values with a kernel \(h\): \[\hat x_c(t) = \sum_n x[n]\; h\big(t/T - n\big)\] \[\Longleftrightarrow\;\; \hat X_c(f) = T\,H(f/f_s)\, X(e^{j2\pi f/f_s})\]

The spectrum of \(x[n]\) repeats around every multiple of \(f_s\). The frequency response \(H\) determines how much of these images remains.

  • drop-sample: \(h\) = rect, \(|H(\nu)| = |\operatorname{sinc} \nu|\)
  • linear: \(h\) = rect \(*\) rect, \(|H(\nu)| = \operatorname{sinc}^2 \nu\)

The remaining images are then read at rate \(r\) and alias into the audio band.

 

Level of the first image relative to the component

Component Order 0 Order 1
1 kHz −33 dB −65 dB
5 kHz −18 dB −36 dB

Multisampling

A multisampled instrument stores recordings of every key, at several velocities and with several playing techniques.

Storage estimate for a grand piano

88 keys × 8 velocity layers × 2 minutes = 23.5 hours of audio, or 14.9 GB as 16-bit stereo at 44.1 kHz. The sampler keeps the beginning of each sample in RAM and streams the rest from disk (GigaSampler, 1998).

 

Current piano libraries also include round-robin variants, pedal resonance, release noises and several microphone positions, with tens of gigabytes per instrument.

A sampler can only reproduce situations that were recorded. A half-pressed pedal or a string that is still sounding requires additional recordings.

Key zones and velocity layers

A key zone is a range of \(Z\) adjacent keys served by one recording at its centre, the root key.

  • largest transposition: \(\pm Z/2\) semitones
  • formant shift: up to a factor \(2^{Z/24}\), for \(Z = 3\) about 9 %

Velocity layers are recordings of the same key at different velocities. Between two layers the sampler mixes both recordings. They are nearly uncorrelated, so the mix uses equal-power gains.

Brightness increases with velocity. A low-pass filter controlled by velocity can interpolate the timbre between layers.

 

Question

A sampler stores every third key, each recording at the middle key of its zone. By how much is a note transposed at most, and how far do its formants move?

Answer

At most one semitone, so the formants move by up to \(2^{1/12} - 1 = 5.9\,\%\).

Generating variations

One drum hit, 24 times in a row:

single hit
identical copies
slightly varied copies

For each hit: a new velvet-noise filter (8 random-sign taps within 4 ms) applied to a low-shelf-filtered copy, mixed 14 dB below the direct sound (Fagerström et al. 2021), plus accents and ±2 ms timing variation.

 

Identical repetitions sound mechanical (the machine-gun effect). Samplers use round-robin recordings (several per note) or variation synthesis, a small random processing of each hit.

Early drum machines such as the LM-1 (1980) used one sample per drum and show this effect. Synthesized drums such as the 808 (lecture 7) vary from hit to hit because their noise source and oscillators run freely.

Structure: grains

Gabor’s acoustic quanta

Gabor (1947) proposed to describe sound by elementary signals localised in both time and frequency: \[g_{u,f}(t) = w(t-u)\; e^{j2\pi f t}\]

Let \(\Delta t\) and \(\Delta f\) be the standard deviations of \(|g|^2\) and \(|G|^2\).

Uncertainty relation

\[\Delta t\;\Delta f \;\ge\; \frac{1}{4\pi}\] Equality holds for the Gaussian window.

 

Short atoms are heard as clicks with a spectral colour, long atoms as tones. Granular synthesis builds sounds from thousands of such atoms per second (Xenakis 1959, Roads 1978).

Granular synthesis

\[y(t) = \sum_k w(t - t_k)\; x_c\big(p_k + r_k\,(t - t_k)\big)\]

Grain \(k\) has onset \(t_k\), read position \(p_k\), playback rate \(r_k\) and window \(w\). In the common form: \(\tau_k(t) = p_k + r_k(t - t_k)\), \(w_k(t) = w(t - t_k)\).

 

  • The grains are overlap-added (OLA).
  • The window is zero at both ends, so a grain can start at any read position without a click.
  • Typical lengths are 10–200 ms. The next slide derives the minimum length that keeps the pitch.

Spectrum of a grain

A grain is the product of the source and a window, so its spectrum is a convolution: \[g(t) = w(t)\,x_c(t) \;\Longleftrightarrow\; G(f) = (W * X_c)(f)\]

Every harmonic of the source is replaced by a copy of \(W\). The main lobe of a Hann window of length \(T_g\) is \(4/T_g\) wide. Neighbouring harmonics remain separate if \(4/T_g \le f_0\): \[T_g \;\ge\; 4/f_0 \quad \text{(four periods)}\]

 

Riff at 1.41 s, \(f_0 = 147\) Hz, minimum grain length 27 ms:

Grain length
5 ms
20 ms
80 ms
250 ms

Below 27 ms the pitch is lost; the 5 ms grains are heard as clicks.

Constant overlap-add

Grains with spacing \(H\) add up without amplitude modulation if \[\sum_k w(t - kH) = C \quad \text{for all } t \qquad \text{(COLA)}\]

For the Hann window \(w(t) = \sin^2(\pi t/N)\) and \(H = N/2\): \[w(t) + w(t - N/2) = \sin^2 + \cos^2 = 1\] For \(H = N/4\) two such pairs overlap and \(C = 2\).

 

Poisson summation gives \[\sum_k w(t - kH) = \frac{1}{H}\sum_l W\!\Big(\frac{l}{H}\Big)\, e^{j2\pi l t/H}\] The sum is constant if \(W(l/H) = 0\) for all \(l \neq 0\). The Hann window of length \(N\) has spectral zeros at \(l/N\) for \(|l| \ge 2\), which include all multiples of \(1/H\) for \(H = N/2\).

Grain rate and onset jitter

One grain \(g\), repeated with spacing \(H\), gives a periodic output: \[Y(f) = \frac{1}{H}\sum_l G\Big(\frac{l}{H}\Big)\,\delta\Big(f - \frac{l}{H}\Big)\] Its spectrum has lines at multiples of the grain rate \(1/H\).

With random onset shifts \(t_k = kH + \delta_k\), \(\delta_k \sim \mathcal{N}(0, \sigma^2)\): \[\mathbb{E}\,Y(f) = G(f)\sum_k e^{-j2\pi f kH}\;\underbrace{\mathbb{E}\,e^{-j2\pi f \delta}}_{e^{-2\pi^2 f^2 \sigma^2}}\] The expected amplitude of each line is reduced by the factor \(e^{-2\pi^2 f^2\sigma^2}\). The remaining power becomes broadband noise.

 

With \(H = 25\) ms and \(\sigma = 8\) ms the 40 Hz line drops by 17.6 dB; the higher lines disappear.

50 ms grains
onsets every 25 ms
jitter \(\sigma\) = 8 ms

Concatenative synthesis

A corpus contains recorded units (frames, grains or notes), each described by features such as pitch, loudness and spectral envelope. The target is a sequence of feature vectors \(t_1, \dots, t_M\). The units \(u_1, \dots, u_M\) are chosen to minimise \[C = \sum_m C^{\text{t}}(u_m, t_m) + \sum_m C^{\text{c}}(u_{m-1}, u_m)\]

  • target cost \(C^{\text{t}}\): distance between unit and target features
  • concatenation cost \(C^{\text{c}}\): zero for units adjacent in the corpus, positive otherwise

The minimum is found by dynamic programming (Viterbi), as in unit-selection speech synthesis (Hunt & Black 1996). CataRT (Schwarz 2006) applies the method to music.

 

Speech reproduced from the course recordings

Corpus: guitar riff, drums, piano. Features: dB levels of 40 mel bands (a perceptual frequency scale), mean removed. Units are chosen greedily frame by frame; continuing the previous unit costs 20 % less.

target
mosaic

Pitch-synchronous overlap-add (PSOLA)

In the source-filter model of lecture 7, voiced speech is a sequence of vocal-tract responses \(h\), one per glottal pulse: \[x_c(t) \approx \sum_k h(t - kT_0)\]

  • Analysis: pitch marks one period apart. Hann windows of length \(2T_0\) cut out grains that each contain about one copy of \(h\).
  • Synthesis: the grains are placed with spacing \(T_0/r\). The pitch is multiplied by \(r\), and the formants given by \(h\) stay in place.
  • Duration: grains are repeated or omitted, independently of the pitch change.

 

A fourth up
original
resampled
PSOLA

Measured here: 202 → 269 Hz. Method: Moulines & Charpentier (1990).

Time-scale modification

Time-scale modification (TSM) changes the duration and keeps the pitch, as in the second opening example.

Frames are written with hop \(H_s\) and read with hop \(H_a = H_s/\alpha\); the output is \(\alpha\) times longer. The methods differ in how consecutive frames are aligned:

Method Alignment of consecutive frames
OLA none
WSOLA (waveform similarity OLA) time shift of each frame by up to \(\pm\Delta\)
PSOLA frames centred on pitch marks
phase vocoder phase correction in every frequency bin

 

Speech, 1.8 times longer:

OLA
WSOLA

The phase vocoder versions follow in the next section.

Review: Driedger & Müller 2016.

Waveform similarity overlap-add (WSOLA)

If frame \(m-1\) was read at \(p_{m-1}\), the input continues naturally at \(p_{m-1} + H_s\). The next frame is taken near its nominal position \(mH_a\), at the shift that best matches this continuation:

\[p_m = \arg\max_{|p - mH_a| \le \Delta}\; \sum_{n=0}^{N-1} x[p+n]\; x[p_{m-1} + H_s + n]\]

The frame \(x[p_m + n]\, w[n]\) is then added at \(mH_s\).

 

  • \(N = 1024\) (23 ms), \(H_s = N/2\), \(\Delta = 256\) (5.8 ms).
  • Output frames are unmodified pieces of the input, so transients remain sharp. A transient can be repeated or omitted.
  • One time shift aligns one periodicity. Signals with several fundamentals are aligned less well.
  • Measured \(f_0\) after stretching: 200 Hz (original 202 Hz).

Verhelst & Roelands 1993.

Structure: the phase vocoder

STFT frames

\[X(m, k) = \sum_{n=0}^{N-1} x[mH_a + n]\; w[n]\; e^{-j 2\pi k n / N}\]

  • Frame \(m\) starts at sample \(mH_a\); \(n\) counts from the frame start; bin \(k\) has frequency \(\omega_k = 2\pi k/N\).
  • The magnitude \(|X(m,k)|\) is the amplitude near \(\omega_k\).
  • The phase \(\angle X(m,k)\) is the phase of that component at the frame start.

Time stretching reads frames with hop \(H_a\) and writes them with hop \(H_s > H_a\).

 

Failure case: frames moved with their original phases

Consecutive frames no longer fit together. Their overlap partly cancels, and the result beats.

1.8 times longer
frames moved (OLA)
phase vocoder

A sinusoid in one STFT bin

Let \(x[n] = A\cos(\omega_0 n + \varphi_0)\) with \(\omega_0\) near \(\omega_k\). The positive-frequency part gives \[X(m,k) = \frac{A}{2}\sum_n e^{j(\omega_0 (mH_a + n) + \varphi_0)}\, w[n]\, e^{-j\omega_k n}\] \[= \frac{A}{2}\; e^{j(\omega_0 m H_a + \varphi_0)}\; W(\omega_k - \omega_0)\] with the window spectrum \(W(\omega) = \sum_n w[n]\, e^{-j\omega n}\).

  • The magnitude \(\tfrac{A}{2}\,|W(\omega_k - \omega_0)|\) is the same in all frames.
  • The phase increases by \(\omega_0 H_a\) from frame to frame.

 

The phase increment gives \(\omega_0\) much more accurately than the bin spacing \(f_s/N = 21.5\) Hz.

Question

\(f_s = 44.1\) kHz, \(H_a = 256\), bin centre 1000 Hz, partial at 1010 Hz. By how much do the phase increments per frame differ?

Answer

\(2\pi \cdot 10 \cdot 256 / 44100 = 0.36\) rad.

The phase vocoder

Bin \(k\) predicts a phase increment of \(\omega_k H_a\) per frame. The deviation from this prediction gives the frequency of the partial in that bin:

\[\hat{\omega}_k = \omega_k + \frac{\operatorname{princarg}\big(\angle X(m{+}1,k) - \angle X(m,k) - \omega_k H_a\big)}{H_a}\]

\(\operatorname{princarg}\) wraps a phase into \((-\pi, \pi]\). The output phase is advanced with this frequency and the synthesis hop:

\[\varphi(m{+}1, k) = \varphi(m, k) + \hat{\omega}_k H_s\]

 

Median pitch of the speech example:

\(f_0\)
original 202 Hz
frames moved (OLA) 172 Hz
phase vocoder, 1.8 times longer 201 Hz

Flanagan & Golden, Bell Labs, 1966. Dolson’s tutorial (1986) introduced the method to computer music.

Pitch shifting with the phase vocoder

A pitch shift by \(r\) is a time stretch by \(r\) followed by resampling by \(r\). The duration is unchanged and the pitch is multiplied by \(r\).

A fourth up
resampled: shorter
phase vocoder: same length

Measured: 202 → 269 Hz, ratio 1.33 (target \(2^{5/12} = 1.335\)).

 

Remaining artefacts

  • Phasiness: partials spread over neighbouring bins drift apart in phase. Remedy: phase locking.
  • Smeared transients: an attack is spread over a whole frame. Remedy: process transients separately.
  • Shifted formants: the envelope moves with the pitch. Remedy: restore the spectral envelope.

Phase locking

A partial occupies all bins of a main lobe. The phase vocoder updates these bins independently; small errors accumulate and their relative phases drift. The result sounds distant and reverberant (phasiness).

Identity phase locking (Laroche & Dolson 1999):

  1. Find the peaks \(k_p\) of \(|X(m, \cdot)|\).
  2. Update the phase of each peak bin with the phase vocoder.
  3. Give every other bin the phase difference to its nearest peak that it had in the input: \[\varphi_s(m,k) = \varphi_s(m,k_p) + \angle X(m,k) - \angle X(m,k_p)\]

 

Speech, 1.8 times longer
OLA
WSOLA
phase vocoder
with phase locking

Relative difference between the modified STFT and the STFT of its resynthesis: 0.76 without and 0.19 with phase locking (OLA: 0.79).

Harmonic–percussive separation

In a spectrogram, sustained tones form horizontal lines and percussive onsets vertical lines. Median filters along time and along frequency keep one orientation each (Fitzgerald 2010): \[\tilde H = \operatorname{med}_{\text{time}} |X|, \quad \tilde P = \operatorname{med}_{\text{freq}} |X|, \quad M_H = \frac{\tilde H^2}{\tilde H^2 + \tilde P^2}\] The harmonic part \(M_H X\) is stretched with the phase vocoder, the percussive part \((1 - M_H) X\) with OLA of short frames, and the results are added (Driedger, Müller & Ewert 2014).

 

Guitar and drums
mixture
harmonic part
percussive part

Formant preservation

The source-filter model writes the magnitude spectrum as \(|X(f)| = E(f)\,|S(f)|\). The cepstrum separates the slowly varying envelope \(\log E\) from the harmonic fine structure: \[c[q] = \mathrm{IDFT}\{\log|X|\}, \qquad \log E = \mathrm{DFT}\{c[q]\,\ell[q]\}\] The lifter \(\ell[q]\) is 1 for \(|q| < q_c\) and 0 otherwise, with \(q_c\) smaller than the pitch period (here \(q_c = 40\) samples, period 218 samples).

After the pitch shift, the original envelope is restored frame by frame: \[Y'(m,k) = Y(m,k)\;\frac{E_x(m,k)}{E_y(m,k)}\]

 

A fourth up
phase vocoder
envelope restored

Magnitude-only representations

STFT redundancy and consistency

With hop \(H < N\), the STFT has \(N/H\) times more values than the signal. The examples here use \(N = 2048\) and \(H = 256\), a factor of 8. A modified array \(Y(m,k)\) with changed magnitudes or phases is in general not the STFT of any signal.

Griffin & Lim (1984) showed that the signal whose STFT is closest to \(Y\) in the least-squares sense is the weighted overlap-add of the inverse DFTs \(y_m\): \[\hat y[n] = \frac{\sum_m w[n - mH]\; y_m[n - mH]}{\sum_m w^2[n - mH]}\]

 

Consistency

An array \(Y\) is consistent if \(\mathrm{STFT}\big(\mathrm{ISTFT}(Y)\big) = Y\), where the inverse STFT (ISTFT) is the resynthesis formula on the left.

A phase vocoder produces a modified array. Resynthesis with the formula on the left yields the nearest consistent array.

Reconstruction from the magnitude

Only \(|X(m,k)|\) is kept, and a phase is chosen:

original
zero phase
random phase
Griffin–Lim, 100 iterations
fast Griffin–Lim, 100 iterations

 

Griffin–Lim algorithm (1984)

Alternate two steps: replace the magnitude of the current STFT by the target magnitude, then resynthesise and compute the STFT again. The magnitude error does not increase from one iteration to the next.

Spectral error \(\| |X| - |\hat X| \| / \| |X| \|\): random phase 0.74, Griffin–Lim 0.087, fast Griffin–Lim 0.022.

Many signals have nearly the same STFT magnitude, and Griffin–Lim returns one of them. Its phase differs from the original, which is audible as a slightly metallic sound.

Griffin–Lim as alternating projections

The algorithm alternates between two sets of arrays:

  • \(\mathcal{A}\): arrays with the target magnitude \(|X|\), projection \(P_{\mathcal A}(Y) = |X|\, e^{j\angle Y}\)
  • \(\mathcal{C}\): consistent arrays, projection \(P_{\mathcal C}(Y) = \mathrm{STFT}\big(\mathrm{ISTFT}(Y)\big)\)

Each iteration computes \(Y \leftarrow P_{\mathcal A} P_{\mathcal C}\, Y\). Since \(\mathcal A\) is not convex, the result depends on the initial phase.

Fast Griffin–Lim (Perraudin, Balazs & Søndergaard 2013) adds a momentum term: \[t_i = P_{\mathcal A} P_{\mathcal C}\, c_{i-1}, \qquad c_i = t_i + \gamma\,(t_i - t_{i-1})\]

 

Speech example, same random initial phase: error 0.087 (Griffin–Lim) and 0.022 (fast Griffin–Lim, \(\gamma = 0.99\)) after 100 iterations.

Neural vocoders

Text-to-speech systems such as Tacotron 2 (2018) and FastSpeech (2019) generate a mel spectrogram, a magnitude spectrogram on a coarse, perceptually spaced frequency axis. A neural vocoder converts it into a waveform:

\[M(m, b) \;\longrightarrow\; f_\theta(M) \;\longrightarrow\; x[n]\]

The network \(f_\theta\) is trained to produce a waveform that is likely to have the spectrogram \(M\). Examples are autoregressive (WaveNet, 2016), adversarial (HiFi-GAN, 2020) and diffusion models (lecture 12).

 

Channel vocoder (lecture 7) Neural vocoder
representation band envelopes mel spectrogram
generator carrier and filters learned network
missing information set by design learned from data

Question for every representation

Which information does it keep, and which must the generator infer?

Wrap-up

The four questions, for samples and grains

Sampling Granular Phase vocoder
Representation the recording windowed grains STFT magnitude and phase
Controls note, loop, rate position, rate, density, duration, jitter time and pitch independently
Built-in structure read pointer and interpolator overlap-add (COLA) one sinusoid per bin, with estimated frequency
Fixed vs. learned recorded recorded, rearranged analysed; magnitude-only versions are completed by neural vocoders

Take away

Recording as a signal

All methods read the bandlimited signal behind the samples at new instants. Faster reading scales time and frequency together, interpolation acts as a low-pass filter, and a loop is a periodic signal.

 

Separating time and pitch

Windowed grains make read position and pitch independent. WSOLA and PSOLA align grains in time, the phase vocoder corrects the phase of each bin, and formant preservation restores the spectral envelope.

 

Magnitude-only representations

They discard the phase. Griffin–Lim finds a consistent phase; neural vocoders learn a plausible one.

Further reading

  • C. Roads, Microsound, MIT Press, 2001
  • D. Gabor, “Acoustical quanta and the theory of hearing,” Nature 159, 1947
  • B. Truax, “Real-time granular synthesis with a digital signal processor,” Computer Music J. 12(2), 1988
  • J. L. Flanagan, R. M. Golden, “Phase vocoder,” Bell System Technical J. 45(9), 1966
  • M. Dolson, “The phase vocoder: a tutorial,” Computer Music J. 10(4), 1986
  • J. Laroche, M. Dolson, “Improved phase vocoder time-scale modification of audio,” IEEE Trans. Speech Audio Process. 7(3), 1999
  • J. Driedger, M. Müller, “A review of time-scale modification of music signals,” Applied Sciences 6(2), 2016
  • J. Fagerström, S. J. Schlecht, V. Välimäki, “One-to-many conversion for percussive samples,” DAFx, 2021
  • D. Griffin, J. Lim, “Signal estimation from modified short-time Fourier transform,” IEEE Trans. ASSP 32(2), 1984
  • A. van den Oord et al., “WaveNet: a generative model for raw audio,” arXiv:1609.03499, 2016
  • J. Kong, J. Kim, J. Bae, “HiFi-GAN: generative adversarial networks for efficient and high fidelity speech synthesis,” NeurIPS 2020

Examples in this deck: slides/make_samples_media.py (phase vocoder, phase locking, WSOLA, PSOLA, Griffin–Lim and the mosaic in NumPy/SciPy). Interpolation examples: slides/make_synthesis_media.py. Guitar, speech and drum recordings from the pyFDN audio set.

Further reading: foundations and methods

  • J. O. Smith, P. Gossett, “A flexible sampling-rate conversion method,” Proc. ICASSP, 1984
  • D. C. Massie, “Wavetable sampling synthesis,” in M. Kahrs, K. Brandenburg (eds.), Applications of Digital Signal Processing to Audio and Acoustics, Kluwer, 1998
  • U. Zölzer (ed.), DAFX: Digital Audio Effects, 2nd ed., Wiley, 2011 (chapters on time-frequency processing, time and pitch)
  • M. Saldanha, J. F. Corso, “Timbre cues and the identification of musical instruments,” J. Acoust. Soc. Am. 36(11), 1964
  • G. Weinreich, “Coupled piano strings,” J. Acoust. Soc. Am. 62(6), 1977
  • E. Moulines, F. Charpentier, “Pitch-synchronous waveform processing techniques for text-to-speech synthesis using diphones,” Speech Communication 9(5–6), 1990
  • W. Verhelst, M. Roelands, “An overlap-add technique based on waveform similarity (WSOLA) for high quality time-scale modification of speech,” Proc. ICASSP, 1993
  • D. Fitzgerald, “Harmonic/percussive separation using median filtering,” DAFx, 2010
  • J. Driedger, M. Müller, S. Ewert, “Improving time-scale modification of music signals using harmonic-percussive separation,” IEEE Signal Process. Lett. 21(1), 2014
  • N. Perraudin, P. Balazs, P. L. Søndergaard, “A fast Griffin–Lim algorithm,” Proc. WASPAA, 2013
  • A. J. Hunt, A. W. Black, “Unit selection in a concatenative speech synthesis system using a large speech database,” Proc. ICASSP, 1996
  • D. Schwarz, “Concatenative sound synthesis: the early years,” J. New Music Research 35(1), 2006