C#中.wav的平均幅度

时间:2022-10-01 19:45:10

Does anyone know a way to get the mean amplitude of a .wav file using C# (even if it means calling an outside command line program and parsing the output)? Thanks!

有没有人知道使用C#获取.wav文件的平均幅度的方法(即使它意味着调用外部命令行程序并解析输出)?谢谢!

3 个解决方案

#1


Here is a snip that reads in a stereo wav and puts the data in two arrays. It's untested because I had to remove some code (converting to mono and calculate a moving average)

这是一个读取立体声wav并将数据放入两个数组的片段。这是未经测试的,因为我不得不删除一些代码(转换为单声道并计算移动平均线)

    /// <summary>
    ///  Read in wav file and put into Left and right array
    /// </summary>
    /// <param name="fileName"></param>
    private void ReadWavfiles(string fileName)
    {
        byte[] fa = File.ReadAllBytes(fileName);

        int startByte = 0;

        // look for data header
        {
            var x = 0;
            while (x < fa.Length)
            {
                if (fa[x]     == 'd' && fa[x + 1] == 'a' && 
                    fa[x + 2] == 't' && fa[x + 3] == 'a')
                {
                    startByte = x + 8;
                    break;
                }
                x++;
            }
        }

        // Split out channels from sample
        var sLeft = new short[fa.Length / 4];
        var sRight = new short[fa.Length / 4];

        {
            var x = 0;
            var length = fa.Length;
            for (int s = startByte; s < length; s = s + 4)
            {
                sLeft[x] = (short)(fa[s + 1] * 0x100 + fa[s]);
                sRight[x] = (short)(fa[s + 3] * 0x100 + fa[s + 2]);
                x++;
            }
        }

        // do somthing with the wav data in sLeft and sRight
    }

#2


The NAudio library for .NET sounds like your best bet. It allows access to the waveform of an audio file, which can loop over to calculate the value of the mean ampltiude.

用于.NET的NAudio库听起来是您最好的选择。它允许访问音频文件的波形,可以循环以计算平均放大器的值。

#3


Normally the root-mean-squared method is used to calculate the "mean" amplitude of sin(x)-like signals.

通常,均方根法用于计算类似sin(x)的信号的“平均”幅度。

#1


Here is a snip that reads in a stereo wav and puts the data in two arrays. It's untested because I had to remove some code (converting to mono and calculate a moving average)

这是一个读取立体声wav并将数据放入两个数组的片段。这是未经测试的,因为我不得不删除一些代码(转换为单声道并计算移动平均线)

    /// <summary>
    ///  Read in wav file and put into Left and right array
    /// </summary>
    /// <param name="fileName"></param>
    private void ReadWavfiles(string fileName)
    {
        byte[] fa = File.ReadAllBytes(fileName);

        int startByte = 0;

        // look for data header
        {
            var x = 0;
            while (x < fa.Length)
            {
                if (fa[x]     == 'd' && fa[x + 1] == 'a' && 
                    fa[x + 2] == 't' && fa[x + 3] == 'a')
                {
                    startByte = x + 8;
                    break;
                }
                x++;
            }
        }

        // Split out channels from sample
        var sLeft = new short[fa.Length / 4];
        var sRight = new short[fa.Length / 4];

        {
            var x = 0;
            var length = fa.Length;
            for (int s = startByte; s < length; s = s + 4)
            {
                sLeft[x] = (short)(fa[s + 1] * 0x100 + fa[s]);
                sRight[x] = (short)(fa[s + 3] * 0x100 + fa[s + 2]);
                x++;
            }
        }

        // do somthing with the wav data in sLeft and sRight
    }

#2


The NAudio library for .NET sounds like your best bet. It allows access to the waveform of an audio file, which can loop over to calculate the value of the mean ampltiude.

用于.NET的NAudio库听起来是您最好的选择。它允许访问音频文件的波形,可以循环以计算平均放大器的值。

#3


Normally the root-mean-squared method is used to calculate the "mean" amplitude of sin(x)-like signals.

通常,均方根法用于计算类似sin(x)的信号的“平均”幅度。