Graphics NAudio 2通道波形到图形
各位同事,你好! 救命啊,头已经断了。。。 下面的代码我从WAV文件中获取一个波形,并将其输出到图形中。。。我打赌如何在左通道和右通道上分割图形的输出。。。请尝试品尝NAudio的手册,但不了解我需要什么Graphics NAudio 2通道波形到图形,graphics,naudio,Graphics,Naudio,各位同事,你好! 救命啊,头已经断了。。。 下面的代码我从WAV文件中获取一个波形,并将其输出到图形中。。。我打赌如何在左通道和右通道上分割图形的输出。。。请尝试品尝NAudio的手册,但不了解我需要什么 #region draw container SolidBrush whiteBrush = new SolidBrush(Color.White); SolidBrush blackBrush = new SolidBrush(Color.Black);
#region draw container
SolidBrush whiteBrush = new SolidBrush(Color.White);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Pen blackPen = new Pen(Color.Black, 1);
e.Graphics.FillRectangle(whiteBrush, 0, 0, this.Width-2, this.Height-2);
e.Graphics.DrawLine(blackPen, 0, 0, this.Width-2, 0);
e.Graphics.DrawLine(blackPen, 0, this.Height / 2, this.Width-2, this.Height / 2);
e.Graphics.DrawLine(blackPen, 0, this.Height - 2, this.Width-2, this.Height-2);
e.Graphics.DrawLine(blackPen, 0, 0, 0, this.Height-2);
e.Graphics.DrawLine(blackPen, this.Width-2, 0, this.Width-2, this.Height-2);
e.Graphics.DrawString("L:", new Font("Arial", 6, FontStyle.Bold), blackBrush, 2, 2);
e.Graphics.DrawString("R:", new Font("Arial", 6, FontStyle.Bold), blackBrush, 2, (this.Height /2) + 2);
#endregion
if (waveStream != null)
{
waveStream.Position = 0;
int bytesRead;
byte[] waveData = new byte[samplesPerPixel * bytesPerSample];
waveStream.Position = startPosition + (e.ClipRectangle.Left * bytesPerSample * samplesPerPixel);
Pen linePen_L = new Pen(PenColor_L, PenWidth);
Pen linePen_R = new Pen(PenColor_R, PenWidth);
//bool _left = true; bool _right = false;
for (float x = e.ClipRectangle.X; x < e.ClipRectangle.Right; x += 1)
{
//_left = !_left;
//_right = !_right;
short low = 0;
short high = 0;
bytesRead = waveStream.Read(waveData, 0, samplesPerPixel * bytesPerSample);
if (bytesRead == 0)
break;
for (int n = 0; n < bytesRead; n += 2)
{
short sample = BitConverter.ToInt16(waveData, n);
if (sample < low) low = sample;
if (sample > high) high = sample;
}
float lowPercent = ((((float)low) - short.MinValue) / ushort.MaxValue);
float highPercent = ((((float)high) - short.MinValue) / ushort.MaxValue);
//if (_left)
e.Graphics.DrawLine(linePen_L, x, (this.Height * lowPercent) /2, x, (this.Height * highPercent)/2);
//if (_right)
//e.Graphics.DrawLine(linePen_R, x, ((this.Height * lowPercent) /2) + this.Height/2, x, ((this.Height * highPercent) /2) + this.Height / 2);
}
}
#区域绘制容器
SolidBrush whiteBrush=新的SolidBrush(颜色为白色);
SolidBrush blackBrush=新的SolidBrush(颜色为黑色);
钢笔黑色钢笔=新钢笔(Color.Black,1);
e、 Graphics.FillRectangle(白色笔刷,0,0,this.Width-2,this.Height-2);
e、 图形绘制线(黑笔,0,0,this.Width-2,0);
e、 图形绘制线(黑笔,0,this.Height/2,this.Width-2,this.Height/2);
e、 图形绘制线(黑笔,0,this.Height-2,this.Width-2,this.Height-2);
e、 图形绘制线(黑笔,0,0,0,this.Height-2);
e、 图形绘制线(黑笔,this.Width-2,0,this.Width-2,this.Height-2);
e、 图形。抽绳(“L:”,新字体(“Arial”,6,FontStyle.Bold),黑线,2,2);
e、 图形。抽绳(“R:”,新字体(“Arial”,6,FontStyle.Bold),blackBrush,2,(this.Height/2)+2);
#端区
if(waveStream!=null)
{
波流位置=0;
int字节读取;
字节[]waveData=新字节[samplesPerPixel*bytesPerSample];
waveStream.Position=startPosition+(e.ClipRectangle.Left*字节数/样本数/像素);
Pen linePen_L=新笔(铅笔色,笔宽);
钢笔线条钢笔=新钢笔(铅笔颜色,钢笔宽度);
//布尔左=真;布尔右=假;
对于(float x=e.ClipRectangle.x;x高)高=样本;
}
浮点低百分比=(((浮点)低)-short.MinValue)/ushort.MaxValue);
浮点高百分比=(((浮点)高)-short.MinValue)/ushort.MaxValue);
//如果(左)
e、 图形绘制线(linePen_L,x,(this.Height*lowPercent)/2,x,(this.Height*highPercent)/2);
//如果(右)
//e、 图形绘制线(linePen_R,x,((this.Height*lowPercent)/2)+this.Height/2,x,((this.Height*highPercent)/2)+this.Height/2);
}
}
我找到了方法:)
if(waveStream!=null)
{
波流位置=0;
int字节读取;
字节[]waveData=新字节[samplesPerPixel*bytesPerSample];
waveStream.Position=startPosition+(e.ClipRectangle.Left*字节数/样本数/像素);
Pen linePen_L=新笔(铅笔色,笔宽);
钢笔线条钢笔=新钢笔(铅笔颜色,钢笔宽度);
对于(float x=e.ClipRectangle.x;x高样本)高样本=样本;
n+=2;
短样本\u R=位转换器.ToInt16(waveData,n);
如果(样本数<低样本数)低样本数=样本数;
如果(样本>高样本)高样本=样本;
}
浮点低百分比(L=(((浮点)低值)-short.MinValue)/ushort.MaxValue);
float highPercent_L=(((float)high_L)-short.MinValue)/ushort.MaxValue);
浮点低百分比_R=(((浮点)低_R)-short.MinValue)/ushort.MaxValue);
float highPercent_R=(((float)high_R)-short.MinValue)/ushort.MaxValue);
e、 图形.抽绳(线笔长度,x,(此高度*低百分比长度)/2,x,(此高度*高百分比长度)/2);
e、 图形绘制线(linePen\u R,x,((this.Height*lowPercent\u R)/2)+this.Height/2,x,((this.Height*highPercent\u R)/2)+this.Height/2);
}
}
if (waveStream != null)
{
waveStream.Position = 0;
int bytesRead;
byte[] waveData = new byte[samplesPerPixel * bytesPerSample];
waveStream.Position = startPosition + (e.ClipRectangle.Left * bytesPerSample * samplesPerPixel);
Pen linePen_L = new Pen(PenColor_L, PenWidth);
Pen linePen_R = new Pen(PenColor_R, PenWidth);
for (float x = e.ClipRectangle.X; x < e.ClipRectangle.Right; x += 1)
{
short low_L = 0;
short high_L = 0;
short low_R = 0;
short high_R = 0;
bytesRead = waveStream.Read(waveData, 0, samplesPerPixel * bytesPerSample);
if (bytesRead == 0)
break;
for (int n = 0; n < bytesRead; n += 2)
{
short sample_L = BitConverter.ToInt16(waveData, n);
if (sample_L < low_L) low_L = sample_L;
if (sample_L > high_L) high_L = sample_L;
n += 2;
short sample_R = BitConverter.ToInt16(waveData, n);
if (sample_R < low_R) low_R = sample_R;
if (sample_R > high_R) high_R = sample_R;
}
float lowPercent_L = ((((float)low_L) - short.MinValue) / ushort.MaxValue);
float highPercent_L = ((((float)high_L) - short.MinValue) / ushort.MaxValue);
float lowPercent_R = ((((float)low_R) - short.MinValue) / ushort.MaxValue);
float highPercent_R = ((((float)high_R) - short.MinValue) / ushort.MaxValue);
e.Graphics.DrawLine(linePen_L, x, (this.Height * lowPercent_L) / 2, x, (this.Height * highPercent_L) / 2);
e.Graphics.DrawLine(linePen_R, x, ((this.Height * lowPercent_R) / 2) + this.Height / 2, x, ((this.Height * highPercent_R) / 2) + this.Height / 2);
}
}