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