1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Erwin 'vaiursch' Herrsche |
---|
24 | * Kevin Young |
---|
25 | * Reto Grieder |
---|
26 | * Co-authors: |
---|
27 | * ... |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef _SoundManager_H__ |
---|
31 | #define _SoundManager_H__ |
---|
32 | |
---|
33 | #include "OrxonoxPrereqs.h" |
---|
34 | |
---|
35 | #include <list> |
---|
36 | #include <map> |
---|
37 | #include <string> |
---|
38 | #include <boost/shared_ptr.hpp> |
---|
39 | |
---|
40 | #include "util/Singleton.h" |
---|
41 | #include "core/OrxonoxClass.h" |
---|
42 | #include "core/SmartPtr.h" |
---|
43 | |
---|
44 | // forward declaration |
---|
45 | typedef int ALenum; |
---|
46 | |
---|
47 | // tolua_begin |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | // forward declaration |
---|
51 | class SoundBuffer; |
---|
52 | |
---|
53 | //! Enum for the sound type. |
---|
54 | namespace SoundType |
---|
55 | { |
---|
56 | enum Value |
---|
57 | { |
---|
58 | All = 0, |
---|
59 | Music = 1, |
---|
60 | Effects = 2 |
---|
61 | }; |
---|
62 | } |
---|
63 | |
---|
64 | //! The SoundManager class manages the OpenAL device, context and listener position. |
---|
65 | class _OrxonoxExport SoundManager |
---|
66 | // tolua_end |
---|
67 | : public Singleton<SoundManager>, public OrxonoxClass |
---|
68 | { // tolua_export |
---|
69 | friend class Singleton<SoundManager>; |
---|
70 | |
---|
71 | public: |
---|
72 | SoundManager(); |
---|
73 | ~SoundManager(); |
---|
74 | |
---|
75 | void preUpdate(const Clock& time); |
---|
76 | void setConfigValues(); |
---|
77 | |
---|
78 | // tolua_begin |
---|
79 | static SoundManager& getInstance() { return Singleton<SoundManager>::getInstance(); } |
---|
80 | |
---|
81 | std::string getDeviceName(unsigned int index) const |
---|
82 | { return index < this->deviceNames_.size() ? this->deviceNames_[index] : std::string(); } |
---|
83 | // tolua_end |
---|
84 | |
---|
85 | void setListenerPosition(const Vector3& position); |
---|
86 | void setListenerOrientation(const Quaternion& orientation); |
---|
87 | |
---|
88 | void registerAmbientSound(AmbientSound* newAmbient); |
---|
89 | void unregisterAmbientSound(AmbientSound* oldAmbient); |
---|
90 | void pauseAmbientSound(AmbientSound* ambient); |
---|
91 | |
---|
92 | // tolua_begin |
---|
93 | void setVolume(float vol, SoundType::Value type); |
---|
94 | float getVolume(SoundType::Value type); |
---|
95 | float getRealVolume(SoundType::Value type); |
---|
96 | |
---|
97 | void toggleMute(SoundType::Value type); |
---|
98 | bool getMute(SoundType::Value type); |
---|
99 | // tolua_end |
---|
100 | |
---|
101 | shared_ptr<SoundBuffer> getSoundBuffer(const std::string& filename); |
---|
102 | void releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer); |
---|
103 | |
---|
104 | ALuint getSoundSource(); |
---|
105 | void releaseSoundSource(ALuint source); |
---|
106 | |
---|
107 | static std::string getALErrorString(ALenum error); |
---|
108 | |
---|
109 | private: |
---|
110 | void processCrossFading(float dt); |
---|
111 | void fadeIn(const SmartPtr<AmbientSound>& sound); |
---|
112 | void fadeOut(const SmartPtr<AmbientSound>& sound); |
---|
113 | |
---|
114 | void checkFadeStepValidity(); |
---|
115 | |
---|
116 | void checkVolumeValidity(SoundType::Value type); |
---|
117 | void checkSoundVolumeValidity() { this->checkVolumeValidity(SoundType::All); } |
---|
118 | void checkAmbientVolumeValidity() { this->checkVolumeValidity(SoundType::Music); } |
---|
119 | void checkEffectsVolumeValidity() { this->checkVolumeValidity(SoundType::Effects); } |
---|
120 | void updateVolume(SoundType::Value type); |
---|
121 | |
---|
122 | // OpenAL device/context related |
---|
123 | std::vector<std::string> deviceNames_; |
---|
124 | ALCdevice* device_; |
---|
125 | ALCcontext* context_; |
---|
126 | |
---|
127 | // Ambient sound related |
---|
128 | typedef std::list<std::pair<AmbientSound*, bool> > AmbientList; |
---|
129 | AmbientList ambientSounds_; |
---|
130 | //! Absolute change per second (0.1 means 10% of the nominal volume) for cross fading |
---|
131 | float crossFadeStep_; |
---|
132 | std::list<SmartPtr<AmbientSound> > fadeInList_; |
---|
133 | std::list<SmartPtr<AmbientSound> > fadeOutList_; |
---|
134 | |
---|
135 | // Volume related |
---|
136 | float volume_[3]; |
---|
137 | float mute_[3]; |
---|
138 | |
---|
139 | // Sound buffer related |
---|
140 | static const unsigned int maxEffectsPoolSize_s = 40 * 1024 * 1024; |
---|
141 | unsigned int effectsPoolSize_; |
---|
142 | typedef std::list<shared_ptr<SoundBuffer> > EffectsPoolList; |
---|
143 | EffectsPoolList effectsPool_; |
---|
144 | typedef std::map<std::string, shared_ptr<SoundBuffer> > SoundBufferMap; |
---|
145 | SoundBufferMap soundBuffers_; |
---|
146 | |
---|
147 | // Sound source related |
---|
148 | unsigned int maxSources_; |
---|
149 | std::vector<ALuint> soundSources_; |
---|
150 | |
---|
151 | static SoundManager* singletonPtr_s; |
---|
152 | }; // tolua_export |
---|
153 | } // tolua_export |
---|
154 | |
---|
155 | #endif /* _SoundManager_H__ */ |
---|