AudioSource.GetSpectrumData() dont work

using UnityEngine;
using System.Collections;

public class SpawnBasedOnAudio : MonoBehaviour
{
    AudioSource audio;
    public GameObject objectPrefab;
    public float spawnThreshold = 0.5f;
    public int frequency = 0;
    public FFTWindow fftWindow;
    //public float debugValue;
   
    private float[] samples = new float[1024]; //MUST BE A POWER OF TWO
   
    void Start()
    {

        audio = GetComponent<AudioSource>();

    }
   
    // Update is called once per frame
    void Update ()
    {
        audio.GetSpectrumData(samples, 0, fftWindow);

        //debugValue = samples[frequency];

        if(samples[frequency] > spawnThreshold)
        {
            Instantiate(objectPrefab, new Vector3(Random.RandomRange(-9, 8), 10, 0), Quaternion.identity);
        }
    }
}

The object is to create a beat of music. (low tones).
How to do it? Why code does not work?

what more concerning is that you are planning to Instantiate objects according to your threshold. There is overhead and memory allocation each time you call Instantiate which will lead to GC spikes down the line

You should rather create however many prefabs you need ahead of time and cache them in some or other collection and use/reuse these as needed. ( this is known as object pooling)

If you are looking to isolate the low tones im sure you can use much less samples 1024 is overkill. Then from experience I can tell you for some or other reason the very first value in the returned samples is often zero , rather do a Mathf.max on the first few samples and use that.

Another thing is these (frequency) values are affected by the amplitude of the audio sample so tweaking your threshold for one audio sample will not mean it will work the same with others.

@shaderbytes That’s how it should be written? Quite does not find in this topic (audio)
I know you’re behind me do not you write, but Show me how it should be done.