WarrenWeckesser / wavio

A Python module for reading and writing WAV files using numpy arrays.
133 stars 19 forks source link

Question: Average amplitude of audio clip #9

Closed alrhalford closed 5 years ago

alrhalford commented 5 years ago

Hi, I have a wav file, and I need a crude estimate of its average loudness (to measure ambient noise levels in a room). Does the following make sense:

s = wavio.read(file).data
mean_amplitude = numpy.mean(s)

or have I fundamentally misunderstood what wavio returns when it read a wav file?

nikolai-kummer commented 5 years ago

You are dealing with audio, which is generally sinusoidal. The average value of a sinusoid is 0 or a constant value.

You should look into the "root mean square value", rather than the average to determine loudness. That will likely be better suited for what you are trying to do.

alrhalford commented 5 years ago

Good point, ok, but if I use RMS, that should be fine?

nikolai-kummer commented 5 years ago

Yeah that should work. If it does not then there might be issues with the file that was loaded. you can try running some error checks to make sure it was loaded properly:

clip = wavio.read(file)

length_in_seconds = len(clip.data)/clip.rate # length of loaded data in seconds
type(clip.data) #this should return numpy.ndarray

mean_amplitude = numpy.mean(clip.data) 
alrhalford commented 5 years ago

Yep, perfect. Had a bit of an issue with integer overflow, but got there in the end. Thank you!