有谁知道一种使用C#获取.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
    }

关于c# - C#中.wav的平均幅度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1064168/

10-15 22:37