コード例 #1
0
        private void thread_run()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            while (threadRunning)
            {
                if (stopwatch.ElapsedMilliseconds > ThreadTargetElapsedTime.TotalMilliseconds)
                {
                    stopwatch.Restart();

                    if (lastFFT != null)
                    {
                        if (smoothFFT == null || smoothFFT.Length != lastFFT.Length)
                        {
                            smoothFFT = new ComplexValue[lastFFT.Length];
                        }
                        if (lastFFT2 == null || lastFFT2.Length != lastFFT.Length)
                        {
                            lastFFT2 = new ComplexValue[lastFFT.Length];
                        }

                        for (int i = 0; i < lastFFT.Length; i++)
                        {
                            var smoothX = MathHelper.Lerp(smoothFFT[i].X, lastFFT[i].X, FFTSmoothness); // TODO: Time
                            var smoothY = MathHelper.Lerp(smoothFFT[i].Y, lastFFT[i].Y, FFTSmoothness);
                            smoothFFT[i] = new ComplexValue(smoothX, smoothY);

                            lastFFT2[i] = new ComplexValue(lastFFT[i].X, lastFFT[i].Y);
                        }
                    }

                    var samples = new List <AudioSample>(CurrentAnalyzedAudio.Samples);
                    samples.InsertRange(0, lastSamples);

                    var newBag = new ConcurrentBag <AudioSample>();
                    Interlocked.Exchange(ref lastSamples, newBag);

                    CurrentAnalyzedAudio = new AnalyzedAudio(lastFFT2, smoothFFT, samples.Take(SamplesHistory).ToArray());
                }
            }
        }
コード例 #2
0
        public AudioAnalyzer(AudioPlayback audioPlayback, int samplesHistory = 3, float FFTSmoothness = 0.25f)
        {
            this.SamplesHistory = samplesHistory;
            this.FFTSmoothness  = FFTSmoothness;

            this.ThreadTargetElapsedTime = TimeSpan.FromSeconds(1.0f / 60);

            this.CurrentAnalyzedAudio = new AnalyzedAudio(new ComplexValue[0], new ComplexValue[0], new AudioSample[0]);
            this.lastSamples          = new ConcurrentBag <AudioSample>();

            this.audioPlayback = audioPlayback;
            this.audioPlayback.MaximumCalculated += audioGraph_MaximumCalculated;
            this.audioPlayback.FftCalculated     += audioGraph_FftCalculated;

            // TODO: If the sound stops it stops raporting the data

            this.threadRunning = true;
            this.thread        = new Thread(new ThreadStart(thread_run));
            this.thread.Start();
        }