mirror of
https://github.com/Stone-Red-Code/SimpleWinformsAudioVisualizer.git
synced 2026-09-04 17:16:25 +02:00
69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
using NAudio.Dsp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AudioVisualizer
|
|
{
|
|
/*
|
|
*This class is not created by me(Stone_Red)
|
|
*I forgot who mad this class if i find the source of this class i will credit the original creator
|
|
*/
|
|
class SampleAggregator
|
|
{
|
|
// FFT
|
|
public event EventHandler<FftEventArgs> FftCalculated;
|
|
public bool PerformFFT { get; set; }
|
|
private Complex[] fftBuffer;
|
|
private FftEventArgs fftArgs;
|
|
private int fftPos;
|
|
private int fftLength;
|
|
private int m;
|
|
|
|
public SampleAggregator(int fftLength)
|
|
{
|
|
if (!IsPowerOfTwo(fftLength))
|
|
{
|
|
throw new ArgumentException("FFT Length must be a power of two");
|
|
}
|
|
this.m = (int)Math.Log(fftLength, 2.0);
|
|
this.fftLength = fftLength;
|
|
this.fftBuffer = new Complex[fftLength];
|
|
this.fftArgs = new FftEventArgs(fftBuffer);
|
|
}
|
|
|
|
bool IsPowerOfTwo(int x)
|
|
{
|
|
return (x & (x - 1)) == 0;
|
|
}
|
|
|
|
public void Add(float value)
|
|
{
|
|
if (PerformFFT && FftCalculated != null)
|
|
{
|
|
fftBuffer[fftPos].X = (float)(value * FastFourierTransform.HammingWindow(fftPos, fftLength));
|
|
fftBuffer[fftPos].Y = 0; // This is always zero with audio.
|
|
fftPos++;
|
|
if (fftPos >= fftLength)
|
|
{
|
|
fftPos = 0;
|
|
FastFourierTransform.FFT(true, m, fftBuffer);
|
|
FftCalculated(this, fftArgs);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class FftEventArgs : EventArgs
|
|
{
|
|
public FftEventArgs(Complex[] result)
|
|
{
|
|
this.Result = result;
|
|
}
|
|
public Complex[] Result { get; private set; }
|
|
}
|
|
}
|