BASS.NET API for the Un4seen BASS Audio LibraryBASSBuffer ClassBASS.NET API for the Un4seen BASS Audio Library
Implements a fast and generic circular ring buffer (FiFo).
Inheritance Hierarchy

SystemObject
  Un4seen.BassBASSBuffer

Namespace: Un4seen.Bass
Assembly: Bass.Net (in Bass.Net.dll) Version: 2.4.12.0
Syntax

public sealed class BASSBuffer

The BASSBuffer type exposes the following members.

Constructors

  NameDescription
Public methodBASSBuffer
Default Constructor using the default buffer size.
Public methodBASSBuffer(Single, Int32, Int32, Int32)
Creates an instance of the ring buffer with a size acconding to the given parameters.
Top
Properties

  NameDescription
Public propertyBps
Gets the Bytes per sample used with the ring buffer (1=8-bit, 2=16-bit default, 4=32-bit).
Public propertyBufferLength
Gets the total length in bytes of the ring buffer.
Public propertyNumChans
Gets the number of channels used with the ring buffer (1=mono, 2=stereo default, etc).
Public propertyReaders
Gets or Sets the number of parallel Readers which can be used with the ring buffer.
Public propertySampleRate
Gets the sample rate in Hz used with the ring buffer (default is 44100 Hz).
Top
Methods

  NameDescription
Public methodClear
Clears the ring buffer (zeros all elements) and resets all read and write pointers.
Public methodCount
Returns the number of bytes available in the ring buffer for reading.
Public methodDispose
Implement IDisposable.
Protected methodFinalize
Finalization code.
(Overrides ObjectFinalize.)
Public methodRead(Byte, Int32, Int32)
Reads (removes) the given number of sample data from the ring buffer.
Public methodRead(IntPtr, Int32, Int32)
Reads (removes) the given number of sample data from the ring buffer.
Public methodResize
Resizes (enlarges) the ring buffer by the given factor.
Public methodSpace
Returns the number of bytes available in the ring buffer for writing until an overflow of the read pointer will happen.
Public methodWrite(Byte, Int32)
Writes (add) the given number of sample data to the ring buffer.
Public methodWrite(IntPtr, Int32)
Writes (add) the given number of sample data to the ring buffer.
Top
Remarks

This implementation uses an internal byte array as a buffer and can be used in multi-threading environments, since all members are thread-safe.

The default size of the ring buffer is 2 seconds (when using it with 44.1kHz, 16-bit stereo sample data).

This class supports multiple readers using the ring buffer (default is 1). Use the Readers property to set the maximum number of parallel readers accessing the ring buffer.

The ring buffer is not a dynamic buffer, meaning it has a fixed size once created. However, the ring buffer size might be increased at any time through the Resize(Single) method, even if you should avoid this for performance reasons.

Use the Write(IntPtr, Int32) method to add sample data to the ring buffer. And use the Read(IntPtr, Int32, Int32) method to retrieve sample data from the ring buffer.

This class might be useful for full-duplex monitoring of a recording streams or in order to clone existing streams.

In order to reduce latency, you might probably want to minimize the recording update period (see BASS_RecordStart(Int32, Int32, BASSFlag, RECORDPROC, IntPtr). You'll probably also want to reduce the update period (BASS_CONFIG_UPDATEPERIOD) and buffer length (BASS_CONFIG_BUFFER), to reduce output latency (see BASS_SetConfig(BASSConfig, Int32)).

Examples

BASS Recording with full-duplex monitoring:
VB
Private _myRecProc As RECORDPROC ' make it global, so that the GC can not remove it
Private _recHandle As Integer = 0
' The buffer: 44.1kHz, 16-bit, stereo (like we record!)
Private _monBuffer As New BASSBuffer(2F, 44100, 2, 16)
Private _monStream As Integer = 0
Private _monProc As STREAMPROC = Nothing
...
' enable lower latency settings (optional)
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, 20)
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_BUFFER, 100)
' start recording with 20ms update period
_myRecProc = New RECORDPROC(MyRecording)
_recHandle = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_DEFAULT, 20, _myRecProc, IntPtr.Zero)
' setup the full-duplex monitoring
_monProc = New STREAMPROC(AddressOf MonitoringStream)
_monStream = Bass.BASS_StreamCreate(44100, 2, 0, _monProc, IntPtr.Zero) ' user = reader#
Bass.BASS_ChannelPlay(_monStream, False)

Private Function MyRecording(handle As Integer, buffer As IntPtr, length As Integer, user As IntPtr) As Boolean
  monBuffer.Write(buffer, length)
End Function

Private Function MonitoringStream(handle As Integer, buffer As IntPtr, length As Integer, user As IntPtr) As Integer
  Return monBuffer.Read(buffer, length, user.ToInt32())
End Function
private RECORDPROC _myRecProc; // make it global, so that the GC can not remove it
private int _recHandle = 0;
// The buffer: 44.1kHz, 16-bit, stereo (like we record!)
private BASSBuffer _monBuffer = new BASSBuffer(2f, 44100, 2, 16);
private int _monStream = 0;
private STREAMPROC _monProc = null;
...
// enable lower latency settings (optional)
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, 20);
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_BUFFER, 100);
// start recording with 20ms update period
_myRecProc = new RECORDPROC(MyRecording);
_recHandle = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_DEFAULT, 20, _myRecProc, IntPtr.Zero);
// setup the full-duplex monitoring
_monProc = new STREAMPROC(MonitoringStream);
_monStream = Bass.BASS_StreamCreate(44100, 2, 0, _monProc, IntPtr.Zero); // user = reader#
Bass.BASS_ChannelPlay(_monStream, false);

private bool MyRecording(int handle, IntPtr buffer, int length, IntPtr user)
{
    monBuffer.Write(buffer, length);
}

private int MonitoringStream(int handle, IntPtr buffer, int length, IntPtr user)
{
    return monBuffer.Read(buffer, length, user.ToInt32());
}
See Also

Reference