11.0 Changing playback speed#
We have actually seen resampling in one other guise already. When wavetable synthesis reads a table faster or slower to change its pitch, that is resampling. The same idea lets us change the speed of a recording, and with it, its pitch.
Here the goal is to change a clip’s duration from \(T^1\) to \(T^2\) while keeping the sample rate fixed. The new length is
and we read the original at interpolated positions exactly as before, now with the ratio \(T^1/T^2\):
Stretching or squeezing the signal in time shifts every frequency it contains by the factor \(T^1/T^2\). Playing a clip at twice the speed halves its duration and raises every frequency by an octave, chipmunk-style.
Notice that changing the sample rate and changing the speed are fundamentally the same operation. The only difference is the ratio used to convert between sample indices, and whether we play the result back at a new sample rate or the original one. In Pyquist, we can change speed by reinterpreting the sample rate and then resampling back:
ratio = 2.0 # 2x speed, up an octave
sped_up = pq.Audio(audio.samples, int(audio.sample_rate * ratio))
sped_up = sped_up.resample(audio.sample_rate)