I am programming a clone of Hero's guitar (violin) as a final project for this school year.
The idea is to take part in my electric violin, analyze it using FFT, make some logic and drawing and bring it out through the speakers. Perhaps some steps in parallel threads.
I already have an Asio low latency input output, but I had a big problem with real-time FFT implementation.
This is the code that installs asioOut along with sampleAggregator. The aggregator sample must store the samples that are added each time AudioAvailable () is called and starts the FFT calculation when the number of samples exceeds fftLength.
private static int fftLength = 8192;
private SampleAggregator sampleAggregator = new SampleAggregator(fftLength);
void asioStartPlaying(object sender, EventArgs e)
{
sampleAggregator.PerformFFT = true;
sampleAggregator.FftCalculated += new EventHandler<FftEventArgs>(FftCalculated);
var asioOut = new AsioOut();
BufferedWaveProvider wavprov = new BufferedWaveProvider(new WaveFormat(48000, 1));
asioOut.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs> (asio_DataAvailable);
asioOut.InitRecordAndPlayback(wavprov, 1, 25);
asioOut.Play();
}
void asio_DataAvailable(object sender, AsioAudioAvailableEventArgs e)
{
byte[] buf = new byte[e.SamplesPerBuffer*4];
for (int i = 0; i < e.InputBuffers.Length; i++)
{
Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer*4);
Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer*4);
}
for (int i = 0; i < buf.Length; i=i+4)
{
float sample32 = BitConverter.ToSingle(buf, i);
sampleAggregator.Add(sample32);
}
e.WrittenToOutputBuffers = true;
}
SampleAggregator - , NAudio fft #.
Asio Int32LSB. buf 0 255.
, fft ( SampleAggregator).
void FftCalculated(object sender, FftEventArgs e)
{
for (var i = 0; i < e.Result.Length; i++)
{
Debug.WriteLine("FFT output.");
Debug.WriteLine(e.Result[i].X);
Debug.WriteLine(e.Result[i].Y);
}
}
FFT NaN .
, float.
- ?
EDIT_1: DataAvailable()
for (int i = 0; i < e.SamplesPerBuffer * 4; i++)
{
float sample32 = Convert.ToSingle(buf[i]);
sampleAggregator.Add(sample32);
}
FFT . , . asio float. .
e.GetAsInterleavedSamples - ?
:
X: -5,304741
Y: -0,7160959
X: 6 270798
Y: -0,4169312
X: -8,851931
Y: -0,4485725
, - , . .