Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        ShiftAudioHistogramTextureData();

        audioSpectrumAnalyser.UpdateSpectrumData();
        pitch = audioSpectrumAnalyser.pitch;

        float[] audioSpectrumData = audioSpectrumAnalyser.audioSpectrumData;

        float maximumAmplitude = audioSpectrumAnalyser.dominantFrequencyBandIndex >= 0 ? audioSpectrumData[audioSpectrumAnalyser.dominantFrequencyBandIndex] : 0.0f;

        if (maximumAmplitude > maxAmplitudeForAudio)
        {
            maxAmplitudeForAudio    = maximumAmplitude;
            inverseMaximumAmplitude = 1 / maximumAmplitude;
        }

        float scale = (maximumAmplitude > 0.0002 ? 1 / maximumAmplitude : 1); // inverseMaximumAmplitude;

        //Debug.Log("Begin Sample Data");
        for (int index = 0; index < audioTextureWidth; index++)
        {
            //Debug.Log("Sample Data: " + audioSpectrumData[index]);

            float red = 0.0f, blue = 0.0f, green = 0.0f;
            if (index == audioSpectrumAnalyser.dominantFrequencyBandIndex)
            {
                green = audioSpectrumData[index] * scale;
            }
            else
            {
                red = audioSpectrumData[index] * scale;
            }

            Color pixel = new Color(red, green, blue, 1.0f);
            audioTexture.SetPixel(index, 0, pixel);
        }
        //Debug.Log("End Sample Data");

        audioTexture.Apply();
    }
    // Update is called once per frame
    void Update()
    {
        audioSpectrumAnalyser.UpdateSpectrumData();

        float[] audioSpectrumData = audioSpectrumAnalyser.audioSpectrumData;

        ParticleSystem.MainModule main = theParticleSystem.main;

        // Alter the size
        main.startSizeMultiplier = audioSpectrumAnalyser.dominantFrequencyBandIndex >= 0 ? audioSpectrumData[audioSpectrumAnalyser.dominantFrequencyBandIndex] * 2.0f : 0.0f;

        // Alter the colour
        float bandIndex = audioSpectrumAnalyser.dominantFrequencyBandIndex >= 0 ? audioSpectrumAnalyser.dominantFrequencyBandIndex : 0;
        // NOTE: Arbitrary scale here due to the dominant frequency band often being at the lower end of the spectrum.
        float normalisedBandIndex = bandIndex * 32.0f / (float)((int)AudioSpectrumAnalyser.AudioSampleSize.Medium - 1);
        float red = 1.0f - normalisedBandIndex;

        Debug.Log("bandIndex = " + bandIndex + " Normalised = " + normalisedBandIndex + " red = " + red);
        Color particleColour = new Color(red, 0.0f, 0.0f, 1.0f);

        main.startColor = particleColour;
    }