Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnAudioFilterRead sound spatialisation

Discussion in 'Audio & Video' started by sloopidoopi, Oct 22, 2015.

  1. sloopidoopi

    sloopidoopi

    Joined:
    Jan 2, 2010
    Posts:
    244
    Hi,
    I'm injecting audio data into Unity with the OnAudioFilterRead callback . I can hear the audio but it is always a 2D sound. When my AudioListener is moving away from the Audiosource the volume gets lower but there is no 3D effect (panning)
    There is an old thread with the same question but no real answer.

    Is this a missing feature of the OnAudioFilterRead method or do i miss something ?
     
    cxode likes this.
  2. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    That stuff has already happened when the sound reaches your audio filter. Therefore, make the AudioSource play a short looping audio clip where every sample point has a value of not 0, but 1. Make your audio filter multiply the original sample value with the one you're generating, rather than overwriting it. That way, the Audio Source's panning and volume will be applied, including the rolloff. The only information lost will be stereo separation, which can be reproduced in the filter itself, though it'll take some more skills.
     
    Last edited: Oct 22, 2015
    CharlesVerron and cxode like this.
  3. sloopidoopi

    sloopidoopi

    Joined:
    Jan 2, 2010
    Posts:
    244
    @Nifflas : Many thanks for this tip! I made a dummy audio file and it worked :) .
     
  4. Nifflas

    Nifflas

    Joined:
    Jun 13, 2013
    Posts:
    118
    I'm glad I could help!
     
  5. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Thanks so much! Five years later, you have helped me as well :D

    For future folks who find themselves on this thread, I was able to generate the described clip using AudioClip.Create and AudioClip.SetData.

    https://docs.unity3d.com/ScriptReference/AudioClip.Create.html
    https://docs.unity3d.com/ScriptReference/AudioClip.SetData.html
     
  6. devluz

    devluz

    Joined:
    Aug 20, 2014
    Posts:
    66
    Thanks from 2023. Very helpful!

    It looks roughly like this in my test. mydata being a buffer with mono audio data.

    Code (CSharp):
    1.     public void Awake()
    2.     {
    3.         audioSource = this.gameObject.GetComponent<AudioSource>();
    4.  
    5.         int len = 4800;
    6.         AudioClip clip = AudioClip.Create("dummy", 4800, 1, 48000, false);
    7.         float[] data = new float[len];
    8.         for(int i = 0; i < data.Length; i++)
    9.         {
    10.             data[i] = 1;
    11.         }
    12.         clip.SetData(data, 0);
    13.         audioSource.loop = true;
    14.         audioSource.clip = clip;
    15.     }
    16.  
    17.     void OnAudioFilterRead(float[] data, int channels)
    18.     {
    19.         float[] mydata = fromSomewhereElse;
    20.         int requestedFrames = data.Length / channels;
    21.         for (int i = 0; i < requestedFrames; i++)
    22.         {
    23.             for(int k = 0; k < channels; k++)
    24.             {
    25.                 data[i * channels + k] = data[i * channels + k] * mydata[i];
    26.             }
    27.         }
    28.     }
     
    cxode likes this.