Beispiel #1
0
    private Beat[] ProcessStereo(float[] samples, int numOfSamples, int frequency)
    {
        Debug.Log("Processing Stereo Song");

        float[] rightSamples = new float[numOfSamples];
        float[] leftSamples  = new float[numOfSamples];
        for (int i = 0; i < numOfSamples; i += 1)
        {
            rightSamples[i] = samples[i * 2];
            leftSamples[i]  = samples[(i * 2) + 1];
        }

        Beat[] beatTrack = new Beat[windowIterations + 1];

        for (int i = 0; i < beatTrack.Length; i += 1)
        {
            float[] tempRight = new float[windowInterval];
            float[] tempLeft  = new float[windowInterval];
            if (i < windowIterations)
            {
                for (int j = 0; j < windowInterval; j += 1)
                {
                    tempRight[j] = rightSamples[(windowInterval * i) + j];
                    tempLeft[j]  = leftSamples[(windowInterval * i) + j];
                }
            }
            else
            {
                for (int j = 0; j < lastWindowSize; j += 1)
                {
                    tempRight[j] = rightSamples[(windowInterval * i) + j];
                    tempLeft[j]  = leftSamples[(windowInterval * i) + j];
                }
            }
            List <float[]> bandsRight = AudioAnalyser.GetDistinctBands(FastFourierTransform.FftMag(tempRight), frequency, windowInterval / 2.0f);
            List <float[]> bandsLeft  = AudioAnalyser.GetDistinctBands(FastFourierTransform.FftMag(tempLeft), frequency, windowInterval / 2.0f);
            if (energyHistories.Count == 0)
            {
                Debug.Log("Creating histories");
                energyHistories = new List <float[]>();
                for (int b = 0; b < bandsRight.Count; b += 1)
                {
                    energyHistories.Add(new float[historyBufferLength]);
                    bandVariances.Add(new float[historyBufferLength]);
                }
            }
            beatTrack[i] = CheckForBeatBands(bandsRight, bandsLeft);
            if (beatTrack[i] != null)
            {
                beatTrack[i].windowNumber = i;
                beatTrack[i].timeStamp    = WindowPositionToTime(i);
            }
        }

        return(beatTrack);
    }
Beispiel #2
0
    private Beat[] ProcessMono(float[] samples, int frequency)
    {
        Debug.Log("Processing Mono Song");

        Beat[] beatTrack = new Beat[windowIterations + 1];

        for (int i = 0; i < beatTrack.Length; i += 1)
        {
            float[] temp = new float[windowInterval];
            if (i < windowIterations)
            {
                for (int j = 0; j < windowInterval; j += 1)
                {
                    temp[j] = samples[(windowInterval * i) + j];
                }
            }
            else
            {
                for (int j = 0; j < lastWindowSize; j += 1)
                {
                    temp[j] = samples[(windowInterval * i) + j];
                }
            }
            List <float[]> bands = AudioAnalyser.GetDistinctBands(FastFourierTransform.FftMag(temp), frequency, windowInterval / 2.0f);
            if (energyHistories.Count == 0)
            {
                Debug.Log("Creating histories");
                energyHistories = new List <float[]>();
                for (int b = 0; b < bands.Count - 1; b += 1)
                {
                    energyHistories.Add(new float[historyBufferLength]);
                    bandVariances.Add(new float[historyBufferLength]);
                }
            }
            beatTrack[i] = CheckForBeatBands(bands, null);
            if (beatTrack[i] != null)
            {
                beatTrack[i].windowNumber = i;
                beatTrack[i].timeStamp    = WindowPositionToTime(i);
            }
        }

        return(beatTrack);
    }
    void CheckForBeat()
    {
        GameObject beatCube = GameObject.FindGameObjectWithTag("Beat Cube");
        Renderer   cubeRend = beatCube.GetComponent <Renderer>();

        float[] rightChannel = new float[SAMPLE_SIZE];
        float[] leftChannel  = new float[SAMPLE_SIZE];
        source.GetOutputData(rightChannel, 0);
        source.GetOutputData(leftChannel, 1);

        rightChannel = FastFourierTransform.FftMag(rightChannel);
        leftChannel  = FastFourierTransform.FftMag(leftChannel);

        float instantEnergy      = AudioAnalyser.GetInstantEnergy(rightChannel, leftChannel);
        float localAverageEnergy = AudioAnalyser.GetLocalAverageEnergy(instantEnergyHistory);
        float variance           = AudioAnalyser.GetEnergyVariance(instantEnergyHistory, localAverageEnergy);
        float constant           = AudioAnalyser.GetEnergyFormulaConstant(variance, 100.5142857f);

        float[] shiftArray = new float[instantEnergyHistory.Length];

        Array.Copy(instantEnergyHistory, 0, shiftArray, 1, instantEnergyHistory.Length - 1);
        shiftArray[0] = instantEnergy;
        Array.Copy(shiftArray, instantEnergyHistory, shiftArray.Length);
        float beatEnergyTarget = constant * localAverageEnergy;

        UnityEngine.Debug.Log("Instant Energy: " + instantEnergyHistory[0] + "\nAverage Energy: " + localAverageEnergy + "\nEnergy Constant: " + constant + "\nBeat target: " + beatEnergyTarget);
        if (instantEnergy > beatEnergyTarget)
        {
            UnityEngine.Debug.Log("Beat Detected");
            cubeRend.material.color = Color.green;
            beatCount += 1;
        }
        else
        {
            cubeRend.material.color = Color.red;
        }
    }