Civ backgrounds for minimap
[0ad.git] / source / soundmanager / SoundManager.h
blobd7af873a31f993302b98bed3534a848c74310772
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"
23 #if CONFIG2_AUDIO
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"
32 #include "ps/CStr.h"
33 #include "ps/Profiler2.h"
34 #include "simulation2/system/Entity.h"
36 #include <map>
37 #include <mutex>
38 #include <vector>
40 #define AL_CHECK CSoundManager::al_check(__func__, __LINE__)
42 struct ALSourceHolder
44 /// Title of the column
45 ALuint ALSource;
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);
61 protected:
63 ALCcontext* m_Context;
64 ALCdevice* m_Device;
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;
73 float m_Gain;
74 float m_MusicGain;
75 float m_AmbientGain;
76 float m_ActionGain;
77 float m_UIGain;
78 bool m_Enabled;
79 long m_BufferSize;
80 int m_BufferCount;
81 bool m_SoundEnabled;
82 bool m_MusicEnabled;
84 bool m_MusicPaused;
85 bool m_AmbientPaused;
86 bool m_ActionPaused;
87 bool m_RunningPlaylist;
88 bool m_PlayingPlaylist;
89 bool m_LoopingPlaylist;
91 long m_PlaylistGap;
92 long m_DistressErrCount;
93 long m_DistressTime;
95 CStr8 m_SoundCardNames;
96 CStr8 m_OpenALVersion;
97 public:
98 CSoundManager(ALCdevice* device);
99 virtual ~CSoundManager();
101 void StartWorker();
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();
134 void IdleTask();
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);
149 bool InDistress();
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);
168 protected:
169 void InitListener();
170 Status AlcInit();
171 void SetMusicItem(ISoundItem* anItem);
173 private:
174 CSoundManager(CSoundManager* UNUSED(other)){};
177 #else // !CONFIG2_AUDIO
179 #define AL_CHECK
181 #endif // !CONFIG2_AUDIO
183 #endif // INCLUDED_SOUNDMANAGER_H