Reads the audio a CsoundUnity instance publishes on one of its named audio channels, one finished block at a time, from any thread.
Obtain one from CsoundUnity.GetAudioChannelReader. Each reader keeps its own position in the stream, so several readers on the same channel all see every block: a meter, a visualiser and a gameplay script do not take blocks away from one another.
What arrives is always a block the producer had finished writing. That costs one DSP buffer of latency, and it is the reason to read here rather than from CsoundUnity.namedAudioChannelDataDict, which is the live working array and can be observed half-written.
A reader survives CsoundUnity.Stop and CsoundUnity.Restart: it resolves the channel by name on every read, so it picks up again when the instance does.
private float[] _block = new float[2048];
void Start()
{
_reader = GetComponent<CsoundUnity>().GetAudioChannelReader("main_out_0");
}
void Update()
{
var n = _reader.
Read(_block);
if (n <= 0) return;
var sum = 0f;
for (var i = 0; i < n; i++) sum += _block[i] * _block[i];
_level = Mathf.Sqrt(sum / n);
}
int Read(float[] destination)
Copies the most recently published block into destination . Safe to call from any thread,...
Definition AudioChannelReader.cs:113
AudioChannelReader(CsoundUnity csound, string channelName)
Definition AudioChannelReader.cs:67