Skip to content

SoundHelper

Shmellyorc edited this page Aug 31, 2026 · 2 revisions

SoundHelper provides utility methods for sound playback including pool management, volume control, category volumes, and sound group playback.


Overview

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

Master Volume

MasterVolume

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;

Category Volumes

GetCategoryVolume

Gets the volume for a specific category.

float sfxVolume = SoundHelper.GetCategoryVolume(SoundCategory.SFX);
float musicVolume = SoundHelper.GetCategoryVolume(SoundCategory.Music);

SetCategoryVolume

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);

Pooled Playback

PlayPooled

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
);

PlayPooledWithVariation

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
);

Sound Groups

RegisterSoundGroup

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
);

PlayFromGroup

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
);

Random Helpers

RandomPitch

Generates a random pitch value within a range.

float pitch = SoundHelper.RandomPitch(0.15f);  // Random between 0.85 and 1.15

RandomPan

Generates a random pan value within a range.

float pan = SoundHelper.RandomPan(0.8f);  // Random between -0.8 and 0.8

Batch Operations

StopAll

Stops all active sounds.

SoundHelper.StopAll();  // Stops everything

StopAll (by name)

Stops all active sounds with a specific name.

SoundHelper.StopAll("explosion");  // Stops all explosion sounds

PauseAll

Pauses all active sounds.

SoundHelper.PauseAll();  // Pause everything

ResumeAll

Resumes all paused sounds.

SoundHelper.ResumeAll();  // Resume everything

Pool Status

ActiveSoundCount

Gets the number of currently active sounds.

int active = SoundHelper.ActiveSoundCount;

AvailableSoundCount

Gets the number of available sound instances in the pool.

int available = SoundHelper.AvailableSoundCount;

TotalSoundCount

Gets the total number of sound instances in the pool.

int total = SoundHelper.TotalSoundCount;

IsPoolExhausted

Gets a value indicating whether the sound pool is exhausted.

if (SoundHelper.IsPoolExhausted)
{
    Console.WriteLine("Sound pool is full!");
}

Examples

Footstep Sounds with Variation

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
    );
}

Weapon Swing with Pan

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
    );
}

UI Sounds

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
    );
}

Pause/Resume on Game Pause

public void OnGamePause()
{
    // Pause all sounds except UI
    SoundHelper.PauseAll();
}

public void OnGameResume()
{
    // Resume all sounds
    SoundHelper.ResumeAll();
}

Summary

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

Back to Home

Clone this wiki locally