]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/soundmgr_openal.cxx
math: Move lerp function into SGMisc.
[simgear.git] / simgear / sound / soundmgr_openal.cxx
index 3a0b2979c3a4d841caf11cdddd67518fcd44e8e1..55647200f1679b56fdd5bc030762caebe80e0fd1 100644 (file)
@@ -47,6 +47,7 @@
 #include <simgear/math/SGMath.hxx>
 
 using std::string;
+using std::vector;
 
 extern bool isNaN(float *v);
 
@@ -110,7 +111,7 @@ SGSoundMgr::~SGSoundMgr() {
 // initialize the sound manager
 void SGSoundMgr::init(const char *devname) {
 
-    SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
+    SG_LOG( SG_SOUND, SG_INFO, "Initializing OpenAL sound manager" );
 
     ALCdevice *device = alcOpenDevice(devname);
     if ( testForError(device, "Audio device not available, trying default") ) {
@@ -136,7 +137,7 @@ void SGSoundMgr::init(const char *devname) {
     }
 
     if (_context != NULL)
-        SG_LOG(SG_GENERAL, SG_ALERT, "context is already assigned");
+        SG_LOG(SG_SOUND, SG_ALERT, "context is already assigned");
     _context = context;
     _working = true;
 
@@ -183,7 +184,7 @@ void SGSoundMgr::init(const char *devname) {
     }
 
     if (_free_sources.size() == 0) {
-        SG_LOG(SG_GENERAL, SG_ALERT, "Unable to grab any OpenAL sources!");
+        SG_LOG(SG_SOUND, SG_ALERT, "Unable to grab any OpenAL sources!");
     }
 }
 
@@ -214,6 +215,7 @@ void SGSoundMgr::stop() {
     for (unsigned int i=0; i<_free_sources.size(); i++) {
         ALuint source = _free_sources[i];
         alDeleteSources( 1 , &source );
+        testForALError("SGSoundMgr::stop: delete sources");
     }
     _free_sources.clear();
 
@@ -224,6 +226,7 @@ void SGSoundMgr::stop() {
         refUint ref = buffers_current->second;
         ALuint buffer = ref.id;
         alDeleteBuffers(1, &buffer);
+        testForALError("SGSoundMgr::stop: delete buffers");
     }
     _buffers.clear();
 
@@ -282,6 +285,7 @@ void SGSoundMgr::unbind ()
     for (unsigned int i=0; i<_free_sources.size(); i++) {
         ALuint source = _free_sources[i];
         alDeleteSources( 1 , &source );
+        testForALError("SGSoundMgr::unbind: delete sources");
     }
 
     _free_sources.clear();
@@ -411,7 +415,7 @@ void SGSoundMgr::set_volume( float v )
 // and hence orientation) of the sources.
 //
 // The Sound Manager is (and should be) the only one knowing about source
-// management. Sources further away should be suspendped to free resources for
+// management. Sources further away should be suspended to free resources for
 // newly added sounds close by.
 unsigned int SGSoundMgr::request_source()
 {
@@ -423,7 +427,7 @@ unsigned int SGSoundMgr::request_source()
        _sources_in_use.push_back(source);
     }
     else
-       SG_LOG( SG_GENERAL, SG_INFO, "No more free sources available\n");
+       SG_LOG( SG_SOUND, SG_BULK, "Sound manager: No more free sources available!\n");
 
     return source;
 }
@@ -438,7 +442,7 @@ void SGSoundMgr::release_source( unsigned int source )
         ALint result;
 
         alGetSourcei( source, AL_SOURCE_STATE, &result );
-        if ( result == AL_PLAYING ) {
+        if ( result == AL_PLAYING || result == AL_PAUSED ) {
             alSourceStop( source );
         }
 
@@ -476,9 +480,10 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample)
               bool res = load(sample_name, &sample_data, &format, &size, &freq);
               if (res == false) return NO_BUFFER;
             } catch (sg_exception& e) {
-              SG_LOG(SG_GENERAL, SG_ALERT,
-                     "failed to load sound buffer:" << e.getFormattedMessage());
-              return NO_BUFFER;
+              SG_LOG(SG_SOUND, SG_ALERT,
+                    "failed to load sound buffer: " << e.getFormattedMessage());
+              sample->set_buffer( SGSoundMgr::FAILED_BUFFER );
+              return FAILED_BUFFER;
             }
             
             sample->set_frequency( freq );
@@ -604,7 +609,7 @@ bool SGSoundMgr::load(string &samplepath, void **dbuf, int *fmt,
             // occurs: e.g. -43 on Mac when file is not found.
             // In this case, alGetString() sets 'Invalid Enum' error, so
             // showing with the original error number is helpful.
-            stringstream ss;
+            std::stringstream ss;
             ss << alGetString(alGetError()) << "(" << error << ")";
             msg.append(ss.str());
         }
@@ -613,6 +618,11 @@ bool SGSoundMgr::load(string &samplepath, void **dbuf, int *fmt,
     }
 #endif
 
+    if (format == AL_FORMAT_STEREO8 || format == AL_FORMAT_STEREO16) {
+        free(data);
+        throw sg_io_exception("Warning: STEREO files are not supported for 3D audio effects: " + samplepath);
+    }
+
     *dbuf = (void *)data;
     *fmt = (int)format;
     *sz = (size_t)size;
@@ -651,7 +661,7 @@ vector<const char*> SGSoundMgr::get_available_devices()
 bool SGSoundMgr::testForError(void *p, string s)
 {
    if (p == NULL) {
-      SG_LOG( SG_GENERAL, SG_ALERT, "Error: " << s);
+      SG_LOG( SG_SOUND, SG_ALERT, "Error: " << s);
       return true;
    }
    return false;
@@ -662,7 +672,7 @@ bool SGSoundMgr::testForALError(string s)
 {
     ALenum error = alGetError();
     if (error != AL_NO_ERROR)  {
-       SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (sound manager): "
+       SG_LOG( SG_SOUND, SG_ALERT, "AL Error (sound manager): "
                                       << alGetString(error) << " at " << s);
        return true;
     }
@@ -674,7 +684,7 @@ bool SGSoundMgr::testForALCError(string s)
     ALCenum error;
     error = alcGetError(_device);
     if (error != ALC_NO_ERROR) {
-        SG_LOG( SG_GENERAL, SG_ALERT, "ALC Error (sound manager): "
+        SG_LOG( SG_SOUND, SG_ALERT, "ALC Error (sound manager): "
                                        << alcGetString(_device, error) << " at "
                                        << s);
         return true;
@@ -688,7 +698,7 @@ bool SGSoundMgr::testForALUTError(string s)
     ALenum error;
     error =  alutGetError ();
     if (error != ALUT_ERROR_NO_ERROR) {
-        SG_LOG( SG_GENERAL, SG_ALERT, "ALUT Error (sound manager): "
+        SG_LOG( SG_SOUND, SG_ALERT, "ALUT Error (sound manager): "
                                        << alutGetErrorString(error) << " at "
                                        << s);
         return true;