1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef INCLUDED_SOUNDMANAGER_H
19 #define INCLUDED_SOUNDMANAGER_H
21 #include "lib/config2.h"
25 #include "ISoundManager.h"
26 #include "data/SoundData.h"
27 #include "items/ISoundItem.h"
28 #include "scripting/SoundGroup.h"
30 #include "lib/external_libraries/openal.h"
31 #include "lib/file/vfs/vfs_path.h"
33 #include "ps/Profiler2.h"
34 #include "simulation2/system/Entity.h"
40 #define AL_CHECK CSoundManager::al_check(__func__, __LINE__)
44 /// Title of the column
46 ISoundItem
* SourceItem
;
49 typedef std::vector
<VfsPath
> PlayList
;
50 typedef std::vector
<ISoundItem
*> ItemsList
;
51 typedef std::map
<entity_id_t
, ISoundItem
*> ItemsMap
;
52 typedef std::map
<std::wstring
, CSoundGroup
*> SoundGroupMap
;
54 class CSoundManagerWorker
;
57 class CSoundManager
: public ISoundManager
59 NONCOPYABLE(CSoundManager
);
63 ALCcontext
* m_Context
;
65 ALSourceHolder
* m_ALSourceBuffer
;
66 ISoundItem
* m_CurrentTune
;
67 ISoundItem
* m_CurrentEnvirons
;
68 CSoundManagerWorker
* m_Worker
;
69 std::mutex m_DistressMutex
;
70 PlayList
* m_PlayListItems
;
71 SoundGroupMap m_SoundGroups
;
87 bool m_RunningPlaylist
;
88 bool m_PlayingPlaylist
;
89 bool m_LoopingPlaylist
;
92 long m_DistressErrCount
;
95 CStr8 m_SoundCardNames
;
96 CStr8 m_OpenALVersion
;
98 CSoundManager(ALCdevice
* device
);
99 virtual ~CSoundManager();
103 ISoundItem
* LoadItem(const VfsPath
& itemPath
);
104 ISoundItem
* ItemForData(CSoundData
* itemData
);
105 ISoundItem
* ItemForEntity(entity_id_t source
, CSoundData
* sndData
);
107 Status
ReloadChangedFiles(const VfsPath
& path
);
109 void ClearPlayListItems();
110 void StartPlayList(bool doLoop
);
111 void AddPlayListItem(const VfsPath
& itemPath
);
113 static void CreateSoundManager();
114 static void SetEnabled(bool doEnable
);
115 static Status
ReloadChangedFileCB(void* param
, const VfsPath
& path
);
117 static void CloseGame();
119 static void al_ReportError(ALenum err
, const char* caller
, int line
);
120 static void al_check(const char* caller
, int line
);
122 void SetMusicEnabled(bool isEnabled
);
123 void SetSoundEnabled(bool enabled
);
125 ALuint
GetALSource(ISoundItem
* anItem
);
126 void ReleaseALSource(ALuint theSource
);
127 ISoundItem
* ItemFromData(CSoundData
* itemData
);
129 ISoundItem
* ItemFromWAV(VfsPath
& fname
);
130 ISoundItem
* ItemFromOgg(VfsPath
& fname
);
132 ISoundItem
* GetSoundItem(unsigned long itemRow
);
133 unsigned long Count();
136 void SetMemoryUsage(long bufferSize
, int bufferCount
);
137 long GetBufferCount();
138 long GetBufferSize();
139 CStr8
GetSoundCardNames() const;
140 CStr8
GetOpenALVersion() const;
142 void PlayAsMusic(const VfsPath
& itemPath
, bool looping
);
143 void PlayAsAmbient(const VfsPath
& itemPath
, bool looping
);
144 void PlayAsUI(const VfsPath
& itemPath
, bool looping
);
145 void PlayAsGroup(const VfsPath
& groupPath
, const CVector3D
& sourcePos
, entity_id_t source
, bool ownedSound
);
147 void PlayGroupItem(ISoundItem
* anItem
, ALfloat groupGain
);
150 void SetDistressThroughShortage();
151 void SetDistressThroughError();
153 void Pause(bool pauseIt
);
154 void PauseMusic(bool pauseIt
);
155 void PauseAmbient(bool pauseIt
);
156 void PauseAction(bool pauseIt
);
158 void RunHardwareDetection();
160 void SetAmbientItem(ISoundItem
* anItem
);
162 void SetMasterGain(float gain
);
163 void SetMusicGain(float gain
);
164 void SetAmbientGain(float gain
);
165 void SetActionGain(float gain
);
166 void SetUIGain(float gain
);
171 void SetMusicItem(ISoundItem
* anItem
);
174 CSoundManager(CSoundManager
* UNUSED(other
)){};
177 #else // !CONFIG2_AUDIO
181 #endif // !CONFIG2_AUDIO
183 #endif // INCLUDED_SOUNDMANAGER_H