What to use in place of GetOutputData?

I jumped back into Unity after being absent for a while, and before, I think I was using 4.3, now running 5.5. So I’m working through old code and trying to figure out how a particular class worked, and why it doesn’t anymore. I had it working in 4.x, but it doesn’t anymore.

I wanted to stream data from one AudioSource into my own custom AudioClip, and play that clip at another time. The AudioSource was mute, play on awake, and looped, so it would continuously play at start even thought it wasn’t heard. Then I could stream that data into my own clip and play it like secret messages or something.

Here is a summary of what I did.

I was calling GetOutputData, which would stream data from an AudioSource into some destination parameter. Once I determined which AudioSource I wanted to grab data from, I would supply that as the object for GetOutputData. And I would output it to a buffer.

SomeAudioSource.GetOutputData(buffer,0)

Prior to this though, I defined my buffer to be a floating point array of a certain size, 16384. An int was 4 bytes, and 16384 elements * 4 bytes yields 65,536 (or 64k) for the size of the buffer. I don’t remember why I made the buffer a float though, but the values in side were integers.

buffer = new float[16384];

I left the buffer at 64k because I didn’t want to huge arrays of numbers in memory. Depending on how long I wanted the second audio clip to play I had a 2nd buffer that would expand by 64k chunks. I added the the 64k of output data to a 2nd buffer until a person chose to stop.

exBuffer.AddRange(buffer);

To make it seamless, I had to create a timer that would count for the duration of the 64k output data. In this case, my audio that was playing was 44kHz, so I had to get how long it would have to play to play through 64k of audio. The equation was SIZE / FREQUENCY. The size was 16384 (16384 values of int) and the frequency was 44100 (44kHz). The time was about 0.37 seconds. So my timer had to increment each frame by the time for each frame, and when it exceeded our 0.37 seconds, it would reset and push out the audio to the 2nd buffer.

timer+= Time.deltaTime;

So you end up having something like this

timer += Time.deltaTime;
if (timer >= _size/_frequency)
{
    SomeAudioSource.GetOutputData(buffer,0);
    exBuffer.AddRange(buffer);
    timer= 0;
}

I only wanted my created audio clip to be no more than 10 seconds, so I had to put an if statement in there to see if it exceeded the size of 10 seconds, which would be frequency * the seconds I wanted to play.

exBuffer.Count + _size >= _frequency * (_seconds+1);

Then I just create an AudioClip, the appropriate size to hold the data, and use SetData to inject the data into the AudioClip.

_MyAudioClip = AudioClip.Create("output",buffer2.Count,1,frequency,false,false);
_MyAudioClip.SetData(buffer2.ToArray(),0);

Just assign that clip to a source, and you are set.

Now with Unity 5.x, they deprecated my version of AudioClip.Create. The first bool parameter was for 3D audio, but that isn’t there anymore, so I can take out that bool value from the argument list. But the GetOutputData that I used is also deprecated to. So it doesn’t even work the same way anymore. So what used to strip raw data from an audio source now only picks up data that I hear. So if the AudioSource is mute, it will be mute when played back. I’ve tested things to make sure it was still grabbing data, and it is, but the original AudioSource that I’m pulling from can not be mute in order to play back a segment of that audio. Otherwise the segment of audio that is played back is also mute. Since this worked in 4.x, I’m assuming there is something in the API that can still allow it function this way, but I do not know how.

Is there anything I can use to replace GetOutputData to have the same desired effect?

Thanks for any help.

I found this, which I don’t like.

“It doesn’t make sense to have an AudioSource muted, effectively producing no signal, and have getOutputData return signal. This fix was put in place to bring the behaviour inline with what is conceptually happening with the signal path”

I did want to get data from a muted source. Why did they feel the need to remove it? That messed up my designer for my a large portion of my project.