NAudio 2声道波形到图形

时间:2018-07-25 13:27:31

标签: graphics naudio

早上好,同事们! 救命,头部已经坏了... 以下代码使我从WAV文件中获取了一个wave,并将其输出到Graphics中……我押注于如何在左右声道上拆分图形的输出……请……!试图品尝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);
            }

        }

1 个答案:

答案 0 :(得分:0)

我找到了方法:)

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

        }