Sound Manager prints debug info if uncached sounds are requested to be played
[supertux.git] / src / audio / sound_manager.cpp
index 6905621..b2697c1 100644 (file)
 #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"
 
+#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()
@@ -108,15 +122,17 @@ 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 0;
+    throw std::runtime_error("sound disabled");
+
+  std::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource());
 
   ALuint buffer;
-  
-  // reuse an existing static sound buffer            
+
+  // reuse an existing static sound buffer
   SoundBuffers::iterator i = buffers.find(filename);
   if(i != buffers.end()) {
     buffer = i->second;
@@ -132,11 +148,26 @@ SoundManager::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;
   }
-  
-  SoundSource* source = new SoundSource();
+
   alSourcei(source->source, AL_BUFFER, buffer);
-  return source;  
+  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
@@ -144,7 +175,7 @@ SoundManager::preload(const std::string& filename)
 {
   if(!sound_enabled)
     return;
-  
+
   SoundBuffers::iterator i = buffers.find(filename);
   // already loaded?
   if(i != buffers.end())
@@ -162,34 +193,54 @@ SoundManager::preload(const std::string& filename)
 void
 SoundManager::play(const std::string& filename, const Vector& pos)
 {
+  if(!sound_enabled)
+    return;
+
   try {
-    SoundSource* source = create_sound_source(filename);
-    if(source == NULL)
-      return;
+    std::auto_ptr<OpenALSoundSource> source
+        (intern_create_sound_source(filename));
+
     if(pos == Vector(-1, -1)) {
-      alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
+      source->set_rollof_factor(0);
     } else {
       source->set_position(pos);
     }
     source->play();
-    sources.push_back(source);
+    sources.push_back(source.release());
   } catch(std::exception& e) {
     log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
   }
 }
 
 void
-SoundManager::play_and_delete(SoundSource* source)
+SoundManager::manage_source(SoundSource* source)
 {
-  if (!source) {
-    log_debug << "ignoring NULL SoundSource" << std::endl;
-    return;
+  assert(source != NULL);
+
+  OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
+  if(openal_source != NULL) {
+    sources.push_back(openal_source);
   }
-  try {
-    source->play();
-    sources.push_back(source);
-  } catch(std::exception& e) {
-    log_warning << "Couldn't play SoundSource: " << e.what() << std::endl;
+}
+
+void
+SoundManager::register_for_update( StreamSoundSource* sss ){
+  if( sss != NULL ){
+    update_list.push_back( sss );
+  }
+}
+
+void
+SoundManager::remove_from_update( StreamSoundSource* sss  ){
+  if( sss != NULL ){
+    StreamSoundSources::iterator i = update_list.begin();
+       while( i != update_list.end() ){
+      if( *i == sss ){
+        i = update_list.erase(i);
+      } else {
+        i++;
+      }
+    }
   }
 }
 
@@ -267,12 +318,12 @@ SoundManager::play_music(const std::string& filename, bool fade)
 void
 SoundManager::set_listener_position(const Vector& pos)
 {
-  static Uint32 lastticks = 0;
+  static Uint32 lastticks = SDL_GetTicks();
 
   Uint32 current_ticks = SDL_GetTicks();
   if(current_ticks - lastticks < 300)
     return;
-  lastticks = current_ticks;                 
+  lastticks = current_ticks;
 
   alListener3f(AL_POSITION, pos.x, pos.y, 0);
 }
@@ -286,18 +337,19 @@ SoundManager::set_listener_velocity(const Vector& vel)
 void
 SoundManager::update()
 {
-  static float lasttime = real_time;
+  static Uint32 lasttime = SDL_GetTicks();
+  Uint32 now = SDL_GetTicks();
 
-  if(real_time - lasttime < 0.3)
+  if(now - lasttime < 300)
     return;
-  lasttime = real_time;
+  lasttime = now;
 
   // update and check for finished sound sources
   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
-    SoundSource* source = *i;
+    OpenALSoundSource* source = *i;
 
     source->update();
-    
+
     if(!source->playing()) {
       delete source;
       i = sources.erase(i);
@@ -315,6 +367,13 @@ SoundManager::update()
     alcProcessContext(context);
     check_alc_error("Error while processing audio context: ");
   }
+
+  //run update() for stream_sound_source
+  StreamSoundSources::iterator s = update_list.begin();
+  while( s != update_list.end() ){
+    (*s)->update();
+    s++;
+  }
 }
 
 ALenum
@@ -337,7 +396,7 @@ SoundManager::get_sample_format(SoundFile* file)
       throw std::runtime_error("Only 16 and 8 bit samples supported");
     }
   }
-  
+
   throw std::runtime_error("Only 1 and 2 channel samples supported");
 }
 
@@ -358,7 +417,7 @@ SoundManager::check_alc_error(const char* message)
     std::stringstream msg;
     msg << message << alcGetString(device, err);
     throw std::runtime_error(msg.str());
-  }                
+  }
 }
 
 void
@@ -369,6 +428,5 @@ SoundManager::check_al_error(const char* message)
     std::stringstream msg;
     msg << message << alGetString(err);
     throw std::runtime_error(msg.str());
-  }  
+  }
 }
-