Fft实时 - AsioOut + SampleAggregator

时间:2016-05-27 03:47:08

标签: c# wpf naudio

我需要帮助。

我目前正在研究我的论文项目,这是一个由ASIO驱动程序编写指法的应用程序。简而言之,写下你玩的东西。我在所有方面都是新的Naudio,而且我正在使用poblema将声音转换为FFT。我用一种名为" GuitarLink"的设备捕捉声音。它贯穿ASIO4ALL。 所以我需要实时比较和弦的频率。

目前,该程序给了我两个值,X和Y. (我正在使用" SampleAggregator" Class和AsioOut)

MAIN

  private SampleAggregator sampleAggregator = new SampleAggregator(fftLength);

public MainWindow()
    {
        InitializeComponent();

        dispatcherTimer.Tick += new EventHandler(SoClose);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
        dispatcherTimer.Start();
    }

 void SoClose(object sender, EventArgs e)
    {
        try{
            if (PBass == true){

                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();

                I1E.Text = "" + frecuencia;
            }              

        }
        catch
        {
            MessageBox.Show("Error de bajo presupuesto", "Obviamente algo anda mal");
        }
 private void FftCalculated(object sender, FftEventArgs e)
    {

        //throw new NotImplementedException();
        for (var i = 0; i < e.Result.Length; i++)
        {

            A = e.Result[i].X;
            B = e.Result[i].Y;

            frecuencia = B;

            //Debug.WriteLine("FFT output.");
            //Debug.WriteLine(e.Result[i].X);
            //Debug.WriteLine(e.Result[i].Y);
        }

    }


    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 sample = Convert.ToSingle(buf[i] + buf[i + 1] + buf[i + 2] + buf[i + 3]);
            sampleAggregator.Add(sample);
        }

        e.WrittenToOutputBuffers = true;
    }

SampleAggregator类

public class SampleAggregator
{
    // FFT
    public event EventHandler<FftEventArgs> FftCalculated;
    public bool PerformFFT { get; set; }

    // This Complex is NAudio's own! 
    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)
        {
            // Remember the window function! There are many others as well.
            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
   {


  [DebuggerStepThrough]
   public FftEventArgs(Complex[] result)
   {
       this.Result = result;
   }
   public Complex[] Result { get; private set; }

   public string resultado ="";



   void FftCalculated(object sender, FftEventArgs e)
   {

   }

问题是,当我播放音符时,传递的值不受影响

对不起,如果你不明白,我的英语不是很好

0 个答案:

没有答案