6

私はC#4.0 / WPFリアルタイムスペクトラムアナライザーに取り組んでいます(別のプロジェクトのベースとして)。サウンドカードでリアルタイムのオーディオ出力を取得するためにNAudioの最後のバージョンを使用し、Spectrum Analyzer WPFコントロール用にWPFSoundVisualizationLib(http://wpfsvl.codeplex.com/)を使用します。この驚くべきツールを使用すると、作業はほぼ完了しますが、正しく機能しません:-(

私は機能的なSpectrumを持っていますが、情報は権利ではなく、問題がどこから来ているのかわかりません...(SpectrumをSpotifyのSpectrum /イコライザーであるEqualifyと比較しましたが、同じものはありません行動)

これは私のメインクラスです:

using System;
using System.Windows;
using WPFSoundVisualizationLib;

namespace MySpectrumAnalyser
{
    public partial class MainWindow : Window
    {
        private RealTimePlayback _playback;
        private bool _record;

        public MainWindow()
        {
            InitializeComponent();
            this.Topmost = true;
            this.Closing += MainWindow_Closing;
            this.spectrum.FFTComplexity = FFTDataSize.FFT2048;
            this.spectrum.RefreshInterval = 60;
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this._record)
            {
                this._playback.Stop();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (this._playback == null)
            {
                this._playback = new RealTimePlayback();
                this.spectrum.RegisterSoundPlayer(this._playback);
            }

            if (!this._record)
            {
                this._playback.Start();
                this.Dispatcher.Invoke(new Action(delegate
                {
                    this.btnRecord.Content = "Stop";
                }));
            }
            else
            {
                this._playback.Stop();
                this.Dispatcher.Invoke(new Action(delegate
                {
                    this.btnRecord.Content = "Start";
                }));
            }

            this._record = !this._record;
        }
    }
}

そして、私のループバックアナライザー(WPFSoundVisualizationLib Spectrumコントロールで使用するためのISpectrumPlayerを実装します)。

LoopbackCaptureは、NAudio.CoreAudioApi.WasapiCaptureを継承します。

Wasapiから受信したデータはバイト配列です(32ビットPCM、44.1kHz、2チャネル、サンプルあたり32ビット)

using NAudio.Dsp;
using NAudio.Wave;
using System;
using WPFSoundVisualizationLib;

namespace MySpectrumAnalyser
{
    public class RealTimePlayback : ISpectrumPlayer
    {
        private LoopbackCapture _capture;
        private object _lock;
        private int _fftPos;
        private int _fftLength;
        private Complex[] _fftBuffer;
        private float[] _lastFftBuffer;
        private bool _fftBufferAvailable;
        private int _m;

        public RealTimePlayback()
        {
            this._lock = new object();

            this._capture = new LoopbackCapture();
            this._capture.DataAvailable += this.DataAvailable;

            this._m = (int)Math.Log(this._fftLength, 2.0);
            this._fftLength = 2048; // 44.1kHz.
            this._fftBuffer = new Complex[this._fftLength];
            this._lastFftBuffer = new float[this._fftLength];
        }

        public WaveFormat Format
        {
            get
            {
                return this._capture.WaveFormat;
            }
        }

        private float[] ConvertByteToFloat(byte[] array, int length)
        {
            int samplesNeeded = length / 4;
            float[] floatArr = new float[samplesNeeded];

            for (int i = 0; i < samplesNeeded; i++)
            {
                floatArr[i] = BitConverter.ToSingle(array, i * 4);
            }

            return floatArr;
        }

        private void DataAvailable(object sender, WaveInEventArgs e)
        {
            // Convert byte[] to float[].
            float[] data = ConvertByteToFloat(e.Buffer, e.BytesRecorded);

            // For all data. Skip right channel on stereo (i += this.Format.Channels).
            for (int i = 0; i < data.Length; i += this.Format.Channels)
            {
                this._fftBuffer[_fftPos].X = (float)(data[i] * FastFourierTransform.HannWindow(_fftPos, _fftLength));
                this._fftBuffer[_fftPos].Y = 0;
                this._fftPos++;

                if (this._fftPos >= this._fftLength)
                {
                    this._fftPos = 0;

                    // NAudio FFT implementation.
                    FastFourierTransform.FFT(true, this._m, this._fftBuffer);

                    // Copy to buffer.
                    lock (this._lock)
                    {
                        for (int c = 0; c < this._fftLength; c++)
                        {
                            this._lastFftBuffer[c] = this._fftBuffer[c].X;
                        }

                        this._fftBufferAvailable = true;
                    }
                }
            }
        }

        public void Start()
        {
            this._capture.StartRecording();
        }

        public void Stop()
        {
            this._capture.StopRecording();
        }

        public bool GetFFTData(float[] fftDataBuffer)
        {
            lock (this._lock)
            {
                // Use last available buffer.
                if (this._fftBufferAvailable)
                {
                    this._lastFftBuffer.CopyTo(fftDataBuffer, 0);
                    this._fftBufferAvailable = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public int GetFFTFrequencyIndex(int frequency)
        {
            int index = (int)(frequency / (this.Format.SampleRate / this._fftLength / this.Format.Channels));
            return index;
        }

        public bool IsPlaying
        {
            get { return true; }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    }
}

GetFFTDataは、Spectrumを更新するために60msごとにWPFコントロールによって呼び出されます。

4

3 に答える 3

0

あなたの着信波フォーマットは間違いなくIEEEフロートですか?32ビットintの場合はどうなりますか?

私はそれがIEEE floatだと思います...これはMSDN Hereでは説明されていません

バイト配列を Int32 配列 (float にキャスト) に変換しようとしましたが、結果は最悪です:

private float[] ConvertByteToFloat(byte[] array, int length)
{
    int samplesNeeded = length / 4;
    float[] floatArr = new float[samplesNeeded];

    for (int i = 0; i < samplesNeeded; i++)
    {
        floatArr[i] = (float)BitConverter.ToInt32(array, i * 4);
    }

    return floatArr;
}
于 2012-11-15T16:47:57.930 に答える