C#中.wav的平均振幅 [英] Mean amplitude of a .wav in C#

查看:30
本文介绍了C#中.wav的平均振幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

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

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)

    /// <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#中.wav的平均振幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆