summaryrefslogtreecommitdiff
path: root/hammer/soundsystem.h
blob: 230e01cb457bf349f979f20a5f25beea4dd5e6d5 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Sound management functions. Exposes a list of available sounds.
//
// $NoKeywords: $
//=============================================================================//

#ifndef SOUNDSSYSTEM_H
#define SOUNDSSYSTEM_H

#ifdef _WIN32
#pragma once
#endif

#include "utlvector.h"

//-----------------------------------------------------------------------------
// Contains lists of all sounds	available for use
//-----------------------------------------------------------------------------
enum SoundType_t
{
	SOUND_TYPE_RAW = 0,
	SOUND_TYPE_GAMESOUND,
	SOUND_TYPE_SCENE,		// vcd file

	SOUND_TYPE_COUNT,
};

class CSoundSystem
{
public:
	CSoundSystem(void);
	virtual ~CSoundSystem(void);

	bool Initialize( );
	void ShutDown(void);

	// Build the list of sounds
	bool BuildSoundList( SoundType_t type );

	// Sound list iteration
	int SoundCount( SoundType_t type );
	const char *SoundName( SoundType_t type, int nIndex );
	const char *SoundFile( SoundType_t type, int nIndex );
	const char *SoundSourceFile( SoundType_t type, int nIndex );

	// Search through all the sounds for the specified one.
	bool FindSoundByName( const char *pFilename, SoundType_t *type, int *nIndex );

	// Plays a sound
	bool Play( SoundType_t type, int nIndex );
	bool PlayScene( const char *pFileName );	// Play the first sound in the specified scene.
	
	// Stops any playing sound.
	void StopSound();

	// Opens the source file associated with a sound
	void OpenSource( SoundType_t type, int nIndex );

private:
	struct SoundInfo_t
	{
		char *m_pSoundName;
		char *m_pSoundFile;
		const char *m_pSourceFile;
	};

	struct StringCache_t
	{
		enum
		{
			STRING_CACHE_SIZE = 128 * 1024,
		};

		char m_pBuf[STRING_CACHE_SIZE];
		int m_nTailIndex;	// Next address to fill
		StringCache_t *m_pNext;
	};

	struct SoundList_t
	{
		CUtlVector< SoundInfo_t >	m_Sounds;
		StringCache_t	*m_pStrings;
	};

private:
	typedef bool (CSoundSystem::*pDirCallbackFn)( const char *pDirectoryName );


	// Allocate, deallocate a string cache
	StringCache_t *CreateStringCache( StringCache_t* pPrevious );
	void DestroyStringCache( StringCache_t *pCache );

	// Adds a string to the string cache
	char *AddStringToCache( SoundType_t type, const char *pString );

	// Adds a sound to a sound list
	void AddSoundToList( SoundType_t type, const char *pSoundName, const char *pActualFile, const char *pSourceFile );

	// Cleans up the sound list
	void CleanupSoundList( SoundType_t type );

	// Goes into all subdirectories and calls the callback for each one..
	bool RecurseIntoDirectories( char const* pDirectoryName, pDirCallbackFn fn );

	// Add all sounds that lie within a single directory recursively
	bool ProcessDirectory_RawFileList( char const* pDirectoryName );
	bool ProcessDirectory_SceneFileList( char const* pDirectoryName );

	// Add all sounds that lie within a single directory
	void BuildFileListInDirectory( char const* pDirectoryName, const char *pExt, SoundType_t soundType );

	// Gamesounds may have macros embedded in them
	void AddGameSoundToList( const char *pGameSound, char const *pFileName, const char *pSourceFile );

	// Load all game sounds from a particular file 
	void AddGameSoundsFromFile( const char *pFileName );

	// Populate the list of game sounds
	bool BuildGameSoundList();

private:
	SoundList_t m_SoundList[SOUND_TYPE_COUNT];	
};


//-----------------------------------------------------------------------------
// Sound list iteration
//-----------------------------------------------------------------------------
inline int CSoundSystem::SoundCount( SoundType_t type )
{
	return m_SoundList[type].m_Sounds.Count();
}

inline const char *CSoundSystem::SoundName( SoundType_t type, int nIndex )
{
	return m_SoundList[type].m_Sounds[nIndex].m_pSoundName;
}

inline const char *CSoundSystem::SoundFile( SoundType_t type, int nIndex )
{
	return m_SoundList[type].m_Sounds[nIndex].m_pSoundFile;
}

inline const char *CSoundSystem::SoundSourceFile( SoundType_t type, int nIndex )
{
	return m_SoundList[type].m_Sounds[nIndex].m_pSourceFile;
}

extern CSoundSystem g_Sounds;


#endif // SOUNDSYSTEM_H