Files
alien-attack/AlientAttack.MonoGame/Audio/AudioManager.cs

284 lines
10 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using System;
namespace AlienAttack.MonoGame.Audio;
public sealed class AudioManager
{
public float MasterVolume { get; set; } = 1f;
public float SfxVolume { get; set; } = 0.8f;
public float MusicVolume { get; set; } = 0.7f;
private readonly Random _random = new(12345);
// Pools
private VariantSoundPool _playerGunPool = default!;
private VariantSoundPool _enemyGunPool = default!;
private VariantSoundPool _explosionPool = default!;
private VariantSoundPool _impactPool = default!;
// Rate limiters (global caps per category/event)
private RateLimiter _playerGunLimiter = new(0.06f); // max ~16 plays/sec
private RateLimiter _enemyGunLimiter = new(0.12f); // max ~8 plays/sec (global for all enemies)
private RateLimiter _impactLimiter = new(0.04f);
// Music
private Song? _currentSong;
private float _musicDuck = 1f; // 1.0 = normal, <1 = ducked
private float _musicDuckTarget = 1f;
private float _musicDuckSpeed = 6f; // how fast it moves toward target (bigger = snappier)
public void LoadContent(ContentManager content)
{
// Load SoundEffects once
LoadPlayerGunPool(content);
LoadEnemyGunPool(content);
LoadImpactPool(content);
LoadExplosionPool(content);
//var playerGun = content.Load<SoundEffect>("Audio/Sfx/player_fire");
//var enemyGun = content.Load<SoundEffect>("Audio/Sfx/enemy_fire");
//var explosion = content.Load<SoundEffect>("Audio/Sfx/explosion");
//var impact = content.Load<SoundEffect>("Audio/Sfx/impact");
// Create pools with sane voice caps
//_playerGunPool = new SoundPool(playerGun, maxVoices: 2);
//_enemyGunPool = new SoundPool(enemyGun, maxVoices: 3);
//_explosionPool = new SoundPool(explosion, maxVoices: 6);
//_impactPool = new SoundPool(impact, maxVoices: 4);
}
private void LoadPlayerGunPool(ContentManager content)
{
var playerGunVariants = new[]
{
content.Load<SoundEffect>("Sfx/GUNAuto_Assault Rifle A Fire_01_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/GUNAuto_Assault Rifle A Fire_02_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/GUNAuto_Assault Rifle A Fire_03_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/GUNAuto_Assault Rifle A Fire_04_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/GUNAuto_Assault Rifle A Fire_05_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/GUNAuto_Assault Rifle A Fire_06_SFRMS_SCIWPNS")
};
_playerGunPool = new VariantSoundPool(playerGunVariants, voicesPerVariant: 1, rng: _random);
}
private void LoadEnemyGunPool(ContentManager content)
{
var variants = new[]
{
content.Load<SoundEffect>("Sfx/Pistol/GUNPis_Pistol Fire_01_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Pistol/GUNPis_Pistol Fire_02_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Pistol/GUNPis_Pistol Fire_03_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Pistol/GUNPis_Pistol Fire_04_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Pistol/GUNPis_Pistol Fire_05_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Pistol/GUNPis_Pistol Fire_06_SFRMS_SCIWPNS"),
};
_enemyGunPool = new VariantSoundPool(variants, voicesPerVariant: 1, rng: _random);
}
private void LoadImpactPool(ContentManager content)
{
var variants = new[]
{
content.Load<SoundEffect>("Sfx/Impact/BLLTImpt_Impact Object_01_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Impact/BLLTImpt_Impact Object_02_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Impact/BLLTImpt_Impact Object_03_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Impact/BLLTImpt_Impact Object_04_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Impact/BLLTImpt_Impact Object_05_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Impact/BLLTImpt_Impact Object_06_SFRMS_SCIWPNS"),
};
_impactPool = new VariantSoundPool(variants, voicesPerVariant: 1, rng: _random);
}
private void LoadExplosionPool(ContentManager content)
{
var variants = new[]
{
content.Load<SoundEffect>("Sfx/Explosions/EXPLDsgn_Explosion Impact_01_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Explosions/EXPLDsgn_Explosion Impact_02_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Explosions/EXPLDsgn_Explosion Impact_03_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Explosions/EXPLDsgn_Explosion Impact_04_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Explosions/EXPLDsgn_Explosion Impact_05_SFRMS_SCIWPNS"),
content.Load<SoundEffect>("Sfx/Explosions/EXPLDsgn_Explosion Impact_06_SFRMS_SCIWPNS")
};
_explosionPool = new VariantSoundPool(variants, voicesPerVariant: 1, rng: _random);
}
// Call this once per frame
public void Update(GameTime gameTime)
{
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
_playerGunLimiter.Update(dt);
_enemyGunLimiter.Update(dt);
_impactLimiter.Update(dt);
// Smoothly approach the duck target
_musicDuck = Approach(_musicDuck, _musicDuckTarget, _musicDuckSpeed * dt);
// Apply music volume
MediaPlayer.Volume = Clamp01(MasterVolume * MusicVolume * _musicDuck);
}
// -----------------------
// Public SFX entry points
// -----------------------
public void PlayPlayerFire()
{
if (!_playerGunLimiter.TryConsume()) return;
// Bullet sounds should be short + quiet-ish
float baseVol = 0.35f;
float vol = baseVol * RandRange(0.92f, 1.05f);
float pitch = RandRange(-0.05f, 0.05f);
_playerGunPool.Play(
volume: Clamp01(MasterVolume * SfxVolume * vol),
pitch: pitch
);
}
public void PlayEnemyFire()
{
if (!_enemyGunLimiter.TryConsume()) return;
float baseVol = 0.28f;
float vol = baseVol * RandRange(0.90f, 1.05f);
float pitch = RandRange(-0.07f, 0.07f);
_enemyGunPool.Play(
volume: Clamp01(MasterVolume * SfxVolume * vol),
pitch: pitch
);
}
public void PlayImpact()
{
if (!_impactLimiter.TryConsume()) return;
float baseVol = 0.25f;
float vol = baseVol * RandRange(0.90f, 1.10f);
float pitch = RandRange(-0.08f, 0.08f);
_impactPool.Play(
volume: Clamp01(MasterVolume * SfxVolume * vol),
pitch: pitch
);
}
public void PlayExplosion(bool duckMusic = true)
{
float baseVol = 0.85f;
float vol = baseVol * RandRange(0.95f, 1.05f);
float pitch = RandRange(-0.04f, 0.04f);
_explosionPool.Play(
volume: Clamp01(MasterVolume * SfxVolume * vol),
pitch: pitch
);
if (duckMusic)
{
// Duck music quickly, then let it recover
DuckMusic(amount: 0.55f, holdSeconds: 0.20f, releaseSeconds: 0.35f);
}
}
// -----------------------
// Music
// -----------------------
public void PlayMusic(ContentManager content, string songAssetName, bool loop = true)
{
// Example asset name: "Audio/Music/level1"
_currentSong = content.Load<Song>(songAssetName);
MediaPlayer.IsRepeating = loop;
MediaPlayer.Volume = Clamp01(MasterVolume * MusicVolume * _musicDuck);
MediaPlayer.Play(_currentSong);
}
public void StopMusic() => MediaPlayer.Stop();
public void PauseMusic() => MediaPlayer.Pause();
public void ResumeMusic() => MediaPlayer.Resume();
// Duck helper: amount < 1.0 means quieter music (e.g., 0.55)
public void DuckMusic(float amount, float holdSeconds, float releaseSeconds)
{
amount = Math.Clamp(amount, 0.05f, 1f);
_musicDuckTarget = amount;
// Use a tiny internal timer without introducing a scheduler:
// We'll set a one-shot "return to 1" using a lightweight countdown.
_duckReturnTimer = holdSeconds;
_duckReleaseSeconds = Math.Max(0.01f, releaseSeconds);
}
private float _duckReturnTimer = 0f;
private float _duckReleaseSeconds = 0.35f;
// Call from Update to manage duck release
private void HandleDuckRelease(float dt)
{
if (_musicDuckTarget < 1f)
{
_duckReturnTimer -= dt;
if (_duckReturnTimer <= 0f)
{
// start releasing
_musicDuckTarget = 1f;
_musicDuckSpeed = 1f / _duckReleaseSeconds; // approx
// after it reaches 1, you can restore default speed if you want
}
}
else
{
// restore default “snappiness” once fully normal
if (Math.Abs(_musicDuck - 1f) < 0.001f)
_musicDuckSpeed = 6f;
}
}
// Update override to include duck release handling
public void UpdateWithDuck(GameTime gameTime)
{
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
_playerGunLimiter.Update(dt);
_enemyGunLimiter.Update(dt);
_impactLimiter.Update(dt);
HandleDuckRelease(dt);
_musicDuck = Approach(_musicDuck, _musicDuckTarget, _musicDuckSpeed * dt);
MediaPlayer.Volume = Clamp01(MasterVolume * MusicVolume * _musicDuck);
}
// -----------------------
// Utilities
// -----------------------
private float RandRange(float min, float max)
=> (float)(min + _random.NextDouble() * (max - min));
private static float Clamp01(float v) => Math.Clamp(v, 0f, 1f);
private static float Approach(float current, float target, float maxDelta)
{
if (current < target)
return Math.Min(current + maxDelta, target);
if (current > target)
return Math.Max(current - maxDelta, target);
return current;
}
}