示例#1
0
    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);

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

        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;

        Debug.Log("Instant Energy: " + instantEnergyHistory[0] + "\nAverage Energy: " + localAverageEnergy + "\nEnergy Constant: " + constant + "\nBeat target: " + beatEnergyTarget);
        if (instantEnergy > beatEnergyTarget)
        {
            Debug.Log("Beat Detected");
            cubeRend.material.color = Color.green;
            beatCount += 1;
        }
        else
        {
            cubeRend.material.color = Color.red;
        }
    }
示例#2
0
    private Beat CheckForBeat(float[] rightChannel, float[] leftChannel)
    {
        float instantEnergy      = AudioAnalyser.GetInstantEnergy(rightChannel, leftChannel);
        float localAverageEnergy = AudioAnalyser.GetLocalAverageEnergy(instantEnergyHistory);
        float variance           = AudioAnalyser.GetEnergyVariance(instantEnergyHistory, localAverageEnergy);
        float constant           = AudioAnalyser.GetEnergyFormulaConstant(variance, sensitivity);

        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;

        Debug.Log("Instant Energy: " + instantEnergyHistory[0] + "\nAverage Energy: " + localAverageEnergy + "\nEnergy Constant: " + constant + "\nBeat target: " + beatEnergyTarget);
        if (instantEnergy > beatEnergyTarget && instantEnergy > Mathf.Epsilon)
        {
            return(new Beat(0, 0, instantEnergy, localAverageEnergy, beatEnergyTarget));
        }
        return(null);
    }