- Home /
Can we make better audio visualizer than it?
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(AudioSource))] public class AudioVisualizer : MonoBehaviour { AudioSource _MyAudioSource; public static float[] Samples = new float[512]; public static float[] _FrequencyBand = new float[8];
void Start()
{
_MyAudioSource = GetComponent<AudioSource>();
}
void Update()
{
GetSpectrumAudioSource();
MakeFrequencyBand();
}
void GetSpectrumAudioSource()
{
_MyAudioSource.GetSpectrumData(Samples, 0, FFTWindow.Blackman);
}
void MakeFrequencyBand()
{
int count = 0;
for (int i = 0; i < 8; i++)
{
float average = 0;
int SampleCount = (int)Mathf.Pow(2, i) * 2;
if (i == 7)
{
SampleCount += 2;
}
for (int j = 0; j < SampleCount; j++)
{
average += Samples[count] * (count + 1);
count++;
}
average /= count;
_FrequencyBand[i] = average * 10;
}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Param : $$anonymous$$onoBehaviour { public int band; public float _StartScale,_Scale$$anonymous$$ultiplier;
void Start()
{
}
void Update()
{
transform.localScale = new Vector3(0.1f,(AudioVisualizer._FrequencyBand[band]*_Scale$$anonymous$$ultiplier)+_StartScale,0.01f);
}
}
Your answer
Follow this Question
Related Questions
Lower volume when another audio source plays 1 Answer
Custom inspector curves similar to the 'Audio Source' volume/pan/spread Curves 0 Answers
how can i mute all audio sources in the scene? 2 Answers
Is there a more efficient way to trigger an audio source from an array to play than using a timer? 0 Answers
make visualizer smoother 1 Answer