Python Remove Silence in WAV Using Librosa – Librosa Tutorial

By | November 16, 2021

In this tutorial, we will introduce how to use python librosa to remove silence in a wav file, which is very useful if you plan to process wav files.

Here are two kinds of removing silence from wav file, we will introduce one by one.

Read a wav file using librosa

We can use librosa.load() to read a wav data. Here is an example:

import librosa
import soundfile as sf

audio_file = r'F:\test.wav'
#read wav data
audio, sr = librosa.load(audio_file, sr= 8000, mono=True)
print(audio.shape, sr)

Run this code we will find:

(101600,) 8000

We can find this wav file contains 101600 length data.

Remove silence at the beginning and end in a wav file

We can use librosa.effects.trim() to do it.

librosa.effects.trim() is defined as:

def trim(y, top_db=60, ref=np.max, frame_length=2048, hop_length=512):

It will trim leading and trailing silence from an audio signal.

Here is an example:

clip = librosa.effects.trim(audio, top_db= 10)

In this code, we will remove audio signal that is lower than 10db.

Here clip contains audio signal without silence. We can save it as follows:

sf.write('F:\\2.wav', clip[0], sr)

Remove all silence in a wav file

We can use librosa.effects.split() to remove all silence in a wav file.

First, we can use librosa.effects.split() to split a wav file based on silence.

clips = librosa.effects.split(audio, top_db=10)

Then, we can combine these audio clips.

wav_data = []
for c in clips:
    data = audio[c[0]: c[1]]

sf.write('5s.wav', wav_data, sr)

Here top_db is important, which determines what signal is silence.

librosa.effects.split() is defined:

def split(y, top_db=60, ref=np.max, frame_length=2048, hop_length=512):

Here y is the audio data.

One thought on “Python Remove Silence in WAV Using Librosa – Librosa Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *