How do I get the sampling rate from AudioEffectCapture?

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m using the AudioEffectCapture.get_frames_available() and AudioEffectCapture.get_buffer() to read samples from the microphone. How do I get the sampling rate so I know how many seconds this frame range covers?

I was just looking at the docs (which I’m sure you’ve seen). You can set the buffer_length in seconds. I think that’s what you want here.


Sidenote

Sound sampling frequency is actually insanely high, it’s usually configured per-microphone. I remember in one of my projects I had to set the mic sampling freqency to some 44100Hz in order for Godot to recognize the source.
What I’m getting at is I don’t think the mic sampling freqency is something you want to interact with anyways. Who knows what happens to the sound samples, the OS and drivers might do some processing on them to smooth them out or something. Audio is weird like that

The issue there is that AudioEffectCapture.get_frames_available() is returning different sized buffers. In my testing, I’m sometimes getting buffers length 512 and other times 1024. On other machines they might be a different size. It seems unlikely to me that 512 frames covers .1 seconds.

I need to figure out what the time is between samples, and that isn’t going to be as simple as buffer_length / data_buffer.size(). I don’t see anything in AudioEffectCapture about the sampling rate. I could probably use something much lower than 44100Hz for my purposes, but I don’t see anywhere to set that.

Edit:
It looks like you’re using an AudioEffectSpectrumAnalyzerInstance, so I suppose you don’t need to check the sample rate directly.

1 Like