Unity3d onAudioFilterRead call frequency

Unity3D behavior has a callback onAudioFilterRead() which can be used to record PCM data from an audio source to a wav file, possibly modifying it on-the-fly. In that post, we show the relations between audio buffer size, sampling frequency and the frequency with which onAudioFilterRead is called.

Call frequency

Let’s consider the following Unity3d script:



public class AudioRec : MonoBehaviour
{
    void Start() {
        AudioConfiguration ac = AudioSettings.GetConfiguration();
        Debug.Log("Filter: AudioSettings: dspTime: " + dspTime 
        + " bufSize: " + ac.dspBufferSize + " sampleRate: " + ac.sampleRate
        + " speakerMode: " + ac.speakerMode );
    }
    void OnAudioFilterRead(float[] data, int channels)
    {
        Debug.Log("Filter: channels: " + channels + 
            " dataLen: " + data.Length + 
            " dspTime: " + AudioSettings.dspTime
        );
    }
}

We want to calculate how frequently the callback OnAudioFilterRead() is called.

From calling to GetConfiguration we learn about the following parameters:

buffer size sample rate speaker mode
1024 48000 Stereo

Note that that buffer size is given in samples, not in bytes!

48KHz sampling rate means 48000 samples per seconds are generated by the audio system or 1 sample is generated per 1/48000 = 0.000020833 seconds. The buffer of 1024 samples is filled during 0.000020833 * 1024 = 0.021333333 seconds = 21.3 milliseconds. When the buffer is filled, onAudioFilterRead is called.

Let’s check the log generated by OnAudioFilterRead function:

02-03 19:24:03.065 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.2
02-03 19:24:03.085 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.22133333333333
02-03 19:24:03.105 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.24266666666667
02-03 19:24:03.126 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.264
02-03 19:24:03.146 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.28533333333333
02-03 19:24:03.176 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.30666666666667
02-03 19:24:03.197 15661 16939 I Unity   : Filter: channels: 2 dataLen: 2048 dspTime: 3.328

We can clearly see our calculation is correct.

Data size

Buffer size is given in number of samples. For two channels, number of samples is doubled. Therefore, number of data elements with buffer of length 1024 samples is 2*1024 = 2048. Note: sample contains all the channels available.

Unity3d converts integer representation of raw PCM data (Int = 16 bit) to float (float = 32 bit), putting the integer interval [-32767, 32767] to the float interval [-1.0f, 1.0f]. Thus, 32 bit of a single data’s float element fits 16 bits of raw audio data.

Number of bytes of raw audio data in 2048 elements of data array is 2048*2 (=16 bit) = 4096 bytes.


See also