1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef _SOUND_H_
4 #define _SOUND_H_
5 
6 #include "System/Sound/ISound.h"
7 
8 #include <set>
9 #include <string>
10 #include <map>
11 #include <vector>
12 #include <boost/ptr_container/ptr_vector.hpp>
13 #include <boost/thread/recursive_mutex.hpp>
14 #include <AL/alc.h>
15 
16 #include "System/float3.h"
17 
18 #include "SoundItem.h"
19 
20 class CSoundSource;
21 class SoundBuffer;
22 class SoundItem;
23 
24 namespace boost {
25 	class thread;
26 }
27 
28 
29 /// Default sound system implementation (OpenAL)
30 class CSound : public ISound
31 {
32 public:
33 	CSound();
34 	virtual ~CSound();
35 
36 	virtual bool HasSoundItem(const std::string& name) const;
37 	virtual size_t GetSoundId(const std::string& name);
38 	SoundItem* GetSoundItem(size_t id) const;
39 
40 	virtual CSoundSource* GetNextBestSource(bool lock = true);
41 
42 	virtual void UpdateListener(const float3& campos, const float3& camdir, const float3& camup, float lastFrameTime);
43 	virtual void NewFrame();
44 
45 	/// @see ConfigHandler::ConfigNotifyCallback
46 	virtual void ConfigNotify(const std::string& key, const std::string& value);
47 	virtual void PitchAdjust(const float newPitch);
48 
49 	virtual bool Mute();
50 	virtual bool IsMuted() const;
51 
52 	virtual void Iconified(bool state);
53 
54 	virtual void PrintDebugInfo();
55 	virtual bool LoadSoundDefsImpl(const std::string& fileName);
GetListenerPos()56 	const float3& GetListenerPos() const {
57 		return myPos;
58 	}
59 
60 private:
61 	typedef std::map<std::string, std::string> soundItemDef;
62 	typedef std::map<std::string, soundItemDef> soundItemDefMap;
63 
64 private:
65 	void StartThread(int maxSounds);
66 	void Update();
67 	int GetMaxMonoSources(ALCdevice* device, int maxSounds);
68 
69 	size_t MakeItemFromDef(const soundItemDef& itemDef);
70 
71 	size_t LoadSoundBuffer(const std::string& filename);
72 
73 private:
74 	float masterVolume;
75 	bool mute;
76 	/// we do not play if minimized / iconified
77 	bool appIsIconified;
78 	bool pitchAdjust;
79 
80 	typedef std::map<std::string, size_t> soundMapT;
81 	typedef std::vector<SoundItem*> soundVecT;
82 	soundMapT soundMap;
83 	soundVecT sounds;
84 
85 	/// unscaled
86 	float3 myPos;
87 	float3 prevVelocity;
88 
89 	typedef boost::ptr_vector<CSoundSource> sourceVecT;
90 	sourceVecT sources;
91 
92 	soundItemDef defaultItem;
93 	soundItemDefMap soundItemDefs;
94 
95 	boost::thread* soundThread;
96 
97 	volatile bool soundThreadQuit;
98 };
99 
100 #endif // _SOUND_H_
101