Momo-Space-Diner-Code-Repo / AudioVisualizers / MultiSourceAudioVisualizerLight.cs
MultiSourceAudioVisualizerLight.cs
Raw
using System.Collections;
using UnityEngine;

public class MultiSourceAudioVisualizerLight : MonoBehaviour
{
    public Light visualizerLight; // Reference to the Light component
    public float smoothingSpeed = 5f; // Controls how fast the light responds
    private float[] spectrumData = new float[256]; // Raw spectrum data
    private float[] smoothedSpectrumData = new float[256]; // Smoothed spectrum data

    public Gradient spectrumGradient; // A Gradient to define the colors from low to high frequencies
    public float colorChangeMultiplier = 20f; // Amplifies the spectrum input for the gradient
    public float spectrumExponent = 1.5f; // Apply a power curve to emphasize smaller values

    public float maxLightIntensity = 5f; // Maximum light intensity
    public float minLightIntensity = 0.5f; // Minimum light intensity

    void Update()
    {
        // Reset spectrumData to zero at the start of each frame
        for (int i = 0; i < spectrumData.Length; i++)
        {
            spectrumData[i] = 0f;
        }

        // Find all AudioSource objects in the scene
        AudioSource[] audioSources = FindObjectsOfType<AudioSource>();

        // Accumulate spectrum data from all audio sources
        foreach (AudioSource audioSource in audioSources)
        {
            if (audioSource.isPlaying && !audioSource.mute)
            {
                float[] tempSpectrum = new float[spectrumData.Length];
                audioSource.GetSpectrumData(tempSpectrum, 0, FFTWindow.Rectangular);

                for (int i = 0; i < spectrumData.Length; i++)
                {
                    spectrumData[i] += tempSpectrum[i];
                }
            }
        }

        // Smooth out the spectrum data
        for (int i = 0; i < spectrumData.Length; i++)
        {
            smoothedSpectrumData[i] = Mathf.Lerp(smoothedSpectrumData[i], spectrumData[i], Time.deltaTime * smoothingSpeed);
        }

        // Calculate overall intensity based on spectrum data
        float averageSpectrum = 0f;
        for (int i = 0; i < smoothedSpectrumData.Length; i++)
        {
            averageSpectrum += smoothedSpectrumData[i];
        }
        averageSpectrum /= smoothedSpectrumData.Length;

        // Apply a power curve to emphasize smaller spectrum values
        float adjustedSpectrum = Mathf.Pow(Mathf.Clamp01(averageSpectrum * colorChangeMultiplier), spectrumExponent);

        // Update light color and intensity based on the adjusted spectrum
        if (visualizerLight != null)
        {
            // Get the color from the gradient based on the adjusted spectrum
            Color gradientColor = spectrumGradient.Evaluate(adjustedSpectrum);

            // Adjust light intensity using the original average spectrum data
            float finalIntensity = Mathf.Lerp(minLightIntensity, maxLightIntensity, Mathf.Clamp01(averageSpectrum * 10f));

            // Apply the calculated color and intensity to the light
            visualizerLight.color = gradientColor;
            visualizerLight.intensity = finalIntensity;
        }
        else
        {
            Debug.LogWarning("Visualizer Light is not assigned.");
        }
    }
}