/// <summary> /// sets graph data /// </summary> public void SetAudioData(float[] audioData) { this.audioData = audioData; float[] spectrumData = AudioAnalyzer.CreateSpectrumAnalysisLomont(audioData, fftWindowsSize); float[] m_mag; float[] m_freq; AudioAnalyzer.PrepareSpectrumAnalysis(spectrumData, sampleRate, fftWindowsSize, out m_mag, out m_freq, out foundMaxFrequency, out foundMaxDecibel); bmp = AudioAnalyzer.GetSpectrumImage(ref m_mag, ref m_freq, new Size(this.Width, this.Height), showMinFrequency, showMaxFrequency, foundMaxDecibel, foundMaxFrequency); // force redraw this.Invalidate(); }
/// <summary> /// Use the FFT results to update the height and y position of the bars /// </summary> private void UpdateSpectrumShapes() { bool allZero = true; if (DoSpectrumGraph) { #region Draw Spectrum Analysis Graph float[] mag; float[] freq; float foundMaxFreq, foundMaxDecibel; double sampleRate = soundPlayer.SampleRate; int fftWindowsSize = soundPlayer.FftDataSize; AudioAnalyzer.PrepareSpectrumAnalysis(channelData, sampleRate, fftWindowsSize, out mag, out freq, out foundMaxFreq, out foundMaxDecibel); this.offlineBitmap = AudioAnalyzer.GetSpectrumImage(ref mag, ref freq, new Size(this.Width, this.Height), MinimumFrequency, MaximumFrequency, foundMaxDecibel, foundMaxFreq); #endregion } else { #region Draw Bars double fftBucketHeight = 0f; double barHeight = 0f; double lastPeakHeight = 0f; double peakYPos = 0f; double height = this.Height; int barIndex = 0; double peakDotHeight = Math.Max(barWidth / 2.0f, 1); double barHeightScale = (height - peakDotHeight); for (int i = minimumFrequencyIndex; i <= maximumFrequencyIndex; i++) { // If we're paused, keep drawing, but set the current height to 0 so the peaks fall. if (!soundPlayer.IsPlaying) { barHeight = 0f; } else // Draw the maximum value for the bar's band { switch (BarHeightScaling) { case BarHeightScalingStyles.Decibel: double dbValue = 20 * Math.Log10((double)channelData[i]); fftBucketHeight = ((dbValue - minDBValue) / dbScale) * barHeightScale; break; case BarHeightScalingStyles.Linear: fftBucketHeight = (channelData[i] * scaleFactorLinear) * barHeightScale; break; case BarHeightScalingStyles.Sqrt: fftBucketHeight = (((Math.Sqrt((double)channelData[i])) * scaleFactorSqr) * barHeightScale); break; } if (barHeight < fftBucketHeight) { barHeight = fftBucketHeight; } if (barHeight < 0f) { barHeight = 0f; } } // If this is the last FFT bucket in the bar's group, draw the bar. int currentIndexMax = IsFrequencyScaleLinear ? barIndexMax[barIndex] : barLogScaleIndexMax[barIndex]; if (i == currentIndexMax) { // Peaks can't surpass the height of the control. if (barHeight > height) { barHeight = height; } if (AveragePeaks && barIndex > 0) { barHeight = (lastPeakHeight + barHeight) / 2; } peakYPos = barHeight; if (channelPeakData[barIndex] < peakYPos) { channelPeakData[barIndex] = (float)peakYPos; } else { channelPeakData[barIndex] = (float)(peakYPos + (PeakFallDelay * channelPeakData[barIndex])) / ((float)(PeakFallDelay + 1)); } double xCoord = BarSpacing + (barWidth * barIndex) + (BarSpacing * barIndex) + 1; Rectangle barRect = barShapes[barIndex]; barRect.Y = (int)((height - 1) - barHeight); barRect.Height = (int)barHeight; barShapes[barIndex] = barRect; Rectangle peakRect = peakShapes[barIndex]; peakRect.Y = (int)((height - 1) - channelPeakData[barIndex] - peakDotHeight); peakShapes[barIndex] = peakRect; if (channelPeakData[barIndex] > 0.05) { allZero = false; } lastPeakHeight = barHeight; barHeight = 0f; barIndex++; } } #endregion } if (allZero && !soundPlayer.IsPlaying) { animationTimer.Stop(); } }