<p>I'm tasked with building a .NET client app to detect silence in a WAV files.</p>

<p>Is this possible with the built-in Windows APIs? Or alternately, any good libraries out there to help with this?</p>

[{'answer_id': 19357, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C" rel="noreferrer">http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C</a></p>\n\n<p>This has all the code necessary to strip silence, and mix wave files.</p>\n\n<p>Enjoy.</p>\n'}, {'answer_id': 19363, 'author': 'chitza', 'author_id': 2073, 'author_profile': 'https://Stackoverflow.com/users/2073', 'pm_score': 1, 'selected': False, 'text': '<p>I don\'t think you\'ll find any built-in APIs for detection of silence. But you can always use good ol\' math/discreete signal processing to find out loudness.\nHere\'s a small example: <a href="http://msdn.microsoft.com/en-us/magazine/cc163341.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc163341.aspx</a></p>\n'}, {'answer_id': 19373, 'author': 'Simon Gillbee', 'author_id': 756, 'author_profile': 'https://Stackoverflow.com/users/756', 'pm_score': 5, 'selected': True, 'text': '<p>Audio analysis is a difficult thing requiring a lot of complex math (think Fourier Transforms). The question you have to ask is "what is silence". If the audio that you are trying to edit is captured from an analog source, the chances are that there isn\'t any silence... they will only be areas of soft noise (line hum, ambient background noise, etc).</p>\n\n<p>All that said, an algorithm that should work would be to determine a minimum volume (amplitude) threshold and duration (say, &lt;10dbA for more than 2 seconds) and then simply do a volume analysis of the waveform looking for areas that meet this criteria (with perhaps some filters for millisecond spikes). I\'ve never written this in C#, but this <a href="http://www.codeproject.com/Articles/20025/Sound-visualizer-in-C" rel="noreferrer">CodeProject article</a> looks interesting; it describes C# code to draw a waveform... that is the same kind of code which could be used to do other amplitude analysis.</p>\n'}, {'answer_id': 31597, 'author': 'Mark Borgerding', 'author_id': 3343, 'author_profile': 'https://Stackoverflow.com/users/3343', 'pm_score': 3, 'selected': False, 'text': '<p>If you want to efficiently calculate the average power over a sliding window:  square each sample, then add it to a running total.  Subtract the squared value from N samples previous. Then move to the next step. This is the simplest form of a <a href="http://en.wikipedia.org/wiki/Cascaded_integrator-comb" rel="noreferrer">CIC</a> Filter. <a href="http://en.wikipedia.org/wiki/Parseval&#39;s_theorem" rel="noreferrer" title="Parseval&#39;s Theorem">Parseval\'s Theorem</a> tells us that this power calculation is applicable to both time and frequency domains.</p>\n\n<p>Also you may want to add <a href="http://en.wikipedia.org/wiki/Hysteresis" rel="noreferrer">Hysteresis</a> to the system to avoid switching on&amp;off rapidly when power level is dancing about the threshold level.</p>\n'}, {'answer_id': 1607001, 'author': 'Manu', 'author_id': 2133, 'author_profile': 'https://Stackoverflow.com/users/2133', 'pm_score': 1, 'selected': False, 'text': '<p>Use <a href="http://sox.sourceforge.net/" rel="nofollow noreferrer">Sox</a>. It can remove leading and trailing silences, but you\'ll have to call it as an exe from your app.</p>\n'}, {'answer_id': 21500363, 'author': 'Aleks', 'author_id': 3258422, 'author_profile': 'https://Stackoverflow.com/users/3258422', 'pm_score': -1, 'selected': False, 'text': '<p>See code below from <a href="http://alvas.net/alvas.audio,tips.aspx#tip117" rel="nofollow">Detecting audio silence in WAV files using C#</a></p>\n\n<pre><code>private static void SkipSilent(string fileName, short silentLevel)\n{\n    WaveReader wr = new WaveReader(File.OpenRead(fileName));\n    IntPtr format = wr.ReadFormat();\n    WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"), \n        AudioCompressionManager.FormatBytes(format));\n    int i = 0;\n    while (true)\n    {\n        byte[] data = wr.ReadData(i, 1);\n        if (data.Length == 0)\n        {\n            break;\n        }\n        if (!AudioCompressionManager.CheckSilent(format, data, silentLevel))\n        {\n            ww.WriteData(data);\n        }\n    }\n    ww.Close();\n    wr.Close();\n}\n</code></pre>\n'}, {'answer_id': 46024371, 'author': '41686d6564 stands w. Palestine', 'author_id': 8967612, 'author_profile': 'https://Stackoverflow.com/users/8967612', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m using <a href="https://github.com/naudio/NAudio" rel="noreferrer">NAudio</a>, and I wanted to detect the silence in audio files so I can either report or truncate.</p>\n\n<p>After a lot of research, I came up with this basic implementation. So, I wrote an extension method for the <a href="https://github.com/naudio/NAudio/blob/master/NAudio/Wave/WaveStreams/AudioFileReader.cs" rel="noreferrer"><code>AudioFileReader</code></a> class which returns the silence duration at the start/end of the file, or starting from a specific position.</p>\n\n<p>Here:</p>\n\n<pre><code>static class AudioFileReaderExt\n{\n    public enum SilenceLocation { Start, End }\n\n    private static bool IsSilence(float amplitude, sbyte threshold)\n    {\n        double dB = 20 * Math.Log10(Math.Abs(amplitude));\n        return dB &lt; threshold;\n    }\n    public static TimeSpan GetSilenceDuration(this AudioFileReader reader,\n                                              SilenceLocation location,\n                                              sbyte silenceThreshold = -40)\n    {\n        int counter = 0;\n        bool volumeFound = false;\n        bool eof = false;\n        long oldPosition = reader.Position;\n\n        var buffer = new float[reader.WaveFormat.SampleRate * 4];\n        while (!volumeFound &amp;&amp; !eof)\n        {\n            int samplesRead = reader.Read(buffer, 0, buffer.Length);\n            if (samplesRead == 0)\n                eof = true;\n\n            for (int n = 0; n &lt; samplesRead; n++)\n            {\n                if (IsSilence(buffer[n], silenceThreshold))\n                {\n                    counter++;\n                }\n                else\n                {\n                    if (location == SilenceLocation.Start)\n                    {\n                        volumeFound = true;\n                        break;\n                    }\n                    else if (location == SilenceLocation.End)\n                    {\n                        counter = 0;\n                    }\n                }\n            }\n        }\n\n        // reset position\n        reader.Position = oldPosition;\n\n        double silenceSamples = (double)counter / reader.WaveFormat.Channels;\n        double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;\n        return TimeSpan.FromMilliseconds(silenceDuration);\n    }\n}\n</code></pre>\n\n<p>This will accept almost any audio file format <em>not just WAV</em>.</p>\n\n<p><strong>Usage:</strong></p>\n\n<pre><code>using (AudioFileReader reader = new AudioFileReader(filePath))\n{\n    TimeSpan duration = reader.GetSilenceDuration(AudioFileReaderExt.SilenceLocation.Start);\n    Console.WriteLine(duration.TotalMilliseconds);\n}\n</code></pre>\n\n<p><strong>References:</strong></p>\n\n<ul>\n<li><a href="https://stackoverflow.com/a/9812267/4934172">How audio dB levels are calculated</a>.</li>\n<li><a href="https://stackoverflow.com/q/29761331/4934172">Floating-point samples range</a>.</li>\n<li><a href="https://sound.stackexchange.com/q/23746">More about amplitude</a>.</li>\n</ul>\n'}, {'answer_id': 60262870, 'author': 'Ibai', 'author_id': 9955237, 'author_profile': 'https://Stackoverflow.com/users/9955237', 'pm_score': 3, 'selected': False, 'text': '<p>Here a nice variant to detect threshold alternatings:</p>\n\n<pre><code>static class AudioFileReaderExt\n{\n\n\n    private static bool IsSilence(float amplitude, sbyte threshold)\n    {\n        double dB = 20 * Math.Log10(Math.Abs(amplitude));\n        return dB &lt; threshold;\n    }\n\n    private static bool IsBeep(float amplitude, sbyte threshold)\n    {\n        double dB = 20 * Math.Log10(Math.Abs(amplitude));\n        return dB &gt; threshold;\n    }\n\n    public static double GetBeepDuration(this AudioFileReader reader,\n                                              double StartPosition, sbyte silenceThreshold = -40)\n    {\n        int counter = 0;\n        bool eof = false;\n        int initial = (int)(StartPosition * reader.WaveFormat.Channels * reader.WaveFormat.SampleRate / 1000);\n        if (initial &gt; reader.Length) return -1;\n        reader.Position = initial;\n        var buffer = new float[reader.WaveFormat.SampleRate * 4];\n        while (!eof)\n        {\n            int samplesRead = reader.Read(buffer, 0, buffer.Length);\n            if (samplesRead == 0)\n                eof = true;\n\n            for (int n = initial; n &lt; samplesRead; n++)\n            {\n                if (IsBeep(buffer[n], silenceThreshold))\n                {\n                    counter++;\n                }\n                else\n                {\n                    eof=true; break;\n                }\n            }\n        }\n\n\n        double silenceSamples = (double)counter / reader.WaveFormat.Channels;\n        double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;\n\n        return TimeSpan.FromMilliseconds(silenceDuration).TotalMilliseconds;\n    }\n\n    public static double GetSilenceDuration(this AudioFileReader reader,\n                                              double StartPosition, sbyte silenceThreshold = -40)\n    {\n        int counter = 0;\n        bool eof = false;\n        int initial = (int)(StartPosition * reader.WaveFormat.Channels * reader.WaveFormat.SampleRate / 1000);\n        if (initial &gt; reader.Length) return -1;\n        reader.Position = initial;\n        var buffer = new float[reader.WaveFormat.SampleRate * 4];\n        while (!eof)\n        {\n            int samplesRead = reader.Read(buffer, 0, buffer.Length);\n            if (samplesRead == 0)                    \n                eof=true;\n\n            for (int n = initial; n &lt; samplesRead; n++)\n            {\n                if (IsSilence(buffer[n], silenceThreshold))\n                {\n                    counter++;\n                }\n                else\n                {\n                    eof=true; break;\n                }\n            }\n        }\n\n\n        double silenceSamples = (double)counter / reader.WaveFormat.Channels;\n        double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;\n\n        return TimeSpan.FromMilliseconds(silenceDuration).TotalMilliseconds;\n    }\n\n\n}\n</code></pre>\n\n<p>Main usage:</p>\n\n<pre><code>using (AudioFileReader reader = new AudioFileReader("test.wav"))\n        {\n            double duratioff = 1;\n            double duration = 1;\n            double position = 1;\n            while (duratioff &gt;-1 &amp;&amp; duration &gt;-1)\n            {\n                duration = reader.GetBeepDuration(position);\n                Console.WriteLine(duration);\n                position = position + duration;\n                duratioff = reader.GetSilenceDuration(position);\n                Console.WriteLine(-duratioff);\n                position = position + duratioff;\n            }\n        }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/19353', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/536/']