Search code examples
c#naudio

Show each channel in WaveViever - Naudio


How can I draw only one channel. My example work for mono, but sound is stereo. I want here show only left or only right channel.

protected override void OnPaint(PaintEventArgs e)
    {
        if (waveStream != null)
        {
            waveStream.Position = 0;
            int bytesRead;
            byte[] waveData = new byte[samplesPerPixel * bytesPerSample];
            waveStream.Position = startPosition + (e.ClipRectangle.Left * bytesPerSample * samplesPerPixel);

            using (Pen linePen = new Pen(PenColor, PenWidth))
            {
                for (float x = e.ClipRectangle.X; x < e.ClipRectangle.Right; x += 1)
                {
                    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);
                    e.Graphics.DrawLine(linePen, x, this.Height * lowPercent, x, this.Height * highPercent);
                }
            }
        }

        base.OnPaint(e);
    }

I read, 1-3-5... for first channel, and 2-4-6... for second channel. But if in for loop change this, I got different graph, but not correct. How can I fix that?


Solution

  • Use WaveWiever Painter. Work real-time and for two channels.