87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using Microsoft.Xna.Framework;
|
|
using NAudio.Dsp;
|
|
using NAudio.Wave;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace RhythmBullet.Zer01HD.Audio
|
|
{
|
|
public class TransparentSampleProvider : ISampleProvider
|
|
{
|
|
private const int FFT_SIZE = 2048;
|
|
private readonly int m;
|
|
private ISampleProvider source;
|
|
private Complex[] fftBuffer;
|
|
private float[] spectrum;
|
|
private int channelCount;
|
|
public bool performFFT;
|
|
|
|
|
|
|
|
public TransparentSampleProvider(ISampleProvider sampleProvider)
|
|
{
|
|
m = (int)Math.Log(FFT_SIZE);
|
|
source = sampleProvider;
|
|
channelCount = sampleProvider.WaveFormat.Channels;
|
|
fftBuffer = new Complex[FFT_SIZE];
|
|
spectrum = new float[FFT_SIZE];
|
|
}
|
|
|
|
public WaveFormat WaveFormat
|
|
{
|
|
get
|
|
{
|
|
return source.WaveFormat;
|
|
}
|
|
}
|
|
|
|
public float GetSpectrumBin(int bin)
|
|
{
|
|
lock (spectrum)
|
|
{
|
|
return spectrum[bin];
|
|
}
|
|
}
|
|
|
|
public int Read(float[] buffer, int offset, int count)
|
|
{
|
|
int sampleCount = source.Read(buffer, offset, count);
|
|
|
|
if (performFFT)
|
|
{
|
|
int fftOffset = 0;
|
|
for (int s = 0; s < sampleCount; s += channelCount)
|
|
{
|
|
if (s >= FFT_SIZE)
|
|
{
|
|
FastFourierTransform.FFT(true, m, fftBuffer);
|
|
lock (spectrum)
|
|
{
|
|
for (int binID = 0; binID < spectrum.Length; binID++)
|
|
{
|
|
spectrum[binID] = fftBuffer[binID].X;
|
|
}
|
|
}
|
|
fftOffset -= FFT_SIZE;
|
|
}
|
|
float greatestVal = 0;
|
|
for (int c = 0; c < channelCount; c++)
|
|
{
|
|
greatestVal = MathHelper.Max(buffer[s + offset + c], greatestVal);
|
|
}
|
|
|
|
fftBuffer[s + fftOffset].X = (float)(buffer[s + offset] * FastFourierTransform.HammingWindow(s, FFT_SIZE));
|
|
fftBuffer[s + fftOffset].Y = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sampleCount;
|
|
}
|
|
}
|
|
}
|