Skip to content

SoundSystem

GeckoN edited this page Mar 29, 2017 · 7 revisions

The sound system allows you to play sounds, sentences and sentence groups, complete with sound replacement.

HTML documentation can be found here.

Implemented in the CSoundEngine class.

A single global instance exists:

CSoundEngine g_SoundSystem;

Constants

SOUND_CHANNEL

Sound channels represent a number of audio channels that can simultaneously play sounds for any given entity. Note that despite what the name of some channels might imply, none of them are intended for any particular type of sound, with the exception of CHAN_STATIC due to its special behavior in networking code.

ChannelDescription
CHAN_AUTOAutomatically selects a channel for use. You cannot stop sounds while using this channel with StopSound, or in conjunction with the SND_STOP flag.
CHAN_WEAPONSound channel recommended for weapons.
CHAN_VOICESound channel recommended for voice (speaking).
CHAN_ITEMSound channel recommended for items (pickup sounds, etc).
CHAN_BODYSound channel recommended for body events (footsteps, clothes, etc).
CHAN_STREAMGeneral purpose channel.
CHAN_STATICChannel used for sounds that loop, or sounds that play for a long time (e.g. music). Is always sent to players, even if they are not within the Potentially Audible Set (i.e. outside of the audible range).
CHAN_NETWORKVOICE_BASEReserved for player voice data. Do not use.
CHAN_NETWORKVOICE_ENDReserved for player voice data. Do not use.

Sound flags

ConstantDescription
SND_VOLUMEIf set, a volume other than 1 is used. Do not use directly, automatically set.
SND_PITCHIf set, a pitch other than 100 is used. Do not use directly, automatically set.
SND_ATTENUATIONIf set, an attenuation other than ATTN_NORM is used. Do not use directly, automatically set.
SND_ORIGINIf set, an origin is sent instead of using the entity origin. Do not use directly, automatically set.
SND_ENTIf set, the sound is being played for an entity and its origin should be used. Do not use directly, automatically set.
SND_STOPTells the client to stop playing a sound.
SND_CHANGE_VOLIf set, instead of restarting the sound, just changes its volume.
SND_CHANGE_PITCHIf set, instead of restarting the sound, just changes its pitch.
SND_SENTENCEIf set, this is a sentence. Do not use directly, automatically set.
SND_REFRESHIf set, the sound is refreshed so clients that previously did not receive the sound due to not being in the Potentially Audible Set (for all channels except CHAN_STATIC) or not on the server when the sound started (all channels) will still hear it.
SND_FORCE_SINGLEIf set, the sound will not loop even if it normally would, e.g. if it has a cue point.
SND_FORCE_LOOPIf set, the sound will loop even if it normally wouldn't, e.g. if it does not have a cue point.
SND_LINEARIf set, the given attenuation has linear falloff. See Attenuation.
SND_SKIP_ORIGIN_USE_ENTDon't send the sound's origin, instead, rely on entity index to re-create origin on client dll.

Attenuation

Attenuation affects sound falloff.

Falloff normally occurs between 64 and ( 4 / attenuation ) * 256. The flag SND_LINEAR can override this to provide a custom falloff range. When SND_LINEAR is used, the attenuation value is defined as follows: The lower 4 bits are minimum falloff, the higher 4 bits are maximum falloff. Minimum is multiplied by 256, maximum is multiplied by 256 and added to 32.

ConstantDescription
const float ATTN_NONENo attenuation is applied. Falloff is defined as minimum and maximum 10000. Equivalent to ambient_generic "play everywhere" flag.
const float ATTN_NORMNormal attenuation. Equivalent to ambient_generic "large radius" flag.
const float ATTN_IDLEIdle attenuation. Equivalent to ambient_generic "small radius" flag.
const float ATTN_STATICStatic attenuation. Equivalent to ambient_generic "medium radius" flag.

Pitch

Pitch affects playback speed and can be used to simulate different voice depths.

Values are possible in the range [0, 255], where 255 is very high.

ConstantValueDescription
PITCH_NORM100Non-pitch shifted.
PITCH_LOW95Low pitch. Can be used for deep voices.
PITCH_HIGH120High pitch. Can be used for high voices (helium, children, etc).

Other

ConstantDescription
const float VOL_NORMNormal volume level.

Methods

PrecacheSound

void PrecacheSound(const string& in szFilename)

This method allows you to precache a sound for use. Sounds must be precached in MapInit or an entity's Precache method.

The file path starts in sound/.

ArgumentPurpose
szFilenameName of the file to precache.

FindSoundReplacementSample

string FindSoundReplacementSample(CBaseEntity@ pEntity, const string& in szSample) const

Finds the sound replacement sample for the given entity and sample. If the sample is replaced, it will return the sample that is used instead. Otherwise, szSample is returned.

Both the entity's sound replacement file as well as the global file will be checked.

ArgumentPurpose
pEntityEntity whose sound replacement file will be checked. Must be valid.
szSampleSample to look up.

PlaySound

void PlaySound(edict_t@ entity, SOUND_CHANNEL channel, const string& in sample, float volume, float attenuation, int flags = 0, int pitch = PITCH_NORM, int target_ent_unreliable = 0, bool setOrigin = false, const Vector& in vecOrigin = g_vecZero)

Plays a sound. The sound is not considered for sound replacement.

ArgumentPurpose
pEntityEntity that the sound will be attached to. Sounds are played at the entity's origin.
channelSound channel to use. An entity can have a sound playing on every channel it has at the same time. See SOUND_CHANNEL.
sampleName of the sound to play.
volumeVolume to play the sound at. This value has a range of [0, 1], where 0 is muted, and 1 is maximum volume.
attenuationAttenuation. Values lie in the range [0, 4]. Higher values decrease the falloff range. See Attenuation.
flagsBit vector containing a number of flags. See Sound flags.
pitchPitch to use. See Pitch.
target_ent_unreliableIf not zero, this is the entity index of a player to send this sound to unreliably. Useful for non-critical sounds.
setOriginIf true, use the given origin to play the sound, instead of the entity origin.
vecOriginIf setOrigin is true, this is the origin in the world to play the sound at.

StopSound

void StopSound(edict_t@ entity, SOUND_CHANNEL channel, const string& in szSample, const bool fUseSoundReplacement = true)

Stops a sound that is playing on the given entity's channel.

ArgumentPurpose
pEntityEntity whose sound will be stopped.
channelSound channel to stop.
sampleName of the sound to stop.
fUseSoundReplacementWhether to use sound replacement to find the actual sample or not.

EmitSoundDyn

void EmitSoundDyn(edict_t@ entity, SOUND_CHANNEL channel, const string& in szSample, float flVolume, float flAttenuation, int iFlags = 0, int iPitch = PITCH_NORM, int target_ent_unreliable = 0)

Plays a sound on the given entity's channel. The sound may be sound replaced if there is a replacement sample provided by a replacement file.

ArgumentPurpose
pEntityEntity that the sound will be attached to. Sounds are played at the entity's origin.
channelSound channel to use. An entity can have a sound playing on every channel it has at the same time. See SOUND_CHANNEL.
szSampleName of the sound to play.
flVolumeVolume to play the sound at. This value has a range of [0, 1], where 0 is muted, and 1 is maximum volume.
flAttenuationAttenuation. Values lie in the range [0, 4]. Higher values decrease the falloff range. See Attenuation.
iFlagsBit vector containing a number of flags. See Sound flags.
iPitchPitch to use. See Pitch.
target_ent_unreliableIf not zero, this is the entity index of a player to send this sound to unreliably. Useful for non-critical sounds.

EmitSound

void EmitSound(edict_t@ entity, SOUND_CHANNEL channel, const string& in szSample, float flVolume, float flAttenuation)

Identical to EmitSoundDyn, but provides default values for omitted arguments.

EmitSoundSuit

void EmitSoundSuit(edict_t@ entity, const string& in szSample)

Play a specific sentence over the HEV suit speaker - just pass player entity, and !sentencename. Pitch is randomly calculated to fall around PITCH_NORM, volume is determined by the suitvolume cvar. Attenuation is ATTN_NORM.

ArgumentPurpose
entityEntity where the suit sound will be played. Can be any entity, not just a player.
szSampleSentence to play.

EmitGroupIdSuit

void EmitGroupIdSuit(edict_t@ entity, int isentencereg)

Play a sentence, randomly selected from the passed in group id, over the HEV suit speaker.

ArgumentPurpose
entityEntity where the suit sound will be played. Can be any entity, not just a player.
isentenceregSentence group to play. Must be a sentence group id retrieved using the sound system's sentence methods.

EmitGroupNameSuit

void EmitGroupNameSuit(edict_t@ entity, const string& in szSample)

Play a sentence, randomly selected from the passed in groupname.

ArgumentPurpose
entityEntity where the suit sound will be played. Can be any entity, not just a player.
szSampleSentence group to select a sentence from.

LookupSentenceIndex

int LookupSentenceIndex(const string& in sentenceName)

Looks up the sentence index of the given sentence.

ArgumentPurpose
sentenceNameName of the sentence to find.

Return value:

Sentence index if it was found, 0 otherwise.

LookupSentenceGroupIndex

int LookupSentenceGroupIndex(const string& in szGroup)

Looks up the sentence group index of the given sentence.

ArgumentPurpose
szGroupName of the sentence group to find.

Return value:

Sentence group index if it was found, 0 otherwise.

PlaySentenceGroup

int PlaySentenceGroup(edict_t@ entity, const string& in szGroupName, float volume, float attenuation, int flags, int pitch)

Plays a sentence from a sentence group. The sentence is randomly selected.

ArgumentPurpose
entityEntity where the sentence will be played.
szGroupNameGroup to select a sentence from.
volumeVolume.
attenuationAttenuation.
flagsSound flags.
pitchPitch.

Return value: Index of the sentence that was played, or -1 if no such sentence group exists.

PlaySentenceGroup

int PlaySentenceGroup(edict_t@ entity, int iGroupIndex, float volume, float attenuation, int flags, int pitch)

Plays a sentence from a sentence group. The sentence is randomly selected.

ArgumentPurpose
entityEntity where the sentence will be played.
iGroupIndexGroup index to select a sentence from.
volumeVolume.
attenuationAttenuation.
flagsSound flags.
pitchPitch.

Return value:

Index of the sentence that was played.

PlaySentenceGroupSequential

int PlaySentenceGroupSequential(edict_t@ entity, const string& in szGroupName, float volume, float attenuation, int flags, int pitch, int ipick, const bool bReset)

Plays a sentence group sequentially.

ArgumentPurpose
entityEntity where the sentence will be played.
szGroupNameName of the group to get the next sentence from.
volumeVolume.
attenuationAttenuation.
flagsSound flags.
pitchPitch.
ipickSentence to pick.
bResetIf true, the first sentence in the group will be used instead of ipick.

Return value:

Index of the sentence after the sentence that was played.

EmitAmbientSound

void EmitAmbientSound(edict_t@ entity, const Vector& in vecOrigin, const string& in szSample, float flVolume, float flAttenuation, int fFlags, int iPitch)

Plays an ambient sound. Ambient sounds use CHAN_STATIC.

ArgumentPurpose
pEntityEntity that the sound will be attached to. Sounds are played at the entity's origin.
vecOriginOrigin to play the sound at.
szSampleName of the sound to play.
flVolumeVolume to play the sound at. This value has a range of [0, 1], where 0 is muted, and 1 is maximum volume.
flAttenuationAttenuation. Values lie in the range [0, 4]. Higher values decrease the falloff range. See Attenuation.
iFlagsBit vector containing a number of flags. See Sound flags.
iPitchPitch to use. See Pitch.

PlayHitSound

float PlayHitSound(TraceResult& in tr, const Vector& in vecSrc, const Vector& in vecEnd, int iBulletType)

Plays a hit sound based on the trace result's hit target. Returns the volume at which the hit is being played.

ArgumentPurpose
trTraceline to use.
vecSrcStarting point for texture trace. Typically the location where a bullet is being fired from, or where a melee weapon starts its trace from.
vecEndEnd point for texture trace. Typically the location that defines the end position that a bullet traceline goes to, or where a melee weapon ends its trace at.
iBulletTypeSee the Bullet enum.

Clone this wiki locally