MerlijnWajer/SNARP — pull request #2 by casebeer (2013-09-13 03:46:11+00:00)
Title: Dbfs delta silence algo

This is the second of two pull requests in sequence. Here, I've modified the silence detection algorithm.

Rather than specifying absolute SILENCE_MIN and SILENCE_MAX values, I'm measuring the delta between the max_ and min_ sample values. By specifying that delta as a silence limit, we don't have to worry about DC offsets – that is, when the signal is not centered on 0 (or 127, for 8 bit unsigned audio).

For additional robustness, I've added a second "delta" range measurement: the IQR, or inter-quartile range of the samples in the chunk. This is just the limits of the middle 50% of the sample values, and since sound is generally symmetric across the zero point, is effectively a measurement of the range of the 50th percentile of the sample values.

The IQR is far less susceptible to transient noise than the overall peak to peak range. Since I'm allowing either the peak or IQR setting to declare a chunk "audible," you can set a much more conservative peak delta, avoiding false positives from transient yet loud crackles, and a more liberal IQR delta, avoiding missing legitimate audible noise, which should be long enough in duration to raise the IQR, which truly transient noise should do less so.

Finally, I've then converted the input values for these ranges into decibels against the full scale, or dbFS. This just means that rather than having to convert your silence limits for every different sample width, you can just specify a ratio of the overall sample range.

For instance, if you wanted to set your peak limit at a signal that used 50% of the entire sample range (way too loud in real life!), you'd just say -6 dBFS. Whether that sample width is 8 bits or 16 bits, the dBFS is a ratio of the total sample width, so it need not be converted.

I've also added some logging and analysis of these peak and IQR dBFS values, making it easier to analyze a sample of your source audio and see on a histogram where the noise and silence cutoffs should be. Here's a screenshot of such a histogram:

![figure_squelched_audio_deltas](https://f.cloud.github.com/assets/349839/1135857/f6d7be8c-1c26-11e3-9df5-531b60049359.png)