-
Notifications
You must be signed in to change notification settings - Fork 1
SoundHelper
SoundHelper provides utility methods for sound playback including pool management, volume control, category volumes, and sound group playback.
| Feature | Description |
|---|---|
| Pool Access | Direct access to the sound instance pool |
| Master Volume | Global volume control for all sounds |
| Category Volumes | Per-category volume control (SFX, Music, UI, etc.) |
| Pooled Playback | Play sounds from the pool with one call |
| Pitch Variation | Random pitch variation for natural variety |
| Sound Groups | Register and play random sounds from groups |
| Batch Operations | Stop, pause, or resume all sounds |
Gets or sets the master volume of all sounds.
// Set master volume to 80%
SoundHelper.MasterVolume = 0.8f;
// Get current master volume
float currentVolume = SoundHelper.MasterVolume;Gets the volume for a specific category.
float sfxVolume = SoundHelper.GetCategoryVolume(SoundCategory.SFX);
float musicVolume = SoundHelper.GetCategoryVolume(SoundCategory.Music);Sets the volume for a specific category and updates all active instances.
// Set SFX volume to 90%
SoundHelper.SetCategoryVolume(SoundCategory.SFX, 0.9f);
// Set Music volume to 50%
SoundHelper.SetCategoryVolume(SoundCategory.Music, 0.5f);Plays a sound from the pool.
// Basic playback
var instance = SoundHelper.PlayPooled(mySound, 0.8f, 0f, 1f, SoundCategory.SFX);
// With custom settings
var instance2 = SoundHelper.PlayPooled(
mySound,
volume: 0.8f,
pan: -0.5f, // Left channel
pitch: 1.2f, // Higher pitch
category: SoundCategory.SFX
);Plays a sound from the pool with random pitch variation.
// Play with random pitch variation (±10%)
var instance = SoundHelper.PlayPooledWithVariation(
mySound,
pitchRange: 0.15f, // ±15% variation
volume: 0.8f,
pan: 0f,
category: SoundCategory.SFX
);Registers a sound group for random selection.
// Register footsteps group
SoundHelper.RegisterSoundGroup("footsteps",
footstep1,
footstep2,
footstep3,
footstep4
);
// Register weapon sounds group
SoundHelper.RegisterSoundGroup("weapons",
swordSwing,
axeSwing,
clubSwing
);Plays a random sound from a registered group.
// Play a random footstep
var instance = SoundHelper.PlayFromGroup(
"footsteps",
volume: 0.7f,
pan: 0f,
pitch: 1f,
category: SoundCategory.SFX
);
// Play with random pitch variation
var instance2 = SoundHelper.PlayFromGroup(
"footsteps",
volume: 0.7f,
pan: 0f,
pitch: 1f,
category: SoundCategory.SFX,
withVariation: true,
pitchRange: 0.1f
);Generates a random pitch value within a range.
float pitch = SoundHelper.RandomPitch(0.15f); // Random between 0.85 and 1.15Generates a random pan value within a range.
float pan = SoundHelper.RandomPan(0.8f); // Random between -0.8 and 0.8Stops all active sounds.
SoundHelper.StopAll(); // Stops everythingStops all active sounds with a specific name.
SoundHelper.StopAll("explosion"); // Stops all explosion soundsPauses all active sounds.
SoundHelper.PauseAll(); // Pause everythingResumes all paused sounds.
SoundHelper.ResumeAll(); // Resume everythingGets the number of currently active sounds.
int active = SoundHelper.ActiveSoundCount;Gets the number of available sound instances in the pool.
int available = SoundHelper.AvailableSoundCount;Gets the total number of sound instances in the pool.
int total = SoundHelper.TotalSoundCount;Gets a value indicating whether the sound pool is exhausted.
if (SoundHelper.IsPoolExhausted)
{
Console.WriteLine("Sound pool is full!");
}public void PlayFootstep()
{
// Play a random footstep with pitch variation
var instance = SoundHelper.PlayFromGroup(
"footsteps",
volume: 0.6f,
pan: RandomPan(0.5f),
pitch: 1f,
category: SoundCategory.SFX,
withVariation: true,
pitchRange: 0.12f
);
}public void PlaySwordSwing(Vect2 playerPos, Vect2 enemyPos)
{
// Calculate pan based on enemy position relative to player
float pan = Math.Clamp((enemyPos.X - playerPos.X) / 500f, -1f, 1f);
SoundHelper.PlayPooled(
swordSwing,
volume: 0.9f,
pan: pan,
pitch: SoundHelper.RandomPitch(0.05f),
category: SoundCategory.SFX
);
}public void OnButtonClick()
{
SoundHelper.PlayPooled(
buttonClick,
volume: 0.7f,
pitch: SoundHelper.RandomPitch(0.05f),
category: SoundCategory.UI
);
}
public void OnMenuOpen()
{
SoundHelper.PlayPooled(
menuOpen,
volume: 0.5f,
category: SoundCategory.UI
);
}public void OnGamePause()
{
// Pause all sounds except UI
SoundHelper.PauseAll();
}
public void OnGameResume()
{
// Resume all sounds
SoundHelper.ResumeAll();
}| Method | Description |
|---|---|
MasterVolume |
Gets or sets the global volume |
GetCategoryVolume |
Gets volume for a category |
SetCategoryVolume |
Sets volume for a category |
PlayPooled |
Plays a sound from the pool |
PlayPooledWithVariation |
Plays a sound with pitch variation |
RegisterSoundGroup |
Registers a sound group |
PlayFromGroup |
Plays a random sound from a group |
StopAll |
Stops all active sounds |
PauseAll |
Pauses all active sounds |
ResumeAll |
Resumes all paused sounds |
RandomPitch |
Generates a random pitch value |
RandomPan |
Generates a random pan value |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions