Cabbage Logo
Back to Cabbage Site

Fixing spectrum analyzer for OnAudioFilterRead

hey there @giovannibedetti and others - i’m still in my project and now willing to tackle the GetSpectrumData() issue that seems to be glitched when using procedural generation in OnAudioFilterRead. i’m wondering if a direct output from the data stream in a CsoundUnity instance might be helpful as input rather than simply using the AudioSource’s output? i started using Google Gemini to help me write the scripts and it came up with this:

public class SpectrumDisplay : MonoBehaviour
{
    public AudioSource testSource;
    LineRenderer lr;
    float[] samples = new float[1024];
    [SerializeField] Vector2 _sizeMult = new Vector2(0.04f, 0.1f);
    [SerializeField] float _maxHeight = 50f;

    void Start()
    {
        this.lr = GetComponent<LineRenderer>();
        lr.positionCount = samples.Length;
    }

    void OnAudioFilterRead(float[] data, int channels)
    {
        // Assuming your procedural audio generation fills the "data" array
        // with audio samples
        Array.Copy(data, samples, samples.Length);

        for (int i = 0; i < samples.Length; i++)
        {
            var pos = new Vector3(i * _sizeMult.x, Mathf.Clamp(samples[i] * (_maxHeight + i * i), 0, _maxHeight) * _sizeMult.y, 0);
            lr.SetPosition(i, pos);
        }
    }
}

so in line 2601 in the CsoundUnity script there’s an OnAudioFilterRead function and i was thinking what if you could call something right after ProcessBlock? or send the info directly into the spectrum script and avoid GetSpectrumData completely? i think not Update() though because OnAudioFilterRead runs in its own thread, independent of Update().

i’m definitely not knowledgeable about this so feel free to educate me. thanks!

It could work, but it sounds very expensive.
We could expose an array of the current block (the Unity OnAudioFilterRead block, usually 1024 samples, not the Csound kr block).
I will try something later in the afternoon.

Sorry I didn’t have the chance to look at it yet, busy week :sweat: