Change the Audio Speed in Python – Python Tutorial

By | January 18, 2022

In this tutorial, we will introduce how to change or modify the speed of an audio file using python. You will use python pysndfx library to implement it.

Preliminary

In order to use python pysndfx, we should install sox application firstly, you can view this tutorial to learn how to install.

A Step Guide to Install SoX (Sound eXchange) on Windows 10 – Python Tutorial

Then we can use pip to install pysndfx

pip install pysndfx

How to use python pysndfx to modify the speed of an audio file

In this section, we will introduce you how to do.

Step 1: we will load some libraries

import soundfile as sf
from pysndfx import AudioEffectsChain

Step 2: we will read a wav audio file using soundfile

For example:

sound_path = 'test.wav'
s, rate = sf.read(sound_path)

Here we will read the data of test.wav, which is a single channel audio.

Step 3: use AudioEffectsChain to change the speed of an audio file

For example:

fx = (AudioEffectsChain().speed(0.8))
s = fx(s, sample_in=rate)

In this example, we will change the speed to 0.8*speed.

Step 4: save modified audio

Finally, we will save audio data using soundfile

For example:

dst = 'test_1.2.wav'
sf.write(dst, s, rate, 'PCM_16')

The audio file may look like:

Change the Audio Speed in Python - Python Tutorial

Leave a Reply