X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Faudio%2Fsound_manager.cpp;h=9f2b36f62ae183634910d1a1c19e9389829c6e03;hb=e85c039cd8cea18876cc52fe718296c640ec1b36;hp=7d285ffb3e598fee2119a9a5d899eb1803142e10;hpb=fea3446f05e1e7673607b835c269d3e8d1929ab3;p=supertux.git diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index 7d285ffb3..9f2b36f62 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -1,12 +1,10 @@ -// $Id$ -// // SuperTux // Copyright (C) 2006 Matthias Braun // -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,42 +12,31 @@ // GNU General Public License for more details. // // 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 +// along with this program. If not, see . -#include "sound_manager.hpp" +#include "audio/sound_manager.hpp" +#include +#include #include -#include #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" - -#ifndef DEBUG - /** Older openal versions often miss this function and it isn't that vital for - * supertux... - */ -#ifdef alcGetString -#undef alcGetString -#endif -#define alcGetString(x,y) "" -#endif - -SoundManager* sound_manager = 0; - -SoundManager::SoundManager() - : device(0), context(0), sound_enabled(false), music_source(0), - music_enabled(false) +#include "audio/dummy_sound_source.hpp" +#include "audio/sound_file.hpp" +#include "audio/stream_sound_source.hpp" +#include "util/log.hpp" + +SoundManager::SoundManager() : + device(0), + context(0), + sound_enabled(false), + buffers(), + sources(), + update_list(), + music_source(0), + music_enabled(false), + current_music() { try { device = alcOpenDevice(0); @@ -67,12 +54,14 @@ SoundManager::SoundManager() sound_enabled = true; music_enabled = true; } catch(std::exception& e) { - if(context != NULL) + if(context != NULL) { alcDestroyContext(context); - context = NULL; - if(device != NULL) + context = NULL; + } + if(device != NULL) { alcCloseDevice(device); - device = NULL; + device = NULL; + } log_warning << "Couldn't initialize audio device: " << e.what() << std::endl; print_openal_version(); } @@ -93,9 +82,11 @@ SoundManager::~SoundManager() if(context != NULL) { alcDestroyContext(context); + context = NULL; } if(device != NULL) { alcCloseDevice(device); + device = NULL; } } @@ -110,8 +101,8 @@ SoundManager::load_file_into_buffer(SoundFile* file) try { file->read(samples, file->size); alBufferData(buffer, format, samples, - static_cast (file->size), - static_cast (file->rate)); + static_cast (file->size), + static_cast (file->rate)); check_al_error("Couldn't fill audio buffer: "); } catch(...) { delete[] samples; @@ -125,10 +116,9 @@ SoundManager::load_file_into_buffer(SoundFile* file) OpenALSoundSource* SoundManager::intern_create_sound_source(const std::string& filename) { - if(!sound_enabled) - throw std::runtime_error("sound disabled"); + assert(sound_enabled); - std::auto_ptr source (new OpenALSoundSource()); + std::unique_ptr source (new OpenALSoundSource()); ALuint buffer; @@ -138,7 +128,7 @@ SoundManager::intern_create_sound_source(const std::string& filename) buffer = i->second; } else { // Load sound file - std::auto_ptr file (load_sound_file(filename)); + std::unique_ptr file (load_sound_file(filename)); if(file->size < 100000) { buffer = load_file_into_buffer(file.get()); @@ -148,6 +138,8 @@ SoundManager::intern_create_sound_source(const std::string& filename) source->set_sound_file(file.release()); return source; } + + log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl; } alSourcei(source->source, AL_BUFFER, buffer); @@ -178,14 +170,17 @@ SoundManager::preload(const std::string& filename) // already loaded? if(i != buffers.end()) return; + try { + std::unique_ptr file (load_sound_file(filename)); + // only keep small files + if(file->size >= 100000) + 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)); + ALuint buffer = load_file_into_buffer(file.get()); + buffers.insert(std::make_pair(filename, buffer)); + } catch(std::exception& e) { + log_warning << "Error while preloading sound file: " << e.what() << std::endl; + } } void @@ -195,11 +190,11 @@ SoundManager::play(const std::string& filename, const Vector& pos) return; try { - std::auto_ptr source - (intern_create_sound_source(filename)); + std::unique_ptr source + (intern_create_sound_source(filename)); - if(pos == Vector(-1, -1)) { - source->set_rollof_factor(0); + if(pos.x < 0 || pos.y < 0) { + source->set_relative(true); } else { source->set_position(pos); } @@ -232,7 +227,7 @@ void SoundManager::remove_from_update( StreamSoundSource* sss ){ if( sss != NULL ){ StreamSoundSources::iterator i = update_list.begin(); - while( i != update_list.end() ){ + while( i != update_list.end() ){ if( *i == sss ){ i = update_list.erase(i); } else { @@ -263,7 +258,7 @@ SoundManager::enable_music(bool enable) } else { if(music_source) { delete music_source; - music_source = 0; + music_source = NULL; } } } @@ -273,7 +268,7 @@ SoundManager::stop_music(float fadetime) { if(fadetime > 0) { if(music_source - && music_source->get_fade_state() != StreamSoundSource::FadingOff) + && music_source->get_fade_state() != StreamSoundSource::FadingOff) music_source->set_fading(StreamSoundSource::FadingOff, fadetime); } else { delete music_source; @@ -298,10 +293,10 @@ SoundManager::play_music(const std::string& filename, bool fade) } try { - std::auto_ptr newmusic (new StreamSoundSource()); - alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0); + std::unique_ptr newmusic (new StreamSoundSource()); newmusic->set_sound_file(load_sound_file(filename)); newmusic->set_looping(true); + newmusic->set_relative(true); if(fade) newmusic->set_fading(StreamSoundSource::FadingOn, .5f); newmusic->play(); @@ -310,6 +305,8 @@ SoundManager::play_music(const std::string& filename, bool fade) music_source = newmusic.release(); } catch(std::exception& e) { log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl; + // When this happens, previous music continued playing, stop it, just in case. + stop_music(0); } } @@ -428,3 +425,5 @@ SoundManager::check_al_error(const char* message) throw std::runtime_error(msg.str()); } } + +/* EOF */