2 using System.ComponentModel;
4 using System.Drawing.Drawing2D;
5 using System.Drawing.Text;
8 namespace Visualization
10 public class LineSpectrum : SpectrumBase
12 private int _barCount;
13 private double _barSpacing;
14 private double _barWidth;
15 private Size _currentSize;
18 public LineSpectrum(FftSize fftSize)
24 public double BarWidth
26 get { return _barWidth; }
29 public double BarSpacing
31 get { return _barSpacing; }
35 throw new ArgumentOutOfRangeException("value");
37 UpdateFrequencyMapping();
39 RaisePropertyChanged("BarSpacing");
40 RaisePropertyChanged("BarWidth");
46 get { return _barCount; }
50 throw new ArgumentOutOfRangeException("value");
52 SpectrumResolution = value;
53 UpdateFrequencyMapping();
55 RaisePropertyChanged("BarCount");
56 RaisePropertyChanged("BarWidth");
60 [BrowsableAttribute(false)]
61 public Size CurrentSize
63 get { return _currentSize; }
67 RaisePropertyChanged("CurrentSize");
74 /// <returns></returns>
77 return SpectrumProvider.GetFftData(iFftBuffer, this);
80 private bool CreateSpectrumLine(Image aImage, Brush brush, Color background, bool highQuality)
82 //get the fft result from the spectrum provider
83 using (var pen = new Pen(brush, (float)_barWidth))
85 using (Graphics graphics = Graphics.FromImage(aImage))
87 PrepareGraphics(graphics, highQuality);
88 graphics.Clear(background);
90 CreateSpectrumLineInternal(graphics, pen, iFftBuffer, aImage.Size);
100 /// <param name="size"></param>
101 /// <param name="color1"></param>
102 /// <param name="color2"></param>
103 /// <param name="background"></param>
104 /// <param name="highQuality"></param>
105 /// <returns></returns>
106 public bool Render(Image aImage, Color color1, Color color2, Color background, bool highQuality)
108 if (!UpdateFrequencyMappingIfNessesary(aImage.Size))
113 using (Brush brush = new LinearGradientBrush(new RectangleF(0, 0, (float)_barWidth, aImage.Size.Height), color2, color1, LinearGradientMode.Vertical))
115 return CreateSpectrumLine(aImage, brush, background, highQuality);
122 /// <param name="graphics"></param>
123 /// <param name="pen"></param>
124 /// <param name="fftBuffer"></param>
125 /// <param name="size"></param>
126 private void CreateSpectrumLineInternal(Graphics graphics, Pen pen, float[] fftBuffer, Size size)
128 int height = size.Height;
129 //prepare the fft result for rendering
130 SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(height, fftBuffer);
132 //connect the calculated points with lines
133 for (int i = 0; i < spectrumPoints.Length; i++)
135 SpectrumPointData p = spectrumPoints[i];
136 int barIndex = p.SpectrumPointIndex;
137 double xCoord = BarSpacing * (barIndex + 1) + (_barWidth * barIndex) + _barWidth / 2;
139 var p1 = new PointF((float)xCoord, height);
140 var p2 = new PointF((float)xCoord, height - (float)p.Value);
142 graphics.DrawLine(pen, p1, p2);
146 protected override void UpdateFrequencyMapping()
148 _barWidth = Math.Max(((_currentSize.Width - (BarSpacing * (BarCount + 1))) / BarCount), 0.00001);
149 base.UpdateFrequencyMapping();
152 private bool UpdateFrequencyMappingIfNessesary(Size newSize)
154 if (newSize != CurrentSize)
156 CurrentSize = newSize;
157 UpdateFrequencyMapping();
160 return newSize.Width > 0 && newSize.Height > 0;
163 private void PrepareGraphics(Graphics graphics, bool highQuality)
167 graphics.SmoothingMode = SmoothingMode.AntiAlias;
168 graphics.CompositingQuality = CompositingQuality.AssumeLinear;
169 graphics.PixelOffsetMode = PixelOffsetMode.Default;
170 graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
174 graphics.SmoothingMode = SmoothingMode.HighSpeed;
175 graphics.CompositingQuality = CompositingQuality.HighSpeed;
176 graphics.PixelOffsetMode = PixelOffsetMode.None;
177 graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;