NAudio 2 通道波到图形

NAudio 2-channels waves to graphics

同事们,你们好! 救命,头已经坏了... 下面的代码我从 WAV 文件中得到一个 wave 并将其输出到图形中......我打赌如何在左右通道上分割图形的输出......拜托......!试图品尝 NAudio 上的手册,但对我需要的东西一无所知......

#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);
            }

        }

我找到方法了:)

            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);
            }

        }