// Handle-less AL operations: listener and global state, and GetString/GetExtensions. Handle-first
// operations are members on AlSource/AlBuffer; the Gen*/Delete* span methods are generated.
// Nothing here checks the AL error state automatically (call Al.ThrowIfError() explicitly), and
// handle inputs are not pre-validated: a bad object name sets AL_INVALID_NAME natively.
//
// Public half of the partial class; the vector-shaped marshalling helpers live in
// Al.Wrapper.Internal.cs.
namespace Brutal.OpenAlApi;
public static unsafe partial class Al
{
// --- Strings -----------------------------------------------------------------------------------------
///
/// Gets an AL string property (alGetString): vendor, version, renderer, or the
/// extension list. when OpenAL returns its NULL sentinel.
///
public static string? GetString(AlGetString param)
{
// Raw byte* return, copied to managed before anything can invalidate the library-owned
// buffer. A null result is either the NULL sentinel or an error.
return StringUtils.Utf8ToString(PInvoke.GetString((int)param));
}
/// Gets the space-separated AL extension list (AL_EXTENSIONS) as an array.
public static string[] GetExtensions()
{
var s = GetString(AlGetString.Extensions);
return string.IsNullOrEmpty(s) ? [] : s.Split(' ', StringSplitOptions.RemoveEmptyEntries);
}
// --- Global state ----------------------------------------------------------------------------------
/// Gets/sets the distance attenuation model (AL_DISTANCE_MODEL).
public static AlDistanceModel DistanceModel
{
get => (AlDistanceModel)PInvoke.GetInteger(Constants.AL_DISTANCE_MODEL);
set => PInvoke.DistanceModel((int)value);
}
/// Gets/sets the Doppler factor (AL_DOPPLER_FACTOR, default 1).
public static float DopplerFactor
{
get => PInvoke.GetFloat(Constants.AL_DOPPLER_FACTOR);
set => PInvoke.DopplerFactor(value);
}
/// Gets/sets the speed of sound used for Doppler (AL_SPEED_OF_SOUND, default 343.3).
public static float SpeedOfSound
{
get => PInvoke.GetFloat(Constants.AL_SPEED_OF_SOUND);
set => PInvoke.SpeedOfSound(value);
}
// --- Deferred updates, resampler queries, gain limit, and start-delay --------------------------------
///
/// Defers applying source/effect-slot state changes until
/// (alDeferUpdatesSOFT), so a batch of changes takes effect atomically instead of one at a
/// time. Provided by AL_SOFT_deferred_updates.
///
public static void DeferUpdates() => PInvoke.DeferUpdatesSOFT();
///
/// Applies state changes deferred since (alProcessUpdatesSOFT).
/// Provided by AL_SOFT_deferred_updates.
///
public static void ProcessUpdates() => PInvoke.ProcessUpdatesSOFT();
///
/// Defers updates for the returned scope's lifetime (pairs with
/// for a block). Provided by
/// AL_SOFT_deferred_updates.
///
public static DeferredUpdatesScope DeferUpdatesScope()
{
DeferUpdates();
return new DeferredUpdatesScope();
}
///
/// The names of the available resamplers (AL_RESAMPLER_NAME_SOFT), indexed the same
/// as a source's resampler property value. Allocates a fresh array on every access -
/// startup-tier enumeration, not a per-frame query. Provided by
/// AL_SOFT_source_resampler.
///
public static string[] ResamplerNames
{
get
{
var count = PInvoke.GetInteger(Constants.AL_NUM_RESAMPLERS_SOFT);
var names = new string[count];
for (var i = 0; i < count; i++)
{
names[i] =
StringUtils.Utf8ToString(PInvoke.GetStringiSOFT(Constants.AL_RESAMPLER_NAME_SOFT, i))
?? string.Empty;
}
return names;
}
}
///
/// The index into of the implementation's default resampler
/// (AL_DEFAULT_RESAMPLER_SOFT). Provided by AL_SOFT_source_resampler.
///
public static int DefaultResampler => PInvoke.GetInteger(Constants.AL_DEFAULT_RESAMPLER_SOFT);
///
/// The implementation's maximum per-source gain multiplier (AL_GAIN_LIMIT_SOFT); a
/// source's Gain is silently clamped to this value. Provided by
/// AL_SOFT_gain_clamp_ex.
///
public static float GainLimit => PInvoke.GetFloat(Constants.AL_GAIN_LIMIT_SOFT);
///
/// Schedules playback of several sources to start together at a device-clock time, in
/// nanoseconds (alSourcePlayAtTimevSOFT; the clock is
/// ). Provided by AL_SOFT_source_start_delay.
///
public static void PlaySourcesAt(ReadOnlySpan sources, long deviceClockTime)
{
if (sources.IsEmpty)
return;
var names = sources.Cast();
fixed (uint* p = names)
PInvoke.SourcePlayAtTimevSOFT(names.Length, p, deviceClockTime);
}
}
///
/// A -able scope pairing with
/// : state changes made while the scope is open are applied
/// atomically when it is disposed. Provided by AL_SOFT_deferred_updates.
///
public readonly ref struct DeferredUpdatesScope
{
/// Applies the deferred updates (alProcessUpdatesSOFT).
public void Dispose() => Al.ProcessUpdates();
}