Using AudioListener.GetSpectrumData to get an audio spectrum to use to modulate the Max Particles in a particle system. I use this exact method with other parts of my sketch to modulate the intensity of Lighting, the size of Cubes, Spheres, and Text…) but this will not work on the Particle System. Any ideas?
using UnityEngine;
public class Max_Particles_Blow_Spectrum : MonoBehaviour {
[Range(1.0f, 30000.0f)]
public float phi = 0.0f;
private ParticleSystem ps;
void Start()
{
ps = GetComponent<ParticleSystem>();
}
void Update()
{
float[] phi = AudioListener.GetSpectrumData(1024, 0, FFTWindow.Hamming);
var main = ps.main;
float amplitude = Mathf.RoundToInt(phi[1]) ;
// i thought it was an issue with the float array to I tried converting it to an int
main.maxParticles = Mathf.RoundToInt(amplitude);
}
void OnGUI()
{
phi = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), phi, 1f, 30000.0f);
}
}
and for example, here is code that works to adjust the intensity of a light:
Thank you so much for your help, this is fantastic. This is really helping me learn about the best way to do these types procedures.
What I am really trying to achieve is that when I have low frequencies, let’s say, a bass drop, then I would get large numbers, like an explosion of particles. Would this work for something like that?
I’m not actually sure whether GetSpectrumData or GetOutputData is better for calculating overall volume.
GetOutputData might be better for doing that, that’s what’s being used in the example I linked to.
I don’t really have experience doing this, just played with it now.
It seems like you’d want to look at particular frequency bands (ranges).
Each of the elements in the array returned from GetSpectrumData actually represents a frequency band, but you could group them up further to reduce resolution, and/or change the size of the array to increase or decrease the resolution.Then you can watch for audio volume within a particular frequency range.
Start here to see how the array works, and how to pick out frequences
I just changed the example above so that the overall volume is now affecting the particle gameobject transform position Y
And as for the frequency, you can select what elements of the frequency array you want to watch, and it averages the volume within those elements, and it checks to see if it meets a threshold and it will get the particle effect to emit particles if it exceeds the threshold.
I was trying to isolate a particular sound to certain elements and thresholds.
It’s only using 256 resolution though. Probably eaiser to draw a representation of the whole thing to get a visualization and more easily pinpoints the bands a sound affects.
I’m thinking there is merit in fetching the sum of a band, average of a band, peak and min of a band, because you might want to use all of those in calcs. Fetching the dominant band would be useful as well as just looking at specific bands.