使用C#检测WAV文件中的音频静音

Jud*_*ngo 28 .net c# audio

我的任务是构建一个.NET客户端应用程序来检测WAV文件中的静音.

内置Windows API可以实现这一点吗?或者,那里有任何好的图书馆来帮助解决这个问题?

Sim*_*bee 14

音频分析是一项困难的事情,需要大量复杂的数学运算(想想傅立叶变换).你要问的问题是"什么是沉默".如果您尝试编辑的音频是从模拟信号源捕获的,则可能没有任何静音...它们只会是软噪声区域(线路嗡嗡声,环境背景噪声等).

总而言之,一个应该工作的算法是确定最小音量(幅度)阈值和持续时间(例如,<10dbA超过2秒),然后简单地对波形进行体积分析,寻找符合此标准的区域(可能有一些过滤器用于毫秒尖峰).我从来没有在C#中写过这个,但这个CodeProject文章看起来很有趣; 它描述了绘制波形的C#代码......这是可用于进行其他振幅分析的相同类型的代码.


Fly*_*wat 10

http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C

这具有剥离静音和混合波形文件所需的所有代码.

请享用.


Mar*_*ing 9

如果要有效计算滑动窗口的平均功率:对每个样本求平方,然后将其添加到运行总计中.从之前的N个样本中减去平方值.然后转到下一步.这是CIC过滤器的最简单形式.Parseval定理告诉我们,这个功率计算适用于时域和频域.

此外,您可能希望向系统添加滞后,以避免在功率水平在阈值水平附近跳动时快速打开和关闭.


Iba*_*bai 6

这里有一个很好的变体来检测阈值交替:

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;
    }


}
Run Code Online (Sandbox Code Playgroud)

主要用途:

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;
            }
        }
Run Code Online (Sandbox Code Playgroud)


Ahm*_*eed 5

我正在使用NAudio,我想检测音频文件中的静音,以便我可以报告或截断。

经过大量研究,我想出了这个基本实现。所以,我写了一个扩展方法AudioFileReader该类返回文件开始/结束或从特定位置开始的静音持续时间。

这里:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将接受几乎所有音频文件格式,而不仅仅是 WAV

用法:

using (AudioFileReader reader = new AudioFileReader(filePath))
{
    TimeSpan duration = reader.GetSilenceDuration(AudioFileReaderExt.SilenceLocation.Start);
    Console.WriteLine(duration.TotalMilliseconds);
}
Run Code Online (Sandbox Code Playgroud)

参考: