How to detect empty wave file in Python?

4.6k views Asked by At

I am trying to sort through a bunch of audio samples where some of them are fully blank. Can someone help me figure out the best way to detect if a file is blank (silent) for its duration? I've found a bunch of ways of reading wav files, but thus far nothing on how to determine if a file is blank.

The code I have thus far:

import soundfile as sf

path = '/Users/InNov8/Desktop/Elektron_Octotrack_Chain_Maker_v2/Bar 25 -- High 303/'

file1 = 'Bar 25 --- [tr] ---  4-Kick 2.aif' # blank
file2 = 'Bar 25 --- [tr] ---  10 Shaker.aif' # not blank
file3 = 'Bar 25 --- [tr] ---  14 HARD SNARE.aif' # blank

f1 = path + file1
x, fs = sf.read(f1)
print x
print fs
f2 = path + file2
x, fs = sf.read(f2)
print x
print fs
3

There are 3 answers

0
Anil_M On

If I understand correctly, you have bunch of audio files and some of them are below a certain threshold that are causing issue. If you are open to using external libraries, you can use pydub to detect if given audio_segment is above/below given threshold.

First of all , you can install pydub on windows using

   pip install pydub

Next you can load audio file (example is for wav, but you can find methods to load other types of files in the link).

Per website:

AudioSegment(…).dBFS
Returns the loudness of the AudioSegment in dBFS (db relative to the maximum possible loudness). A Square wave at maximum amplitude will be roughly 0 dBFS (maximum loudness), whereas a Sine Wave at maximum amplitude will be roughly -3 dBFS

Load file and find out its loudness

from pydub import AudioSegment
sound = AudioSegment.from_file("sound1.wav")

loudness = sound.dBFS

You can load multiple files and compare loudness as above.

Alternately, if you are looking for loudness for certain portion of a file, then you will need to split audio segment in chunks and then check loudness for each chunk. Again, you can find details on the site.

0
Manish On

Thanks to Dietrich Epp for putting me on the right track.

Here's the code that works to move the nearly empty files to another directory

import soundfile as np
import os
import sys
import shutil


path = '/Users/InNov8/Desktop/Elektron_Octotrack_Chain_Maker_v2/'

source = 'Bar 25 -- High 303/'

dst = 'Bar 25 -- High 303 -- blank/'


source_path = path + source
dst_path = path + dst

files = []

for dirname, dirnames, filenames in os.walk(source_path):
    for filename in filenames:
        if 'aif' in filename or 'wav' in filename:
            if 'asd' not in filename:
                print os.path.join(dirname, filename)
                files.append(filename)


for f in files:
    x, fs = np.read(source_path + f)
    vol_rms =  x.max() - x.min()

    print f
    print vol_rms

    if vol_rms <= 6.103515625e-05:
        print '\n\nTRUE'
        print 'MOVE: ', source_path + f
        print 'DST: ', dst_path + f
        shutil.move(source_path + f, dst_path + f)

    print '\n\n\n'
5
Dietrich Epp On

Check that the data in the file contains only zeroes. The soundfile module is based on NumPy, so you can do that using some functions which are a fair bit faster than just using any(). See: Test if numpy array contains only zeros

Note that just because a file sounds blank doesn't mean that it's actually all zeroes. It could just be very quiet, or contain a small amount of noise.