X-Git-Url: https://git.octo.it/?p=supertux.git;a=blobdiff_plain;f=src%2Faudio%2Fsound_manager.cpp;h=ace96c1290d18acc3e92e28fe4a261edac86d38a;hp=b3eb73137fea96323b6bde601e34421506b7d569;hb=8e52a5b000d732e96b1cc461163c4778b434dc27;hpb=561d285b2ea0fc084f54ef62b1f9c9d65a4877c8 diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index b3eb73137..ace96c129 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,32 +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" - -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(), + music_enabled(false), + current_music() { try { device = alcOpenDevice(0); @@ -57,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(); } @@ -70,11 +69,8 @@ SoundManager::SoundManager() SoundManager::~SoundManager() { - delete music_source; - - for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) { - delete *i; - } + music_source.reset(); + sources.clear(); for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) { ALuint buffer = i->second; @@ -83,48 +79,37 @@ SoundManager::~SoundManager() if(context != NULL) { alcDestroyContext(context); + context = NULL; } if(device != NULL) { alcCloseDevice(device); + device = NULL; } } ALuint -SoundManager::load_file_into_buffer(SoundFile* file) +SoundManager::load_file_into_buffer(SoundFile& file) { ALenum format = get_sample_format(file); ALuint buffer; alGenBuffers(1, &buffer); check_al_error("Couldn't create audio buffer: "); - char* samples = new char[file->size]; - try { - file->read(samples, file->size); - alBufferData(buffer, format, samples, - static_cast (file->size), - static_cast (file->rate)); - check_al_error("Couldn't fill audio buffer: "); - } catch(...) { - delete[] samples; - throw; - } - delete[] samples; + std::unique_ptr samples(new char[file.size]); + file.read(samples.get(), file.size); + alBufferData(buffer, format, samples.get(), + static_cast(file.size), + static_cast(file.rate)); + check_al_error("Couldn't fill audio buffer: "); return buffer; } -SoundSource* -SoundManager::create_sound_source(const std::string& filename) +std::unique_ptr +SoundManager::intern_create_sound_source(const std::string& filename) { - if(!sound_enabled) - return create_dummy_sound_source(); + assert(sound_enabled); - std::auto_ptr source; - try { - source.reset(new OpenALSoundSource()); - } catch(std::exception& e) { - log_warning << "Couldn't create audio source: " << e.what() << std::endl; - return create_dummy_sound_source(); - } + std::unique_ptr source(new OpenALSoundSource); ALuint buffer; @@ -133,26 +118,37 @@ SoundManager::create_sound_source(const std::string& filename) if(i != buffers.end()) { buffer = i->second; } else { - try { - // Load sound file - std::auto_ptr file (load_sound_file(filename)); + // Load sound file + std::unique_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(); + if(file->size < 100000) { + buffer = load_file_into_buffer(*file); + buffers.insert(std::make_pair(filename, buffer)); + } else { + std::unique_ptr source_(new StreamSoundSource); + source_->set_sound_file(std::move(file)); + return std::move(source_); } + + log_debug << "Uncached sound \"" << filename << "\" requested to be played" << std::endl; } alSourcei(source->source, AL_BUFFER, buffer); - return source.release(); + return std::move(source); +} + +std::unique_ptr +SoundManager::create_sound_source(const std::string& filename) +{ + if(!sound_enabled) + return create_dummy_sound_source(); + + try { + return intern_create_sound_source(filename); + } catch(std::exception &e) { + log_warning << "Couldn't create audio source: " << e.what() << std::endl; + return create_dummy_sound_source(); + } } void @@ -165,14 +161,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); + buffers.insert(std::make_pair(filename, buffer)); + } catch(std::exception& e) { + log_warning << "Error while preloading sound file: " << e.what() << std::endl; + } } void @@ -182,48 +181,51 @@ SoundManager::play(const std::string& filename, const Vector& pos) return; try { - std::auto_ptr source - (static_cast (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); } source->play(); - sources.push_back(source.release()); + sources.push_back(std::move(source)); } catch(std::exception& e) { log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl; } } void -SoundManager::manage_source(SoundSource* source) +SoundManager::manage_source(std::unique_ptr source) { - assert(source != NULL); - - OpenALSoundSource* openal_source = dynamic_cast (source); - if(openal_source != NULL) { - sources.push_back(openal_source); + assert(source); + if (dynamic_cast(source.get())) + { + std::unique_ptr openal_source(dynamic_cast(source.release())); + sources.push_back(std::move(openal_source)); } } void -SoundManager::register_for_update( StreamSoundSource* sss ){ - if( sss != NULL ){ - update_list.push_back( sss ); +SoundManager::register_for_update(StreamSoundSource* sss) +{ + if (sss) + { + update_list.push_back(sss); } } void -SoundManager::remove_from_update( StreamSoundSource* sss ){ - if( sss != NULL ){ +SoundManager::remove_from_update(StreamSoundSource* sss) +{ + if (sss) + { 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 { - i++; + ++i; } } } @@ -249,8 +251,7 @@ SoundManager::enable_music(bool enable) play_music(current_music); } else { if(music_source) { - delete music_source; - music_source = 0; + music_source.reset(); } } } @@ -260,11 +261,10 @@ 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; - music_source = NULL; + music_source.reset(); } current_music = ""; } @@ -279,24 +279,48 @@ SoundManager::play_music(const std::string& filename, bool fade) return; if(filename == "") { - delete music_source; - music_source = NULL; + music_source.reset(); return; } 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(); - delete music_source; - music_source = newmusic.release(); + music_source = std::move(newmusic); } 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); + } +} + +void +SoundManager::pause_music(float fadetime) +{ + if(fadetime > 0) { + if(music_source + && music_source->get_fade_state() != StreamSoundSource::FadingPause) + music_source->set_fading(StreamSoundSource::FadingPause, fadetime); + } else { + music_source->pause(); + } +} + +void +SoundManager::resume_music(float fadetime) +{ + if(fadetime > 0) { + if(music_source + && music_source->get_fade_state() != StreamSoundSource::FadingResume) + music_source->set_fading(StreamSoundSource::FadingResume, fadetime); + } else { + music_source->resume(); } } @@ -331,12 +355,11 @@ SoundManager::update() // update and check for finished sound sources for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) { - OpenALSoundSource* source = *i; + auto& source = *i; source->update(); if(!source->playing()) { - delete source; i = sources.erase(i); } else { ++i; @@ -357,25 +380,25 @@ SoundManager::update() StreamSoundSources::iterator s = update_list.begin(); while( s != update_list.end() ){ (*s)->update(); - s++; + ++s; } } ALenum -SoundManager::get_sample_format(SoundFile* file) +SoundManager::get_sample_format(const SoundFile& file) { - if(file->channels == 2) { - if(file->bits_per_sample == 16) { + if(file.channels == 2) { + if(file.bits_per_sample == 16) { return AL_FORMAT_STEREO16; - } else if(file->bits_per_sample == 8) { + } else if(file.bits_per_sample == 8) { return AL_FORMAT_STEREO8; } else { throw std::runtime_error("Only 16 and 8 bit samples supported"); } - } else if(file->channels == 1) { - if(file->bits_per_sample == 16) { + } else if(file.channels == 1) { + if(file.bits_per_sample == 16) { return AL_FORMAT_MONO16; - } else if(file->bits_per_sample == 8) { + } else if(file.bits_per_sample == 8) { return AL_FORMAT_MONO8; } else { throw std::runtime_error("Only 16 and 8 bit samples supported"); @@ -415,3 +438,5 @@ SoundManager::check_al_error(const char* message) throw std::runtime_error(msg.str()); } } + +/* EOF */