// Sealed IDisposable wrapper over the raw AlcCaptureDevice handle: Alc.CaptureOpenDevice // constructs it, Dispose closes it. No finalizer and no Destroy() alias - disposal is explicit. // Capture and playback devices are both a native ALCdevice*, but are closed with different native // calls; the distinct handle type makes passing one to the wrong close op a compile error. // // Public half of the type; the format/buffer-size helpers live in CaptureDevice.Internal.cs. namespace Brutal.OpenAlApi; /// /// An OpenAL capture device, opened by /// . /// /// /// Owns the underlying native capture device: call when done. Converts /// implicitly to the raw, non-owning handle. /// public sealed partial class CaptureDevice : IDisposable, INullable { /// The raw capture-device handle. public AlcCaptureDevice Handle { get; private set; } // The frame size (bytes per sample-frame) for the format this device was opened with. Always // known: Alc.CaptureOpenDevice derives it for the four standard AlFormat values and rejects any // other format unless the caller states the size explicitly. Samples() below relies on it. private readonly int _bytesPerFrame; // Lifetime-constant query result, cached on first access instead of re-querying native (and // re-allocating) every time. A capture device has no reopen path, so this is never invalidated. private string? _specifier; internal CaptureDevice(AlcCaptureDevice handle, int bytesPerFrame) { Handle = handle; _bytesPerFrame = bytesPerFrame; } // Disposal guard shared by every member that touches native state or returns cached native // answers - a disposed capture device must never answer plausibly. Handle, IsNull() and the // implicit conversion stay unguarded (the raw escape hatch), matching Device. private AlcCaptureDevice CheckedHandle { get { ObjectDisposedException.ThrowIf(IsNull(), this); return Handle; } } /// Returns if this capture device has already been disposed. public bool IsNull() => Handle.IsNull(); /// The capture device's name (ALC_CAPTURE_DEVICE_SPECIFIER). Cached. public string? Specifier { get { // Guard before the cache, not just before the query - as in Device's cached getters. ObjectDisposedException.ThrowIf(IsNull(), this); return _specifier ??= Alc.GetString(Handle, AlcGetString.CaptureDeviceSpecifier); } } /// /// The number of captured sample-frames available to read (ALC_CAPTURE_SAMPLES). /// Poll this between and to size the read. /// public int AvailableSamples => Alc.GetInteger(CheckedHandle, AlcInteger.CaptureSamples); /// /// Reads and clears this capture device's ALC error (alcGetError), without throwing /// on a latched error. /// public AlcError GetError() { ObjectDisposedException.ThrowIf(IsNull(), this); return Alc.GetError(Handle); } /// /// Reads and clears this capture device's ALC error, throwing if /// one was set. Never called automatically by wrapper methods, as with /// . /// public void ThrowIfError() { ObjectDisposedException.ThrowIf(IsNull(), this); Alc.ThrowIfError(Handle); } /// public void Start() => Alc.PInvoke.CaptureStart(CheckedHandle); /// public void Stop() => Alc.PInvoke.CaptureStop(CheckedHandle); /// /// Reads captured samples out of the device's internal buffer (alcCaptureSamples). /// Native code writes frames regardless of the buffer's length, /// so the length is validated against the frame size the device was opened with (throws /// on a too-small buffer). /// public void Samples(Span buffer, int samples) { ObjectDisposedException.ThrowIf(IsNull(), this); CheckBufferLength(_bytesPerFrame, samples, buffer.Length); unsafe { fixed (byte* p = buffer) Alc.PInvoke.CaptureSamples(Handle, p, samples); } } /// /// Closes the capture device (alcCaptureCloseDevice), throwing /// if the native close reports failure - the explicit-lifetime /// counterpart of , which swallows it. The wrapper is disposed either /// way, so a later (a safety net) is a no-op. /// public void Close() { ObjectDisposedException.ThrowIf(IsNull(), this); if (!CloseCore()) throw new AlcException(Alc.GetError(), "alcCaptureCloseDevice failed."); } /// /// Closes the capture device (alcCaptureCloseDevice). Never throws: a failed close /// leaves its reason on the global ALC error latch - call instead when /// failure should surface. Idempotent but not safe to call concurrently; no finalizer - an /// undisposed capture device leaks. /// public void Dispose() { if (IsNull()) return; // Same rationale as Device.Dispose: never replace a using-body exception; the reason // stays on the global latch. CloseCore(); } private bool CloseCore() { // The wrapper dies either way - see Device.CloseCore. var closed = Alc.PInvoke.CaptureCloseDevice(Handle); Handle = AlcCaptureDevice.Null; return closed; } /// Implicitly converts to the raw handle. public static implicit operator AlcCaptureDevice(CaptureDevice capture) => capture.Handle; }