correctly fix the case when openal fails to create new sources (this time it's really...
authorMatthias Braun <matze@braunis.de>
Tue, 23 Jan 2007 12:28:17 +0000 (12:28 +0000)
committerMatthias Braun <matze@braunis.de>
Tue, 23 Jan 2007 12:28:17 +0000 (12:28 +0000)
SVN-Revision: 4659

src/audio/sound_manager.cpp
src/audio/sound_manager.hpp

index fd3320e..2e83630 100644 (file)
@@ -122,19 +122,13 @@ 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();
+    throw new std::exception("sound disabled");
 
-  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;
 
@@ -143,21 +137,16 @@ 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));
-
-      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();
+    // 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;
     }
   }
 
@@ -165,6 +154,20 @@ SoundManager::create_sound_source(const std::string& filename)
   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)
 {
@@ -193,7 +196,7 @@ 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);
index edf9290..356c198 100644 (file)
@@ -43,8 +43,7 @@ public:
    * Creates a new sound source object which plays the specified soundfile.
    * You are responsible for deleting the sound source later (this will stop the
    * sound).
-   * This function might throw exceptions. It returns 0 if no audio device is
-   * available.
+   * This function never throws exceptions, but might return a DummySoundSource
    */
   SoundSource* create_sound_source(const std::string& filename);
   /**
@@ -88,6 +87,8 @@ private:
   friend class OpenALSoundSource;
   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);