c7e624aee92d5e0ecc871a7ea306a0ab81821de2
[supertux.git] / src / audio / sound_manager.cpp
1 //  $Id: sound_manager.cpp 2334 2005-04-04 16:26:14Z grumbel $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program 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.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <cmath>
22 #include <cassert>
23 #include <stdexcept>
24 #include <sstream>
25
26 #include "audio/sound_manager.h"
27 #include "audio/musicref.h"
28 #include "app/globals.h"
29 #include "app/setup.h"
30 #include "moving_object.h"
31 #include "resources.h"
32
33 using namespace SuperTux;
34
35 SoundManager::SoundManager()
36   : current_music(0), m_music_enabled(true) , m_sound_enabled(true),
37     audio_device(false)
38 {
39 }
40
41 SoundManager::~SoundManager()
42 {
43   for(Sounds::iterator i = sounds.begin(); i != sounds.end(); ++i) {
44     Mix_FreeChunk(i->second);
45   }
46   sounds.clear();
47 }
48
49 void
50 SoundManager::play_sound(const std::string& name)
51 {
52   if(!audio_device || !m_sound_enabled)
53     return;
54   
55   Mix_Chunk* chunk = preload_sound(name);
56   if(chunk == 0) {
57     std::cerr << "Sound '" << name << "' not found.\n";
58     return;
59   }
60   Mix_PlayChannel(-1, chunk, 0);  
61 }
62
63 void
64 SoundManager::play_sound(const std::string& sound, const MovingObject* object,
65     const Vector& pos)
66 {
67   // TODO keep track of the object later and move the sound along with the
68   // object.
69   play_sound(sound, object->get_pos(), pos);
70 }
71
72 void
73 SoundManager::play_sound(const std::string& sound, const Vector& pos,
74     const Vector& pos2)
75 {
76   if(!audio_device || !m_sound_enabled)
77     return;
78
79   Mix_Chunk* chunk = preload_sound(sound);
80   if(chunk == 0) {
81     std::cerr << "Sound '" << sound << "' not found.\n";
82     return;                                              
83   }
84
85   // TODO make sure this formula is good
86   float distance 
87     = pos2.x- pos.x;
88   int loud = int(255.0/float(SCREEN_WIDTH*2) * fabsf(distance));
89   if(loud > 255)
90     return;
91
92   int chan = Mix_PlayChannel(-1, chunk, 0);
93   if(chan < 0)
94     return;                                  
95   Mix_SetDistance(chan, loud);
96
97   // very bad way to do this...
98   if(distance > 100)
99     Mix_SetPanning(chan, 230, 24);
100   else if(distance < -100)
101     Mix_SetPanning(chan, 24, 230);
102 }
103
104 MusicRef
105 SoundManager::load_music(const std::string& file)
106 {
107   if(!audio_device)
108     return MusicRef(0);
109
110   if(!exists_music(file)) {
111     std::stringstream msg;
112     msg << "Couldn't load musicfile '" << file << "': " << SDL_GetError();
113     throw std::runtime_error(msg.str());
114   }
115
116   std::map<std::string, MusicResource>::iterator i = musics.find(file);
117   assert(i != musics.end());
118   return MusicRef(& (i->second));
119 }
120
121 bool
122 SoundManager::exists_music(const std::string& file)
123 {
124   if(!audio_device)
125     return true;
126   
127   // song already loaded?
128   std::map<std::string, MusicResource>::iterator i = musics.find(file);
129   if(i != musics.end()) {
130     return true;                                      
131   }
132   
133   Mix_Music* song = Mix_LoadMUS(file.c_str());
134   if(song == 0)
135     return false;
136
137   // insert into music list
138   std::pair<std::map<std::string, MusicResource>::iterator, bool> result = 
139     musics.insert(
140         std::make_pair<std::string, MusicResource> (file, MusicResource()));
141   MusicResource& resource = result.first->second;
142   resource.manager = this;
143   resource.music = song;
144
145   return true;
146 }
147
148 void
149 SoundManager::free_music(MusicResource* )
150 {
151   // TODO free music, currently we can't do this since SDL_mixer seems to have
152   // some bugs if you load/free alot of mod files.  
153 }
154
155 void
156 SoundManager::play_music(const MusicRef& musicref, int loops)
157 {
158   if(!audio_device)
159     return;
160
161   if(musicref.music == 0 || current_music == musicref.music)
162     return;
163
164   if(current_music)
165     current_music->refcount--;
166   
167   current_music = musicref.music;
168   current_music->refcount++;
169   
170   if(m_music_enabled)
171     Mix_PlayMusic(current_music->music, loops);
172 }
173
174 void
175 SoundManager::halt_music()
176 {
177   if(!audio_device)
178     return;
179   
180   Mix_HaltMusic();
181   
182   if(current_music) {
183     current_music->refcount--;
184     if(current_music->refcount == 0)
185       free_music(current_music);
186     current_music = 0;
187   }
188 }
189
190 void
191 SoundManager::enable_music(bool enable)
192 {
193   if(!audio_device)
194     return;
195
196   if(enable == m_music_enabled)
197     return;
198   
199   m_music_enabled = enable;
200   if(m_music_enabled == false) {
201     Mix_HaltMusic();
202   } else {
203     if(current_music)
204       Mix_PlayMusic(current_music->music, -1);
205   }
206 }
207
208 void
209 SoundManager::enable_sound(bool enable)
210 {
211   if(!audio_device)
212     return;
213   
214   m_sound_enabled = enable;
215 }
216
217 SoundManager::MusicResource::~MusicResource()
218 {
219   // don't free music buggy SDL_Mixer crashs for some mod files
220   // Mix_FreeMusic(music);
221 }
222
223 Mix_Chunk* SoundManager::preload_sound(const std::string& name)
224 {
225   if(!audio_device)
226     return 0;
227
228   Sounds::iterator i = sounds.find(name);
229   if(i != sounds.end()) {
230     return i->second;
231   }
232
233   std::string filename = "sounds/";
234   filename += name;
235   filename += ".wav";
236   filename = get_resource_filename(filename);
237   
238   Mix_Chunk* chunk = Mix_LoadWAV(filename.c_str());
239   if(chunk != 0) {
240     sounds.insert(std::make_pair(name, chunk));
241   }
242
243   return chunk;
244 }
245