Draft audio spectrum visualizer.
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;
17 public LineSpectrum(FftSize fftSize)
23 public double BarWidth
25 get { return _barWidth; }
28 public double BarSpacing
30 get { return _barSpacing; }
34 throw new ArgumentOutOfRangeException("value");
36 UpdateFrequencyMapping();
38 RaisePropertyChanged("BarSpacing");
39 RaisePropertyChanged("BarWidth");
45 get { return _barCount; }
49 throw new ArgumentOutOfRangeException("value");
51 SpectrumResolution = value;
52 UpdateFrequencyMapping();
54 RaisePropertyChanged("BarCount");
55 RaisePropertyChanged("BarWidth");
59 [BrowsableAttribute(false)]
60 public Size CurrentSize
62 get { return _currentSize; }
66 RaisePropertyChanged("CurrentSize");
70 public Bitmap CreateSpectrumLine(Size size, Brush brush, Color background, bool highQuality)
72 if (!UpdateFrequencyMappingIfNessesary(size))
75 var fftBuffer = new float[(int)FftSize];
77 //get the fft result from the spectrum provider
78 if (SpectrumProvider.GetFftData(fftBuffer, this))
80 using (var pen = new Pen(brush, (float)_barWidth))
82 var bitmap = new Bitmap(size.Width, size.Height);
84 using (Graphics graphics = Graphics.FromImage(bitmap))
86 PrepareGraphics(graphics, highQuality);
87 graphics.Clear(background);
89 CreateSpectrumLineInternal(graphics, pen, fftBuffer, size);
98 public Bitmap CreateSpectrumLine(Size size, Color color1, Color color2, Color background, bool highQuality)
100 if (!UpdateFrequencyMappingIfNessesary(size))
104 Brush brush = new LinearGradientBrush(new RectangleF(0, 0, (float)_barWidth, size.Height), color2,
105 color1, LinearGradientMode.Vertical))
107 return CreateSpectrumLine(size, brush, background, highQuality);
111 private void CreateSpectrumLineInternal(Graphics graphics, Pen pen, float[] fftBuffer, Size size)
113 int height = size.Height;
114 //prepare the fft result for rendering
115 SpectrumPointData[] spectrumPoints = CalculateSpectrumPoints(height, fftBuffer);
117 //connect the calculated points with lines
118 for (int i = 0; i < spectrumPoints.Length; i++)
120 SpectrumPointData p = spectrumPoints[i];
121 int barIndex = p.SpectrumPointIndex;
122 double xCoord = BarSpacing * (barIndex + 1) + (_barWidth * barIndex) + _barWidth / 2;
124 var p1 = new PointF((float)xCoord, height);
125 var p2 = new PointF((float)xCoord, height - (float)p.Value - 1);
127 graphics.DrawLine(pen, p1, p2);
131 protected override void UpdateFrequencyMapping()
133 _barWidth = Math.Max(((_currentSize.Width - (BarSpacing * (BarCount + 1))) / BarCount), 0.00001);
134 base.UpdateFrequencyMapping();
137 private bool UpdateFrequencyMappingIfNessesary(Size newSize)
139 if (newSize != CurrentSize)
141 CurrentSize = newSize;
142 UpdateFrequencyMapping();
145 return newSize.Width > 0 && newSize.Height > 0;
148 private void PrepareGraphics(Graphics graphics, bool highQuality)
152 graphics.SmoothingMode = SmoothingMode.AntiAlias;
153 graphics.CompositingQuality = CompositingQuality.AssumeLinear;
154 graphics.PixelOffsetMode = PixelOffsetMode.Default;
155 graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
159 graphics.SmoothingMode = SmoothingMode.HighSpeed;
160 graphics.CompositingQuality = CompositingQuality.HighSpeed;
161 graphics.PixelOffsetMode = PixelOffsetMode.None;
162 graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;