试图绘制波形但不绘制任何数据

时间:2014-07-13 12:59:04

标签: c# audio image-processing data-visualization

我正在尝试根据以下答案从WAV文件中绘制波形: https://stackoverflow.com/a/1215472/356635

不幸的是,它每次产生一个大的黑色方块。我正在努力弄清楚为什么会这样,是否有人能够发现问题? normalisedData中的所有值均介于-1 and +1之间。 test.wav文件是Windows wav文件,播放5秒钟的警报声。

这是我的代码:

using System;
using System.Drawing;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        const string rootDir = "C:\\inetpub\\wwwroot\\";
        if (File.Exists(rootDir + "test.png"))
        {
            File.Delete(rootDir + "test.png");
        }

        var stream = new MemoryStream(File.ReadAllBytes(rootDir + "test.wav"));
        var data = stream.ToArray();
        stream.Dispose();
        var normalisedData = FloatArrayFromByteArray(data);

        // Get max value in data
        var maxValue = 0f;
        for (var i = 0; i < normalisedData.Length; i++)
        {
            if (normalisedData[i] > maxValue) maxValue = normalisedData[i];
        }

        // Normalise data
        for (var i = 0; i < normalisedData.Length; i++)
        {
            normalisedData[i] = (data[i] / maxValue) * 100f;
        }       

        // Save picture
        var picture = DrawNormalizedAudio(normalisedData, Color.LimeGreen);
        picture.Save(rootDir + "test.png", System.Drawing.Imaging.ImageFormat.Png);
        picture.Dispose();
    }
    public static Bitmap DrawNormalizedAudio(float[] data, Color color)
    {
        Bitmap bmp;
        bmp = new Bitmap(500,500);

        int BORDER_WIDTH = 5;
        int width = bmp.Width - (2 * BORDER_WIDTH);
        int height = bmp.Height - (2 * BORDER_WIDTH);

        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.Clear(Color.Black);
            Pen pen = new Pen(color);
            int size = data.Length;
            for (int iPixel = 0; iPixel < width; iPixel++)
            {
                // determine start and end points within WAV
                int start = (int)((float)iPixel * ((float)size / (float)width));
                int end = (int)((float)(iPixel + 1) * ((float)size / (float)width));
                float min = float.MaxValue;
                float max = float.MinValue;
                for (int i = start; i < end; i++)
                {
                    float val = data[i];
                    min = val < min ? val : min;
                    max = val > max ? val : max;
                }
                int yMax = BORDER_WIDTH + height - (int)((max + 1) * .5 * height);
                int yMin = BORDER_WIDTH + height - (int)((min + 1) * .5 * height);
                g.DrawLine(pen, iPixel + BORDER_WIDTH, yMax,
                    iPixel + BORDER_WIDTH, yMin);
            }
        }
        return bmp;
    }

    public float[] FloatArrayFromByteArray(byte[] input)
    {
        float[] output = new float[input.Length / 4];
        System.Buffer.BlockCopy(input, 0, output, 0, input.Length);
        return output;
    }
}

1 个答案:

答案 0 :(得分:1)

WAV文件的格式比代码假设的格式更复杂。它们具有描述音频的标题和其他元数据。音频数据通常存储为带符号的16位或8位整数值,但其他格式也是可能的。您需要找到一个库,将WAV文件解码为可在您的应用程序中使用的浮点值,或者使用实用程序将WAV文件转换为原始浮点格式文件。

您的规范化代码也不正确。由于样本值可能为负,因此您需要找到样本绝对值的最大值。您不应该乘以100,因为它会将它们标准化为-100,100。