# How to change speed of audio samples without changing pitch? [duplicate]

This question may be more appropriate on music stack exchange but since sound is a branch of physics and this is more of a technical question relating to sound waves than a musical question I asked it here.

How can you change the speed of audio samples without changing pitch? I have heard songs that are sped up without changing the pitch of the song. I also know that simply compressing an audio sample changes the frequency and thus the pitch of the sound waves that are perceived by our brains. So how is this done without changing pitch?

• I'm voting to close this question as off-topic because it has nothing to do with physics - it belongs on Signal Processing. Dec 13 '17 at 22:28
• this is the correct destination for this question. Dec 14 '17 at 1:35

This question (about "time-scaling" audio) is closely related to pitch shifting, which is time-scaling combined with resampling. But changing the speed without changing pitch is only time-scaling, so there is no resampling involved (contrary to what thomas has suggested).

There are frequency-domain methods (phase-vocoder and sinusoidal modeling) that can change speed for an orchestral mix (or some other broadbanded non-periodic sound) without glitches, but they can become a little "phasey" in their stretched sound.

If it's monophonic, like a solo or a single note or string or voice, a less expensive time-domain approach is sufficient and can sound very good, under this monophonic condition. If it's a single note or tone, the signal is quasiperiodic. That means, in the vicinity of some time, $t_0$, that

$$x(t) \approx x(t - \tau(t_0)) \qquad \text{for } t \approx t_0$$

So $\tau(t_0) > 0$ is the estimated period of the quasiperiodic waveform in the neighborhood of $t_0$. This estimated period $\tau(t_0)$ is estimated by finding the value of $\tau$ that minimizes something like

$$Q_x(\tau, t_0) = \int \Big|x(t) - x(t-\tau)\Big|^2 w(t-t_0) \, dt$$

where the window $w(t-t_0) \ge 0$ is centered around the time $t_0$. If there are multiple candidates of $\tau$ that minimize $Q_x(\tau, t_0)$, usually it is best to pick the smallest $\tau$ in which $Q_x(\tau, t_0)$ is small.

Now imagine copying your note $x(t)$ and offsetting (delaying) the copy in time by the amount $\tau(t_0)$. That delayed copy would be $x(t - \tau(t_0))$. Then you have two identical waveforms, except for the offset of $\tau(t_0)$ but around the time $t_0$ the two waveforms will look almost the same because the offset is exactly one period, based on the estimate of the period around time $t_0$.

Then you can cross-fade (using a Smoothstep function $S(t)$) from the original to the offset copy, the cross-fade will not have any nasty cancellation or "destructive interference" because the waveforms are lined up. And the result will be the same note, but one period longer. (Longer by $\tau(t_0)$ seconds.)

$$y(t) = \Big(1-S\big(\tfrac{t-t_0}{\tau(t_o)}\big) \Big) \, x(t) \ + \ S\big(\tfrac{t-t_0}{\tau(t_o)}\big) \, x(t - \tau(t_0))$$

(That won't stretch it long enough to notice a difference, but if you repeat this operation many times per second, you can stretch a patch of sound by even a factor of two.)

You need to increase the sample rate.

Imagine you are playing 5k samples per second for a 1 Hz signal. If you play the same samples out at 10k per second, then you will have a 2 Hz signal. If instead you upsample by 2x and then play the 10k samples per second you will still have a 1 Hz signal.

In the filter design section of the wiki page they talk about interpolation. If you take the FFT of your original signal in time, append an equal number (to the number of FFT freq samples) of zeros to the end of the existing FFT samples, and then perform the IFFT you will have a 2x upsampled signal in time. The process of zero padding in the frequency domain results in interpolation in the time domain. An interesting side note is that you can perform the same operation to interpolate in the frequency domain.

Alternatively, you could just do a simple averaging between every 2 points in the time domain to get a slightly worse, but possibly less computationally intensive upsampled signal.

• thomas, i must disagree with you. if Murey wants to stretch the sound without changing pitch, there is no resampling and no changing of the sample rate. Dec 14 '17 at 3:23
• Ah, yep, you are right. The question title says change the "speed of audio samples", i.e. sample rate, so I was confused. I thought he was trying to play the same track on some sort of higher fidelity, higher sample rate system. Really he just wants to increase the duration of the song without changing any of the notes. Your answer or the duplicate question link and wiki page seem to answer the question better Dec 14 '17 at 16:15

Doing a good job of this is non-trivial. The particular algorithms also depend on the content. Check out rubber-band library.

• Hi! This is not really the stuff that answers are made of – it would, however, be a very good comment to the question! Dec 13 '17 at 23:20