开源C#代码呈现波形?

开源C#代码呈现波形?,c#,audio,C#,Audio,有没有开源的C代码或库来表示给定字节数组的图形波形?这是开源的: public static void DrawNormalizedAudio(ref float[] data, PictureBox pb, Color color) { Bitmap bmp; if (pb.Image == null) { bmp = new Bitmap(pb.Width, pb.Height); } else { bm

有没有开源的C代码或库来表示给定字节数组的图形波形?

这是开源的:

public static void DrawNormalizedAudio(ref float[] data, PictureBox pb,
    Color color)
{
    Bitmap bmp;
    if (pb.Image == null)
    {
        bmp = new Bitmap(pb.Width, pb.Height);
    }
    else
    {
        bmp = (Bitmap)pb.Image;
    }

    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);
        }
    }
    pb.Image = bmp;
}
我想我非常喜欢
for
循环。

我多年来一直是它的粉丝,并用它来显示各种项目中的各种数据

以下示例代码绘制了介于-1和1之间的双精度数组:

void DisplayWaveGraph(ZedGraphControl graphControl, double[] waveData)
{
    var pane = graphControl.GraphPane;
    pane.Chart.Border.IsVisible = false;
    pane.Chart.Fill.IsVisible = false;
    pane.Fill.Color = Color.Black;
    pane.Margin.All = 0;
    pane.Title.IsVisible = false;
    pane.XAxis.IsVisible = false;
    pane.XAxis.Scale.Max = waveData.Length - 1;
    pane.XAxis.Scale.Min = 0;
    pane.YAxis.IsVisible = false;
    pane.YAxis.Scale.Max = 1;
    pane.YAxis.Scale.Min = -1;
    var timeData = Enumerable.Range(0, waveData.Length)
                             .Select(i => (double) i)
                             .ToArray();
    pane.AddCurve(null, timeData, waveData, Color.Lime, SymbolType.None);
    graphControl.AxisChange();
}
上面的示例通过抑制轴和更改颜色来模拟音频编辑器的样式,以生成以下内容:


在中,有用于在WinForms和WPF中绘制音频波形的代码。看看演示项目,了解如何使用它。

我稍微修改了MusiGenesis的解决方案。 这给了我一个更好的结果,尤其是家庭音乐:)

公共静态位图DrawNormalizedAudio(列表数据、颜色前景色、颜色背景色、大小图像大小)
{
位图bmp=新位图(imageSize.Width、imageSize.Height);
int-BORDER_-WIDTH=0;
浮动宽度=bmp.width-(2*边框宽度);
浮动高度=bmp.高度-(2*边框宽度);
使用(Graphics g=Graphics.FromImage(bmp))
{
g、 清晰(背景色);
画笔=新画笔(前景色);
浮点大小=data.Count;
对于(浮点iPixel=0;iPixel数据计数)
end=data.Count;
浮动posAvg、negAvg;
平均值(数据、开始、结束、超出平均值、超出平均值);
浮动yMax=边框宽度+高度-((posAvg+1)*.5f*高度);
浮动yMin=边框宽度+高度-((负向+1)*.5f*高度);
g、 绘图线(钢笔、iPixel+边框宽度、yMax、iPixel+边框宽度、yMin);
}
}
返回bmp;
}
私有静态无效平均值(列表数据、int startIndex、int ENDIX、out float posAvg、out float negAvg)
{
posAvg=0.0f;
负平均值=0.0f;
int posCount=0,negCount=0;
对于(int i=startIndex;i0)
{
posCount++;
posAvg+=数据[i];
}
其他的
{
negCount++;
negAvg+=数据[i];
}
}
posAvg/=posCount;
negAvg/=negCount;
}

使用robby改编的代码,并使用Graphics.Fill/DrawClosedCurve和抗锯齿,我得到了一个非常好看的结果。

代码如下:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Soundfingerprinting.Audio.Services
{
    public static class AudioVisualizationService
    {
        public class WaveVisualizationConfiguration
        {
            public Nullable<Color> AreaColor { get; set; }
            public Nullable<Color> EdgeColor { get; set; }
            public int EdgeSize { get; set; }
            public Nullable<Rectangle> Bounds { get; set; }
            public double Overlap { get; set; }
            public int Step { get; set; }
        }

        public static void DrawWave(float[] data, Bitmap bitmap, WaveVisualizationConfiguration config = null)
        {
            Color areaColor = Color.FromArgb(0x7F87CEFA);// Color.LightSkyBlue; semi transparent
            Color edgeColor = Color.DarkSlateBlue;
            int edgeSize = 2;
            int step = 2;
            double overlap = 0.10f; // would better use a windowing function
            Rectangle bounds = Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height);

            if (config != null)
            {
                edgeSize = config.EdgeSize;
                if (config.AreaColor.HasValue)
                    areaColor = config.AreaColor.GetValueOrDefault();
                if (config.EdgeColor.HasValue)
                    edgeColor = config.EdgeColor.GetValueOrDefault();
                if (config.Bounds.HasValue)
                    bounds = config.Bounds.GetValueOrDefault();

                step = Math.Max(1, config.Step);
                overlap = config.Overlap;
            }

            float width = bounds.Width;
            float height = bounds.Height;

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                Pen edgePen = new Pen(edgeColor);
                edgePen.LineJoin = LineJoin.Round;
                edgePen.Width = edgeSize;
                Brush areaBrush = new SolidBrush(areaColor);

                float size = data.Length;
                PointF[] topCurve = new PointF[(int)width / step];
                PointF[] bottomCurve = new PointF[(int)width / step];
                int idx = 0;
                for (float iPixel = 0; iPixel < width; iPixel += step)
                {
                    // determine start and end points within WAV
                    int start = (int)(iPixel * (size / width));
                    int end = (int)((iPixel + step) * (size / width));
                    int window = end - start;
                    start -= (int)(overlap * window);
                    end += (int)(overlap * window);
                    if (start < 0)
                        start = 0;
                    if (end > data.Length)
                        end = data.Length;

                    float posAvg, negAvg;
                    averages(data, start, end, out posAvg, out negAvg);

                    float yMax = height - ((posAvg + 1) * .5f * height);
                    float yMin = height - ((negAvg + 1) * .5f * height);
                    float xPos = iPixel + bounds.Left;
                    if (idx >= topCurve.Length)
                        idx = topCurve.Length - 1;
                    topCurve[idx] = new PointF(xPos, yMax);
                    bottomCurve[bottomCurve.Length - idx - 1] = new PointF(xPos, yMin);
                    idx++;
                }

                PointF[] curve = new PointF[topCurve.Length * 2];
                Array.Copy(topCurve, curve, topCurve.Length);
                Array.Copy(bottomCurve, 0, curve, topCurve.Length, bottomCurve.Length);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.FillClosedCurve(areaBrush, curve, FillMode.Winding, 0.15f);
                if (edgeSize > 0)
                    g.DrawClosedCurve(edgePen, curve, 0.15f, FillMode.Winding);
            }

        }

        private static void averages(float[] data, int startIndex, int endIndex, out float posAvg, out float negAvg)
        {
            posAvg = 0.0f;
            negAvg = 0.0f;

            int posCount = 0, negCount = 0;

            for (int i = startIndex; i < endIndex; i++)
            {
                if (data[i] > 0)
                {
                    posCount++;
                    posAvg += data[i];
                }
                else
                {
                    negCount++;
                    negAvg += data[i];
                }
            }

            if (posCount > 0)
                posAvg /= posCount;
            if (negCount > 0)
                negAvg /= negCount;
        }
    }
}
使用系统;
使用系统图;
使用System.Drawing.Drawing2D;
命名空间Soundfingerprinting.Audio.Services
{
公共静态类视听服务
{
公共类WaveVisualizationConfiguration
{
公共可空区域颜色{get;set;}
公共可空EdgeColor{get;set;}
public int EdgeSize{get;set;}
公共可空边界{get;set;}
公共双重叠{get;set;}
公共int步骤{get;set;}
}
公共静态void DrawWave(浮点[]数据、位图、WaveVisualizationConfiguration配置=null)
{
Color AREA Color=Color.FromArgb(0x7F87CEFA);//Color.LightSkyBlue;半透明
Color edgeColor=Color.DarkSlateBlue;
int-edgeSize=2;
int步=2;
double overlap=0.10f;//最好使用窗口功能
矩形边界=矩形.FromLTRB(0,0,bitmap.Width,bitmap.Height);
如果(配置!=null)
{
edgeSize=config.edgeSize;
if(config.AreaColor.HasValue)
areaColor=config.areaColor.GetValueOrDefault();
if(config.EdgeColor.HasValue)
edgeColor=config.edgeColor.GetValueOrDefault();
if(config.Bounds.HasValue)
bounds=config.bounds.GetValueOrDefault();
步骤=数学最大值(1,配置步骤);
重叠=配置重叠;
}
浮动宽度=边界宽度;
浮动高度=边界高度;
使用(Graphics g=Graphics.FromImage(位图))
{
Pen edgePen=新笔(edgeColor);
edgePen.LineJoin=LineJoin.Round;
边缘宽度=边缘尺寸;
笔刷区域笔刷=新的SolidBrush(区域颜色);
浮动大小=数据长度;
PointF[]topCurve=新的PointF[(int)宽度/步长];
PointF[]底部曲线=新的PointF[(int)宽度/步长];
int-idx=0;
对于(浮点iPixel=0;iPixel数据长度)
结束=数据长度;
浮动posAvg、negAvg;
平均值(数据、开始、结束、超出平均值、超出平均值);
浮动yMax=高度-((平均位置+1)*.5f*高度);
浮动yMin=高度-((负向+1)*.5f*高度);
float xPos=iPixel+bounds.Left;
如果(idx>=topCurve.Length)
idx=顶部曲线。长度-1;
topCurve[idx]=新的点F(xPos,yMax);
bottomCurve[bottomCurve.Length-idx-1]=新的点f(xPos,yMin);
idx++;
}
点F[]曲线
void DisplayWaveGraph(ZedGraphControl graphControl, double[] waveData)
{
    var pane = graphControl.GraphPane;
    pane.Chart.Border.IsVisible = false;
    pane.Chart.Fill.IsVisible = false;
    pane.Fill.Color = Color.Black;
    pane.Margin.All = 0;
    pane.Title.IsVisible = false;
    pane.XAxis.IsVisible = false;
    pane.XAxis.Scale.Max = waveData.Length - 1;
    pane.XAxis.Scale.Min = 0;
    pane.YAxis.IsVisible = false;
    pane.YAxis.Scale.Max = 1;
    pane.YAxis.Scale.Min = -1;
    var timeData = Enumerable.Range(0, waveData.Length)
                             .Select(i => (double) i)
                             .ToArray();
    pane.AddCurve(null, timeData, waveData, Color.Lime, SymbolType.None);
    graphControl.AxisChange();
}
public static Bitmap DrawNormalizedAudio(List<float> data, Color foreColor, Color backColor, Size imageSize)
{
    Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height);

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

    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.Clear(backColor);
        Pen pen = new Pen(foreColor);
        float size = data.Count;
        for (float iPixel = 0; iPixel < width; iPixel += 1)
        {
            // determine start and end points within WAV
            int start = (int)(iPixel * (size / width));
            int end = (int)((iPixel + 1) * (size / width));
            if (end > data.Count)
                end = data.Count;

            float posAvg, negAvg;
            averages(data, start, end, out posAvg, out negAvg);

            float yMax = BORDER_WIDTH + height - ((posAvg + 1) * .5f * height);
            float yMin = BORDER_WIDTH + height - ((negAvg + 1) * .5f * height);

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

    return bmp;
}


private static void averages(List<float> data, int startIndex, int endIndex, out float posAvg, out float negAvg)
{
    posAvg = 0.0f;
    negAvg = 0.0f;

    int posCount = 0, negCount = 0;

    for (int i = startIndex; i < endIndex; i++)
    {
        if (data[i] > 0)
        {
            posCount++;
            posAvg += data[i];
        }
        else
        {
            negCount++;
            negAvg += data[i];
        }
    }

    posAvg /= posCount;
    negAvg /= negCount;
}
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Soundfingerprinting.Audio.Services
{
    public static class AudioVisualizationService
    {
        public class WaveVisualizationConfiguration
        {
            public Nullable<Color> AreaColor { get; set; }
            public Nullable<Color> EdgeColor { get; set; }
            public int EdgeSize { get; set; }
            public Nullable<Rectangle> Bounds { get; set; }
            public double Overlap { get; set; }
            public int Step { get; set; }
        }

        public static void DrawWave(float[] data, Bitmap bitmap, WaveVisualizationConfiguration config = null)
        {
            Color areaColor = Color.FromArgb(0x7F87CEFA);// Color.LightSkyBlue; semi transparent
            Color edgeColor = Color.DarkSlateBlue;
            int edgeSize = 2;
            int step = 2;
            double overlap = 0.10f; // would better use a windowing function
            Rectangle bounds = Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height);

            if (config != null)
            {
                edgeSize = config.EdgeSize;
                if (config.AreaColor.HasValue)
                    areaColor = config.AreaColor.GetValueOrDefault();
                if (config.EdgeColor.HasValue)
                    edgeColor = config.EdgeColor.GetValueOrDefault();
                if (config.Bounds.HasValue)
                    bounds = config.Bounds.GetValueOrDefault();

                step = Math.Max(1, config.Step);
                overlap = config.Overlap;
            }

            float width = bounds.Width;
            float height = bounds.Height;

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                Pen edgePen = new Pen(edgeColor);
                edgePen.LineJoin = LineJoin.Round;
                edgePen.Width = edgeSize;
                Brush areaBrush = new SolidBrush(areaColor);

                float size = data.Length;
                PointF[] topCurve = new PointF[(int)width / step];
                PointF[] bottomCurve = new PointF[(int)width / step];
                int idx = 0;
                for (float iPixel = 0; iPixel < width; iPixel += step)
                {
                    // determine start and end points within WAV
                    int start = (int)(iPixel * (size / width));
                    int end = (int)((iPixel + step) * (size / width));
                    int window = end - start;
                    start -= (int)(overlap * window);
                    end += (int)(overlap * window);
                    if (start < 0)
                        start = 0;
                    if (end > data.Length)
                        end = data.Length;

                    float posAvg, negAvg;
                    averages(data, start, end, out posAvg, out negAvg);

                    float yMax = height - ((posAvg + 1) * .5f * height);
                    float yMin = height - ((negAvg + 1) * .5f * height);
                    float xPos = iPixel + bounds.Left;
                    if (idx >= topCurve.Length)
                        idx = topCurve.Length - 1;
                    topCurve[idx] = new PointF(xPos, yMax);
                    bottomCurve[bottomCurve.Length - idx - 1] = new PointF(xPos, yMin);
                    idx++;
                }

                PointF[] curve = new PointF[topCurve.Length * 2];
                Array.Copy(topCurve, curve, topCurve.Length);
                Array.Copy(bottomCurve, 0, curve, topCurve.Length, bottomCurve.Length);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.FillClosedCurve(areaBrush, curve, FillMode.Winding, 0.15f);
                if (edgeSize > 0)
                    g.DrawClosedCurve(edgePen, curve, 0.15f, FillMode.Winding);
            }

        }

        private static void averages(float[] data, int startIndex, int endIndex, out float posAvg, out float negAvg)
        {
            posAvg = 0.0f;
            negAvg = 0.0f;

            int posCount = 0, negCount = 0;

            for (int i = startIndex; i < endIndex; i++)
            {
                if (data[i] > 0)
                {
                    posCount++;
                    posAvg += data[i];
                }
                else
                {
                    negCount++;
                    negAvg += data[i];
                }
            }

            if (posCount > 0)
                posAvg /= posCount;
            if (negCount > 0)
                negAvg /= negCount;
        }
    }
}