Пример #1
0
        void sampleAggregator_FftCalculated(object sender, FftEventArgs e)
        {
            if (!tabLoaded)
            {
                return;
            }
            var mas = e.Result.Select(x => x.X * x.X + x.Y * x.Y).ToArray();

            if (prevFFT == null)
            {
                prevFFT = new float[e.Result.Length / 2];
                Array.Copy(mas, prevFFT, mas.Length / 2);
            }
            for (int i = 0; i < mas.Length / 2; i++)
            {
                mas[i] = Math.Max(mas[i] - prevFFT[i], 0);
            }
            int maxind = 0;

            for (int i = 0; i < mas.Length / 2; i++)
            {
                if (mas[maxind] < mas[i])
                {
                    maxind = i;
                }
            }

            double[] noteBins = new double[Notes.Count];
            if (mas[maxind] > 0.0001)
            {
                for (int i = 0; i < mas.Length / 2; i++) //нормализуем
                {
                    mas[i] /= mas[maxind];
                }

                noteBins = new double[Notes.Count];
                for (int i = 0; i < mas.Length / 2; i++)
                {
                    double frequency = (double)(i * this.sampleRate) / fftlength;
                    int    x         = FindClosestIndex(frequency);
                    noteBins[x] += mas[i];
                }
                var maxBinValue = noteBins.Max();
                noteBins = noteBins.Select(a => a / maxBinValue).ToArray();
            }

            /*using (Graphics g = pictureBox1.CreateGraphics())
             * {
             *  g.Clear(Color.White);
             *  for (int i = 0; i<noteBins.Length; i++)
             *  {
             *      g.DrawLine(p, new Point(i*10, 0), new Point(i*10, (int)(noteBins[i] * 200)));
             *  }
             * }*/
            tablatureProcessor.Compare(noteBins);
        }
Пример #2
0
 public SampleAggregator(int fftLength, int timeScaleFactor = 1)
 {
     if (!IsPowerOfTwo(fftLength))
     {
         throw new ArgumentException("FFT Length must be a power of two");
     }
     this.timeScaleFactor = timeScaleFactor;
     this.m                = (int)Math.Log(fftLength, 2.0);
     this.fftLength        = fftLength;
     this.fftBuffer        = new Complex[fftLength];
     this.fftBufferPrev    = new Complex[fftLength];
     this.fftBufferDoubled = new Complex[fftLength * 2];
     this.fftArgs          = new FftEventArgs(fftBuffer);
 }