1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef VPHYSICS_SOUND_H
#define VPHYSICS_SOUND_H
#ifdef _WIN32
#pragma once
#endif
#include "SoundEmitterSystem/isoundemittersystembase.h"
namespace physicssound
{
struct impactsound_t
{
void *pGameData;
int entityIndex;
int soundChannel;
float volume;
float impactSpeed;
unsigned short surfaceProps;
unsigned short surfacePropsHit;
Vector origin;
};
// UNDONE: Use a sorted container and sort by volume/distance?
struct soundlist_t
{
CUtlVector<impactsound_t> elements;
impactsound_t &GetElement(int index) { return elements[index]; }
impactsound_t &AddElement() { return elements[elements.AddToTail()]; }
int Count() { return elements.Count(); }
void RemoveAll() { elements.RemoveAll(); }
};
void PlayImpactSounds( soundlist_t &list )
{
for ( int i = list.Count()-1; i >= 0; --i )
{
impactsound_t &sound = list.GetElement(i);
const surfacedata_t *psurf = physprops->GetSurfaceData( sound.surfaceProps );
if ( psurf->sounds.impactHard )
{
const surfacedata_t *pHit = physprops->GetSurfaceData( sound.surfacePropsHit );
unsigned short soundName = psurf->sounds.impactHard;
if ( pHit && psurf->sounds.impactSoft )
{
if ( pHit->audio.hardnessFactor < psurf->audio.hardThreshold ||
(psurf->audio.hardVelocityThreshold > 0 && psurf->audio.hardVelocityThreshold > sound.impactSpeed) )
{
soundName = psurf->sounds.impactSoft;
}
}
const char *pSound = physprops->GetString( soundName );
CSoundParameters params;
if ( !CBaseEntity::GetParametersForSound( pSound, params, NULL ) )
break;
if ( sound.volume > 1 )
sound.volume = 1;
CPASAttenuationFilter filter( sound.origin, params.soundlevel );
// JAY: If this entity gets deleted, the sound comes out at the world origin
// this sounds bad! Play on ent 0 for now.
EmitSound_t ep;
ep.m_nChannel = sound.soundChannel;
ep.m_pSoundName = params.soundname;
ep.m_flVolume = params.volume * sound.volume;
ep.m_SoundLevel = params.soundlevel;
ep.m_nPitch = params.pitch;
ep.m_pOrigin = &sound.origin;
CBaseEntity::EmitSound( filter, 0 /*sound.entityIndex*/, ep );
}
}
list.RemoveAll();
}
void AddImpactSound( soundlist_t &list, void *pGameData, int entityIndex, int soundChannel, IPhysicsObject *pObject, int surfaceProps, int surfacePropsHit, float volume, float impactSpeed )
{
impactSpeed += 1e-4;
for ( int i = list.Count()-1; i >= 0; --i )
{
impactsound_t &sound = list.GetElement(i);
// UNDONE: Compare entity or channel somehow?
// UNDONE: Doing one slot per entity is too noisy. So now we use one slot per material
// heuristic - after 4 impacts sounds in one frame, start merging everything
if ( surfaceProps == sound.surfaceProps || list.Count() > 4 )
{
// UNDONE: Store instance volume separate from aggregate volume and compare that?
if ( volume > sound.volume )
{
pObject->GetPosition( &sound.origin, NULL );
sound.pGameData = pGameData;
sound.entityIndex = entityIndex;
sound.soundChannel = soundChannel;
sound.surfacePropsHit = surfacePropsHit;
}
sound.volume += volume;
sound.impactSpeed = MAX(impactSpeed,sound.impactSpeed);
return;
}
}
impactsound_t &sound = list.AddElement();
sound.pGameData = pGameData;
sound.entityIndex = entityIndex;
sound.soundChannel = soundChannel;
pObject->GetPosition( &sound.origin, NULL );
sound.surfaceProps = surfaceProps;
sound.surfacePropsHit = surfacePropsHit;
sound.volume = volume;
sound.impactSpeed = impactSpeed;
}
struct breaksound_t
{
Vector origin;
int surfacePropsBreak;
};
void AddBreakSound( CUtlVector<breaksound_t> &list, const Vector &origin, unsigned short surfaceProps )
{
const surfacedata_t *psurf = physprops->GetSurfaceData( surfaceProps );
if ( !psurf->sounds.breakSound )
return;
for ( int i = list.Count()-1; i >= 0; --i )
{
breaksound_t &sound = list.Element(i);
// Allow 3 break sounds before you start merging anything.
if ( list.Count() > 2 && surfaceProps == sound.surfacePropsBreak )
{
sound.origin = (sound.origin + origin) * 0.5f;
return;
}
}
breaksound_t sound;
sound.origin = origin;
sound.surfacePropsBreak = surfaceProps;
list.AddToTail(sound);
}
void PlayBreakSounds( CUtlVector<breaksound_t> &list )
{
for ( int i = list.Count()-1; i >= 0; --i )
{
breaksound_t &sound = list.Element(i);
const surfacedata_t *psurf = physprops->GetSurfaceData( sound.surfacePropsBreak );
const char *pSound = physprops->GetString( psurf->sounds.breakSound );
CSoundParameters params;
if ( !CBaseEntity::GetParametersForSound( pSound, params, NULL ) )
return;
// Play from the world, because the entity is breaking, so it'll be destroyed soon
CPASAttenuationFilter filter( sound.origin, params.soundlevel );
EmitSound_t ep;
ep.m_nChannel = CHAN_STATIC;
ep.m_pSoundName = params.soundname;
ep.m_flVolume = params.volume;
ep.m_SoundLevel = params.soundlevel;
ep.m_nPitch = params.pitch;
ep.m_pOrigin = &sound.origin;
CBaseEntity::EmitSound( filter, 0 /*sound.entityIndex*/, ep );
}
list.RemoveAll();
}
};
#endif // VPHYSICS_SOUND_H
|