Search Unity

Audio onAudioFilterRead isn't called in pc build

Discussion in 'Audio & Video' started by RaldesTheWhite, Apr 27, 2017.

  1. RaldesTheWhite

    RaldesTheWhite

    Joined:
    Apr 26, 2017
    Posts:
    3
    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)
     
  2. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Your audioSource is probably null (and if audioSource.Play() is not called, the OnAudioFilterRead won't be either)
     
    chambino likes this.
  3. RaldesTheWhite

    RaldesTheWhite

    Joined:
    Apr 26, 2017
    Posts:
    3
    But sometimes it works and AudioSource is permanently on object so how is possible that it would be null?
     
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    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
     
  5. mynameisjohnj

    mynameisjohnj

    Joined:
    Mar 18, 2017
    Posts:
    11
    I wonder if the output of the audio source has "Auto Mixer Suspend" enabled, and that's getting turned on somehow?
     
  6. soilmaster

    soilmaster

    Joined:
    Nov 28, 2013
    Posts:
    2
    Code (CSharp):
    1. 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.
     
  7. Dance_M

    Dance_M

    Joined:
    Aug 17, 2014
    Posts:
    15
    Don't repeat my mistake: use OnAudioFilterRead() exclusively on objects that have an AudioSource attached, or it won't work.