1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3 
4 This file is part of Aquaria.
5 
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #ifndef SOUNDMANAGER_H
22 #define SOUNDMANAGER_H
23 
24 #include <stdlib.h>
25 #include <string>
26 #include <list>
27 #include <queue>
28 #include <set>
29 #include "Vector.h"
30 
31 #define BBGE_BUILD_FMODEX
32 
33 
34 #define BBGE_AUDIO_NOCHANNEL NULL
35 
36 const int BBGE_AUDIO_LOOPINFINITE	= -1;
37 const int BBGE_AUDIO_LOOPNONE		= 0;
38 
39 namespace SoundCore
40 {
41 	typedef void *Buffer;
42 }
43 
44 enum SoundEffectType
45 {
46 	SFX_NONE		= -1,
47 	SFX_FLANGE		= 0,
48 	SFX_MAX
49 };
50 
51 enum SoundLoopType
52 {
53 	SLT_NONE	= -1,
54 	SLT_NORMAL	= 0,
55 	SLT_LOOP	= 0,
56 	SLT_OFF		= 1,
57 	SLT_BI		= 2
58 };
59 
60 enum SoundFadeType
61 {
62 	SFT_NONE	= -1,
63 	SFT_IN		= 0,
64 	SFT_CROSS	= 1,
65 	SFT_OUT		= 2
66 };
67 
68 enum SoundConditionType
69 {
70 	SCT_NONE			= -1,
71 	SCT_NORMAL			= 0,
72 	SCT_ISNOTPLAYING	= 1
73 };
74 
75 enum SoundVoiceType
76 {
77 	SVT_QUEUE			= 0,
78 	SVT_INTERRUPT		= 1
79 };
80 
81 enum SoundLoadType
82 {
83 	SFXLOAD_CACHE		= 0,
84 	SFXLOAD_LOCAL		= 1
85 };
86 
87 struct PlaySfx
88 {
PlaySfxPlaySfx89 	PlaySfx() : priority(0.5), handle(0), vol(1), fade(SFT_NONE),
90 		time(0), freq(1), loops(0),
91 		maxdist(0), x(0), y(0), relative(true), positional(false) {}
92 
93 	std::string name;
94 	intptr_t handle;
95 	float vol;
96 	float time;
97 	float freq;
98 	int loops;
99 	float priority;
100 	float maxdist; // distance gain attenuation. if 0: use default value, -1: don't attenuate at all
101 	SoundFadeType fade;
102 	float x, y;
103 	bool relative; // relative to listener?
104 	bool positional; // if true, this indicates that we want positional sound (stereo will be downmixed to mono to make OpenAL happy)
105 };
106 
107 class SoundHolder; // defined below
108 
109 class SoundManager
110 {
111 public:
112 	SoundManager(const std::string &defaultDevice="");
113 	~SoundManager();
114 
115 	void stopAll();
116 
117 	void setChannelVolume(void *chan, float v);
118 
119 	void loadSoundCache(const std::string &spath="sfx/cache/", const std::string &ftype=".ogg", void progressCallback()=NULL);
120 
121 	void stopAllSfx();
122 
123 	void clearLocalSounds();
124 
setVoicePath2(const std::string & voicePath2)125 	void setVoicePath2(const std::string &voicePath2) { this->voicePath2 = voicePath2; }
126 
127 	SoundCore::Buffer loadLocalSound(const std::string &sound);
128 	SoundCore::Buffer loadSoundIntoBank(const std::string &filename, const std::string &path, const std::string &format, SoundLoadType = SFXLOAD_CACHE);
129 	SoundCore::Buffer getBuffer(const std::string &name);
130 
131 	void *playSfx(const PlaySfx &play);
132 	void *playSfx(const std::string &name, float vol=1);
133 
134 	bool playMusic(const std::string &name, SoundLoopType=SLT_NORMAL, SoundFadeType sft=SFT_NONE, float trans=0, SoundConditionType sct=SCT_NORMAL);
135 	bool playVoice(const std::string &name, SoundVoiceType=SVT_QUEUE, float vmod=-1);
136 
137 	float getMusicFader();
138 	float getVoxFader();
139 
140 	void setListenerPos(float x, float y);
141 	void setSoundPos(void *channel, float x, float y);
142 	void setSoundRelative(void *channel, bool relative);
143 
144 	std::string getVolumeString();
145 
146 	void toggleEffectMusic(SoundEffectType effect, bool on);
147 
148 	void clearFadingSfx();
149 
150 	void setMusicSpeed(float speed);
151 	void setModSpeed(float speed);
152 
153 	bool isPlayingMusic(const std::string &name);
154 
155 	void setSfxChannelsVolume(float v);
156 
157 	bool isPaused();
158 
159 	void stopVoice();
160 	void stopAllVoice();
161 	void stopMusic();
162 	void stopSfx(void *channel);
163 
164 	void fadeSfx(void *channel, SoundFadeType sft=SFT_OUT, float t=0.8);
165 
166 	void fadeMusic(SoundFadeType sft=SFT_OUT, float t=1);
167 
168 	bool isPlayingMusic();
169 	bool isPlayingVoice();
170 
171 	void onVoiceEnded();
172 
173 	void setVoiceFader(float v);
174 
175 	void setMusicFader(float v, float t=0);
176 
177 	void setMusicVolume(float v);
178 	void setSfxVolume(float v);
179 	void setVoiceVolume(float v);
180 
181 	float getSfxVol();
182 
183 	void updateChannelVolume(void *ch, float v=1);
184 
185 	void pause();
186 	void resume();
187 
188 	/*
189 	void setMusVol(float v, float t=0);
190 	void setSfxVol(float v, float t=0);
191 	void setVoxVol(float v, float t=0);
192 
193 	void setMusMul(float v, float t=0);
194 	void setSfxMul(float v, float t=0);
195 	void setVoxMul(float v, float t=0);
196 
197 	float getTotalSfxVol();
198 	float getTotalMusVol();
199 	float getTotalVoxVol();
200 	*/
201 
202 
203 	float getVoiceTime();
204 
205 	void update(float dt);
206 
207 	bool enabled;
208 
209 	bool checkError();
210 
211 	void error(const std::string &errMsg);
212 
213 	EventPtr event_playVoice, event_stopVoice;
214 
215 	std::string lastVoice, lastMusic;
216 
217 	typedef std::list<std::string> LocalSounds;
218 	LocalSounds localSounds;
219 	void setOverrideVoiceFader(float v);
220 
221 	std::string audioPath2;
222 
223 	void getStats(int *curAlloc, int *maxAlloc);
224 
225 	std::string reverbKeyword;
226 private:
227 
228 	std::string voicePath2;
229 
230 	float overrideVoiceFader;
231 	// sound voice music
232 	InterpolatedVector voxVol;
233 	InterpolatedVector musVol;
234 	float sfxVol;
235 	float voiceFader, sfxFader;
236 
237 	std::queue<std::string> voxQueue;
238 
239 	void (*loadProgressCallback)();
240 };
241 
242 class SoundHolder
243 {
244 	friend class SoundManager;
245 public:
246 	void updateSoundPosition(float x, float y);
247 	void stopAllSounds();
248 	void unlinkSound(void *channel);
249 	void linkSound(void *channel);
250 	void unlinkAllSounds();
251 
252 protected:
253 	virtual ~SoundHolder();
254 
255 private:
256 	std::set<void*> activeSounds;
257 };
258 
259 
260 extern SoundManager *sound;
261 
262 
263 
264 
265 
266 #endif
267