Change the speed of the sound file

I want to change the speed of an audio file, but I don’t understand how to do this. I suppose that some type of interpolation should take place in the case of deceleration, but I'm not sure how to achieve acceleration - perhaps an average of several samples? Regardless of whether it changes the tempo or pitch, it doesn't really matter at the moment, I would like to know how to do both, but I would like to at least do one or the other to get started.

If anyone has math references for these types of operations, they will be very grateful!

Thanks Ben

+4
source share
2 answers

There are two options for speeding up a sound file:

  • Increase sampling rate
  • Reduce the number of samples per unit time.

In any of these methods, an increase in playback speed will have a corresponding change in pitch.

Sample rate increase

Increasing the sampling rate will increase the speed of sound reproduction. For example, switching from a sampling frequency of 22 kHz to 44 kHz will cause playback to play twice as fast as the original. In this method, the original sampling data is not changed - only the sound reproduction parameters need to be changed.

Reduce the number of samples per unit time

In this method, the sampling frequency of the reproduction is kept constant, but the number of samples decreases — some of the samples are discarded.

A naive approach to reproducing sound twice as fast as the original one is to delete all other samples and play back at the original playback sample rate.

However, with this approach, some of the information will be lost, and I expect that some artifacts will be introduced into the audio, so this is not the most desirable approach.

Although I have not tried it myself, the idea of ​​averaging samples to create a new sample was a good approach to start with. This, apparently, means that instead of just discarding the audio information, it can be “saved” to some extent by the averaging process.

As a rough idea of ​​the process, here a piece of pseudocode doubles the playback speed:

original_samples = [0, 0.1, 0.2, 0.3, 0.4, 0.5] def faster(samples): new_samples = [] for i = 0 to samples.length: if i is even: new_samples.add(0.5 * (samples[i] + samples[i+1])) return new_samples faster_samples = faster(original_samples) 

I also posted an answer to the question “ Getting started with software audio, ” where I talked in detail about some of the basic audio manipulations that can be performed, which may be interesting.

+9
source

There is a good explanation for converting sample rates on Wikipedia. Basically, you convert your signal to the least common multiple of the two sampling rates, filter out any frequencies that do not match the target sampling frequency (or do not come from the source), and select new samples in the target sample. There are mathematical tricks to make the calculation take significantly less resources (multiphase decomposition), but this should help you get started.

+3
source

All Articles