Class FloatRingBuffer
- Lock-free single-producer/single-consumer ring buffer for float PCM samples.
- Overwrite policy: when full, drops the oldest samples by advancing a producer-owned drop floor.
- ReadOrSilence() always fills the requested output length (pads with zeros) to avoid audio underruns.
- TryRead() reads up to output length and returns how many samples were copied.
- SPSC only: exactly one producer thread and exactly one consumer thread.
Inheritance
FloatRingBuffer
Assembly: Glitch9.IO.dll
Syntax
public sealed class FloatRingBuffer : IDisposable
Constructors
|
Edit this page
View Source
FloatRingBuffer(int)
Declaration
public FloatRingBuffer(int capacity)
Parameters
| Type |
Name |
Description |
| int |
capacity |
|
Properties
|
Edit this page
View Source
AvailableRead
- Approximate readable sample count (clamped to int range).
Declaration
public int AvailableRead { get; }
Property Value
|
Edit this page
View Source
Capacity
- Ring buffer capacity in samples (power of two).
Declaration
public int Capacity { get; }
Property Value
|
Edit this page
View Source
DroppedSamples
- Total number of samples logically dropped due to overwrite.
Declaration
public long DroppedSamples { get; }
Property Value
Methods
|
Edit this page
View Source
Clear()
- Clears cursors and drop counters.
- Safe only when streaming is stopped (no concurrent Read/Write expected).
Declaration
|
Edit this page
View Source
Dispose()
- Disposes the ring buffer by clearing all cursors and counters.
- Note: This class uses only managed memory; Dispose is provided for explicit cleanup semantics.
Declaration
|
Edit this page
View Source
GetCursors()
Declaration
public (long Read, long Write, long Floor) GetCursors()
Returns
|
Edit this page
View Source
ReadOrSilence(Span<float>)
- Reads exactly output.Length samples if possible; otherwise reads what is available and zero-fills the rest.
- This is the recommended method for audio callbacks to avoid underruns.
- Returns how many samples came from the buffer (0..output.Length).
Declaration
public int ReadOrSilence(Span<float> output)
Parameters
Returns
|
Edit this page
View Source
TryRead(Span<float>)
- Tries to read up to output.Length samples.
- Returns the number of samples copied (may be 0).
- If overwrite is detected (drop floor advanced past our cursor), it catches up and returns 0.
Declaration
public int TryRead(Span<float> output)
Parameters
Returns
|
Edit this page
View Source
Write(ReadOnlySpan<float>)
- Writes samples to the buffer.
- If the write would exceed capacity, drops the oldest samples by advancing the drop floor.
- Returns the number of samples accepted (equals input length).
Declaration
public int Write(ReadOnlySpan<float> input)
Parameters
Returns
Implements
Extension Methods