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) print(clip[0].shape)
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) print(clips)
Then, we can combine these audio clips.
wav_data = [] for c in clips: print(c) data = audio[c[0]: c[1]] wav_data.extend(data) 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.
Nicely explained 🙂