Server/Spectrum/LineSpectrum.cs
author StephaneLenclud
Wed, 04 Jan 2017 18:43:28 +0100
changeset 274 920fea7a6427
parent 273 e5f85a895a62
child 275 a4a341accc89
permissions -rw-r--r--
Proper basic support for Audio Visualizer.
StephaneLenclud@273
     1
using System;
StephaneLenclud@273
     2
using System.ComponentModel;
StephaneLenclud@273
     3
using System.Drawing;
StephaneLenclud@273
     4
using System.Drawing.Drawing2D;
StephaneLenclud@273
     5
using System.Drawing.Text;
StephaneLenclud@273
     6
using CSCore.DSP;
StephaneLenclud@273
     7
StephaneLenclud@273
     8
namespace Visualization
StephaneLenclud@273
     9
{
StephaneLenclud@273
    10
    public class LineSpectrum : SpectrumBase
StephaneLenclud@273
    11
    {
StephaneLenclud@273
    12
        private int _barCount;
StephaneLenclud@273
    13
        private double _barSpacing;
StephaneLenclud@273
    14
        private double _barWidth;
StephaneLenclud@273
    15
        private Size _currentSize;
StephaneLenclud@274
    16
        
StephaneLenclud@274
    17
        
StephaneLenclud@273
    18
        public LineSpectrum(FftSize fftSize)
StephaneLenclud@273
    19
        {
StephaneLenclud@274
    20
            FftSize = fftSize;            
StephaneLenclud@273
    21
        }
StephaneLenclud@273
    22
StephaneLenclud@273
    23
        [Browsable(false)]
StephaneLenclud@273
    24
        public double BarWidth
StephaneLenclud@273
    25
        {
StephaneLenclud@273
    26
            get { return _barWidth; }
StephaneLenclud@273
    27
        }
StephaneLenclud@273
    28
StephaneLenclud@273
    29
        public double BarSpacing
StephaneLenclud@273
    30
        {
StephaneLenclud@273
    31
            get { return _barSpacing; }
StephaneLenclud@273
    32
            set
StephaneLenclud@273
    33
            {
StephaneLenclud@273
    34
                if (value < 0)
StephaneLenclud@273
    35
                    throw new ArgumentOutOfRangeException("value");
StephaneLenclud@273
    36
                _barSpacing = value;
StephaneLenclud@273
    37
                UpdateFrequencyMapping();
StephaneLenclud@273
    38
StephaneLenclud@273
    39
                RaisePropertyChanged("BarSpacing");
StephaneLenclud@273
    40
                RaisePropertyChanged("BarWidth");
StephaneLenclud@273
    41
            }
StephaneLenclud@273
    42
        }
StephaneLenclud@273
    43
StephaneLenclud@273
    44
        public int BarCount
StephaneLenclud@273
    45
        {
StephaneLenclud@273
    46
            get { return _barCount; }
StephaneLenclud@273
    47
            set
StephaneLenclud@273
    48
            {
StephaneLenclud@273
    49
                if (value <= 0)
StephaneLenclud@273
    50
                    throw new ArgumentOutOfRangeException("value");
StephaneLenclud@273
    51
                _barCount = value;
StephaneLenclud@273
    52
                SpectrumResolution = value;
StephaneLenclud@273
    53
                UpdateFrequencyMapping();
StephaneLenclud@273
    54
StephaneLenclud@273
    55
                RaisePropertyChanged("BarCount");
StephaneLenclud@273
    56
                RaisePropertyChanged("BarWidth");
StephaneLenclud@273
    57
            }
StephaneLenclud@273
    58
        }
StephaneLenclud@273
    59
StephaneLenclud@273
    60
        [BrowsableAttribute(false)]
StephaneLenclud@273
    61
        public Size CurrentSize
StephaneLenclud@273
    62
        {
StephaneLenclud@273
    63
            get { return _currentSize; }
StephaneLenclud@273
    64
            protected set
StephaneLenclud@273
    65
            {
StephaneLenclud@273
    66
                _currentSize = value;
StephaneLenclud@273
    67
                RaisePropertyChanged("CurrentSize");
StephaneLenclud@273
    68
            }
StephaneLenclud@273
    69
        }
StephaneLenclud@273
    70
StephaneLenclud@274
    71
        /// <summary>
StephaneLenclud@274
    72
        /// Update our math.
StephaneLenclud@274
    73
        /// </summary>
StephaneLenclud@274
    74
        /// <returns></returns>
StephaneLenclud@274
    75
        public bool Update()
StephaneLenclud@274
    76
        {
StephaneLenclud@274
    77
            return SpectrumProvider.GetFftData(iFftBuffer, this);
StephaneLenclud@274
    78
        }
StephaneLenclud@274
    79
StephaneLenclud@273
    80
        public Bitmap CreateSpectrumLine(Size size, Brush brush, Color background, bool highQuality)
StephaneLenclud@273
    81
        {
StephaneLenclud@273
    82
            if (!UpdateFrequencyMappingIfNessesary(size))
StephaneLenclud@274
    83
            {
StephaneLenclud@273
    84
                return null;
StephaneLenclud@274
    85
            }
StephaneLenclud@273
    86
StephaneLenclud@274
    87
            //get the fft result from the spectrum provider            
StephaneLenclud@274
    88
            using (var pen = new Pen(brush, (float)_barWidth))
StephaneLenclud@274
    89
            {
StephaneLenclud@274
    90
                var bitmap = new Bitmap(size.Width, size.Height);
StephaneLenclud@273
    91
StephaneLenclud@274
    92
                using (Graphics graphics = Graphics.FromImage(bitmap))
StephaneLenclud@273
    93
                {
StephaneLenclud@274
    94
                    PrepareGraphics(graphics, highQuality);
StephaneLenclud@274
    95
                    graphics.Clear(background);
StephaneLenclud@273
    96
StephaneLenclud@274
    97
                    CreateSpectrumLineInternal(graphics, pen, iFftBuffer, size);
StephaneLenclud@274
    98
                }
StephaneLenclud@273
    99
StephaneLenclud@274
   100
            return bitmap;   
StephaneLenclud@274
   101
            }            
StephaneLenclud@273
   102
        }
StephaneLenclud@273
   103
StephaneLenclud@274
   104
        /// <summary>
StephaneLenclud@274
   105
        /// 
StephaneLenclud@274
   106
        /// </summary>
StephaneLenclud@274
   107
        /// <param name="size"></param>
StephaneLenclud@274
   108
        /// <param name="color1"></param>
StephaneLenclud@274
   109
        /// <param name="color2"></param>
StephaneLenclud@274
   110
        /// <param name="background"></param>
StephaneLenclud@274
   111
        /// <param name="highQuality"></param>
StephaneLenclud@274
   112
        /// <returns></returns>
StephaneLenclud@274
   113
        public Bitmap Render(Size size, Color color1, Color color2, Color background, bool highQuality)
StephaneLenclud@273
   114
        {
StephaneLenclud@273
   115
            if (!UpdateFrequencyMappingIfNessesary(size))
StephaneLenclud@274
   116
            {
StephaneLenclud@273
   117
                return null;
StephaneLenclud@274
   118
            }
StephaneLenclud@273
   119
StephaneLenclud@273
   120
            using (
StephaneLenclud@273
   121
                Brush brush = new LinearGradientBrush(new RectangleF(0, 0, (float)_barWidth, size.Height), color2,
StephaneLenclud@273
   122
                    color1, LinearGradientMode.Vertical))
StephaneLenclud@273
   123
            {
StephaneLenclud@273
   124
                return CreateSpectrumLine(size, brush, background, highQuality);
StephaneLenclud@273
   125
            }
StephaneLenclud@273
   126
        }
StephaneLenclud@273
   127
StephaneLenclud@273
   128
        private void CreateSpectrumLineInternal(Graphics graphics, Pen pen, float[] fftBuffer, Size size)
StephaneLenclud@273
   129
        {
StephaneLenclud@273
   130
            int height = size.Height;
StephaneLenclud@273
   131
            //prepare the fft result for rendering 
StephaneLenclud@273
   132
            SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(height, fftBuffer);
StephaneLenclud@273
   133
StephaneLenclud@273
   134
            //connect the calculated points with lines
StephaneLenclud@273
   135
            for (int i = 0; i < spectrumPoints.Length; i++)
StephaneLenclud@273
   136
            {
StephaneLenclud@273
   137
                SpectrumPointData p = spectrumPoints[i];
StephaneLenclud@273
   138
                int barIndex = p.SpectrumPointIndex;
StephaneLenclud@273
   139
                double xCoord = BarSpacing * (barIndex + 1) + (_barWidth * barIndex) + _barWidth / 2;
StephaneLenclud@273
   140
StephaneLenclud@273
   141
                var p1 = new PointF((float)xCoord, height);
StephaneLenclud@273
   142
                var p2 = new PointF((float)xCoord, height - (float)p.Value - 1);
StephaneLenclud@273
   143
StephaneLenclud@273
   144
                graphics.DrawLine(pen, p1, p2);
StephaneLenclud@273
   145
            }
StephaneLenclud@273
   146
        }
StephaneLenclud@273
   147
StephaneLenclud@273
   148
        protected override void UpdateFrequencyMapping()
StephaneLenclud@273
   149
        {
StephaneLenclud@273
   150
            _barWidth = Math.Max(((_currentSize.Width - (BarSpacing * (BarCount + 1))) / BarCount), 0.00001);
StephaneLenclud@273
   151
            base.UpdateFrequencyMapping();
StephaneLenclud@273
   152
        }
StephaneLenclud@273
   153
StephaneLenclud@273
   154
        private bool UpdateFrequencyMappingIfNessesary(Size newSize)
StephaneLenclud@273
   155
        {
StephaneLenclud@273
   156
            if (newSize != CurrentSize)
StephaneLenclud@273
   157
            {
StephaneLenclud@273
   158
                CurrentSize = newSize;
StephaneLenclud@273
   159
                UpdateFrequencyMapping();
StephaneLenclud@273
   160
            }
StephaneLenclud@273
   161
StephaneLenclud@273
   162
            return newSize.Width > 0 && newSize.Height > 0;
StephaneLenclud@273
   163
        }
StephaneLenclud@273
   164
StephaneLenclud@273
   165
        private void PrepareGraphics(Graphics graphics, bool highQuality)
StephaneLenclud@273
   166
        {
StephaneLenclud@273
   167
            if (highQuality)
StephaneLenclud@273
   168
            {
StephaneLenclud@273
   169
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
StephaneLenclud@273
   170
                graphics.CompositingQuality = CompositingQuality.AssumeLinear;
StephaneLenclud@273
   171
                graphics.PixelOffsetMode = PixelOffsetMode.Default;
StephaneLenclud@273
   172
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
StephaneLenclud@273
   173
            }
StephaneLenclud@273
   174
            else
StephaneLenclud@273
   175
            {
StephaneLenclud@273
   176
                graphics.SmoothingMode = SmoothingMode.HighSpeed;
StephaneLenclud@273
   177
                graphics.CompositingQuality = CompositingQuality.HighSpeed;
StephaneLenclud@273
   178
                graphics.PixelOffsetMode = PixelOffsetMode.None;
StephaneLenclud@273
   179
                graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
StephaneLenclud@273
   180
            }
StephaneLenclud@273
   181
        }
StephaneLenclud@273
   182
    }
StephaneLenclud@273
   183
}