C#显示波形
使用此选项:C#显示波形,c#,waveform,C#,Waveform,使用此选项: 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;
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;
}
它说操作溢出(不能被零除)或类似的东西。关于这个问题有什么线索吗?谢谢
更新:
我用来调用函数的代码是:
fileName = "c:\\sound\\happy_birthday.wav";
byte[] bytes = File.ReadAllBytes(fileName);
float[] getval = FloatArrayFromByteArray(bytes);
DrawNormalizedAudio(ref getval, pictureBox1, Color.White);
你正在除以零。不要这样做,因为此操作无效。检查传递给
绘图线的值是否不为0
int yMin = BORDER_WIDTH + height - (int)((min + 1) * .5 * height);
您正在使用带符号的int数据类型-发生的情况是,此计算溢出并导致负值,您得到-2147483572
,即int.MaxValue+75
(溢出),是不是min
是一个较大的负值,导致结果比int.MaxValue
大75?应该是min
和max
是错误的
float min = float.MinValue;
float max = float.MaxValue;
类似的东西还是被零除?在那一点上yMax和yMin是什么?@Bobby Alexander-它只是说了附加信息:溢出错误。@jonsca-yMax是41,yMin是-2147483572。是否可能是因为某种原因,DrawLine在计算线的斜率,因为你的x值是相同的,这可能是导致问题的原因,但我不确定。试着做第二个x值iPixel+BORDER\u WIDTH+5
或其他什么。对不起,我不明白我是怎么把值翻转过来的。但是,如果ymin的值是75,那么考虑到ymax的值是41,它看起来就不符合逻辑了?@cnewbie:我正在编辑我的答案-min
的值是多少?哦,我明白了。很抱歉最小值为-3.296672E+38。似乎问题出在最小值上,对吗?然而,从代码上看,它似乎是从字节数组中提取和处理的。@cnewbie:这就是你的problem@cnewbie:这相当于Math.Min(val,Min)
No,它们设置正确-在搜索集合中的最大值和最小值之前,最大值的初始值应设置为最小值,最小值的初始值应设置为最大值。否则算法将无法工作。
float min = float.MinValue;
float max = float.MaxValue;