Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Updated WaveformPainter

$
0
0

Hello, I have extended the NAudio.Gui.WaveformPainter class to support colored samples. This way you can colorize the waveform. I use it for displaying RX/TX audio in my voip app's waveform.

It should work the same as before, but I added an new AddMax(float,Color) function. Changed some variables such as List<float> samples to also hold color information.

Would be nice to see this updated in NAudio, thanks!

WaveformPainter

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NAudio.Gui
{
    /// <summary>
    /// Windows Forms control for painting audio waveforms, coloring support added by Brandon Hansen, KG6YPI
    /// </summary>
    public partial class WaveformPainter : Control
    {
        Pen foregroundPen;
        List<KeyValuePair<float, Color>> samples = new List<KeyValuePair<float, Color>>(1000);

        int maxSamples;
        int insertPos;

        /// <summary>
        /// Constructs a new instance of the WaveFormPainter class
        /// </summary>
        public WaveformPainter()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer, true);
            InitializeComponent();
            OnForeColorChanged(EventArgs.Empty);
            OnResize(EventArgs.Empty);
        }

        /// <summary>
        /// On Resize
        /// </summary>
        protected override void OnResize(EventArgs e)
        {
            maxSamples = this.Width;
            base.OnResize(e);
        }

        /// <summary>
        /// On ForeColor Changed
        /// </summary>
        /// <param name="e"></param>
        protected override void OnForeColorChanged(EventArgs e)
        {
            foregroundPen = new Pen(ForeColor);
            base.OnForeColorChanged(e);
        }

        /// <summary>
        /// Add Max Value.
        /// </summary>
        /// <param name="maxSample"></param>
        public void AddMax(float maxSample)
        {
            if (maxSamples == 0)
            {
                // sometimes when you minimise, max samples can be set to 0
                return;
            }
            if (samples.Count <= maxSamples)
            {
                samples.Add(new KeyValuePair<float, Color>(maxSample, foregroundPen.Color));
            }
            else if (insertPos < maxSamples)
            {
                samples[insertPos] = new KeyValuePair<float, Color>(maxSample, foregroundPen.Color);
            }
            insertPos++;
            insertPos %= maxSamples;

            this.Invalidate();
        }

        /// <summary>
        /// Add Max Value with specified Color.
        /// </summary>
        /// <param name="maxSample"></param>
        /// <param name="color"></param>
        public void AddMax(float maxSample, Color color)
        {
            if (maxSamples == 0)
            {
                // sometimes when you minimise, max samples can be set to 0
                return;
            }
            if (samples.Count <= maxSamples)
            {
                samples.Add(new KeyValuePair<float, Color>(maxSample, color));
            }
            else if (insertPos < maxSamples)
            {
                samples[insertPos] = new KeyValuePair<float, Color>(maxSample, color);
            }
            insertPos++;
            insertPos %= maxSamples;

            this.Invalidate();
        }

        /// <summary>
        /// On Paint
        /// </summary>
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            KeyValuePair<float, Color> _sample;
            for (int x = 0; x < this.Width; x++)
            {
                _sample = GetSample(x - this.Width + insertPos);
                float lineHeight = this.Height * _sample.Key;
                float y1 = (this.Height - lineHeight) / 2;
                pe.Graphics.DrawLine(new Pen(_sample.Value), x, y1, x, y1 + lineHeight);
            }
        }

        KeyValuePair<float,Color> GetSample(int index)
        {
            if (index < 0)
                index += maxSamples;
            if (index >= 0 & index < samples.Count)
                return samples[index];

            return new KeyValuePair<float, Color>(0, foregroundPen.Color);
        }
    }
}


Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>