// Sealed IDisposable wrapper over a loopback device handle (ALC_SOFT_loopback). The loopback
// handle IS an ALCdevice*, so this class wraps an internal Device around the same handle and
// delegates CreateContext/Dispose to it. Device's playback-only members (Pause, ClockTime, HRTF,
// ...) are deliberately not re-exposed: they mean nothing on a device with no real output.
namespace Brutal.OpenAlApi;
///
/// A loopback device, opened by . Provided by ALC_SOFT_loopback.
///
///
/// A loopback device has no real audio hardware behind it and no background mixing thread: it
/// renders into an app-owned buffer only when one of the
/// overloads is called. Create a context on it with the render format specified via
/// (Frequency, FormatChannels,
/// FormatType), make it current, then drive playback and rendering explicitly. Owns the
/// underlying native device: call after disposing every context created
/// on it. Converts implicitly to the raw, non-owning handle.
///
/// The created context's is an internal plumbing
/// wrapper around the same handle: its playback-only members are
/// meaningless here and surface native errors. Its rejects
/// direct use and its is a no-op; close or dispose the owning
/// instead.
///
///
public sealed class LoopbackDevice : IDisposable, INullable
{
private readonly Device _device;
// Bytes per sample frame of the device's render format, captured on each CreateContext; 0
// until the first context establishes a format. Render's span bound - see CheckBufferLength.
private int _bytesPerFrame;
private LoopbackDevice(AlcDevice handle) => _device = new Device(handle, ownedByLoopback: true);
///
/// Opens a loopback device (alcLoopbackOpenDeviceSOFT); a name
/// means the default device.
///
public static unsafe LoopbackDevice Open(string? deviceName = null)
{
// No trustworthy open device yet, so a null return is diagnosed via the global error - same
// protocol as Alc.OpenDevice. A null name pins as a null native pointer (the default).
using var utf8 = new Utf8Buffer(deviceName, stackalloc byte[Utf8Buffer.StackBufferSize]);
AlcDevice handle;
fixed (byte* p = utf8.Span)
handle = Alc.PInvoke.LoopbackOpenDeviceSOFT(p);
if (handle.IsNull())
throw new AlcException(Alc.GetError(), "alcLoopbackOpenDeviceSOFT failed.");
return new LoopbackDevice(handle);
}
/// The raw device handle.
public AlcDevice Handle => _device.Handle;
/// Returns if this device has not been opened, or has already been disposed.
public bool IsNull() => _device.IsNull();
// Disposal guard reporting the LoopbackDevice itself, not the internal plumbing Device, as
// the disposed object. Handle, IsNull() and the implicit conversion stay unguarded as the raw
// escape hatch, matching Device.
private AlcDevice CheckedHandle
{
get
{
ObjectDisposedException.ThrowIf(IsNull(), this);
return Handle;
}
}
/// Queries whether a render format is supported (alcIsRenderFormatSupportedSOFT).
public bool IsRenderFormatSupported(int frequency, AlcChannelLayout channels, AlcSampleType sampleType) =>
Alc.PInvoke.IsRenderFormatSupportedSOFT(CheckedHandle, frequency, (int)channels, (int)sampleType);
///
/// Creates a context on this device (alcCreateContext). Specify the render format via
/// - see the class remarks.
///
public Context CreateContext(Context.CreateInfo info = default)
{
ObjectDisposedException.ThrowIf(IsNull(), this);
var context = _device.CreateContext(info);
_bytesPerFrame = QueryBytesPerFrame();
return context;
}
// The render format lives on the DEVICE (context creation configures it), so query it back
// rather than parsing info.Attributes: the query reports what the native side actually chose,
// and repeat context creations that change the format update it. Unknown future enum values
// yield 0, which Render refuses rather than guessing a frame size.
private unsafe int QueryBytesPerFrame()
{
var channelCount = (AlcChannelLayout)Query(Alc.Constants.ALC_FORMAT_CHANNELS_SOFT) switch
{
AlcChannelLayout.Mono => 1,
AlcChannelLayout.Stereo => 2,
AlcChannelLayout.Quad => 4,
AlcChannelLayout.Surround51 => 6,
AlcChannelLayout.Surround61 => 7,
AlcChannelLayout.Surround71 => 8,
// B-Format with ambisonic order N carries (N + 1)^2 channels.
AlcChannelLayout.Bformat3d => AmbisonicChannels(Query(Alc.Constants.ALC_AMBISONIC_ORDER_SOFT)),
_ => 0,
};
var sampleSize = (AlcSampleType)Query(Alc.Constants.ALC_FORMAT_TYPE_SOFT) switch
{
AlcSampleType.Byte or AlcSampleType.UnsignedByte => 1,
AlcSampleType.Short or AlcSampleType.UnsignedShort => 2,
AlcSampleType.Int or AlcSampleType.UnsignedInt or AlcSampleType.Float => 4,
_ => 0,
};
return channelCount * sampleSize;
static int AmbisonicChannels(int order) => (order + 1) * (order + 1);
int Query(int param)
{
var value = 0;
Alc.PInvoke.GetIntegerv(Handle, param, 1, &value);
return value;
}
}
///
/// Renders frames into
/// (alcRenderSamplesSOFT). counts sample FRAMES, not
/// samples or bytes: the buffer must hold at least sampleFrames * channels *
/// sizeof(sample) bytes, per the render format the last context created on this device
/// established, and is validated against it - a too-small buffer throws
/// rather than letting the native render write out of
/// bounds.
///
///
/// Drains after rendering, so a throwing
/// surfaces here. The drain is process-wide: exceptions
/// queued by any other callback handler since the last drain also rethrow here.
///
public unsafe void Render(Span buffer, int sampleFrames)
{
// The disposal guard outranks the no-render-format guard below: disposed wins.
ObjectDisposedException.ThrowIf(IsNull(), this);
// alcRenderSamplesSOFT writes frames * bytes-per-frame with no size parameter at all
// (upstream is deliberately real-time lean and cannot validate), so an unbounded call here
// is a silent managed-heap corruption - same hazard CaptureDevice.Samples guards against.
if (_bytesPerFrame == 0)
{
throw new InvalidOperationException(
"No render format is established. Create a context on this device (with Frequency/"
+ "FormatChannels/FormatType attributes) before rendering."
);
}
CheckBufferLength(_bytesPerFrame, sampleFrames, buffer.Length);
fixed (byte* p = buffer)
Alc.PInvoke.RenderSamplesSOFT(Handle, p, sampleFrames);
Al.ThrowCallbackExceptions();
}
// Mirrors CaptureDevice.CheckBufferLength. Widens to long before multiplying: sampleFrames *
// bytesPerFrame can overflow 32 bits, which would wrap and silently defeat the guard.
private static void CheckBufferLength(int bytesPerFrame, int sampleFrames, int bufferLength)
{
ArgumentOutOfRangeException.ThrowIfNegative(sampleFrames);
if (bufferLength < (long)sampleFrames * bytesPerFrame)
{
throw new ArgumentException(
$"buffer must hold at least {sampleFrames} frames * {bytesPerFrame} bytes = {(long)sampleFrames * bytesPerFrame} bytes, but was {bufferLength}.",
"buffer"
);
}
}
///
public void Render(Span buffer, int sampleFrames) => Render(MemoryMarshal.AsBytes(buffer), sampleFrames);
///
public void Render(Span buffer, int sampleFrames) => Render(MemoryMarshal.AsBytes(buffer), sampleFrames);
///
public void Close()
{
// Guarded here so the exception reports the LoopbackDevice, not the plumbing Device.
ObjectDisposedException.ThrowIf(IsNull(), this);
_device.CloseUnchecked();
}
///
public void Dispose() => _device.DisposeUnchecked();
/// Implicitly converts to the raw handle.
public static implicit operator AlcDevice(LoopbackDevice device) => device.Handle;
}