CsoundUnity 4.0.0-alpha.3
Loading...
Searching...
No Matches
Csound.Unity.AudioChannelReader Class Referencesealed

Reads the audio a CsoundUnity instance publishes on one of its named audio channels, one finished block at a time, from any thread. More...

Public Member Functions

int Read (float[] destination)
 Copies the most recently published block into destination . Safe to call from any thread, including an audio callback: it neither blocks nor allocates.

Package Functions

 AudioChannelReader (CsoundUnity csound, string channelName)

Properties

string ChannelName [get]
 The named audio channel this reader follows.
int Length [get]
 Number of samples in the most recently published block, or 0 while the instance has published nothing on this channel.
int Channels [get]
 Number of interleaved channels carried by the published block.
bool HasNewBlock [get]
 true when a block has been published that this reader has not taken yet. Use it to skip the work around a read — a repaint, a mixdown — without performing the copy.

Detailed Description

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 AudioChannelReader _reader;
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; // nothing new, or a block was dropped
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

Constructor & Destructor Documentation

◆ AudioChannelReader()

Csound.Unity.AudioChannelReader.AudioChannelReader ( CsoundUnity csound,
string channelName )
inlinepackage

Member Function Documentation

◆ Read()

int Csound.Unity.AudioChannelReader.Read ( float[] destination)
inline

Copies the most recently published block into destination . Safe to call from any thread, including an audio callback: it neither blocks nor allocates.

Parameters
destinationBuffer to copy into. Make it at least Length long; a shorter one receives the start of the block and the rest is discarded.
Returns
The number of samples copied; 0 when nothing new has been published since the last call, in which case destination is left alone; and -1 when the producer overtook the copy, in which case destination holds a partial block and must not be used. The next call retries, so a -1 costs one block, not the stream.

Property Documentation

◆ ChannelName

string Csound.Unity.AudioChannelReader.ChannelName
get

The named audio channel this reader follows.

◆ Channels

int Csound.Unity.AudioChannelReader.Channels
get

Number of interleaved channels carried by the published block.

◆ HasNewBlock

bool Csound.Unity.AudioChannelReader.HasNewBlock
get

true when a block has been published that this reader has not taken yet. Use it to skip the work around a read — a repaint, a mixdown — without performing the copy.

◆ Length

int Csound.Unity.AudioChannelReader.Length
get

Number of samples in the most recently published block, or 0 while the instance has published nothing on this channel.


The documentation for this class was generated from the following file: