X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Faudio%2Fsound_manager.cpp;h=cc10db7ce0265a42c25d58abedb0c710d8148f3b;hb=b51f8f420a99f380d9389bdd7a020b9a33a72820;hp=a05ca019cccc506dc0836c59e8eb2d05e0acb877;hpb=07ddaed2a657e4d2a3d038fed223fc5827159caf;p=supertux.git diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index a05ca019c..cc10db7ce 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -16,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include #include "sound_manager.hpp" @@ -23,22 +24,25 @@ #include #include #include +#include #include "sound_file.hpp" #include "sound_source.hpp" +#include "openal_sound_source.hpp" #include "stream_sound_source.hpp" +#include "dummy_sound_source.hpp" #include "log.hpp" +#include "timer.hpp" SoundManager* sound_manager = 0; SoundManager::SoundManager() : device(0), context(0), sound_enabled(false), music_source(0), - music_enabled(true) + music_enabled(false) { try { device = alcOpenDevice(0); - if(device == 0) { - print_openal_version(); + if (device == NULL) { throw std::runtime_error("Couldn't open audio device."); } @@ -50,12 +54,16 @@ SoundManager::SoundManager() check_al_error("Audio error after init: "); sound_enabled = true; + music_enabled = true; } catch(std::exception& e) { - device = 0; - context = 0; - log_warning << "Couldn't initialize audio device:" << e.what() << std::endl; + if(context != NULL) + alcDestroyContext(context); + context = NULL; + if(device != NULL) + alcCloseDevice(device); + device = NULL; + log_warning << "Couldn't initialize audio device: " << e.what() << std::endl; print_openal_version(); - throw e; } } @@ -72,10 +80,10 @@ SoundManager::~SoundManager() alDeleteBuffers(1, &buffer); } - if(context != 0) { + if(context != NULL) { alcDestroyContext(context); } - if(device != 0) { + if(device != NULL) { alcCloseDevice(device); } } @@ -107,7 +115,7 @@ SoundSource* SoundManager::create_sound_source(const std::string& filename) { if(!sound_enabled) - return 0; + return create_dummy_sound_source(); ALuint buffer; @@ -116,56 +124,97 @@ SoundManager::create_sound_source(const std::string& filename) if(i != buffers.end()) { buffer = i->second; } else { - // Load sound file - std::auto_ptr file (load_sound_file(filename)); - - if(file->size < 100000) { - buffer = load_file_into_buffer(file.get()); - buffers.insert(std::make_pair(filename, buffer)); - } else { - StreamSoundSource* source = new StreamSoundSource(); - source->set_sound_file(file.release()); - return source; + try { + // Load sound file + std::auto_ptr file (load_sound_file(filename)); + + if(file->size < 100000) { + buffer = load_file_into_buffer(file.get()); + buffers.insert(std::make_pair(filename, buffer)); + } else { + StreamSoundSource* source = new StreamSoundSource(); + source->set_sound_file(file.release()); + return source; + } + } catch(std::exception& e) { + log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl; + return create_dummy_sound_source(); } } - SoundSource* source = new SoundSource(); + OpenALSoundSource* source = new OpenALSoundSource(); alSourcei(source->source, AL_BUFFER, buffer); return source; } void +SoundManager::preload(const std::string& filename) +{ + if(!sound_enabled) + return; + + SoundBuffers::iterator i = buffers.find(filename); + // already loaded? + if(i != buffers.end()) + return; + + std::auto_ptr file (load_sound_file(filename)); + // only keep small files + if(file->size >= 100000) + return; + + ALuint buffer = load_file_into_buffer(file.get()); + buffers.insert(std::make_pair(filename, buffer)); +} + +void SoundManager::play(const std::string& filename, const Vector& pos) { + if(!sound_enabled) + return; + try { - SoundSource* source = create_sound_source(filename); - if(source == 0) - return; + std::auto_ptr source + (static_cast (create_sound_source(filename))); + if(pos == Vector(-1, -1)) { - alSourcef(source->source, AL_ROLLOFF_FACTOR, 0); + source->set_rollof_factor(0); } else { source->set_position(pos); } source->play(); - sources.push_back(source); + sources.push_back(source.release()); } catch(std::exception& e) { log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl; } } void +SoundManager::manage_source(SoundSource* source) +{ + assert(source != NULL); + + OpenALSoundSource* openal_source = dynamic_cast (source); + if(openal_source != NULL) { + sources.push_back(openal_source); + } +} + +void SoundManager::enable_sound(bool enable) { - if(device == 0) + if(device == NULL) return; + sound_enabled = enable; } void SoundManager::enable_music(bool enable) { - if(device == 0) + if(device == NULL) return; + music_enabled = enable; if(music_enabled) { play_music(current_music); @@ -186,7 +235,7 @@ SoundManager::stop_music(float fadetime) music_source->set_fading(StreamSoundSource::FadingOff, fadetime); } else { delete music_source; - music_source = 0; + music_source = NULL; } current_music = ""; } @@ -202,7 +251,7 @@ SoundManager::play_music(const std::string& filename, bool fade) if(filename == "") { delete music_source; - music_source = 0; + music_source = NULL; return; } @@ -244,16 +293,15 @@ SoundManager::set_listener_velocity(const Vector& vel) void SoundManager::update() { - static Uint32 lastticks = 0; + static float lasttime = real_time; - Uint32 current_ticks = SDL_GetTicks(); - if(current_ticks - lastticks < 300) + if(real_time - lasttime < 0.3) return; - lastticks = current_ticks; + lasttime = real_time; // update and check for finished sound sources for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) { - SoundSource* source = *i; + OpenALSoundSource* source = *i; source->update(); @@ -268,9 +316,12 @@ SoundManager::update() if(music_source) { music_source->update(); } - - alcProcessContext(context); - check_alc_error("Error while processing audio context: "); + + if (context) + { + alcProcessContext(context); + check_alc_error("Error while processing audio context: "); + } } ALenum