onAudioFilterRead isn't called in pc build

Hello,
I created procedural sound generation (you press keys and it produce realistic flute sound). A have object witch have AudioSource and my script.

void Start()
{
AudioConfiguration audioConfig = AudioSettings.GetConfiguration();
audioConfig.speakerMode = AudioSpeakerMode.Mono;
audioConfig.sampleRate = 44100;
audioConfig.dspBufferSize = 1024;
AudioSettings.Reset(audioConfig);

if (audioSource != null)
audioSource.Play();
}
void OnAudioFilterRead(float[ ] data, int channels)
{
synthesizer.GetBuffer(data);
}

As you can see, I use OnAudioFilterRead method. In editor is everything ok. But when I make PC build there is some bug… OnAudioFilterRead isn’t called. But only sometimes. I asynchronously load scene (1st level) form menu and it works fine, then asynchronously load next level and bug will appear. Sometimes is broken both of levels.

On other hand, I use same method in Automatic procedural generating, which play specific songs and there is everything ok (on PC build).

Have anyone some advice? I read many posts, but none has solved my problem.

(yes, I know that I posted this thread yesterday, but I didn’t realize that it was on bad forum)

Your audioSource is probably null (and if audioSource.Play() is not called, the OnAudioFilterRead won’t be either)

1 Like

But sometimes it works and AudioSource is permanently on object so how is possible that it would be null?

I think the asynchronous loading might have something with it - maybe it doesn’t load the clip properly ( or more precisely, the clip is not yet properly loaded when the source is requested to start ) - but these are just guesses, unfortuately
also, I’d probably reset the audio settings only once when the application starts

I wonder if the output of the audio source has “Auto Mixer Suspend” enabled, and that’s getting turned on somehow?

void OnAudioFilterRead(float[] data, int channels) was not getting called in a 2020 version for me, upgrading to 2021 for the exact same project worked.

Don’t repeat my mistake: use OnAudioFilterRead() exclusively on objects that have an AudioSource attached, or it won’t work.