views:

811

answers:

3

I'm working on a project where I need to know the amplitude of sound coming in from a microphone on a computer.

I'm currently using Python with the Snack Sound Toolkit and I can record audio coming in from the microphone, but I need to know how loud that audio is. I could save the recording to a file and use another toolkit to read in the amplitude at given points in time from the audio file, or try and get the amplitude while the audio is coming in (which could be more error prone).

Are there any libraries or sample code that can help me out with this? I've been looking and so far the Snack Sound Toolkit seems to be my best hope, yet there doesn't seem to be a way to get direct access to amplitude.

+2  A: 

Looking at the Snack Sound Toolkit examples, there seems to be a dbPowerSpectrum function.

From the reference:

dBPowerSpectrum ( )

Computes the log FFT power spectrum of the sound (at the sample number given in the start option) and returns a list of dB values. See the section item for a description of the rest of the options. Optionally an ending point can be given, using the end option. In this case the result is the average of consecutive FFTs in the specified range. Their default spacing is taken from the fftlength but this can be changed using the skip option, which tells how many points to move the FFT window each step. Options:

EDIT: I am assuming when you say amplitude, you mean how "loud" the sound appears to a human, and not the time domain voltage(Which would probably be 0 throughout the entire length since the integral of sine waves is going to be 0. eg: 10 * sin(t) is louder than 5 * sin(t), but their average value over time is 0. (You do not want to send non-AC voltages to a speaker anyways)).

To get how loud the sound is, you will need to determine the amplitudes of each frequency component. This is done with a Fourier Transform (FFT), which breaks down the sound into it's frequency components. The dbPowerSpectrum function seems to give you a list of the magnitudes (forgive me if this differs from the exact definition of a power spectrum) of each frequency. To get the total volume, you can just sum the entire list (Which will be close, xept it still might be different from percieved loudness since the human ear has a frequency response itself).

CookieOfFortune
+1  A: 

I disagree completely with this "answer" from CookieOfFortune.

granted, the question is poorly phrased... but this answer is making things much more complex than necessary. I am assuming that by 'amplitude' you mean perceived loudness. as technically each sample in the (PCM) audio stream represents an amplitude of the signal at a given time-slice. to get a loudness representation try a simple RMS calculation:

RMS

|K<

kent
The library did not provide a RMS function. I believe the integral of the power spectrum over all frequencies should be mathematically proportional (If not directly equivalent) to the RMS. (Power spectrum is in W/hz, RMS is in W. Integrate over all hz and you are left with W).
CookieOfFortune
you are not wrong. but doing an FFT to get the power of a discrete signal is like shooting a housefly with a shotgun: it works, but is rather wasteful.
kent
A: 

I'm not sure if this will help, but skimpygimpy provides facilities for parsing WAVE files into python sequences and back -- you could potentially use this to examine the wave form samples directly and do what you like. You will have to read some source, these subcomponents are not documented.

Aaron Watters