Pyrogenesis  trunk
SoundManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #ifndef INCLUDED_SOUNDMANAGER_H
19 #define INCLUDED_SOUNDMANAGER_H
20 
21 #include "lib/config2.h"
22 
23 #if CONFIG2_AUDIO
24 
25 #include "ISoundManager.h"
26 #include "data/SoundData.h"
27 #include "items/ISoundItem.h"
28 #include "scripting/SoundGroup.h"
29 
31 #include "lib/file/vfs/vfs_path.h"
32 #include "ps/CStr.h"
33 #include "ps/Profiler2.h"
35 
36 #include <map>
37 #include <mutex>
38 #include <vector>
39 
40 #define AL_CHECK CSoundManager::al_check(__func__, __LINE__)
41 
43 {
44  /// Title of the column
45  ALuint ALSource;
47 };
48 
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;
53 
55 
56 
58 {
60 
61 protected:
62 
63  ALCcontext* m_Context;
64  ALCdevice* m_Device;
69  std::mutex m_DistressMutex;
72 
73  float m_Gain;
74  float m_MusicGain;
76  float m_ActionGain;
77  float m_UIGain;
78  bool m_Enabled;
83 
90 
94 
97 public:
98  CSoundManager(ALCdevice* device);
99  virtual ~CSoundManager();
100 
101  void StartWorker();
102 
103  ISoundItem* LoadItem(const VfsPath& itemPath);
104  ISoundItem* ItemForData(CSoundData* itemData);
105  ISoundItem* ItemForEntity(entity_id_t source, CSoundData* sndData);
106 
107  Status ReloadChangedFiles(const VfsPath& path);
108 
109  void ClearPlayListItems();
110  void StartPlayList(bool doLoop);
111  void AddPlayListItem(const VfsPath& itemPath);
112 
113  static void CreateSoundManager();
114  static void SetEnabled(bool doEnable);
115  static Status ReloadChangedFileCB(void* param, const VfsPath& path);
116 
117  static void CloseGame();
118 
119  static void al_ReportError(ALenum err, const char* caller, int line);
120  static void al_check(const char* caller, int line);
121 
122  void SetMusicEnabled(bool isEnabled);
123  void SetSoundEnabled(bool enabled);
124 
125  ALuint GetALSource(ISoundItem* anItem);
126  void ReleaseALSource(ALuint theSource);
127  ISoundItem* ItemFromData(CSoundData* itemData);
128 
129  ISoundItem* ItemFromWAV(VfsPath& fname);
130  ISoundItem* ItemFromOgg(VfsPath& fname);
131 
132  ISoundItem* GetSoundItem(unsigned long itemRow);
133  unsigned long Count();
134  void IdleTask();
135 
136  void SetMemoryUsage(long bufferSize, int bufferCount);
137  long GetBufferCount();
138  long GetBufferSize();
139  CStr8 GetSoundCardNames() const;
140  CStr8 GetOpenALVersion() const;
141 
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);
146 
147  void PlayGroupItem(ISoundItem* anItem, ALfloat groupGain);
148 
149  bool InDistress();
150  void SetDistressThroughShortage();
151  void SetDistressThroughError();
152 
153  void Pause(bool pauseIt);
154  void PauseMusic(bool pauseIt);
155  void PauseAmbient(bool pauseIt);
156  void PauseAction(bool pauseIt);
157 
158  void RunHardwareDetection();
159 
160  void SetAmbientItem(ISoundItem* anItem);
161 
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);
167 
168 protected:
169  void InitListener();
170  Status AlcInit();
171  void SetMusicItem(ISoundItem* anItem);
172 
173 private:
175 };
176 
177 #else // !CONFIG2_AUDIO
178 
179 #define AL_CHECK
180 
181 #endif // !CONFIG2_AUDIO
182 
183 #endif // INCLUDED_SOUNDMANAGER_H
184 
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
void SetAmbientGain(float gain)
Definition: JSInterface_Sound.cpp:100
ALuint ALSource
Title of the column.
Definition: SoundManager.h:45
bool m_LoopingPlaylist
Definition: SoundManager.h:89
bool m_Enabled
Definition: SoundManager.h:78
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
Definition: SoundManager.cpp:44
float m_UIGain
Definition: SoundManager.h:77
float m_AmbientGain
Definition: SoundManager.h:75
SoundGroupMap m_SoundGroups
Definition: SoundManager.h:71
std::vector< ISoundItem * > ItemsList
Definition: SoundManager.h:50
bool m_SoundEnabled
Definition: SoundManager.h:81
Definition: SoundManager.h:57
bool m_MusicPaused
Definition: SoundManager.h:84
std::mutex m_DistressMutex
Definition: SoundManager.h:69
ALCdevice * m_Device
Definition: SoundManager.h:64
ALCcontext * m_Context
Definition: SoundManager.h:63
PlayList * m_PlayListItems
Definition: SoundManager.h:70
CStr8 m_OpenALVersion
Definition: SoundManager.h:96
Definition: ISoundItem.h:29
Definition: Vector3D.h:30
std::map< entity_id_t, ISoundItem * > ItemsMap
Definition: SoundManager.h:51
long m_DistressErrCount
Definition: SoundManager.h:92
ISoundItem * m_CurrentEnvirons
Definition: SoundManager.h:67
void SetMasterGain(float gain)
Definition: JSInterface_Sound.cpp:88
CSoundManager(CSoundManager *other)
Definition: SoundManager.h:174
long m_BufferSize
Definition: SoundManager.h:79
CSoundManagerWorker * m_Worker
Definition: SoundManager.h:68
Status ReloadChangedFiles()
poll for directory change notifications and reload all affected files.
Definition: Filesystem.cpp:76
bool m_RunningPlaylist
Definition: SoundManager.h:87
Definition: SoundData.h:34
Definition: path.h:79
void SetActionGain(float gain)
Definition: JSInterface_Sound.cpp:106
New profiler (complementing the older CProfileManager)
Definition: SoundManager.h:42
int m_BufferCount
Definition: SoundManager.h:80
ISoundItem * SourceItem
Definition: SoundManager.h:46
std::vector< VfsPath > PlayList
Definition: SoundManager.h:49
bool m_MusicEnabled
Definition: SoundManager.h:82
long m_DistressTime
Definition: SoundManager.h:93
i64 Status
Error handling system.
Definition: status.h:169
void SetMusicGain(float gain)
Definition: JSInterface_Sound.cpp:94
void RunHardwareDetection()
Runs hardware-detection script to adjust default config settings and/or emit warnings depending on th...
Definition: HWDetect.cpp:66
Definition: ISoundManager.h:28
ISoundItem * m_CurrentTune
Definition: SoundManager.h:66
static Status ReloadChangedFileCB(void *param, const VfsPath &path)
Definition: ObjectManager.cpp:44
float m_MusicGain
Definition: SoundManager.h:74
long m_PlaylistGap
Definition: SoundManager.h:91
bool m_PlayingPlaylist
Definition: SoundManager.h:88
std::map< std::wstring, CSoundGroup * > SoundGroupMap
Definition: SoundManager.h:52
float m_ActionGain
Definition: SoundManager.h:76
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
bool m_ActionPaused
Definition: SoundManager.h:86
bool m_AmbientPaused
Definition: SoundManager.h:85
ALSourceHolder * m_ALSourceBuffer
Definition: SoundManager.h:65
float m_Gain
Definition: SoundManager.h:73
CStr8 m_SoundCardNames
Definition: SoundManager.h:95
void SetUIGain(float gain)
Definition: JSInterface_Sound.cpp:112