Update README.md to include project description, developer documentation links, and license information.
CI / build-test (push) Successful in 1m6s
CI / build-test (push) Successful in 1m6s
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using Microsoft.Xna.Framework.Audio;
|
||||
using MrGameEng.Core;
|
||||
|
||||
namespace MrGameEng.Audio;
|
||||
|
||||
/// <summary>
|
||||
/// Sound-effect playback with a module-level volume, plus the <see cref="Music"/> player.
|
||||
/// Registered as a service via <c>context.UseAudio()</c>.
|
||||
/// </summary>
|
||||
public sealed class AudioManager : IDisposable
|
||||
{
|
||||
/// <summary>The streaming music player.</summary>
|
||||
public MusicPlayer Music { get; } = new();
|
||||
|
||||
/// <summary>Volume multiplier applied to every sound effect, 0..1.</summary>
|
||||
public float SoundVolume
|
||||
{
|
||||
get => _soundVolume;
|
||||
set => _soundVolume = Math.Clamp(value, 0f, 1f);
|
||||
}
|
||||
|
||||
private float _soundVolume = 1f;
|
||||
|
||||
/// <summary>Plays a sound effect (fire and forget).</summary>
|
||||
/// <param name="sound">The loaded sound effect.</param>
|
||||
/// <param name="volume">Per-play volume 0..1, multiplied with <see cref="SoundVolume"/>.</param>
|
||||
/// <param name="pitch">Pitch offset in octaves, -1..1.</param>
|
||||
/// <param name="pan">Stereo pan, -1 (left) .. 1 (right).</param>
|
||||
public void Play(SoundEffect sound, float volume = 1f, float pitch = 0f, float pan = 0f) =>
|
||||
sound.Play(Math.Clamp(volume, 0f, 1f) * _soundVolume, pitch, pan);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose() => Music.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>Wires the audio module into the engine.</summary>
|
||||
public static class AudioEngineExtensions
|
||||
{
|
||||
/// <summary>Creates the <see cref="AudioManager"/> and registers it as a service. Call once at startup.</summary>
|
||||
public static AudioManager UseAudio(this EngineContext context)
|
||||
{
|
||||
var manager = new AudioManager();
|
||||
context.Services.Add(manager);
|
||||
return manager;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user