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

Wait-free, single-producer / multiple-consumer snapshot buffer used to hand blocks of audio from the thread that produces them to any number of threads that observe them. More...

Package Functions

void Publish (float[] source, int length, int channels=1)
 Publishes a block of samples. Call from the producer thread only.
void Publish (double[] source, int length, int channels=1)
 Publishes a block of double samples, converting to float during the copy. Call from the producer thread only.
int Read (float[] destination, int length, ref int cursor)
 Copies the most recently published block into destination , if it is newer than the caller's cursor. Non-consuming: other consumers are unaffected. Call from any thread.
int Read (float[] destination, int length, ref int cursor, out bool dropped)
 As Read(float[], int, ref int), and tells the two reasons for a zero apart. Consumers that hand the data to someone else need that distinction: on a dropped block destination holds a partial copy.
void Clear ()
 Drops the published block, so consumers see the buffer as empty until the producer publishes again. Used when an instance stops, to stop routed destinations from repeating the last block it produced.

Properties

int Version [get]
 Version stamp of the most recently published block, or 0 if nothing has been published yet. Use it to tell whether a Read would return anything without performing the copy — for example to skip a redundant repaint.
int Length [get]
 Number of valid samples in the most recently published block.
int Channels [get]
 Number of interleaved audio channels in the most recently published block.

Detailed Description

Wait-free, single-producer / multiple-consumer snapshot buffer used to hand blocks of audio from the thread that produces them to any number of threads that observe them.

Why this exists. Audio produced inside OnAudioFilterRead is consumed by readers that run on a different thread: another CsoundUnity instance's audio callback (audio input routing), or the main thread (inspector waveform/spectrum monitors, user scripts polling in Update). Reading the producer's live working array directly is unsafe on two counts: Unity does not define the order in which OnAudioFilterRead runs across AudioSources, so a reader can observe a block that is only half-written; and plain array element access across threads carries no memory barrier, so on 32-bit ARM a double can be read torn. Both show up as clicks.

How it works. The producer drops a slot's version stamp before writing into it and gives it a new, monotonically increasing one once the block is complete, so a slot being written carries no stamp at all. Consumers read the most recently published slot and re-check its stamp after copying: a stamp that is gone or has moved means the producer lapped them, and the block is discarded rather than delivered torn. Being lapped takes a consumer Slots full DSP periods behind the producer, which a plain array copy never is. A full barrier on each side keeps the stamp and the samples in that order on a processor that reorders memory accesses, as ARM does.

Neither side ever blocks, spins or allocates in steady state, so this is safe to call from the audio thread. There must be exactly one producer; consumers may be many, and each keeps its own cursor so a block is delivered at most once per consumer without consumers stealing blocks from each other.

Data is stored as float: readers overwhelmingly want floats (Unity audio buffers, routing mixdown, monitors), so converting once on publish is cheaper than converting once per consumer.

Member Function Documentation

◆ Clear()

void Csound.Unity.CsoundSharedBuffer.Clear ( )
inlinepackage

Drops the published block, so consumers see the buffer as empty until the producer publishes again. Used when an instance stops, to stop routed destinations from repeating the last block it produced.

◆ Publish() [1/2]

void Csound.Unity.CsoundSharedBuffer.Publish ( double[] source,
int length,
int channels = 1 )
inlinepackage

Publishes a block of double samples, converting to float during the copy. Call from the producer thread only.

Parameters
sourceSamples to publish.
lengthNumber of samples to take from source .
channelsInterleaved channel count carried by the block.

◆ Publish() [2/2]

void Csound.Unity.CsoundSharedBuffer.Publish ( float[] source,
int length,
int channels = 1 )
inlinepackage

Publishes a block of samples. Call from the producer thread only.

Parameters
sourceSamples to publish.
lengthNumber of samples to take from source .
channelsInterleaved channel count carried by the block.

◆ Read() [1/2]

int Csound.Unity.CsoundSharedBuffer.Read ( float[] destination,
int length,
ref int cursor )
inlinepackage

Copies the most recently published block into destination , if it is newer than the caller's cursor. Non-consuming: other consumers are unaffected. Call from any thread.

Parameters
destinationBuffer to copy into.
lengthMaximum number of samples to copy.
cursorThe caller's private cursor. Pass the same variable on every call; seed it with 0. It is advanced only when a block is actually delivered.
Returns
The number of samples copied. Zero means nothing new was available, or the producer lapped the caller mid-copy and the partially-copied block was rejected — in which case destination may hold garbage and should not be used.

◆ Read() [2/2]

int Csound.Unity.CsoundSharedBuffer.Read ( float[] destination,
int length,
ref int cursor,
out bool dropped )
inlinepackage

As Read(float[], int, ref int), and tells the two reasons for a zero apart. Consumers that hand the data to someone else need that distinction: on a dropped block destination holds a partial copy.

Parameters
destinationThe array the block is copied into.
lengthHow many samples to copy.
cursorThis consumer's position, carried between calls and advanced to the block that was read.
droppedtrue when the producer lapped the copy and the block was rejected. false with a zero return means nothing new was published since cursor , and destination was not written to.

Property Documentation

◆ Channels

int Csound.Unity.CsoundSharedBuffer.Channels
getpackage

Number of interleaved audio channels in the most recently published block.

◆ Length

int Csound.Unity.CsoundSharedBuffer.Length
getpackage

Number of valid samples in the most recently published block.

◆ Version

int Csound.Unity.CsoundSharedBuffer.Version
getpackage

Version stamp of the most recently published block, or 0 if nothing has been published yet. Use it to tell whether a Read would return anything without performing the copy — for example to skip a redundant repaint.


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