native/audio-native/lingua_audio.c wraps miniaudio (vendored at pinned release 0.11.25 — dual public domain/MIT-0, confirmed from the license statement in the file itself) the same way lingua_physics.c wraps Box3D: ma_engine_config/ma_sound_config are large structs with optional callbacks, so nothing but plain int/float/bool/UTF-8-path scalars crosses the P/Invoke boundary, and handles are this shim's own array-index handles into a fixed ma_sound table, not miniaudio's own pointer types. Same DllImport-not-LibraryImport choice too, for the same reason: zero AllowUnsafeBlocks needed anywhere in this plugin. One real addition beyond mirroring the physics shim: Lingua_Audio_Init takes a useNullBackend flag. miniaudio's null backend runs the exact same load/decode/mix/loop pipeline against a device that discards its output — real coverage of this shim's logic without depending on real audio hardware being present (most CI runners have none) or making an automated test run produce actual sound, which nobody expects. Real gameplay (AudioPlugin) always requests the real backend; only Engine.Audio.Tests asks for the null one. engine.audio adds an AudioSource component (ClipPath/Volume/Loop/ PlayOnAwake) and AudioWorld, which — same shape as PhysicsWorld, same reason: no destruction event exists to hook — diffs Query<AudioSource>() against its own tracked set every Stage.Update, loading a clip the first time it sees one, applying Volume/Loop changes only when they actually changed, and unloading sounds whose GameObject is gone. IAudioService exposes Play/Stop/IsPlaying for gameplay code to trigger a sound instead of it only ever firing on PlayOnAwake. 5 new tests, all against the null backend, all passed after fixing one real bug they caught: AudioWorld originally re-attempted loading (and re-warned about) a missing clip on every single Sync call instead of once — MissingClip_WarnsAndDoesNotThrow failed on the first run with 2 warnings instead of 1, fixed by tracking failed-load GameObjects the same way PhysicsWorld already tracks missing-collider ones. Full suite: 86 tests. Not yet verified: actual audible playback through a real device — the null-backend tests prove this plugin's own logic, but nobody has listened to real output yet. Worth doing deliberately, not as a surprise mid-session — flagging rather than just doing it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using Engine.Audio.Contracts;
|
|
using Engine.Kernel.Plugins;
|
|
using Engine.Kernel.Scheduling;
|
|
using Engine.Kernel.World;
|
|
|
|
namespace Engine.Audio;
|
|
|
|
/// <summary>
|
|
/// M4's audio plugin: miniaudio, over the shim in native/audio-native/ —
|
|
/// see that file's own doc comment for why nothing but scalars and a UTF-8
|
|
/// path string cross the P/Invoke boundary. One system, on Stage.Update
|
|
/// (not FixedUpdate — nothing about audio is physics-timed): sync new/
|
|
/// changed/removed AudioSources every frame.
|
|
/// </summary>
|
|
public sealed class AudioPlugin : IPlugin
|
|
{
|
|
private AudioWorld? _world;
|
|
|
|
public void Configure(IPluginContext ctx)
|
|
{
|
|
_world = new AudioWorld(ctx.Log, useNullBackend: false);
|
|
ctx.Services.Provide<IAudioService>(new AudioService(_world));
|
|
|
|
ctx.Schedule.Add(Stage.Update, Sync).Reads<AudioSource>();
|
|
|
|
ctx.Log.Info("audio engine ready (miniaudio)");
|
|
}
|
|
|
|
public void Shutdown(IPluginContext ctx)
|
|
{
|
|
ctx.Schedule.RemoveAllFrom("engine.audio");
|
|
ctx.Services.Revoke<IAudioService>();
|
|
_world?.Dispose();
|
|
_world = null;
|
|
}
|
|
|
|
private void Sync(IWorld world) => _world!.Sync(world);
|
|
}
|