Exemple #1
0
        public List <float> MusicProcesser(string filename, int toneDown = 1)
        {
            List <float> tempo        = new List <float>();
            const float  INSTANCETIME = 0.023256f;

            BeatTimeStamps.Clear();
            Signals.Clear();
            AudioFileReader pcm = new AudioFileReader(filename);

            MusicReader.DisplayInfo(pcm);
            Energize     e      = new Energize();
            List <float> bpms   = new List <float>();
            int          oneMin = 0;

            float[]       input         = new float[2048];
            Queue <float> HistoryBuffer = new Queue <float>();

            //float[] HistoryBuffer = new float[43];
            float[] left  = new float[input.Length / 2];
            float[] right = new float[input.Length / 2];
            float[] mono  = new float[input.Length / 2];
            int     read;

            int   leftCount       = 0;
            int   rightCount      = 0;
            float InstanceEnergy  = 0;
            float sampLocalEnergy = 0;
            float C = 0;

            float timeDetected      = 0.0f;
            BeatSampleProvider samp = new BeatSampleProvider(pcm);
            int beatTriggers        = 0;
            int beatThreshold       = 2;

            while ((read = samp.Read(input, 0, input.Length)) > 0)
            {
                leftCount  = 0;
                rightCount = 0;

                for (int i = 0; i < input.Length - 1; i++)
                {
                    if (i % 2 == 0)
                    {
                        left[leftCount] = input[i];
                        leftCount++;
                    }
                    else
                    {
                        right[rightCount] = input[i];
                        rightCount++;
                    }
                }

                for (int n = 0; n < mono.Length; n++)
                {
                    mono[n] = (left[n] * .5f) + (right[n] * .5f);
                }

                InstanceEnergy = e.MonoInstanceEnergy(input);
                SampleCount++;
                HistoryBuffer.Enqueue(InstanceEnergy);
                timeDetected += INSTANCETIME;
                if (SampleCount == 43)
                {
                    oneMin++;
                    SampleCount = 0;
                }
                if (oneMin == 60)
                {
                    bpms.Add(BPM);

                    BPM    = 0;
                    oneMin = 0;
                }
                if (HistoryBuffer.Count > 43)
                {
                    HistoryBuffer.Dequeue();
                    sampLocalEnergy = e.LocalEnergy(HistoryBuffer.ToArray());
                    // float variance = e.Varaince(HistoryBuffer.ToArray(), sampleSize);
                    if (InstanceEnergy > (e.C(0) * sampLocalEnergy))
                    {
                        if (++beatTriggers == beatThreshold)
                        {
                            BeatTimeStamps.Add(timeDetected);
                            Signals.Add(FastFourierTransform.PopularFreq(mono));
                            BPM++;
                        }
                    }
                    else
                    {
                        beatTriggers = 0;
                    }
                }
            }

            return(bpms);
        }
Exemple #2
0
        //public static void FreqBPM(String filename)
        //{
        //    AudioFileReader data = new AudioFileReader(filename);
        //    DisplayInfo(data);

        //    FastFourierTransform fft = new FastFourierTransform();

        //    float[] input = new float[2048];
        //    float[] HistoryBuffer = new float[43]; //One second of audio(energy)
        //    float[] left = new float[input.Length / 2];
        //    float[] right = new float[input.Length / 2];
        //    float[] subband;
        //    Complex[] comp = new Complex[left.Length];

        //    int HistoryBufferCount = 0;
        //    int readCheck;
        //    int SampleCount = 0;
        //    int BPM = 0;
        //    BeatSampleProvider samp = new BeatSampleProvider(data);
        //    while((readCheck = samp.Read(input,0,input.Length)) > 0)
        //    {

        //        int leftCount = 0;
        //        int rightCount = 0;
        //        for (int i = 0; i < input.Length; i++)
        //        {
        //            if ((i % 2) == 0)
        //            {
        //                left[leftCount] = input[i];
        //                leftCount++;

        //            }
        //            else
        //            {
        //                right[rightCount] = input[i];
        //                rightCount++;
        //            }
        //        }

        //        float[] complexNumbers = new float[1024];

        //        for (int i = 0; i < comp.Length; i++)
        //        {
        //            comp[i] = new Complex(left[i], right[i]);

        //        }
        //        Complex[] temp = FastTransform.FFT(comp);

        //        for (int i = 0; i < temp.Length; i++)
        //        {
        //            complexNumbers[i] = temp[i].Magnitude();
        //        }
        //        subband = FFT.Subbands(complexNumbers);
        //        for (int i = 0; i < subband.Length; i++)
        //        {
        //            if (HistoryBufferCount > HistoryBuffer.Length-1)
        //            {
        //                HistoryBufferCount = 0;
        //            }
        //            HistoryBuffer[HistoryBufferCount] = subband[i];
        //            HistoryBufferCount++;
        //        }

        //        for (int i = 0; i < subband.Length; i++)
        //        {
        //            float average =  FFT.AverageEnergy(HistoryBuffer);
        //            float variance = FFT.Variance(HistoryBuffer, average);
        //            if ((subband[i] > (average * 60)))//5800 for finalboss.mp3
        //            {
        //                BPM++;
        //            }
        //        }
        //    }

        //    Console.WriteLine("BPM: " + BPM);
        //}

        public static List <float> BPM(string filename, int toneDown = 1)
        {
            const float INSTANCETIME = 0.023256f;

            BeatTimeStamps.Clear();
            Signals.Clear();
            AudioFileReader pcm = new AudioFileReader(filename);

            DisplayInfo(pcm);
            Energize     e      = new Energize();
            List <float> bpms   = new List <float>();
            int          oneMin = 0;

            float[]            input         = new float[2048];
            float[]            HistoryBuffer = new float[43];
            float[]            left          = new float[input.Length / 2];
            float[]            right         = new float[input.Length / 2];
            float[]            mono          = new float[input.Length / 2];
            int                read;
            int                SampleCount     = 0;
            int                leftCount       = 0;
            int                rightCount      = 0;
            float              InstanceEnergy  = 0;
            float              sampLocalEnergy = 0;
            float              C            = 0;
            int                BPM          = 0;
            float              timeDetected = 0.0f;
            BeatSampleProvider samp         = new BeatSampleProvider(pcm);

            while ((read = samp.Read(input, 0, input.Length)) > 0)
            {
                leftCount  = 0;
                rightCount = 0;

                for (int i = 0; i < input.Length - 1; i++)
                {
                    if (i % 2 == 0)
                    {
                        left[leftCount] = input[i];
                        leftCount++;
                    }
                    else
                    {
                        right[rightCount] = input[i];
                        rightCount++;
                    }
                }

                for (int n = 0; n < mono.Length; n++)
                {
                    mono[n] = (left[n] * .5f) + (right[n] * .5f);
                }
                if (oneMin == 60)
                {
                    bpms.Add(BPM / toneDown);
                    oneMin = 0;
                    BPM    = 0;
                }
                if (SampleCount < 43)
                {
                    HistoryBuffer[SampleCount++] = InstanceEnergy = e.InstanceEnergy(left, right);
                    timeDetected   += INSTANCETIME;
                    sampLocalEnergy = e.LocalEnergy(HistoryBuffer);
                    float variance = e.Varaince(HistoryBuffer, sampLocalEnergy);
                    C = e.C(variance);

                    if (InstanceEnergy > (C * sampLocalEnergy))
                    {
                        BPM++;
                        BeatTimeStamps.Add(timeDetected);
                        Signals.Add(FastFourierTransform.PopularFreq(mono));
                    }
                }

                else
                {
                    oneMin++;
                    InstanceEnergy = e.InstanceEnergy(left, right);
                    SampleCount    = 0;
                    timeDetected  += INSTANCETIME;
                    // isFull = true;
                    sampLocalEnergy = e.LocalEnergy(HistoryBuffer);
                    float variance = e.Varaince(HistoryBuffer, sampLocalEnergy);
                    C = e.C(variance);

                    if (InstanceEnergy > (C * sampLocalEnergy))
                    {
                        BPM++;
                        BeatTimeStamps.Add(timeDetected);
                        Signals.Add(FastFourierTransform.PopularFreq(mono));
                    }
                }
            }
            bpms.Add(BPM);
            return(bpms);
        }