]> 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 e372f3cc5fd0551bfb73ddd85fa627ae3f3cb4cc..55647200f1679b56fdd5bc030762caebe80e0fd1 100644 (file)
 #endif
 
 #if defined( __APPLE__ )
-# include <OpenAL/alut.h>
+# include <ALUT/alut.h>
 #else
 # include <AL/alut.h>
 #endif
 
 #include <iostream>
 #include <algorithm>
+#include <cstring>
 
 #include "soundmgr_openal.hxx"
 
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/math/SGMath.hxx>
 
+using std::string;
+using std::vector;
+
 extern bool isNaN(float *v);
 
 #define MAX_SOURCES    128
 
+
+#ifndef ALC_ALL_DEVICES_SPECIFIER
+# define ALC_ALL_DEVICES_SPECIFIER     0x1013
+#endif
+
 //
 // Sound Manager
 //
@@ -81,6 +90,8 @@ SGSoundMgr::SGSoundMgr() :
         }
     }
     _alut_init++;
+#else
+  //#error ALUT 1.1 required, ALUT 1.0 is no longer supported, please upgrade
 #endif
 }
 
@@ -100,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") ) {
@@ -126,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;
 
@@ -164,16 +175,16 @@ void SGSoundMgr::init(const char *devname) {
 
     _vendor = (const char *)alGetString(AL_VENDOR);
     _renderer = (const char *)alGetString(AL_RENDERER);
-    if ( (_vendor != "Adalin" && _vendor != "Apple Computer Inc.") &&
-          (_vendor != "OpenAL Community" || (_renderer != "Software" &&
-                        _renderer != "OpenAL Sample Implementation"))
-       )
-    {
+
+    if (_vendor == "Creative Labs Inc.") {
+       _bad_doppler = true;
+
+    } else if (_vendor == "OpenAL Community" && _renderer == "OpenAL Soft") {
        _bad_doppler = true;
     }
 
     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!");
     }
 }
 
@@ -204,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();
 
@@ -214,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();
 
@@ -272,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();
@@ -401,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()
 {
@@ -413,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;
 }
@@ -428,11 +442,12 @@ 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 );
-        testForALError("release source");
+        }
 
-        alSourcei( source, AL_BUFFER, 0 );
+        alSourcei( source, AL_BUFFER, 0 );     // detach the associated buffer
+        testForALError("release_source");
         _free_sources.push_back( source );
         _sources_in_use.erase( it );
     }
@@ -460,17 +475,24 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample)
         if ( sample->is_file() ) {
             int freq, format;
             size_t size;
-            bool res;
-
-            res = load(sample_name, &sample_data, &format, &size, &freq);
-            if (res == false) return buffer;
 
+            try {
+              bool res = load(sample_name, &sample_data, &format, &size, &freq);
+              if (res == false) return NO_BUFFER;
+            } catch (sg_exception& e) {
+              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 );
             sample->set_format( format );
             sample->set_size( size );
-        }
-        else
+
+        } else {
             sample_data = sample->get_data();
+        }
 
         // create an OpenAL buffer handle
         alGenBuffers(1, &buffer);
@@ -482,37 +504,40 @@ unsigned int SGSoundMgr::request_buffer(SGSoundSample *sample)
             ALsizei freq = sample->get_frequency();
             alBufferData( buffer, format, sample_data, size, freq );
 
-            if ( sample->is_file() ) free(sample_data);
-
             if ( !testForALError("buffer add data") ) {
                 sample->set_buffer(buffer);
                 _buffers[sample_name] = refUint(buffer);
             }
         }
+
+        if ( sample->is_file() ) free(sample_data);
     }
     else {
         buffer = sample->get_buffer();
-}
+    }
 
     return buffer;
 }
 
 void SGSoundMgr::release_buffer(SGSoundSample *sample)
 {
-    string sample_name = sample->get_sample_name();
-    buffer_map_iterator buffer_it = _buffers.find( sample_name );
-    if ( buffer_it == _buffers.end() ) {
-        // buffer was not found
-        return;
-    }
+    if ( !sample->is_queue() )
+    {
+        string sample_name = sample->get_sample_name();
+        buffer_map_iterator buffer_it = _buffers.find( sample_name );
+        if ( buffer_it == _buffers.end() ) {
+            // buffer was not found
+            return;
+        }
 
-    sample->no_valid_buffer();
-    buffer_it->second.refctr--;
-    if (buffer_it->second.refctr == 0) {
-        ALuint buffer = buffer_it->second.id;
-        alDeleteBuffers(1, &buffer);
-        _buffers.erase( buffer_it );
-        testForALError("release buffer");
+        sample->no_valid_buffer();
+        buffer_it->second.refctr--;
+        if (buffer_it->second.refctr == 0) {
+            ALuint buffer = buffer_it->second.id;
+            alDeleteBuffers(1, &buffer);
+            _buffers.erase( buffer_it );
+            testForALError("release buffer");
+        }
     }
 }
 
@@ -537,10 +562,7 @@ void SGSoundMgr::update_pos_and_orientation() {
     _at_up_vec[4] = sgv_up[1];
     _at_up_vec[5] = sgv_up[2];
 
-    // static const SGQuatd q(-0.5, -0.5, 0.5, 0.5);
-    // SGQuatd hlOr = SGQuatd::fromLonLat(SGGeod::fromCart(_base_pos));
-    // SGQuatd ec2body = hlOr*_orientation;
-    _absolute_pos = _base_pos; // + ec2body.backTransform( _offset_pos );
+    _absolute_pos = _base_pos;
 }
 
 bool SGSoundMgr::load(string &samplepath, void **dbuf, int *fmt,
@@ -587,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());
         }
@@ -596,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;
@@ -634,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;
@@ -645,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;
     }
@@ -657,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;
@@ -671,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;