]> git.mxchange.org Git - simgear.git/blobdiff - simgear/sound/soundmgr_openal.cxx
Ima Sudonim:
[simgear.git] / simgear / sound / soundmgr_openal.cxx
index daf0729e856ecc46968ccfc65b44045dfbef8beb..9b88e3d001ad6997bc329b94bbcbfd760a241613 100644 (file)
@@ -5,7 +5,7 @@
 //
 // C++-ified by Curtis Olson, started March 2001.
 //
-// Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 //
 // $Id$
 
-#include <iostream>
+#include <simgear/compiler.h>
 
 #if defined(__APPLE__)
 # include <OpenAL/al.h>
-# include <OpenAL/alut.h>
 # include <OpenAL/alc.h>
 #else
 # include <AL/al.h>
-# include <AL/alut.h>
 # include <AL/alc.h>
 #endif
 
+#if defined (__APPLE__)
+#  ifdef __GNUC__
+#    if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
+//  #        include <math.h>
+extern "C" int isnan (double);
+extern "C" int isinf (double);
+#    else
+    // any C++ header file undefines isinf and isnan
+    // so this should be included before <iostream>
+    // the functions are STILL in libm (libSystem on mac os x)
+extern "C" int isnan (double);
+extern "C" int isinf (double);
+#    endif
+#  else
+//    inline int (isinf)(double r) { return isinf(r); }
+//    inline int (isnan)(double r) { return isnan(r); }
+#  endif
+#endif
+
+#if defined (__FreeBSD__)
+#  if __FreeBSD_version < 500000
+     extern "C" {
+       inline int isnan(double r) { return !(r <= 0 || r >= 0); }
+     }
+#  endif
+#endif
+
+#include STL_IOSTREAM
+
 #include <simgear/debug/logstream.hxx>
 #include <simgear/misc/sg_path.hxx>
 
 #include "soundmgr_openal.hxx"
 
+#if defined(__MINGW32__)
+#define isnan(x) _isnan(x)
+#endif
 
 //
 // Sound Manager
 // constructor
 SGSoundMgr::SGSoundMgr() {
 
-    SG_LOG( SG_GENERAL, SG_ALERT, "Initializing OpenAL sound manager" );
+    SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
 
     // initialize OpenAL
-    alutInit( 0, NULL );
-    alGetError();
-    if ( alGetError() == AL_NO_ERROR) {
+    if ( (dev = alcOpenDevice( NULL )) != NULL
+            && ( context = alcCreateContext( dev, NULL )) != NULL ) {
         working = true;
+        alcMakeContextCurrent( context );
     } else {
         working = false;
+        context = 0;
        SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
     }
 
@@ -75,6 +106,7 @@ SGSoundMgr::SGSoundMgr() {
     listener_ori[4] = 1.0;
     listener_ori[5] = 0.0;
 
+    alListenerf( AL_GAIN, 0.0f );
     alListenerfv( AL_POSITION, listener_pos );
     alListenerfv( AL_VELOCITY, listener_vel );
     alListenerfv( AL_ORIENTATION, listener_ori );
@@ -93,6 +125,8 @@ SGSoundMgr::SGSoundMgr() {
 
 SGSoundMgr::~SGSoundMgr() {
 
+    if (context)
+        alcDestroyContext( context );
     //
     // Remove the samples from the sample manager.
     //
@@ -102,8 +136,6 @@ SGSoundMgr::~SGSoundMgr() {
        SGSoundSample *sample = sample_current->second;
        delete sample;
     }
-
-    alutExit();
 }
 
 
@@ -142,11 +174,12 @@ void SGSoundMgr::update( double dt ) {
 void
 SGSoundMgr::pause ()
 {
-    ALCcontext *pCurContext = alcGetCurrentContext();
-    alcSuspendContext( pCurContext );
-    if ( alGetError() != AL_NO_ERROR) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Oops AL error after soundmgr pause()!" );
+    if (context) {
+        alcSuspendContext( context );
+        if ( alGetError() != AL_NO_ERROR) {
+           SG_LOG( SG_GENERAL, SG_ALERT,
+                    "Oops AL error after soundmgr pause()!" );
+        }
     }
 }
 
@@ -154,11 +187,12 @@ SGSoundMgr::pause ()
 void
 SGSoundMgr::resume ()
 {
-    ALCcontext *pCurContext = alcGetCurrentContext();
-    alcProcessContext( pCurContext );
-    if ( alGetError() != AL_NO_ERROR) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Oops AL error after soundmgr resume()!" );
+    if (context) {
+        alcProcessContext( context );
+        if ( alGetError() != AL_NO_ERROR) {
+           SG_LOG( SG_GENERAL, SG_ALERT,
+                    "Oops AL error after soundmgr resume()!" );
+        }
     }
 }
 
@@ -275,6 +309,11 @@ bool SGSoundMgr::stop( const string& refname ) {
 
 // set source position of all managed sounds
 void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
+    if ( isnan(pos[0]) || isnan(pos[1]) || isnan(pos[2]) ) {
+        // bail if a bad position is passed in
+        return;
+    }
+
     sample_map_iterator sample_current = samples.begin();
     sample_map_iterator sample_end = samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {
@@ -286,6 +325,11 @@ void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
 
 // set source velocity of all managed sounds
 void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
+    if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) {
+        // bail if a bad velocity is passed in
+        return;
+    }
+
     sample_map_iterator sample_current = samples.begin();
     sample_map_iterator sample_end = samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {