Пример #1
0
        public void AddSample(float sample)
        {
            SampleProcessed++;

            if (Samples.Count < FFTLength)
            {
                Enqueue(sample);
            }
            else
            {
                Enqueue(sample);

                CurrentGap++;

                if (CurrentGap < SampleGap && SampleProcessed != SampleCount)
                {
                    return;
                }

                CurrentGap = 0;

                if (Samples.Count == FFTLength)
                {
                    Complex[] fft = new Complex[FFTLength];

                    int i = 0;

                    foreach (var s in Samples)
                    {
                        fft[i].X = (float)(s * FastFourierTransform.HammingWindow(i, FFTLength));
                        fft[i].Y = 0;
                        i++;
                    }

                    FastFourierTransform.FFT(true, m, fft);


                    SampleAnalysis[] result = new SampleAnalysis[FFTLength];


                    for (int n = 0; n < fft.Length; n++)
                    {
                        result[n].Frequency = Physics.GetFrequency(n, AudioReader.WaveFormat.SampleRate, FFTLength);
                        result[n].Amplitude = Physics.GetAmplitude(fft[n]);
                    }

                    Processed?.Invoke(result);
                }
            }
        }
Пример #2
0
        private void OnSample(object sender, SampleEventArgs e)
        {
            FFTBuffer[FFTPosition].X = (float)(e.Right * FastFourierTransform.HammingWindow(FFTPosition, FFTLength)); // e.Right ? seems equal to e.Left in this case (mono?)
            FFTBuffer[FFTPosition].Y = 0;
            FFTPosition++;
            if (FFTPosition >= FFTBuffer.Length)
            {
                FastFourierTransform.FFT(true, (int)Math.Log(FFTLength, 2.0), FFTBuffer);
                FFTPosition = 0;

                SampleAnalysis[] result = new SampleAnalysis[FFTLength];

                for (int n = 0; n < FFTBuffer.Length; n++)
                {
                    result[n].Frequency = Physics.GetFrequency(n, AudioReader.WaveFormat.SampleRate, FFTLength);
                    result[n].Amplitude = Physics.GetAmplitude(FFTBuffer[n]);
                }

                Processed?.Invoke(result);
            }
        }