使用 C# 检测 WAV 文件中的音频静音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting audio silence in WAV files using C#
提问by Judah Gabriel Himango
I'm tasked with building a .NET client app to detect silence in a WAV files.
我的任务是构建一个 .NET 客户端应用程序来检测 WAV 文件中的静音。
Is this possible with the built-in Windows APIs? Or alternately, any good libraries out there to help with this?
这可以通过内置的 Windows API 实现吗?或者,有什么好的图书馆可以帮助解决这个问题?
采纳答案by Simon Gillbee
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).
音频分析是一件困难的事情,需要大量复杂的数学运算(想想傅立叶变换)。你必须问的问题是“什么是沉默”。如果您尝试编辑的音频是从模拟源捕获的,则很可能没有任何静音……它们只会是软噪声区域(线路嗡嗡声、环境背景噪声等)。
All that said, an algorithm that should work would be to determine a minimum volume (amplitude) threshold and duration (say, <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 CodeProject articlelooks 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.
综上所述,应该工作的算法是确定最小音量(幅度)阈值和持续时间(例如,<10dbA 超过 2 秒),然后简单地对波形进行音量分析,寻找符合此标准的区域(也许有一些毫秒尖峰过滤器)。我从来没有用 C# 写过这个,但是这篇CodeProject 文章看起来很有趣;它描述了绘制波形的 C# 代码......这与可用于进行其他幅度分析的代码相同。
回答by FlySwat
http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C
http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C
This has all the code necessary to strip silence, and mix wave files.
这具有去除静音和混合波形文件所需的所有代码。
Enjoy.
享受。
回答by chitza
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. Here's a small example: http://msdn.microsoft.com/en-us/magazine/cc163341.aspx
我认为您不会找到任何用于检测静音的内置 API。但是你总是可以使用好的数学/谨慎的信号处理来找出响度。这是一个小例子:http: //msdn.microsoft.com/en-us/magazine/cc163341.aspx
回答by Mark Borgerding
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 CICFilter. Parseval's Theoremtells us that this power calculation is applicable to both time and frequency domains.
如果您想有效地计算滑动窗口上的平均功率:对每个样本进行平方,然后将其添加到运行总数中。从之前的 N 个样本中减去平方值。然后进入下一步。这是CIC滤波器的最简单形式。Parseval 定理告诉我们,这种功率计算适用于时域和频域。
Also you may want to add Hysteresisto the system to avoid switching on&off rapidly when power level is dancing about the threshold level.
此外,您可能希望向系统添加迟滞,以避免在功率水平在阈值水平附近跳动时快速打开和关闭。
回答by Manu
回答by Aleks
See code below from Detecting audio silence in WAV files using C#
请参阅下面的代码,使用 C# 检测 WAV 文件中的音频静音
private static void SkipSilent(string fileName, short silentLevel)
{
WaveReader wr = new WaveReader(File.OpenRead(fileName));
IntPtr format = wr.ReadFormat();
WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"),
AudioCompressionManager.FormatBytes(format));
int i = 0;
while (true)
{
byte[] data = wr.ReadData(i, 1);
if (data.Length == 0)
{
break;
}
if (!AudioCompressionManager.CheckSilent(format, data, silentLevel))
{
ww.WriteData(data);
}
}
ww.Close();
wr.Close();
}
回答by Ahmed Abdelhameed
I'm using NAudio, and I wanted to detect the silence in audio files so I can either report or truncate.
我正在使用NAudio,我想检测音频文件中的静音,以便我可以报告或截断。
After a lot of research, I came up with this basic implementation. So, I wrote an extension method for the AudioFileReader
class which returns the silence duration at the start/end of the file, or starting from a specific position.
经过大量研究,我想出了这个基本实现。因此,我为AudioFileReader
该类编写了一个扩展方法,该方法返回文件开始/结束或从特定位置开始的静音持续时间。
Here:
这里:
static class AudioFileReaderExt
{
public enum SilenceLocation { Start, End }
private static bool IsSilence(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB < threshold;
}
public static TimeSpan GetSilenceDuration(this AudioFileReader reader,
SilenceLocation location,
sbyte silenceThreshold = -40)
{
int counter = 0;
bool volumeFound = false;
bool eof = false;
long oldPosition = reader.Position;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!volumeFound && !eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof = true;
for (int n = 0; n < samplesRead; n++)
{
if (IsSilence(buffer[n], silenceThreshold))
{
counter++;
}
else
{
if (location == SilenceLocation.Start)
{
volumeFound = true;
break;
}
else if (location == SilenceLocation.End)
{
counter = 0;
}
}
}
}
// reset position
reader.Position = oldPosition;
double silenceSamples = (double)counter / reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration);
}
}
This will accept almost any audio file format not just WAV.
这将接受几乎所有音频文件格式,而不仅仅是 WAV。
Usage:
用法:
using (AudioFileReader reader = new AudioFileReader(filePath))
{
TimeSpan duration = reader.GetSilenceDuration(AudioFileReaderExt.SilenceLocation.Start);
Console.WriteLine(duration.TotalMilliseconds);
}
References:
参考:
回答by Ibai
Here a nice variant to detect threshold alternatings:
这里有一个很好的变体来检测阈值交替:
static class AudioFileReaderExt
{
private static bool IsSilence(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB < threshold;
}
private static bool IsBeep(float amplitude, sbyte threshold)
{
double dB = 20 * Math.Log10(Math.Abs(amplitude));
return dB > threshold;
}
public static double GetBeepDuration(this AudioFileReader reader,
double StartPosition, sbyte silenceThreshold = -40)
{
int counter = 0;
bool eof = false;
int initial = (int)(StartPosition * reader.WaveFormat.Channels * reader.WaveFormat.SampleRate / 1000);
if (initial > reader.Length) return -1;
reader.Position = initial;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof = true;
for (int n = initial; n < samplesRead; n++)
{
if (IsBeep(buffer[n], silenceThreshold))
{
counter++;
}
else
{
eof=true; break;
}
}
}
double silenceSamples = (double)counter / reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration).TotalMilliseconds;
}
public static double GetSilenceDuration(this AudioFileReader reader,
double StartPosition, sbyte silenceThreshold = -40)
{
int counter = 0;
bool eof = false;
int initial = (int)(StartPosition * reader.WaveFormat.Channels * reader.WaveFormat.SampleRate / 1000);
if (initial > reader.Length) return -1;
reader.Position = initial;
var buffer = new float[reader.WaveFormat.SampleRate * 4];
while (!eof)
{
int samplesRead = reader.Read(buffer, 0, buffer.Length);
if (samplesRead == 0)
eof=true;
for (int n = initial; n < samplesRead; n++)
{
if (IsSilence(buffer[n], silenceThreshold))
{
counter++;
}
else
{
eof=true; break;
}
}
}
double silenceSamples = (double)counter / reader.WaveFormat.Channels;
double silenceDuration = (silenceSamples / reader.WaveFormat.SampleRate) * 1000;
return TimeSpan.FromMilliseconds(silenceDuration).TotalMilliseconds;
}
}
Main usage:
主要用途:
using (AudioFileReader reader = new AudioFileReader("test.wav"))
{
double duratioff = 1;
double duration = 1;
double position = 1;
while (duratioff >-1 && duration >-1)
{
duration = reader.GetBeepDuration(position);
Console.WriteLine(duration);
position = position + duration;
duratioff = reader.GetSilenceDuration(position);
Console.WriteLine(-duratioff);
position = position + duratioff;
}
}