Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Why doesn't this OnAudioFilterRead interleaving function work?

Discussion in 'Audio & Video' started by reefwirrax, Dec 7, 2014.

  1. reefwirrax


    Sep 20, 2013
    Just learning the OnAudioReadFunction, I thought that it would be ok to do two loops of half data[] length, one to write a mono file, on half of the data[], so that i can then run a filter on the un-interleaved audio data, and then another loop to copy the mono array into a double length interleaved stereo array.

    Unity responds by not running OnAudioFilterRead at all. Why?

    If i want to process very complicated audio using OnAudioFilterRead, independent of framerate, can i call other simple functions from within OnAudioFilterRead and make many if conditions built into the function, should i process the audio in normal unity time?

    Unity freezes and jams the pc until i use taskmanager to restart it, even when the button isn't playing, after i try to run wrong audio code. Here is a simpler example of the error:'

    Code (csharp):
    2.   function OnAudioFilterRead( data:float[] , channels:int )
    3.   {
    4.     increment = frequency * 2 * Mathf.PI / sampleRate;
    5.     for (var i = 0; i < data.Length; i = i + channels)
    6.     {
    7.       phase = phase + increment;
    8.       data[i] = gain*Mathf.Sin(phase);
    9.         // the mono data to each channel
    10.       if (channels == 2) data[i + 1] = data[i];
    11.       if (phase > 2 * Mathf.PI) phase = 0;
    12.     }
    14.      for ( i = 0; i < data.Length; i = i + channels)//  IF I SET THIS TO i++ UNITY CRASHES AND FREEZES???
    15.     {
    16.      data[i]*=0.8;
    17.     }
    18.   }
    Last edited: Dec 7, 2014
  2. reefwirrax


    Sep 20, 2013
    Went into the mountains to sit on a rock and think about OnAudioFilterRead sound making function

    It turns tout that all the audio DSP has to run in the update types of functions, and the DSP just does the end reading and writing of data. You can loop with actual ticks of the cpu clock in update function so it's pretty good.
  3. gregzo


    Dec 17, 2011
    Hmmm, not sure about what you're trying to say...

    OnAudioFilterRead automatically gets called when implemented in a script that's attached to a GO which has an AudioSource or AudioListener component. It is called on the audio thread, just as many times per second as needed to keep the audio data flowing: sampleRate / bufferSizePerChannel times per second, to be precise.

    I can't spot any problem with your code, but I've never tried implementing OnAudioFilterRead in UnityScript.
    Are you calling the method yourself? If so, you should NOT!

    Finally, learning C# is really worth the extra effort, trust me.


  4. reefwirrax


    Sep 20, 2013
    Thanks Gregzo! hehe! i have to disagree :) JavaScript is awesome, it saves so much time! 10 percent less lines is that much less to read through and to write, and i have never found a task that couldnt be done in JS that could in C, even running marching cubes, JS has great control over all the unity program, and if there is something difficult with regex for example, it's possible to writh in C#, the only difference is for DLL's c it's necessary to know it. I can write C i find it clumsy :D

    The crash in the above code could be with JS, i think it's a direct hardware trouble, the code is only a loop, which jammed unity like frozen:)