- Home /
Receiving float[][] instead of float[] for AudioClip.PCMReaderCallback
I'm playing around with making a runtime-generated AudioClip, but I'm running into a strange issue where I'm receiving a float[][] for the sample buffer on my PCMReaderCallback, when I should be receiving a float[].
The code is pretty simple:
using UnityEngine;
using System;
public class RuntimeAudioClip : MonoBehaviour
{
private AudioClip _virtualClip;
void Start()
{
_virtualClip = AudioClip.Create("test", 1, 1, 1, false, true, OnAudioRead);
audio.clip = _virtualClip;
}
void OnAudioRead(float[] data)
{
if (!(data is float[]))
{
throw new Exception(string.Format("expected '{0}', got '{1}'",
typeof(float[]), data.GetType()));
}
}
}
I'm rather befuddled. Changing the type of the "data" parameter causes a compile-time error, since it doesn't match the signature of the PCMReaderCallback delegate.
I verified that this breaks on a minimally-built project in both Unity 3.5 and Unity 4. It's easy to repro this yourself. Just create a new project, attach the above script to an Empty Object, attach an AudioSource component, and run the game. You'll hit the exception immediately.
Any ideas?
The interesting thing is that you can still write to the data array as though it were a float[]. I ran into this issue when I tried to use Array.Copy to copy samples from a local float[] buffer to the float[] data array. I hit an ArrayType$$anonymous$$ismatchException.
I created a local float[][] reference to the data array using the sledgehammer cast approach: (float[][])(object)data. After writing some samples to the array, I attached the $$anonymous$$onoDevelop debugger and tried to inspect values through the float[][] reference. Then Unity crashed. I described the situation in the crash report, so hopefully someone at Unity can help explain what's going on.
Has there been a solution to this one yet? I'm seeing the same thing and would like to know what the best approach is to handle this.
If you need access to the data, you can use OnAudioFilterRead, as described here Generating Procedural Audio. As to the OP's question; I have no idea.
PC$$anonymous$$ReaderCallbacks are super buggy and undocumented. I was advised by Unity's previous audio dev not to go near them for audio generation - use OnAudioFilterRead.
Happy to report the PC$$anonymous$$ReaderCallback did exactly what I needed in fairly straightforward manner on Unity 5.3 (and it did indeed work to just handle a "float[]" parameter, as documented).
Those wishing to use it might be warned that the callback comes from a low level thread and not the main Unity thread; if you wish to supply audio frames from other threads, you will need threadsafe means of handing it in to your callback.
Your answer
Follow this Question
Related Questions
Breathing volume increases as player gets more tired 0 Answers
Beginners troubles with audiosource 0 Answers
Triggering multiple audio clips to play in sequence 1 Answer
Unitywebrequest returns fmod error 1 Answer
OnTriggerExit stop audio 1 Answer