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