Python Music Playback Speed(1x,2x,3x) Change without chipmunk Effect.

Lokesh Deshmukh
Prog-Ramming Solutions
2 min readJul 6, 2020

I was going through so many articles about changing play back speed of mp3, wav file but it was so confusing. So, I came across various articles and learned how this works actually.

In Python, We have a Library AudioSegment it helps to convert .mp3->.wav and vice versa.

from pydub import AudioSegment
sound = AudioSegment.from_mp3("somemp3filename.mp3")
sound.export("file.wav", format="wav")

From the SoundFile library we will take the raw mp3 data and its sample rate.

import soundfile as sf

data, samplerate = sf.read('existing_file.wav')

It gives us data (music data) and Sample Rate. But soundfile supports only “WAV, FLAC, OGG and MAT” format so that’s why you need to convert your .mp3 file to wav first.

Now, you have the data and its sample rate.

To stretch the time you will be using PyRubberBand.

import soundfile as s
import pyrubberband as pyrb
# Play back at 1.5X speed
y_stretch = pyrb.time_stretch(y, sr, 1.5)
# Play back two 1.5x tones
y_shift = pyrb.pitch_shift(y, sr, 1.5)
sf.write("outputfile1X5.wav", y_stretch, sr, format='wav')

This will stretch your music time and pitch_shift will further remove chipmunk effect from the music.

Now, the last step is to convert it to back to mp3.

sound = AudioSegment.from_wav("outputfile1X5.wav")
sound.export("mp3fromatfile.mp3", format="mp3")

Volla!.

Let me help you a little bit more about how to configure all these libraries in your python environment.

Go to you desired folder then in command prompt(Mac OS):

python3 -m venv newven
#this will create new virtual environment
source newvenv/bin/activatepip3 install pydub
pip3 install soundfile
pip3 install pyrubberband
pip3 install numpy
pip3 install AudioSegment

Full Source Code: (save it via name: musicconvertor.py)

import wave
import sys
from pydub import AudioSegment
#sound = AudioSegment.from_file("deviprasadgharpehai.mp3")
sound = AudioSegment.from_mp3(sys.argv[1])
sound.export("file.wav", format="wav")

print(sys.argv[1])

import soundfile as sf
import pyrubberband as pyrb
y, sr = sf.read("file.wav")
# Play back at extra low speed
y_stretch = pyrb.time_stretch(y, sr, 0.5)
# Play back extra low tones
y_shift = pyrb.pitch_shift(y, sr, 0.5)
sf.write("analyzed_filepathX5.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathX5.wav")
sound.export("analyzed_filepathX5.mp3", format="mp3")

# Play back at low speed
y_stretch = pyrb.time_stretch(y, sr, 0.75)
# Play back at low tones
y_shift = pyrb.pitch_shift(y, sr, 0.75)
sf.write("analyzed_filepathX75.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathX75.wav")
sound.export("analyzed_filepathX75.mp3", format="mp3")

# Play back at 1.5X speed
y_stretch = pyrb.time_stretch(y, sr, 1.5)
# Play back two 1.5x tones
y_shift = pyrb.pitch_shift(y, sr, 1.5)
sf.write("analyzed_filepathX105.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathX105.wav")
sound.export("analyzed_filepathX105.mp3", format="mp3")

# Play back at same speed
y_stretch = pyrb.time_stretch(y, sr, 1)
# Play back two smae-tones
y_shift = pyrb.pitch_shift(y, sr, 1)
sf.write("analyzed_filepathXnormal.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathXnormal.wav")
sound.export("analyzed_filepathXnormal.mp3", format="mp3")

Now you can run command:

python3 musicconvertor.py yourfilename.mp3

Feel Free to ask any question.

--

--

Lokesh Deshmukh
Prog-Ramming Solutions

Android developer | Photographer | Web Deveoper| Freelancer