Skip to content

Commit

Permalink
Use std::unique_ptr<> throughout the audio system
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 12, 2014
1 parent 8d26769 commit b88dc6c
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 104 deletions.
12 changes: 10 additions & 2 deletions src/audio/dummy_sound_source.cpp
Expand Up @@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "audio/dummy_sound_source.hpp"

#include <memory>

#include "audio/sound_source.hpp"

class DummySoundSource : public SoundSource
Expand Down Expand Up @@ -70,11 +74,15 @@ class DummySoundSource : public SoundSource

private:
bool is_playing;

private:
DummySoundSource(const DummySoundSource&) = delete;
DummySoundSource& operator=(const DummySoundSource&) = delete;
};

SoundSource* create_dummy_sound_source()
std::unique_ptr<SoundSource> create_dummy_sound_source()
{
return new DummySoundSource();
return std::unique_ptr<SoundSource>(new DummySoundSource);
}

/* EOF */
4 changes: 3 additions & 1 deletion src/audio/dummy_sound_source.hpp
Expand Up @@ -17,9 +17,11 @@
#ifndef HEADER_SUPERTUX_AUDIO_DUMMY_SOUND_SOURCE_HPP
#define HEADER_SUPERTUX_AUDIO_DUMMY_SOUND_SOURCE_HPP

#include <memory>

class SoundSource;

SoundSource* create_dummy_sound_source();
std::unique_ptr<SoundSource> create_dummy_sound_source();

#endif

Expand Down
1 change: 1 addition & 0 deletions src/audio/openal_sound_source.cpp
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "audio/openal_sound_source.hpp"

#include "audio/sound_manager.hpp"

OpenALSoundSource::OpenALSoundSource() :
Expand Down
4 changes: 4 additions & 0 deletions src/audio/openal_sound_source.hpp
Expand Up @@ -45,6 +45,10 @@ class OpenALSoundSource : public SoundSource
friend class SoundManager;

ALuint source;

private:
OpenALSoundSource(const OpenALSoundSource&) = delete;
OpenALSoundSource& operator=(const OpenALSoundSource&) = delete;
};

#endif
Expand Down
10 changes: 5 additions & 5 deletions src/audio/sound_file.cpp
Expand Up @@ -32,7 +32,7 @@
#include "util/file_system.hpp"
#include "util/log.hpp"

SoundFile* load_music_file(const std::string& filename)
std::unique_ptr<SoundFile> load_music_file(const std::string& filename)
{
lisp::Parser parser(false);
const lisp::Lisp* root = parser.parse(filename);
Expand Down Expand Up @@ -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<SoundFile>(new OggSoundFile(file, loop_begin, loop_at));
}

SoundFile* load_sound_file(const std::string& filename)
std::unique_ptr<SoundFile> load_sound_file(const std::string& filename)
{
if(filename.length() > 6
&& filename.compare(filename.length()-6, 6, ".music") == 0) {
Expand All @@ -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<SoundFile>(new WavSoundFile(file));
else if(strncmp(magic, "OggS", 4) == 0)
return new OggSoundFile(file, 0, -1);
return std::unique_ptr<SoundFile>(new OggSoundFile(file, 0, -1));
else
throw SoundError("Unknown file format");
} catch(std::exception& e) {
Expand Down
7 changes: 6 additions & 1 deletion src/audio/sound_file.hpp
Expand Up @@ -18,6 +18,7 @@
#define HEADER_SUPERTUX_AUDIO_SOUND_FILE_HPP

#include <iostream>
#include <memory>

class SoundFile
{
Expand All @@ -40,9 +41,13 @@ class SoundFile
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<SoundFile> load_sound_file(const std::string& filename);

#endif

Expand Down
109 changes: 49 additions & 60 deletions src/audio/sound_manager.cpp
Expand Up @@ -34,7 +34,7 @@ SoundManager::SoundManager() :
buffers(),
sources(),
update_list(),
music_source(0),
music_source(),
music_enabled(false),
current_music()
{
Expand Down Expand Up @@ -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;
Expand All @@ -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<ALsizei> (file->size),
static_cast<ALsizei> (file->rate));
check_al_error("Couldn't fill audio buffer: ");
} catch(...) {
delete[] samples;
throw;
}
delete[] samples;
std::unique_ptr<char[]> samples(new char[file.size]);
file.read(samples.get(), file.size);
alBufferData(buffer, format, samples.get(),
static_cast<ALsizei>(file.size),
static_cast<ALsizei>(file.rate));
check_al_error("Couldn't fill audio buffer: ");

return buffer;
}

OpenALSoundSource*
std::unique_ptr<OpenALSoundSource>
SoundManager::intern_create_sound_source(const std::string& filename)
{
assert(sound_enabled);

std::unique_ptr<OpenALSoundSource> source (new OpenALSoundSource());
std::unique_ptr<OpenALSoundSource> source(new OpenALSoundSource);

ALuint buffer;

Expand All @@ -128,25 +119,25 @@ SoundManager::intern_create_sound_source(const std::string& filename)
buffer = i->second;
} else {
// Load sound file
std::unique_ptr<SoundFile> file (load_sound_file(filename));
std::unique_ptr<SoundFile> 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<StreamSoundSource> 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<SoundSource>
SoundManager::create_sound_source(const std::string& filename)
{
if(!sound_enabled)
Expand Down Expand Up @@ -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;
Expand All @@ -190,42 +181,45 @@ SoundManager::play(const std::string& filename, const Vector& pos)
return;

try {
std::unique_ptr<OpenALSoundSource> source
(intern_create_sound_source(filename));
std::unique_ptr<OpenALSoundSource> source(intern_create_sound_source(filename));

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<SoundSource> source)
{
assert(source != NULL);

OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
if(openal_source != NULL) {
sources.push_back(openal_source);
assert(source);
if (dynamic_cast<OpenALSoundSource*>(source.get()))
{
std::unique_ptr<OpenALSoundSource> openal_source(dynamic_cast<OpenALSoundSource*>(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 ){
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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 = "";
}
Expand All @@ -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;
}

Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down

0 comments on commit b88dc6c

Please sign in to comment.