Use NDEBUG a bit less.
[supertux.git] / src / audio / sound_manager.cpp
index b3eb731..6bd2c31 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux
 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
-//  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
 //  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 <config.h>
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-#include "sound_manager.hpp"
+#include "audio/sound_manager.hpp"
 
+#include <SDL.h>
+#include <assert.h>
 #include <stdexcept>
-#include <iostream>
 #include <sstream>
 #include <memory>
-#include <assert.h>
-#include <SDL.h>
 
-#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(0),
+  music_enabled(false),
+  current_music()
 {
   try {
     device = alcOpenDevice(0);
@@ -100,8 +97,8 @@ SoundManager::load_file_into_buffer(SoundFile* file)
   try {
     file->read(samples, file->size);
     alBufferData(buffer, format, samples,
-        static_cast<ALsizei> (file->size),
-        static_cast<ALsizei> (file->rate));
+                 static_cast<ALsizei> (file->size),
+                 static_cast<ALsizei> (file->rate));
     check_al_error("Couldn't fill audio buffer: ");
   } catch(...) {
     delete[] samples;
@@ -112,19 +109,12 @@ SoundManager::load_file_into_buffer(SoundFile* file)
   return buffer;
 }
 
-SoundSource*
-SoundManager::create_sound_source(const std::string& filename)
+OpenALSoundSource*
+SoundManager::intern_create_sound_source(const std::string& filename)
 {
-  if(!sound_enabled)
-    return create_dummy_sound_source();
+  assert(sound_enabled);
 
-  std::auto_ptr<OpenALSoundSource> 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::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource());
 
   ALuint buffer;
 
@@ -133,28 +123,39 @@ SoundManager::create_sound_source(const std::string& filename)
   if(i != buffers.end()) {
     buffer = i->second;
   } else {
-    try {
-      // Load sound file
-      std::auto_ptr<SoundFile> file (load_sound_file(filename));
+    // Load sound file
+    std::auto_ptr<SoundFile> 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.get());
+      buffers.insert(std::make_pair(filename, buffer));
+    } else {
+      StreamSoundSource* source = new StreamSoundSource();
+      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);
   return source.release();
 }
 
+SoundSource*
+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
 SoundManager::preload(const std::string& filename)
 {
@@ -165,14 +166,17 @@ SoundManager::preload(const std::string& filename)
   // already loaded?
   if(i != buffers.end())
     return;
+  try {
+    std::auto_ptr<SoundFile> file (load_sound_file(filename));
+    // only keep small files
+    if(file->size >= 100000)
+      return;
 
-  std::auto_ptr<SoundFile> 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
@@ -183,10 +187,10 @@ SoundManager::play(const std::string& filename, const Vector& pos)
 
   try {
     std::auto_ptr<OpenALSoundSource> source
-      (static_cast<OpenALSoundSource*> (create_sound_source(filename)));
+      (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);
     }
@@ -219,7 +223,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 {
@@ -260,7 +264,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;
@@ -286,9 +290,9 @@ SoundManager::play_music(const std::string& filename, bool fade)
 
   try {
     std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
-    alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
     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();
@@ -415,3 +419,5 @@ SoundManager::check_al_error(const char* message)
     throw std::runtime_error(msg.str());
   }
 }
+
+/* EOF */