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...
|
| 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.
|
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.