1

在这里,我正在尝试使用此代码获取波形图像,但音频流持续时间较短,我得到的是空白图像。请纠正我哪里出错了。我正在像波浪图像一样跳跃声音云。

    public static void DrawNormalizedAudio(ref float[] data, Color color)
    {
        Bitmap bmp= new Bitmap(1000,200);            

        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.Gray);
            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) * 0.5 * height);

                int yMin = BORDER_WIDTH + height - (int)((min + 1) * 0.5 * height);


                g.DrawLine(pen, iPixel + BORDER_WIDTH, yMax,iPixel + BORDER_WIDTH, yMin);

            }
        }

        bmp.Save("D:\\Images\\soundtrack.png"); 
    }

    public float[] FloatArrayFromStream(System.IO.MemoryStream stream)
    {
        return FloatArrayFromByteArray(stream.GetBuffer());
    }

    public float[] FloatArrayFromByteArray(byte[] input)
    {
        float[] output = new float[input.Length / 4];
        for (int i = 0; i < output.Length; i++)
        {
            output[i] = BitConverter.ToSingle(input, i * 4);
        }
        return output;
    }
4

0 回答 0