1 // $Id: sound_manager.cpp 2334 2005-04-04 16:26:14Z grumbel $
3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de
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.
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.
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.
27 #include "audio/sound_manager.h"
29 #include "audio/musicref.h"
30 #include "moving_object.h"
31 #include "resources.h"
33 SoundManager::SoundManager()
34 : current_music(0), m_music_enabled(true) , m_sound_enabled(true),
39 SoundManager::~SoundManager()
41 for(Sounds::iterator i = sounds.begin(); i != sounds.end(); ++i) {
42 Mix_FreeChunk(i->second);
48 SoundManager::play_sound(const std::string& name,int loops)
50 if(!audio_device || !m_sound_enabled)
53 Mix_Chunk* chunk = preload_sound(name);
55 std::cerr << "Sound '" << name << "' not found.\n";
58 return Mix_PlayChannel(-1, chunk, loops);
63 SoundManager::play_sound(const std::string& sound, const MovingObject* object,
66 // TODO keep track of the object later and move the sound along with the
68 play_sound(sound, object->get_pos(), pos);
72 SoundManager::play_sound(const std::string& sound, const Vector& pos,
75 if(!audio_device || !m_sound_enabled)
78 Mix_Chunk* chunk = preload_sound(sound);
80 std::cerr << "Sound '" << sound << "' not found.\n";
84 // TODO make sure this formula is good
87 int loud = int(255.0/float(1600) * fabsf(distance));
91 int chan = Mix_PlayChannel(-1, chunk, 0);
94 Mix_SetDistance(chan, loud);
96 // very bad way to do this...
98 Mix_SetPanning(chan, 230, 24);
99 else if(distance < -100)
100 Mix_SetPanning(chan, 24, 230);
105 ok, i was stupid. We won't need this now.
107 // Register a sound effect function - basti_
110 SoundManager::register_effect(int channel,Mix_EffectFunc_t f,
111 Mix_EffectDone_t d,void * arg) {
113 if(!audio_device || !m_sound_enabled)
115 Mix_RegisterEffect(channel,f,d,arg);
118 // Adjust the Volume of a channel "on line". Needs sizeof(float) static data.
121 SoundManager::volume_adjust(int chan, void *stream, int len, void *udata) {
123 typedef signed short int atype;
125 ((float *)udata)[1]=((float *)udata)[1]*0.95+
126 (((float *)udata)[0]-
127 ((float *)udata)[1])*0.05; // decay towards [0] - declick
128 float vol=((float*)udata)[1];
130 for (int i=0;i<len/2;i++)
131 ((atype *)stream)[i]=
132 ((atype)(((atype *)stream)[i]*vol));
133 // FIXME: This should be audio-type dependant - how to do this correctly?
135 chan=0; // -Werror sucks
141 SoundManager::load_music(const std::string& file)
146 if(!exists_music(file)) {
147 std::stringstream msg;
148 msg << "Couldn't load musicfile '" << file << "': " << SDL_GetError();
149 throw std::runtime_error(msg.str());
152 std::map<std::string, MusicResource>::iterator i = musics.find(file);
153 assert(i != musics.end());
154 return MusicRef(& (i->second));
158 SoundManager::exists_music(const std::string& file)
163 // song already loaded?
164 std::map<std::string, MusicResource>::iterator i = musics.find(file);
165 if(i != musics.end()) {
169 Mix_Music* song = Mix_LoadMUS(file.c_str());
173 // insert into music list
174 std::pair<std::map<std::string, MusicResource>::iterator, bool> result =
176 std::make_pair<std::string, MusicResource> (file, MusicResource()));
177 MusicResource& resource = result.first->second;
178 resource.manager = this;
179 resource.music = song;
185 SoundManager::free_music(MusicResource* )
187 // TODO free music, currently we can't do this since SDL_mixer seems to have
188 // some bugs if you load/free alot of mod files.
192 SoundManager::play_music(const MusicRef& musicref, int loops)
197 if(musicref.music == 0 || current_music == musicref.music)
201 current_music->refcount--;
203 current_music = musicref.music;
204 current_music->refcount++;
207 Mix_PlayMusic(current_music->music, loops);
211 SoundManager::halt_music()
219 current_music->refcount--;
220 if(current_music->refcount == 0)
221 free_music(current_music);
227 SoundManager::enable_music(bool enable)
232 if(enable == m_music_enabled)
235 m_music_enabled = enable;
236 if(m_music_enabled == false) {
240 Mix_PlayMusic(current_music->music, -1);
245 SoundManager::enable_sound(bool enable)
250 m_sound_enabled = enable;
253 SoundManager::MusicResource::~MusicResource()
255 // don't free music buggy SDL_Mixer crashs for some mod files
256 // Mix_FreeMusic(music);
259 Mix_Chunk* SoundManager::preload_sound(const std::string& name)
264 Sounds::iterator i = sounds.find(name);
265 if(i != sounds.end()) {
269 std::string filename = "sounds/";
272 filename = get_resource_filename(filename);
274 Mix_Chunk* chunk = Mix_LoadWAV(filename.c_str());
276 sounds.insert(std::make_pair(name, chunk));