-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathsoundsystem.cpp
More file actions
306 lines (246 loc) · 7.28 KB
/
soundsystem.cpp
File metadata and controls
306 lines (246 loc) · 7.28 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: DLL interface for low-level sound utilities
//
//===========================================================================//
#include "soundsystem/isoundsystem.h"
#include "filesystem.h"
#include "tier1/strtools.h"
#include "tier1/convar.h"
#include "mathlib/mathlib.h"
#include "soundsystem/snd_device.h"
#include "datacache/idatacache.h"
#include "soundchars.h"
#include "tier1/utldict.h"
#include "snd_wave_source.h"
#include "snd_dev_wave.h"
#include "tier2/tier2.h"
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// External interfaces
//-----------------------------------------------------------------------------
IAudioDevice *g_pAudioDevice = NULL;
//-----------------------------------------------------------------------------
// Globals
//-----------------------------------------------------------------------------
int g_nSoundFrameCount = 0;
//-----------------------------------------------------------------------------
// Purpose: DLL interface for low-level sound utilities
//-----------------------------------------------------------------------------
class CSoundSystem : public CTier2AppSystem< ISoundSystem >
{
typedef CTier2AppSystem< ISoundSystem > BaseClass;
public:
// Inherited from IAppSystem
virtual bool Connect( CreateInterfaceFn factory );
virtual void Disconnect();
virtual void *QueryInterface( const char *pInterfaceName );
virtual InitReturnVal_t Init();
virtual void Shutdown();
void Update( float dt );
void Flush( void );
CAudioSource *FindOrAddSound( const char *filename );
CAudioSource *LoadSound( const char *wavfile );
void PlaySound( CAudioSource *source, float volume, CAudioMixer **ppMixer );
bool IsSoundPlaying( CAudioMixer *pMixer );
CAudioMixer *FindMixer( CAudioSource *source );
void StopAll( void );
void StopSound( CAudioMixer *mixer );
void GetAudioDevices(CUtlVector< audio_device_description_t >& deviceListOut) const;
private:
struct CSoundFile
{
char filename[ 512 ];
CAudioSource *source;
long filetime;
};
IAudioDevice *m_pAudioDevice;
float m_flElapsedTime;
CUtlVector < CSoundFile > m_ActiveSounds;
};
//-----------------------------------------------------------------------------
// Singleton interface
//-----------------------------------------------------------------------------
static CSoundSystem s_SoundSystem;
EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CSoundSystem, ISoundSystem, SOUNDSYSTEM_INTERFACE_VERSION, s_SoundSystem );
//-----------------------------------------------------------------------------
// Connect, disconnect
//-----------------------------------------------------------------------------
bool CSoundSystem::Connect( CreateInterfaceFn factory )
{
if ( !BaseClass::Connect( factory ) )
return false;
g_pDataCache = (IDataCache*)factory( DATACACHE_INTERFACE_VERSION, NULL );
g_pSoundSystem = this;
return (g_pFullFileSystem != NULL) && (g_pDataCache != NULL);
}
void CSoundSystem::Disconnect()
{
g_pSoundSystem = NULL;
g_pDataCache = NULL;
BaseClass::Disconnect();
}
//-----------------------------------------------------------------------------
// Query interface
//-----------------------------------------------------------------------------
void *CSoundSystem::QueryInterface( const char *pInterfaceName )
{
if (!Q_strncmp( pInterfaceName, SOUNDSYSTEM_INTERFACE_VERSION, Q_strlen(SOUNDSYSTEM_INTERFACE_VERSION) + 1))
return (ISoundSystem*)this;
return NULL;
}
//-----------------------------------------------------------------------------
// Init, shutdown
//-----------------------------------------------------------------------------
InitReturnVal_t CSoundSystem::Init()
{
InitReturnVal_t nRetVal = BaseClass::Init();
if ( nRetVal != INIT_OK )
return nRetVal;
MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f );
m_flElapsedTime = 0.0f;
m_pAudioDevice = Audio_CreateWaveDevice();
if ( !m_pAudioDevice->Init() )
return INIT_FAILED;
return INIT_OK;
}
void CSoundSystem::Shutdown()
{
Msg( "Removing %i sounds\n", m_ActiveSounds.Count() );
for ( int i = 0 ; i < m_ActiveSounds.Count(); i++ )
{
CSoundFile *p = &m_ActiveSounds[ i ];
Msg( "Removing sound: %s\n", p->filename );
delete p->source;
}
m_ActiveSounds.RemoveAll();
if ( m_pAudioDevice )
{
m_pAudioDevice->Shutdown();
delete m_pAudioDevice;
}
BaseClass::Shutdown();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CAudioSource *CSoundSystem::FindOrAddSound( const char *filename )
{
CSoundFile *s;
int i;
for ( i = 0; i < m_ActiveSounds.Count(); i++ )
{
s = &m_ActiveSounds[ i ];
Assert( s );
if ( !stricmp( s->filename, filename ) )
{
long filetime = g_pFullFileSystem->GetFileTime( filename );
if ( filetime != s->filetime )
{
Msg( "Reloading sound %s\n", filename );
delete s->source;
s->source = LoadSound( filename );
s->filetime = filetime;
}
return s->source;
}
}
i = m_ActiveSounds.AddToTail();
s = &m_ActiveSounds[ i ];
strcpy( s->filename, filename );
s->source = LoadSound( filename );
s->filetime = g_pFullFileSystem->GetFileTime( filename );
return s->source;
}
CAudioSource *CSoundSystem::LoadSound( const char *wavfile )
{
if ( !m_pAudioDevice )
return NULL;
CAudioSource *wave = AudioSource_Create( wavfile );
return wave;
}
void CSoundSystem::PlaySound( CAudioSource *source, float volume, CAudioMixer **ppMixer )
{
if ( ppMixer )
{
*ppMixer = NULL;
}
if ( m_pAudioDevice )
{
CAudioMixer *mixer = source->CreateMixer();
if ( ppMixer )
{
*ppMixer = mixer;
}
mixer->SetVolume( volume );
m_pAudioDevice->AddSource( mixer );
}
}
void CSoundSystem::Update( float dt )
{
// closecaptionmanager->PreProcess( g_nSoundFrameCount );
if ( m_pAudioDevice )
{
m_pAudioDevice->Update( m_flElapsedTime );
}
// closecaptionmanager->PostProcess( g_nSoundFrameCount, dt );
m_flElapsedTime += dt;
g_nSoundFrameCount++;
}
void CSoundSystem::Flush( void )
{
if ( m_pAudioDevice )
{
m_pAudioDevice->Flush();
}
}
void CSoundSystem::StopAll( void )
{
if ( m_pAudioDevice )
{
m_pAudioDevice->StopSounds();
}
}
void CSoundSystem::StopSound( CAudioMixer *mixer )
{
int idx = m_pAudioDevice->FindSourceIndex( mixer );
if ( idx != -1 )
{
m_pAudioDevice->FreeChannel( idx );
}
}
static eSubSystems_t GetDefaultAudioSubsystem()
{
eSubSystems_t nSubsystem = AUDIO_SUBSYSTEM_XAUDIO;
#if IS_WINDOWS_PC
if (CommandLine()->CheckParm("-directsound"))
{
nSubsystem = AUDIO_SUBSYSTEM_DSOUND;
}
#endif
return nSubsystem;
}
void CSoundSystem::GetAudioDevices(CUtlVector< audio_device_description_t >& deviceListOut) const
{
CAudioDeviceList list;
eSubSystems_t nSubsystem = GetDefaultAudioSubsystem();
list.BuildDeviceList(nSubsystem);
deviceListOut = list.m_list;
}
bool CSoundSystem::IsSoundPlaying( CAudioMixer *pMixer )
{
if ( !m_pAudioDevice || !pMixer )
return false;
//
int index = m_pAudioDevice->FindSourceIndex( pMixer );
if ( index != -1 )
return true;
return false;
}
CAudioMixer *CSoundSystem::FindMixer( CAudioSource *source )
{
if ( !m_pAudioDevice )
return NULL;
return m_pAudioDevice->GetMixerForSource( source );
}