LPC source-filter pitch shift — repitched excitation residual through the unmodified all-pole vocal-tract filter
- Status
- calibrated
- Updated
- Family
- shift, 15 packages under it
- Plugin
pitch-lpc, effect, whole signal, 1 parameter- Category
- time & pitch
- Licence
- MIT
- Dependencies
- 2 packages, all permissive: MIT
- Downloads
- 3,184 last month
- Tests
- 69 in audiojs/shift's CI, passed at befb754,
Parameters
semitones- 0 st
- −24 to 24 st
import lpc from '@audio/shift-lpc'
Canonical LPC source-filter pitch shift (residual-excited linear prediction, RELP lineage). Per frame: fit the vocal-tract all-pole filter A(z) by the autocorrelation method (Levinson-Durbin), inverse-filter the signal down to its spectrally-flat excitation residual, repitch that residual with the delay line splicer, then resynthesize through the unmodified 1/A(z) run continuously with block-switched coefficients. Over the take the synthesis filter's poles never move — the classical speech-processing complement to formant's cepstral envelope preservation, built on a different mechanism (an explicit source-filter model instead of a magnitude-envelope correction).
lpc(audio, { ratio: 1.5 })
lpc(audio, { ratio: 1.5, order: 24, frameSize: 512 })
| Param | Default | |
|---|---|---|
order |
min(frameSize/16, round(2 + sr/1000)) |
AR filter order (pole count) |
frameSize |
1024 |
Analysis/synthesis frame length |
Preserves formant frequencies by construction — the filter that carries them is never touched, only its excitation is repitched.
Destroys pure tones by design: on a single sinusoid the AR envelope IS the partial, so the fit notches it out of the residual: what is left of it shifts, at the tone's level only where the tone is low or the shift small, and its onset rings at the original pitch, the family's defining tradeoff, not a bug. The level match keeps the take at the input's level, so what the body loses, the onset's ring carries, but it raises a render by 20 dB at most: a tone whose match asks more takes 20 dB and comes out under its input by the rest, 5 of 48 tones at ±5 semitones by up to 2.7 dB and 18 of 48 at ±12 by up to 4.7 dB (250 Hz to 4 kHz, 22.05 to 96 kHz). At 44.1 kHz a 110 Hz tone's body keeps its level within 1 dB at every shift to 12 semitones either way, 220 Hz to five semitones and at −12 (11 dB under at +12); 440 Hz lies up to 5 dB under to five semitones and at −12 (24 dB at +12), 880 Hz 8 to 13 dB under at five semitones and 23 to 38 dB at twelve, 1760 Hz 32 to 49 dB and 3520 Hz 47 to 52 dB at five and twelve (node scripts/ends.js lpc).
| f0 err | THD% | alias | attack corr | formant dist | phase coh | shift |
|---|---|---|---|---|---|---|
| 0.53 | 0.1 | 0.455 | 0.975 | 1.400 | 0.937 | 1.679 |
Alias 0.455 is the worst in the collection: the alias test's 14 kHz tone, shifted ×2, lands past Nyquist and is rightly gone from the residual; what is left is its onset ringing the fit's pole at 14 kHz, which the level match raises by its most, 20 dB, so the take comes out 6.8 dB under the input's level. Formant dist 1.400 is higher (worse) than formant's 0.760 on this synthetic vowel too, across every order from 8 to 64: the fitted envelope follows the fixture's individual harmonics as well as its three formants, where formant's deliberately-smoothed cepstral envelope doesn't. None of this is the intended material: real, less strictly periodic speech is what an unmodified synthesis filter is for.
lpc renders a call as one take, whatever it holds: no level is silence, and nothing parts it. Each frame of a quarter frameSize takes the fit of its forward window, the fit window that starts with it, unless another window that holds it whitens its own samples by 0.5 dB more. A held tone's frames gain at most 0.31 dB from another window and a held reed-like note's nothing, so they keep their forward windows and their fit does not step from window to window; a held vowel's frames gain up to 2.01 dB, and 30 % of them take another window; where a take is cut into quieter sound, the frames before the cut, whose forward windows cross it, gain 13.5 dB at the median from a window of the take and take that one, and those after it take fits of what follows; the voice's frames gain 0.65 dB at the median (node scripts/ends.js margin). Where the past predicts what is no longer there, as at a cut, the inverse filter's memory is cleared and the frame's samples on either side take fits of their own; the filter rings on over the cut, as at a stop. A memory is cleared only where that saves the frame at least the share of its residual that the order samples it changes hold, so a held note, which its past predicts, clears none inside it: a reed-like note with steep edges, at 22.05 to 96 kHz and ±5 and ±12 semitones, keeps the loudest 2 ms of its second difference within 6.5 dB of its median, nothing clicking, and the energy between its harmonics 26.1 dB under that on them at least (node scripts/ends.js held). Shifted up, the delay line plays what follows a fall of the filters' gain, a cut or a vowel's end, up to 2560 frames early through the filters before it, which would lift it by their gain; the residual's scale is the filters' gain under its peak, the peak falling back 1 dB in 2560 frames, so what is read across a fall plays at its own level, and the scale, never under the gain's least, holds a long call's level: the voice cut loud and 0.3 s of room noise, written 26 times over, each copy within 1.18 dB of the first at 22.05 kHz, 0.60 dB at 44.1 kHz and 0.36 dB at 96 kHz (node scripts/ends.js long). The last part before a cut is the exception to the mean: every fit window that holds it holds the cut's step, so it whitens worse than the parts before it and its filters lift less, and it takes its own gain where that lies under the mean; what the delay line reads of it early then plays through the filters before it at its own level. A frame of exact zeros has nothing to fit: the last fit goes on, its poles drawn inward 60 Hz a frame from the last sound on, and its ring ends inside the audio manifest's declared tail, so a stop renders the same wherever it falls, at the call's end, mid-call or in a channel that ends early, up to the channel's one gain. Each channel's level is matched once a call: one gain a channel, its energy in over its energy out over the call's frames up to the last sound of any channel, so the level cannot follow the sound and pump, and every channel comes out at its own input's level, so the channels keep their balance: the voice beside a pluck train or a rock beat 20 dB under it, at 22.05 to 96 kHz and ±5 and ±12 semitones, each channel within 4.2e-8 dB of its own input's level, float32's rounding (node scripts/ends.js match). On a steady vowel the energy between the harmonics stays 27.8 to 48.2 dB under the energy on them (the same script). Over the 1024 frames after a stop that ends the call, re the 1024 before it, the ring holds −81.6 to −1.3 dB (node scripts/ends.js lpc).
So a take cut loud keeps its level whatever follows the cut: into exact zeros, Gaussian noise 20 to 100 dB under it or a 16-bit file's dither, its peak 0 to −60 dBFS, at ±5 and ±12 semitones, its first 0.4 s comes out +0.2 to +1.2 dB and its last 0.1 s −1.6 to −0.3 dB re its input at 44.1 kHz, −0.1 to +1.3 and −1.7 to 0.0 dB at 96 kHz, and nothing before the cut sounds over the take: over its last 0.1 s the loudest 64 frames lie at most 1.2 dB over the input's loudest there at 44.1 kHz, 1.6 dB at 22.05 kHz and 5.0 dB at 96 kHz (−60 dBFS, −12 st). A take faded into zeros comes out 0.0 dB re its input, and a 220 Hz tone hovering at 2.0e-5 to 4.5e-5 alone at its own level, −0.0 to 0.0 dB (node scripts/ends.js drop: the voice, audio-lena; the take cut loud at 22.05, 44.1 and 96 kHz, the rest at 44.1 kHz).
What lpc cannot do: keep each sound of a channel at its own level. A channel's one gain is the whole call's: of two notes, the second 12 dB down, with nothing, 300, 1023 or 1025 frames or 0.2 s of zeros, dither or noise 60 dB under between them, the first, which sets the gain, comes out −0.0 to +0.1 dB re its input, and the second, past the 2560 frames over which the delay line plays the first late, −3.8 to +0.4 dB at 44.1 kHz (−3.7 to +0.4 dB over all of it) and −7.3 to +0.8 dB at 96 kHz, its lowest at −12 semitones; a gap of 1025 frames moves it 0.11 dB at most from one of 1023 at 44.1 kHz, 0.32 dB at 96 kHz. The 220 Hz tone hovering low after a loud voice takes the voice's gain, −4.6 to −1.0 dB re its input (node scripts/ends.js drop). Nor can it render a take apart from what follows it within the delay line's reach. The line reads the residual across a cut, up to 2560 frames ahead shifted up and 512 shifted down, its splices 256 frames further, and splices on what it reads, so over the take's last frames its render into noise 60 and 100 dB under departs from its render into zeros: at 44.1 kHz by up to −3.0 dB re the take's RMS shifted up, over 60 dB under it from 1378 frames before the cut, and by −134.4 dB at most shifted down; short of the cut's reach (6921 frames) by −139.4 and −134.4 dB at most, its one gain apart (96 kHz: +4.3 dB from 1664 frames up, −34.8 dB from 128 frames down). At 22.05 kHz, where those frames are a fifth of the take, its first 0.4 s comes out +1.3 to +2.8 dB and its last 0.1 s −9.3 to −2.0 dB re its input, and into noise it departs by up to +2.7 dB re the take's RMS from 1361 frames before the cut shifted up, −44.1 dB from 209 frames shifted down (node scripts/ends.js drop). A sound after the take moves it within that reach: a click 2000 frames on by −11.7 dB re its RMS, read early at its own level, and dither 100 dB under it by up to 3.8 dB; short of that reach (6921 to 6929 frames) the take and its ring are written as at the call's end up to its one gain, in each of 32 renders at a stop mid-call 0.5 s before more sound, into dither 100 dB under, with a click 2000 frames on, and in a channel ending 0.2 s early, all of it (node scripts/ends.js lpc: a sung voice and a 220 Hz tone at 22.05 to 96 kHz, ±5 and ±12 semitones). Nor can it keep a sound gated faster than its scale returns quiet in its gaps: the delay line reads the next burst early through the filters, and the scale that holds a cut take's head lets it through. A vowel gated at 10 and 4 Hz over room noise 60 dB under it keeps its level, −0.4 to 0.0 dB re its input, but the room noise in the gaps comes out 39.6 to 45.0 dB over its own level (node scripts/ends.js long: 44.1 kHz, +5 and +12 semitones); room tone 60 dB under four vowel phrases comes out −0.7 to +4.3 dB re its input in the pauses between them, but shifted up at 22.05 kHz, where the 2560 frames the delay line reads early span a quarter of each pause, its loudest 20 ms reach 13.4 dB over its level there, the input's own 2.3 dB (node scripts/ends.js match). Nor can it start a take mid-sound without a click: over the first 10 ms of a take that starts loud, the output's peak reaches 17.9 dB over the input's (the voice from 2 s at 96 kHz, −12 semitones), 2.4 dB at the median and over 6 dB in 14 of 48 renders (node scripts/ends.js head: the voice from 2 s and the voice cut loud, 22.05 to 96 kHz, ±1, ±5 and ±12 semitones).
Every fit is bandwidth-expanded, a_k·0.994^k (the weighting factor of FED-STD-1016 § 3.7.2), so no pole sits on the unit circle and a pure tone renders bounded: over the take's middle at most 1.06 times its peak at ±5 semitones and 0.98 times at ±12, over its first 4096 frames, where the onset rings the fit's pole, at most 9.82 and 13.66 times (node scripts/ends.js lpc: 250 Hz to 4 kHz, 22.05 to 96 kHz). A tone moved past half the rate, which the shift leaves silent, stays 85.9 dB under its input over its middle at least, but its onset rings at the tone's own pitch, and its match asks more than 20 dB to bring that ring to the input's energy: it takes 20, the take comes out 2.5 to 7.8 dB under its input, and the ring's peak reaches 17.7 dB over the input's (node scripts/ends.js match). After 0.25 s of silence a tone's match asks more: the click, which can land up to 20 ms before the tone, reaches 9.38 times the tone's peak at ±5 semitones and 6.72 times at ±12, and 18 and 36 of the 48 tones come out under their input, by up to 18.7 and 28.8 dB (node scripts/ends.js lpc); a tone moved past half the rate comes out 25.0 to 29.5 dB under its input there, its peak 12.8 dB over the input's (node scripts/ends.js match).
Every level lpc compares is the call's own, a ratio of two energies or gains, none an absolute level, so a call times a factor renders as its render times that factor. A power of two does so bit for bit but where a ring's far tail falls into float32's subnormals, under 2^-126, and rounds otherwise: no sample over 2^-118.7 differs, none within 569.7 dB of the render's peak. Any other factor does so within its rounding, on the voice to −94.3 dB re the peak; on a pure tone a rounding can move the delay line's splices, to −13.8 dB (node scripts/ends.js scale: 675 channel renders, the voice and two tones at 22.05 to 96 kHz, alone and beside a channel 30 dB down, 2^-20 to 2^4).
Use when: Speech/vocals where the formants must not move with pitch, and the material isn't a bare sustained tone.
Not for: Pure tones, synth pads, or anything where the "partial" and the "envelope" are the same thing.
Stream
let write = lpc({ ratio: 1.5 })
let out = write(inputBlock)
let tail = write() // flush
lpc buffers the whole input; write(chunk) accumulates and the batch algorithm runs once on write() (flush, no argument), returning everything at once.
Data
Input is a Float32Array (mono) or an array of Float32Array channels ([left, right, ...]) — anything else throws TypeError. ratio also accepts a function t => ratio (seconds from stream start) or a Float32Array breakpoint envelope (resampled across the input via ratioDuration, default the input's own duration) for time-varying pitch.
Part of @audio/shift — the shift family umbrella. This README is generated from the umbrella docs.
MIT © audiojs