Why doesn't this OnAudioFilterRead interleaving function work?

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:’

  function OnAudioFilterRead( data:float[] , channels:int )
  {
    increment = frequency * 2 * Mathf.PI / sampleRate;
    for (var i = 0; i < data.Length; i = i + channels)
    {
      phase = phase + increment;
      data[i] = gain*Mathf.Sin(phase);
        // the mono data to each channel
      if (channels == 2) data[i + 1] = data[i];
      if (phase > 2 * Mathf.PI) phase = 0;
    }
  
     for ( i = 0; i < data.Length; i = i + channels)//  IF I SET THIS TO i++ UNITY CRASHES AND FREEZES???
    {
     data[i]*=0.8;
    }
  }

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.

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.

Cheers,

Gregzo

Thanks Gregzo! hehe! i have to disagree :slight_smile: 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 :smile:

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:-)