Example-Code / Plugins / AudioFiltererExample
AudioFiltererExample
Raw
private void OnAudioFilterRead(float[] data, int channels)
{
    if (_sampleRate > 0)//--You can't read the sampleRate outside of the main thread. Ie. when unity is loading a scene. This will callback outside of the main thread. SO, we add a guard condition (_sampleRate is > 0 only after off-loading-thread-is exited).
    {
        #region Native implementation
        //--Pinning the arrays with Alloc before passing them to the native code so GC doesn't collect as the native code is accessing them.
        GCHandle bufferLowHandle  = GCHandle.Alloc(bufferLow, GCHandleType.Pinned);
        GCHandle bufferHighHandle = GCHandle.Alloc(bufferHigh, GCHandleType.Pinned);

        try
        {
            ApplyBandStopFilter(data, channels, data.Length, bufferLow, bufferHigh, _lowCutoff, _highCutoff);
        }
        finally
        {
            bufferLowHandle.Free();
            bufferHighHandle.Free();
        }
        #endregion
    }
}