From b88dc6c0a778a019bf0b1f28c2cc9d624d9bbdbd Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Wed, 13 Aug 2014 01:11:48 +0200 Subject: [PATCH] Use std::unique_ptr<> throughout the audio system --- src/audio/dummy_sound_source.cpp | 12 ++++- src/audio/dummy_sound_source.hpp | 4 +- src/audio/openal_sound_source.cpp | 1 + src/audio/openal_sound_source.hpp | 4 ++ src/audio/sound_file.cpp | 10 ++-- src/audio/sound_file.hpp | 7 ++- src/audio/sound_manager.cpp | 109 +++++++++++++++++--------------------- src/audio/sound_manager.hpp | 15 +++--- src/audio/sound_source.hpp | 8 ++- src/audio/stream_sound_source.cpp | 18 +++---- src/audio/stream_sound_source.hpp | 4 +- src/badguy/bomb.cpp | 2 +- src/badguy/dart.cpp | 2 +- src/badguy/flame.cpp | 2 +- src/badguy/goldbomb.cpp | 2 +- src/badguy/haywire.cpp | 4 +- src/badguy/treewillowisp.cpp | 2 +- src/badguy/willowisp.cpp | 2 +- src/object/ambient_sound.cpp | 8 ++- src/object/ambient_sound.hpp | 2 +- 20 files changed, 114 insertions(+), 104 deletions(-) diff --git a/src/audio/dummy_sound_source.cpp b/src/audio/dummy_sound_source.cpp index 178baa71f..97aec965d 100644 --- a/src/audio/dummy_sound_source.cpp +++ b/src/audio/dummy_sound_source.cpp @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#include "audio/dummy_sound_source.hpp" + +#include + #include "audio/sound_source.hpp" class DummySoundSource : public SoundSource @@ -70,11 +74,15 @@ public: private: bool is_playing; + +private: + DummySoundSource(const DummySoundSource&) = delete; + DummySoundSource& operator=(const DummySoundSource&) = delete; }; -SoundSource* create_dummy_sound_source() +std::unique_ptr create_dummy_sound_source() { - return new DummySoundSource(); + return std::unique_ptr(new DummySoundSource); } /* EOF */ diff --git a/src/audio/dummy_sound_source.hpp b/src/audio/dummy_sound_source.hpp index 5162f1db5..12dd5de51 100644 --- a/src/audio/dummy_sound_source.hpp +++ b/src/audio/dummy_sound_source.hpp @@ -17,9 +17,11 @@ #ifndef HEADER_SUPERTUX_AUDIO_DUMMY_SOUND_SOURCE_HPP #define HEADER_SUPERTUX_AUDIO_DUMMY_SOUND_SOURCE_HPP +#include + class SoundSource; -SoundSource* create_dummy_sound_source(); +std::unique_ptr create_dummy_sound_source(); #endif diff --git a/src/audio/openal_sound_source.cpp b/src/audio/openal_sound_source.cpp index 52cf09013..b30b44521 100644 --- a/src/audio/openal_sound_source.cpp +++ b/src/audio/openal_sound_source.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see . #include "audio/openal_sound_source.hpp" + #include "audio/sound_manager.hpp" OpenALSoundSource::OpenALSoundSource() : diff --git a/src/audio/openal_sound_source.hpp b/src/audio/openal_sound_source.hpp index 42e127960..8725d59b9 100644 --- a/src/audio/openal_sound_source.hpp +++ b/src/audio/openal_sound_source.hpp @@ -45,6 +45,10 @@ protected: friend class SoundManager; ALuint source; + +private: + OpenALSoundSource(const OpenALSoundSource&) = delete; + OpenALSoundSource& operator=(const OpenALSoundSource&) = delete; }; #endif diff --git a/src/audio/sound_file.cpp b/src/audio/sound_file.cpp index 576ab85ca..ebe7ea3e1 100644 --- a/src/audio/sound_file.cpp +++ b/src/audio/sound_file.cpp @@ -32,7 +32,7 @@ #include "util/file_system.hpp" #include "util/log.hpp" -SoundFile* load_music_file(const std::string& filename) +std::unique_ptr load_music_file(const std::string& filename) { lisp::Parser parser(false); const lisp::Lisp* root = parser.parse(filename); @@ -62,10 +62,10 @@ SoundFile* load_music_file(const std::string& filename) throw SoundError(msg.str()); } - return new OggSoundFile(file, loop_begin, loop_at); + return std::unique_ptr(new OggSoundFile(file, loop_begin, loop_at)); } -SoundFile* load_sound_file(const std::string& filename) +std::unique_ptr load_sound_file(const std::string& filename) { if(filename.length() > 6 && filename.compare(filename.length()-6, 6, ".music") == 0) { @@ -85,9 +85,9 @@ SoundFile* load_sound_file(const std::string& filename) throw SoundError("Couldn't read magic, file too short"); PHYSFS_seek(file, 0); if(strncmp(magic, "RIFF", 4) == 0) - return new WavSoundFile(file); + return std::unique_ptr(new WavSoundFile(file)); else if(strncmp(magic, "OggS", 4) == 0) - return new OggSoundFile(file, 0, -1); + return std::unique_ptr(new OggSoundFile(file, 0, -1)); else throw SoundError("Unknown file format"); } catch(std::exception& e) { diff --git a/src/audio/sound_file.hpp b/src/audio/sound_file.hpp index 2fc1d3466..5a84cfff3 100644 --- a/src/audio/sound_file.hpp +++ b/src/audio/sound_file.hpp @@ -18,6 +18,7 @@ #define HEADER_SUPERTUX_AUDIO_SOUND_FILE_HPP #include +#include class SoundFile { @@ -40,9 +41,13 @@ public: int bits_per_sample; /// size in bytes size_t size; + +private: + SoundFile(const SoundFile&) = delete; + SoundFile& operator=(const SoundFile&) = delete; }; -SoundFile* load_sound_file(const std::string& filename); +std::unique_ptr load_sound_file(const std::string& filename); #endif diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index b51c0e48d..dd8fdce16 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -34,7 +34,7 @@ SoundManager::SoundManager() : buffers(), sources(), update_list(), - music_source(0), + music_source(), music_enabled(false), current_music() { @@ -69,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; @@ -91,34 +88,28 @@ SoundManager::~SoundManager() } 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; } -OpenALSoundSource* +std::unique_ptr SoundManager::intern_create_sound_source(const std::string& filename) { assert(sound_enabled); - std::unique_ptr source (new OpenALSoundSource()); + std::unique_ptr source(new OpenALSoundSource); ALuint buffer; @@ -128,25 +119,25 @@ SoundManager::intern_create_sound_source(const std::string& filename) buffer = i->second; } else { // Load sound file - std::unique_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()); + buffer = load_file_into_buffer(*file); buffers.insert(std::make_pair(filename, buffer)); } else { - StreamSoundSource* source = new StreamSoundSource(); - source->set_sound_file(file.release()); - return source; + 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); } -SoundSource* +std::unique_ptr SoundManager::create_sound_source(const std::string& filename) { if(!sound_enabled) @@ -176,7 +167,7 @@ SoundManager::preload(const std::string& filename) if(file->size >= 100000) return; - ALuint buffer = load_file_into_buffer(file.get()); + 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; @@ -190,8 +181,7 @@ SoundManager::play(const std::string& filename, const Vector& pos) return; try { - std::unique_ptr source - (intern_create_sound_source(filename)); + std::unique_ptr source(intern_create_sound_source(filename)); if(pos.x < 0 || pos.y < 0) { source->set_relative(true); @@ -199,33 +189,37 @@ SoundManager::play(const std::string& filename, const Vector& pos) 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() ){ if( *i == sss ){ @@ -257,8 +251,7 @@ SoundManager::enable_music(bool enable) play_music(current_music); } else { if(music_source) { - delete music_source; - music_source = NULL; + music_source.reset(); } } } @@ -271,8 +264,7 @@ SoundManager::stop_music(float fadetime) && 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 = ""; } @@ -287,8 +279,7 @@ SoundManager::play_music(const std::string& filename, bool fade) return; if(filename == "") { - delete music_source; - music_source = NULL; + music_source.reset(); return; } @@ -301,8 +292,7 @@ SoundManager::play_music(const std::string& filename, bool 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. @@ -341,12 +331,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; @@ -372,20 +361,20 @@ SoundManager::update() } 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"); diff --git a/src/audio/sound_manager.hpp b/src/audio/sound_manager.hpp index 3744c6265..da042c639 100644 --- a/src/audio/sound_manager.hpp +++ b/src/audio/sound_manager.hpp @@ -18,6 +18,7 @@ #define HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP #include +#include #include #include @@ -44,7 +45,7 @@ public: * sound). * This function never throws exceptions, but might return a DummySoundSource */ - SoundSource* create_sound_source(const std::string& filename); + std::unique_ptr create_sound_source(const std::string& filename); /** * Convenience function to simply play a sound at a given position. */ @@ -53,7 +54,7 @@ public: * Adds the source to the list of managed sources (= the source gets deleted * when it finished playing) */ - void manage_source(SoundSource* source); + void manage_source(std::unique_ptr source); /// preloads a sound, so that you don't get a lag later when playing it void preload(const std::string& name); @@ -87,9 +88,9 @@ private: friend class StreamSoundSource; /** creates a new sound source, might throw exceptions, never returns NULL */ - OpenALSoundSource* intern_create_sound_source(const std::string& filename); - static ALuint load_file_into_buffer(SoundFile* file); - static ALenum get_sample_format(SoundFile* file); + std::unique_ptr intern_create_sound_source(const std::string& filename); + static ALuint load_file_into_buffer(SoundFile& file); + static ALenum get_sample_format(const SoundFile& file); void print_openal_version(); void check_alc_error(const char* message); @@ -101,13 +102,13 @@ private: typedef std::map SoundBuffers; SoundBuffers buffers; - typedef std::vector SoundSources; + typedef std::vector > SoundSources; SoundSources sources; typedef std::vector StreamSoundSources; StreamSoundSources update_list; - StreamSoundSource* music_source; + std::unique_ptr music_source; bool music_enabled; std::string current_music; diff --git a/src/audio/sound_source.hpp b/src/audio/sound_source.hpp index e473e0495..065bfcb40 100644 --- a/src/audio/sound_source.hpp +++ b/src/audio/sound_source.hpp @@ -27,8 +27,8 @@ class Vector; class SoundSource { public: - virtual ~SoundSource() - { } + SoundSource() {} + virtual ~SoundSource() {} virtual void play() = 0; virtual void stop() = 0; @@ -42,6 +42,10 @@ public: virtual void set_position(const Vector& position) = 0; virtual void set_velocity(const Vector& velocity) = 0; virtual void set_reference_distance(float distance) = 0; + +private: + SoundSource(const SoundSource&) = delete; + SoundSource& operator=(const SoundSource&) = delete; }; #endif diff --git a/src/audio/stream_sound_source.cpp b/src/audio/stream_sound_source.cpp index fceab8063..2645e0d65 100644 --- a/src/audio/stream_sound_source.cpp +++ b/src/audio/stream_sound_source.cpp @@ -21,7 +21,7 @@ #include "util/log.hpp" StreamSoundSource::StreamSoundSource() : - file(0), + file(), fade_state(NoFading), fade_start_time(), fade_time(), @@ -37,17 +37,16 @@ StreamSoundSource::~StreamSoundSource() { //don't update me any longer sound_manager->remove_from_update( this ); - delete file; + file.reset(); stop(); alDeleteBuffers(STREAMFRAGMENTS, buffers); SoundManager::check_al_error("Couldn't delete audio buffers: "); } void -StreamSoundSource::set_sound_file(SoundFile* newfile) +StreamSoundSource::set_sound_file(std::unique_ptr newfile) { - delete file; - file = newfile; + file = std::move(newfile); ALint queued; alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); @@ -111,10 +110,10 @@ bool StreamSoundSource::fillBufferAndQueue(ALuint buffer) { // fill buffer - char* bufferdata = new char[STREAMFRAGMENTSIZE]; + std::unique_ptr bufferdata(new char[STREAMFRAGMENTSIZE]); size_t bytesread = 0; do { - bytesread += file->read(bufferdata + bytesread, + bytesread += file->read(bufferdata.get() + bytesread, STREAMFRAGMENTSIZE - bytesread); // end of sound file if(bytesread < STREAMFRAGMENTSIZE) { @@ -126,14 +125,13 @@ StreamSoundSource::fillBufferAndQueue(ALuint buffer) } while(bytesread < STREAMFRAGMENTSIZE); if(bytesread > 0) { - ALenum format = SoundManager::get_sample_format(file); - alBufferData(buffer, format, bufferdata, bytesread, file->rate); + ALenum format = SoundManager::get_sample_format(*file); + alBufferData(buffer, format, bufferdata.get(), bytesread, file->rate); SoundManager::check_al_error("Couldn't refill audio buffer: "); alSourceQueueBuffers(source, 1, &buffer); SoundManager::check_al_error("Couldn't queue audio buffer: "); } - delete[] bufferdata; // return false if there aren't more buffers to fill return bytesread >= STREAMFRAGMENTSIZE; diff --git a/src/audio/stream_sound_source.hpp b/src/audio/stream_sound_source.hpp index d3b3e948a..1dbc63bf9 100644 --- a/src/audio/stream_sound_source.hpp +++ b/src/audio/stream_sound_source.hpp @@ -27,7 +27,7 @@ public: StreamSoundSource(); virtual ~StreamSoundSource(); - void set_sound_file(SoundFile* file); + void set_sound_file(std::unique_ptr newfile); enum FadeState { NoFading, FadingOn, FadingOff }; @@ -54,7 +54,7 @@ private: = STREAMBUFFERSIZE / STREAMFRAGMENTS; bool fillBufferAndQueue(ALuint buffer); - SoundFile* file; + std::unique_ptr file; ALuint buffers[STREAMFRAGMENTS]; FadeState fade_state; diff --git a/src/badguy/bomb.cpp b/src/badguy/bomb.cpp index b0d43c03f..efd0433b6 100644 --- a/src/badguy/bomb.cpp +++ b/src/badguy/bomb.cpp @@ -32,7 +32,7 @@ Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "imag set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1); countMe = false; - ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav")); + ticking = sound_manager->create_sound_source("sounds/fizz.wav"); ticking->set_position(get_pos()); ticking->set_looping(true); ticking->set_gain(2.0); diff --git a/src/badguy/dart.cpp b/src/badguy/dart.cpp index deac0ffda..47889d9d2 100644 --- a/src/badguy/dart.cpp +++ b/src/badguy/dart.cpp @@ -75,7 +75,7 @@ Dart::initialize() void Dart::activate() { - sound_source.reset(sound_manager->create_sound_source(DART_SOUND)); + sound_source = sound_manager->create_sound_source(DART_SOUND); sound_source->set_position(get_pos()); sound_source->set_looping(true); sound_source->set_gain(1.0); diff --git a/src/badguy/flame.cpp b/src/badguy/flame.cpp index 6df88e056..0539577de 100644 --- a/src/badguy/flame.cpp +++ b/src/badguy/flame.cpp @@ -85,7 +85,7 @@ Flame::draw(DrawingContext& context) void Flame::activate() { - sound_source.reset(sound_manager->create_sound_source(FLAME_SOUND)); + sound_source = sound_manager->create_sound_source(FLAME_SOUND); sound_source->set_position(get_pos()); sound_source->set_looping(true); sound_source->set_gain(2.0); diff --git a/src/badguy/goldbomb.cpp b/src/badguy/goldbomb.cpp index 6b626a69a..1fbe184f6 100644 --- a/src/badguy/goldbomb.cpp +++ b/src/badguy/goldbomb.cpp @@ -114,7 +114,7 @@ GoldBomb::collision_squished(GameObject& object) if (player) player->bounce(*this); - ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav")); + ticking = sound_manager->create_sound_source("sounds/fizz.wav"); ticking->set_position(get_pos()); ticking->set_looping(true); ticking->set_gain(2.0); diff --git a/src/badguy/haywire.cpp b/src/badguy/haywire.cpp index 87b2cf11d..9620b70f4 100644 --- a/src/badguy/haywire.cpp +++ b/src/badguy/haywire.cpp @@ -96,12 +96,12 @@ Haywire::collision_squished(GameObject& object) time_until_explosion = TIME_EXPLOSION; is_exploding = true; - ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav")); + ticking = sound_manager->create_sound_source("sounds/fizz.wav"); ticking->set_position(get_pos()); ticking->set_looping(true); ticking->set_reference_distance(32); ticking->play(); - grunting.reset(sound_manager->create_sound_source("sounds/grunts.ogg")); + grunting = sound_manager->create_sound_source("sounds/grunts.ogg"); grunting->set_position(get_pos()); grunting->set_looping(true); grunting->set_reference_distance(32); diff --git a/src/badguy/treewillowisp.cpp b/src/badguy/treewillowisp.cpp index 4888a2ed4..a4d3d8dcf 100644 --- a/src/badguy/treewillowisp.cpp +++ b/src/badguy/treewillowisp.cpp @@ -58,7 +58,7 @@ TreeWillOWisp::~TreeWillOWisp() void TreeWillOWisp::activate() { - sound_source.reset(sound_manager->create_sound_source(TREEWILLOSOUND)); + sound_source = sound_manager->create_sound_source(TREEWILLOSOUND); sound_source->set_position(get_pos()); sound_source->set_looping(true); sound_source->set_gain(2.0); diff --git a/src/badguy/willowisp.cpp b/src/badguy/willowisp.cpp index 88e28d8b4..76298b3ad 100644 --- a/src/badguy/willowisp.cpp +++ b/src/badguy/willowisp.cpp @@ -159,7 +159,7 @@ WillOWisp::active_update(float elapsed_time) void WillOWisp::activate() { - sound_source.reset(sound_manager->create_sound_source(SOUNDFILE)); + sound_source = sound_manager->create_sound_source(SOUNDFILE); sound_source->set_position(get_pos()); sound_source->set_looping(true); sound_source->set_gain(2.0); diff --git a/src/object/ambient_sound.cpp b/src/object/ambient_sound.cpp index f39726409..c2e9f55c2 100644 --- a/src/object/ambient_sound.cpp +++ b/src/object/ambient_sound.cpp @@ -88,7 +88,7 @@ AmbientSound::AmbientSound(const Reader& lisp) : lisp.get("silence_distance",silence_distance); - sound_source = 0; // not playing at the beginning + sound_source.reset(); // not playing at the beginning sound_manager->preload(sample); latency=0; } @@ -144,8 +144,7 @@ AmbientSound::hit(Player& ) void AmbientSound::stop_playing() { - delete sound_source; - sound_source = 0; + sound_source.reset(); } void @@ -162,8 +161,7 @@ AmbientSound::start_playing() sound_source->play(); } catch(std::exception& e) { log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl; - delete sound_source; - sound_source = 0; + sound_source.reset(); remove_me(); } } diff --git a/src/object/ambient_sound.hpp b/src/object/ambient_sound.hpp index 1f059efe3..e438ea9db 100644 --- a/src/object/ambient_sound.hpp +++ b/src/object/ambient_sound.hpp @@ -91,7 +91,7 @@ private: Vector dimension; std::string sample; - SoundSource* sound_source; + std::unique_ptr sound_source; int latency; float distance_factor; /// distance scaling -- 2.11.0