Hi Jon,

thanks for pointing that out. And thanks to you and AJ for the debugging on IRC. Here is a patch (the same you already got via EMail (ok, one debug message is different)), which could fix it. If the bug is still present, please comment out line 56 (#define USE_OPEN_AL_DOPPLER should work) in file simgear\sound\sample_openal.hxx to use the own calculations.
This patch has some debug code and is not intended to go into cvs.
Due to pitch-limits on older OpenAL versions I am thinking of a patch for these versions. The pitch limitation on old OpenAL versions limit the Doppler effect depending on the pitch value without Doppler effect, which could sound odd on some aircrafts. But I only will start to work on that patch if there is a chance to get it into cvs. Therefore I will wait, if the "windows" patch will be accepted. (It could be, that you get warnings about pitch values > 2. Even some OpenAL1.1 versions seem to be limited in pitch while the OpenAL1.1 specification says they aren't)

Maik


Jon Stockill schrieb am 26.06.2007 21:06:
With a cvs build checked out about half an hour ago I've just noticed something very strange - with external views the doppler shift appears to be related to the view angle rather than the approach speed. If you select the chase view then you'll find that the sound is extremely slow >from behind the aircraft, and ridiculously fast from in front. This also still seems to affect the radio chatter, resulting in some highly comical, but not too realistic radio messages.

Jon

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel


Index: sound/sample_openal.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/sound/sample_openal.cxx,v
retrieving revision 1.27
diff -u -p -r1.27 sample_openal.cxx
--- sound/sample_openal.cxx     21 Jun 2007 21:46:21 -0000      1.27
+++ sound/sample_openal.cxx     26 Jun 2007 21:57:20 -0000
@@ -75,12 +75,17 @@ SGSoundSample::SGSoundSample() :
     reference_dist(500.0),
     max_dist(3000.),
     loop(AL_FALSE),
-    playing(false)
+#ifndef USE_OPEN_AL_DOPPLER
+    doppler_pitch_factor(1),
+    doppler_volume_factor(1),
+#endif
+    playing(false),
+    no_Doppler_effect(true)
 {
 }
 
 // constructor
-SGSoundSample::SGSoundSample( const char *path, const char *file) :
+SGSoundSample::SGSoundSample( const char *path, const char *file , bool 
_no_Doppler_effect ) :
     buffer(0),
     source(0),
     pitch(1.0),
@@ -88,7 +93,12 @@ SGSoundSample::SGSoundSample( const char
     reference_dist(500.0),
     max_dist(3000.),
     loop(AL_FALSE),
-    playing(false)
+#ifndef USE_OPEN_AL_DOPPLER
+    doppler_pitch_factor(1),
+    doppler_volume_factor(1),
+#endif
+    playing(false),
+    no_Doppler_effect(_no_Doppler_effect)
 {
     SGPath samplepath( path );
     if ( strlen(file) ) {
@@ -145,7 +155,7 @@ SGSoundSample::SGSoundSample( const char
 }
 
 // constructor
-SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) :
+SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq , bool 
_no_Doppler_effect ) :
     buffer(0),
     source(0),
     pitch(1.0),
@@ -153,7 +163,12 @@ SGSoundSample::SGSoundSample( unsigned c
     reference_dist(500.0),
     max_dist(3000.),
     loop(AL_FALSE),
-    playing(false)
+#ifndef USE_OPEN_AL_DOPPLER
+    doppler_pitch_factor(1),
+    doppler_volume_factor(1),
+#endif
+    playing(false),
+    no_Doppler_effect(_no_Doppler_effect)
 {
     SG_LOG( SG_GENERAL, SG_DEBUG, "In memory sounds sample" );
 
@@ -254,7 +269,9 @@ SGSoundSample::bind_source() {
     alSourcef( source, AL_CONE_INNER_ANGLE, inner );
     alSourcef( source, AL_CONE_OUTER_ANGLE, outer );
     alSourcef( source, AL_CONE_OUTER_GAIN, outergain);
+#ifdef USE_OPEN_AL_DOPPLER
     alSourcefv( source, AL_VELOCITY, source_vel );
+#endif
     alSourcei( source, AL_LOOPING, loop );
 
     alSourcei( source, AL_SOURCE_RELATIVE, AL_TRUE );
@@ -273,8 +290,22 @@ SGSoundSample::set_pitch( double p ) {
     if ( p > 2.0 ) { p = 2.0; }
     pitch = p;
     if (playing) {
+#ifdef USE_OPEN_AL_DOPPLER
         alSourcef( source, AL_PITCH, pitch );
-        print_openal_error("set_pitch");
+#else
+ #ifdef AL_VERSION_1_1
+        alSourcef( source, AL_PITCH, pitch * doppler_pitch_factor );
+ #else
+        p*=doppler_pitch_factor;
+        if ( p < 0.01 ) { p = 0.01; }
+        if ( p > 2.0 ) { p = 2.0; }
+        alSourcef( source, AL_PITCH, p );
+ #endif
+#endif
+        //print_openal_error("set_pitch");
+        char t[256];
+        sprintf(t,"set_pitch: pit: %6.2f dpf: 
%6.2f",pitch,doppler_pitch_factor);
+        print_openal_error(t);
     }
 }
 
@@ -282,7 +313,11 @@ void
 SGSoundSample::set_volume( double v ) {
     volume = v;
     if (playing) {
+#ifdef USE_OPEN_AL_DOPPLER
         alSourcef( source, AL_GAIN, volume );
+#else
+        alSourcef( source, AL_GAIN, volume * doppler_volume_factor );
+#endif
         print_openal_error("set_volume");
     }
 }
@@ -313,6 +348,7 @@ SGSoundSample::set_source_pos( ALfloat *
         sgAddVec3( final_pos, source_pos, offset_pos );
 
         alSourcefv( source, AL_POSITION, final_pos );
+        print_openal_error("set_source_pos");
     }
 }
 
@@ -327,6 +363,7 @@ SGSoundSample::set_offset_pos( ALfloat *
         sgAddVec3( final_pos, source_pos, offset_pos );
 
         alSourcefv( source, AL_POSITION, final_pos );
+        print_openal_error("set_offset_pos");
     }
 }
 
@@ -350,13 +387,91 @@ SGSoundSample::set_orientation( ALfloat 
 }
 
 void
-SGSoundSample::set_source_vel( ALfloat *vel ) {
-    source_vel[0] = vel[0];
-    source_vel[1] = vel[1];
-    source_vel[2] = vel[2];
+SGSoundSample::set_source_vel( ALfloat *vel , ALfloat *listener_vel ) {
+    if (no_Doppler_effect) {
+        source_vel[0] = listener_vel[0];
+        source_vel[1] = listener_vel[1];
+        source_vel[2] = listener_vel[2];
+    } else {
+        source_vel[0] = vel[0];
+        source_vel[1] = vel[1];
+        source_vel[2] = vel[2];
+    }
+#ifdef USE_OPEN_AL_DOPPLER
     if (playing) {
         alSourcefv( source, AL_VELOCITY, source_vel );
     }
+#else
+    if (no_Doppler_effect) {
+        doppler_pitch_factor = 1;
+        doppler_volume_factor = 1;
+
+        /*if (playing) {
+            alSourcefv( source, AL_VELOCITY, source_vel );
+            print_openal_error("set_source_vel_no_doppler");
+        }*/
+        return;
+    }
+    double doppler, mfp;
+    sgVec3 final_pos;
+    sgAddVec3( final_pos, source_pos, offset_pos );
+    mfp = sgLengthVec3(final_pos);
+    if (mfp > 1e-6) {
+        double vls = - sgScalarProductVec3( listener_vel, final_pos ) / mfp;
+        double vss = - sgScalarProductVec3( source_vel, final_pos ) / mfp;
+        if (fabs(340 - vss) > 1e-6)
+        {
+            doppler = (340 - vls) / (340 - vss);
+            doppler = ( doppler > 0) ? ( ( doppler < 10) ? doppler : 10 ) : 0;
+        }
+        else
+            doppler = 0;
+    }
+    else
+        doppler = 1;
+    /* the OpenAL documentation of the Doppler calculation
+    SS: AL_SPEED_OF_SOUND = speed of sound (default value 343.3)
+    DF: AL_DOPPLER_FACTOR = Doppler factor (default 1.0)
+    vls: Listener velocity scalar (scalar, projected on source-to-listener 
vector)
+    vss: Source velocity scalar (scalar, projected on source-to-listener 
vector)
+    SL = source to listener vector
+    SV = Source Velocity vector
+    LV = Listener Velocity vector
+    vls = DotProduct(SL, LV) / Mag(SL)
+    vss = DotProduct(SL, SV) / Mag(SL)
+    Dopper Calculation:
+    vss = min(vss, SS/DF)
+    vls = min(vls, SS/DF)
+    f' = f * (SS - DF*vls) / (SS - DF*vss)
+    */
+    if (doppler > 0.1) {
+        doppler_pitch_factor = doppler;
+        doppler_volume_factor = 1;
+    }
+    else {
+        doppler_pitch_factor = 0.1;
+        doppler_volume_factor = (doppler > 0) ? doppler * 10 : 0;
+    }
+    if (playing) {
+        alSourcef( source, AL_GAIN, volume * doppler_volume_factor );
+        //print_openal_error("set_source_vel: volume");
+        char t[256];
+        sprintf(t,"set_source_vel: vol: %6.2f dvf: 
%6.2f",volume,doppler_volume_factor);
+        print_openal_error(t);
+#ifdef AL_VERSION_1_1
+        alSourcef( source, AL_PITCH, pitch * doppler_pitch_factor );
+ #else
+        double p=pitch*doppler_pitch_factor;
+        if ( p < 0.01 ) { p = 0.01; }
+        if ( p > 2.0 ) { p = 2.0; }
+        alSourcef( source, AL_PITCH, p );
+ #endif
+        //print_openal_error("set_source_vel: pitch");
+        sprintf(t,"set_source_vel: pit: %6.2f dpf: 
%6.2f",pitch,doppler_pitch_factor);
+        print_openal_error(t);
+
+    }
+#endif
 }
 
 void
Index: sound/sample_openal.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/sound/sample_openal.hxx,v
retrieving revision 1.17
diff -u -p -r1.17 sample_openal.hxx
--- sound/sample_openal.hxx     8 Mar 2006 18:16:09 -0000       1.17
+++ sound/sample_openal.hxx     26 Jun 2007 21:57:20 -0000
@@ -52,6 +52,11 @@
 # include <AL/alut.h>
 #endif
 
+#ifndef HAVE_WINDOWS_H
+#define USE_OPEN_AL_DOPPLER should work
+//the Open_AL Doppler calculation seem to be buggy on windows
+#endif
+
 SG_USING_STD(string);
 
 /**
@@ -90,12 +95,17 @@ private:
 
     double pitch;
     double volume;
+#ifndef USE_OPEN_AL_DOPPLER
+    double doppler_pitch_factor;
+    double doppler_volume_factor;
+#endif
     double reference_dist;
     double max_dist;
     ALboolean loop;
 
     bool playing;
     bool bind_source();
+    bool no_Doppler_effect;
 
 public:
 
@@ -112,7 +122,7 @@ public:
        should usually be true unless you want to manipulate the data
        later.)
      */
-    SGSoundSample( const char *path, const char *file );
+    SGSoundSample( const char *path, const char *file , bool no_Doppler_effect 
= true );
 
     /**
      * Constructor.
@@ -123,7 +133,7 @@ public:
        should usually be true unless you want to manipulate the data
        later.)
      */
-    SGSoundSample( unsigned char *_data, int len, int _freq );
+    SGSoundSample( unsigned char *_data, int len, int _freq , bool 
no_Doppler_effect = true );
 
     ~SGSoundSample();
 
@@ -208,7 +218,7 @@ public:
     /**
      * Set velocity of sound source (uses same coordinate system as opengl)
      */
-    void set_source_vel( ALfloat *vel );
+    void set_source_vel( ALfloat *vel , ALfloat *listener_vel );
 
 
     /**
Index: sound/soundmgr_openal.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/sound/soundmgr_openal.cxx,v
retrieving revision 1.25
diff -u -p -r1.25 soundmgr_openal.cxx
--- sound/soundmgr_openal.cxx   22 Oct 2006 19:42:17 -0000      1.25
+++ sound/soundmgr_openal.cxx   26 Jun 2007 21:57:21 -0000
@@ -345,6 +345,6 @@ void SGSoundMgr::set_source_vel_all( ALf
     sample_map_iterator sample_end = samples.end();
     for ( ; sample_current != sample_end; ++sample_current ) {
        SGSoundSample *sample = sample_current->second;
-        sample->set_source_vel( vel );
+        sample->set_source_vel( vel , listener_vel );
     }
 }
Index: sound/soundmgr_openal.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/sound/soundmgr_openal.hxx,v
retrieving revision 1.8
diff -u -p -r1.8 soundmgr_openal.hxx
--- sound/soundmgr_openal.hxx   8 Mar 2006 18:16:09 -0000       1.8
+++ sound/soundmgr_openal.hxx   26 Jun 2007 21:57:23 -0000
@@ -206,7 +206,9 @@ public:
         listener_vel[0] = vel[0];
         listener_vel[1] = vel[1];
         listener_vel[2] = vel[2];
+#ifdef USE_OPEN_AL_DOPPLER
         alListenerfv( AL_VELOCITY, listener_vel );
+#endif
     }
 
     /**
Index: sound/xmlsound.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/sound/xmlsound.cxx,v
retrieving revision 1.21
diff -u -p -r1.21 xmlsound.cxx
--- sound/xmlsound.cxx  23 Jun 2007 16:48:01 -0000      1.21
+++ sound/xmlsound.cxx  26 Jun 2007 21:57:23 -0000
@@ -272,7 +272,8 @@ SGXmlSound::init(SGPropertyNode *root, S
        // "alSource".  The semantics of what is going on here seems
        // confused and needs to be thought through more carefully.
         _sample = new SGSoundSample( path.c_str(),
-                                    node->getStringValue("path", "") );
+                                    node->getStringValue("path", ""),
+                                    false );
 
        _mgr->add( _sample, _name );
    }
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to