SimGear Sound Question - 2006-10-02

index

Sunday, 01 October 2006. SimGear - Sound Fix?

In Windows XP, using SimGear/FlightGear CVS HEAD, I was getting NO SOUND! The build was using MSVC8, but using the DSW/DSP build files, thus pthreads is not enabled.

I had to put -

#ifdef  ENABLE_THREADS
...
#endif // #ifdef  ENABLE_THREADS

around both SGThread.cxx, SGThread.hxx, since these are NOT required for a single threaded version. This would not be needed if you intend running FlightGear in a 'threaded' mode ... this SG/FG compile option should NOT be confused with the fact that I always choose the Multi-threaded (Debug) runtime library (/MT[d]) ...

I did some work on openal_test1 and openal_test2 to compile and run them in windows. This showed, that while openal_test1 worked fine, openal_test2 also produced no sound.

openal_test1 is a direct use of the OpenAL libraries to play a WAV file, while openal_test2 is using the SGSoundMgr class to play the same file.

The 'fix' I did was SMALL, but it may have other consequences not desired ... in soundmgr_openal.cxx - I commented out the setting of the GAIN to 0, and replaced it with setting the GAIN to 1 - like -

    //alListenerf( AL_GAIN, 0.0f );
    alListenerf( AL_GAIN, 1.0f );

That is I had to make the GAIN of the 'listener' 1 instead of zero ...

After this small change, SOUND worked in both openal_test2, and in FlightGear ... not sure why 'others' have not had this problem ... at least I have note read about it on the development board ...

This is done in the 'constructor' of the sound manager ...

// constructor
SGSoundMgr::SGSoundMgr() {

The SGSoundMgr gets instantiated as part of FGGlobals - class FGGlobals - in globals.hxx, no where can I find the 'listener' GAIL is ever elevated ... but there are MANY calls to get the sound manager, and I have not checked them all ;=()

There is a case in fg_fx.cxx to set this -

    double volume = _volume->getDoubleValue();
    if ( volume != last_volume ) {
        smgr->set_volume( volume );
        last_volume = volume;
    }

but the last_volume is always equal volume, so it is never set ... but the value is ZERO anyway, so even if done here, this does not seem to improve the situation ;=))

The diff includes the changes to openal_test1 and openal_test2 to get them to compile and link using MSVC8 ... mainly including <windows.h>, and using Sleep(ms) to replace sleep(secs) ...

I also made the location of the 'jet.wav' file more flexible ... that is you only have to change it once at the top of each file ... and in windows, it seems you get no logging output until the sglog() is initialised.

For some strange reason I had to un-define UNICODE in openal_test2 before I could get it to compile, and have left an output message pragma to REMIND me to look at this some time ... it should NOT have been necessary ... but I was interested in testing SOUND, not playing with more compiler options ... ;=((

index

<diff>

diff -ur SimGear\source\simgear\sound\openal_test1.cxx F:\FG0910-4\SimGear\simgear\sound\openal_test1.cxx
--- SimGear\source\simgear\sound\openal_test1.cxx    Tue Oct 25 16:06:25 2005
+++ F:\FG0910-4\SimGear\simgear\sound\openal_test1.cxx     Wed Sep 13 13:04:43 2006
@@ -1,13 +1,19 @@
 #include <stdio.h>
-
 #ifdef __MINGW32__
 // This is broken, but allows the file to compile without a POSIX
 // environment.
 static unsigned int sleep(unsigned int secs) { return 0; }
 #else
+#ifdef _MSC_VER
+#include    <windows.h> // for Sleep
+#define sleep(a)    Sleep(a * 1000)
+#else // !_MSC_VER
 #include <unistd.h>    // sleep()
+#endif // _MSC_VER y/n
 #endif

+#define DEF_WAV_FILE    "f:/FG0910-4/simgear/simgear/sound/jet.wav"
+
 #if defined( __APPLE__ )
 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
@@ -40,11 +46,25 @@


 int main( int argc, char *argv[] ) {
+
+    sglog().setLogLevels( SG_ALL, SG_DEBUG ); // init log
+
     // initialize OpenAL
     ALCdevice *dev;
     ALCcontext *context;

     // initialize OpenAL
+#ifdef  _MSC_VER
+    if ( ( alutInit (NULL, NULL) ) &&
+         ( ( context = alcGetCurrentContext() ) != NULL ) &&
+         ( ( dev = alcGetContextsDevice( context ) ) != NULL ) )
+    {
+        SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization succeeded." );
+    } else {
+        SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+    }
+
+#else // !#ifdef _MSC_VER
     if ( (dev = alcOpenDevice( NULL )) != NULL
             && ( context = alcCreateContext( dev, NULL )) != NULL ) {
         alcMakeContextCurrent( context );
@@ -52,6 +72,7 @@
         context = 0;
         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
     }
+#endif // !#ifdef _MSC_VER y/n

     // Position of the listener.
     ALfloat listener_pos[3];
@@ -95,11 +116,11 @@
     ALfloat source_vel[3];

     // configuration values
-    ALenum format;
-    ALsizei size;
-    ALvoid* data;
-    ALsizei freq;
-    ALboolean loop;
+    //ALenum format;
+    //ALsizei size;
+    //ALvoid* data;
+    //ALsizei freq;
+    ALboolean loop = true;

     source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
     source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
@@ -117,16 +138,16 @@
     // Load the sample file
 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1

-  buffer = alutCreateBufferFromFile("jet.wav");
+  buffer = alutCreateBufferFromFile(DEF_WAV_FILE);
   if (buffer == AL_NONE) {
     SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
   }

 #else
 # if defined (__APPLE__)
-    alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq );
+    alutLoadWAVFile( (ALbyte *)DEF_WAV_FILE, &format, &data, &size, &freq );
 # else
-    alutLoadWAVFile( (ALbyte *)"jet.wav", &format, &data, &size, &freq, &loop );
+    alutLoadWAVFile( (ALbyte *)DEF_WAV_FILE, &format, &data, &size, &freq, &loop );
 # endif
     if (alGetError() != AL_NO_ERROR) {
         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
@@ -156,6 +177,10 @@
     alSourcePlay( source );

     sleep(10);
+
+    alDeleteSources( 1, &source  );
+    alDeleteBuffers( 1, &buffer );
+    alutExit();

     return 0;
 }
diff -ur SimGear\source\simgear\sound\openal_test2.cxx F:\FG0910-4\SimGear\simgear\sound\openal_test2.cxx
--- SimGear\source\simgear\sound\openal_test2.cxx    Sat Nov 12 12:26:21 2005
+++ F:\FG0910-4\SimGear\simgear\sound\openal_test2.cxx     Sat Sep 30 14:38:47 2006
@@ -1,54 +1,84 @@
+#ifdef _MSC_VER
+#ifdef  UNICODE
+#undef  UNICODE
+#pragma message( "1 - UNICODE IS DEFINED - now undefined ..." )
+#else
+#pragma message( "1 - UNICODE IS NOT DEFINED" )
+#endif  // UNICODE y/n
+#endif // _MSC_VER
+
 #include <stdio.h>
 #ifdef __MINGW32__
 // This is broken, but allows the file to compile without a POSIX
 // environment.
 static unsigned int sleep(unsigned int secs) { return 0; }
 #else
+#ifdef _MSC_VER
+#include    <windows.h> // for Sleep
+#define sleep(a)    Sleep(a * 1000)
+#pragma warning(disable:4101)
+#define DEF_FILE    "jet.wav"
+#define DEF_PATH    "f:\\FG0910-4\\simgear\\simgear\\sound"
+#else // !_MSC_VER
 #include <unistd.h>    // sleep()
+#endif // _MSC_VER y/n
 #endif

 #include "sample_openal.hxx"
 #include "soundmgr_openal.hxx"

-
-int main( int argc, char *argv[] ) {
+void play_sounds( void )
+{
+    SG_LOG( SG_GENERAL, SG_DEBUG, "Initialising sound manager" );
     SGSoundMgr sm;

-    SGSoundSample sample1( ".", "jet.wav" );
+    //sm.set_volume( 1.0f );
+
+    SG_LOG( SG_GENERAL, SG_DEBUG, "Openning " << DEF_PATH << "/" << DEF_FILE );
+    SGSoundSample sample1( DEF_PATH, DEF_FILE );
     sample1.set_volume(0.5);
     sample1.set_volume(0.2);
     sample1.play_looped();
     sleep(1);

-    SGSoundSample sample2( ".", "jet.wav" );
+    SGSoundSample sample2( DEF_PATH, DEF_FILE );
     sample2.set_volume(0.5);
     sample2.set_pitch(0.4);
     sample2.play_looped();
     sleep(1);

-    SGSoundSample sample3( ".", "jet.wav" );
+    SGSoundSample sample3( DEF_PATH, DEF_FILE );
     sample3.set_volume(0.5);
     sample3.set_pitch(0.8);
     sample3.play_looped();
     sleep(1);
 
-    SGSoundSample sample4( ".", "jet.wav" );
+    SGSoundSample sample4( DEF_PATH, DEF_FILE );
     sample4.set_volume(0.5);
     sample4.set_pitch(1.2);
     sample4.play_looped();
     sleep(1);
 
-    SGSoundSample sample5( ".", "jet.wav" );
+    SGSoundSample sample5( DEF_PATH, DEF_FILE );
     sample5.set_volume(0.5);
     sample5.set_pitch(1.6);
     sample5.play_looped();
     sleep(1);
 
-    SGSoundSample sample6( ".", "jet.wav" );
+    SGSoundSample sample6( DEF_PATH, DEF_FILE );
     sample6.set_volume(0.5);
     sample6.set_pitch(2.0);
     sample6.play_looped();
     sleep(1);
-
     sleep(10);
+}
+
+
+int main( int argc, char *argv[] )
+{
+    sglog().setLogLevels( SG_ALL, SG_DEBUG );
+    play_sounds();
+    sleep(1);
 }
diff -ur SimGear\source\simgear\sound\soundmgr_openal.cxx F:\FG0910-4\SimGear\simgear\sound\soundmgr_openal.cxx
--- SimGear\source\simgear\sound\soundmgr_openal.cxx Thu Apr 27 17:10:44 2006
+++ F:\FG0910-4\SimGear\simgear\sound\soundmgr_openal.cxx  Wed Sep 13 13:34:30 2006
@@ -125,7 +125,8 @@
     listener_ori[4] = 1.0;
     listener_ori[5] = 0.0;
 
-    alListenerf( AL_GAIN, 0.0f );
+    //alListenerf( AL_GAIN, 0.0f );
+    alListenerf( AL_GAIN, 1.0f );
     alListenerfv( AL_POSITION, listener_pos );
     alListenerfv( AL_VELOCITY, listener_vel );
     alListenerfv( AL_ORIENTATION, listener_ori );
diff -ur SimGear\source\simgear\threads\SGThread.cxx F:\FG0910-4\SimGear\simgear\threads\SGThread.cxx
--- SimGear\source\simgear\threads\SGThread.cxx      Sun Jan 09 12:24:54 2005
+++ F:\FG0910-4\SimGear\simgear\threads\SGThread.cxx Sun Sep 03 20:07:34 2006
@@ -14,6 +14,8 @@
 #  include "winsock2.h"
 #endif
 
+#ifdef  ENABLE_THREADS
+
 #include "SGThread.hxx"
 
 void*
@@ -104,4 +106,6 @@
     assert( status == 0 );
     return true;
 }
+
+#endif // #ifdef  ENABLE_THREADS
 
diff -ur SimGear\source\simgear\threads\SGThread.hxx F:\FG0910-4\SimGear\simgear\threads\SGThread.hxx
--- SimGear\source\simgear\threads\SGThread.hxx      Thu Mar 09 17:06:51 2006
+++ F:\FG0910-4\SimGear\simgear\threads\SGThread.hxx Sun Sep 03 20:06:45 2006
@@ -25,6 +25,7 @@
 
 #include <simgear/compiler.h>
 
+#ifdef  ENABLE_THREADS
 #include <pthread.h>
 #if defined ( SG_HAVE_STD_INCLUDES )
 #  include <cassert>
@@ -332,5 +333,6 @@
     int status = pthread_cond_wait( &cond, &mutex.mutex );
     assert( status == 0 );
 }
+#endif // #ifdef  ENABLE_THREADS
 
 #endif /* SGTHREAD_HXX_INCLUDED */

</diff>

index

This is also a diff of my MSVC8 solution files, but they also contain a change in the 'copying' of simgear_config.h.vc5 to simgear_config.h ... instead of doing the COPY every time, I added a make file, so that the copying would only be done if simgear_config.h was out of date ... that is changing the command line from 'copy' to running nmake reading my makeconf.mak file - that is -

-                       Description="Generates Simgear_config.h"
-                       CommandLine="copy ..\..\Simgear\simgear_config.h.vc5  ..\..\Simgear\simgear_config.h"
+                       Description="Generating simgear_config.h ..."
+                       CommandLine="nmake -nologo -f ..\..\simgear\makeconf.mak"
                  />

This is a full diff of the 'solution' files ... and below that I have include the makeconf.mak file contents ...

<diff2>

diff -ur SimGear\source\projects\VC8\SimGear.sln F:\FG0910-4\SimGear\projects\VC8\SimGear.sln
--- SimGear\source\projects\VC8\SimGear.sln    Fri Mar 24 20:06:05 2006
+++ F:\FG0910-4\SimGear\projects\VC8\SimGear.sln     Wed Sep 13 12:23:12 2006
@@ -3,6 +3,16 @@
 # Visual C++ Express 2005
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimGear", "SimGear.vcproj", "{952B5B5B-7FC8-4AE9-A664-333322BEEEB0}"
 EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "openal_test1", "..\openal_test1\openal_test1.vcproj", "{24B5C086-8968-40F2-A7FC-1AF31D9A91DB}"
+     ProjectSection(ProjectDependencies) = postProject
+           {952B5B5B-7FC8-4AE9-A664-333322BEEEB0} = {952B5B5B-7FC8-4AE9-A664-333322BEEEB0}
+     EndProjectSection
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "openal_test2", "..\openal_test2\openal_test2.vcproj", "{F1FA197F-EF3C-4ACF-AA19-9EC3F6BE043D}"
+     ProjectSection(ProjectDependencies) = postProject
+           {952B5B5B-7FC8-4AE9-A664-333322BEEEB0} = {952B5B5B-7FC8-4AE9-A664-333322BEEEB0}
+     EndProjectSection
+EndProject
 Global
      GlobalSection(SolutionConfigurationPlatforms) = preSolution
            Debug|Win32 = Debug|Win32
@@ -13,6 +23,14 @@
            {952B5B5B-7FC8-4AE9-A664-333322BEEEB0}.Debug|Win32.Build.0 = Debug|Win32
            {952B5B5B-7FC8-4AE9-A664-333322BEEEB0}.Release|Win32.ActiveCfg = Release|Win32
            {952B5B5B-7FC8-4AE9-A664-333322BEEEB0}.Release|Win32.Build.0 = Release|Win32
+           {24B5C086-8968-40F2-A7FC-1AF31D9A91DB}.Debug|Win32.ActiveCfg = Debug|Win32
+           {24B5C086-8968-40F2-A7FC-1AF31D9A91DB}.Debug|Win32.Build.0 = Debug|Win32
+           {24B5C086-8968-40F2-A7FC-1AF31D9A91DB}.Release|Win32.ActiveCfg = Release|Win32
+           {24B5C086-8968-40F2-A7FC-1AF31D9A91DB}.Release|Win32.Build.0 = Release|Win32
+           {F1FA197F-EF3C-4ACF-AA19-9EC3F6BE043D}.Debug|Win32.ActiveCfg = Debug|Win32
+           {F1FA197F-EF3C-4ACF-AA19-9EC3F6BE043D}.Debug|Win32.Build.0 = Debug|Win32
+           {F1FA197F-EF3C-4ACF-AA19-9EC3F6BE043D}.Release|Win32.ActiveCfg = Release|Win32
+           {F1FA197F-EF3C-4ACF-AA19-9EC3F6BE043D}.Release|Win32.Build.0 = Release|Win32
      EndGlobalSection
      GlobalSection(SolutionProperties) = preSolution
            HideSolutionNode = FALSE
diff -ur SimGear\source\projects\VC8\SimGear.vcproj F:\FG0910-4\SimGear\projects\VC8\SimGear.vcproj
--- SimGear\source\projects\VC8\SimGear.vcproj Sat Sep 30 11:38:58 2006
+++ F:\FG0910-4\SimGear\projects\VC8\SimGear.vcproj  Tue Sep 05 15:19:59 2006
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>
 <VisualStudioProject
      ProjectType="Visual C++"
-     Version="8,00"
+     Version="8.00"
      Name="SimGear"
      ProjectGUID="{952B5B5B-7FC8-4AE9-A664-333322BEEEB0}"
      RootNamespace="SimGear"
@@ -23,11 +23,14 @@
                  >
                  <Tool
                        Name="VCPreBuildEventTool"
-                       Description="Generates Simgear_config.h"
-                       CommandLine="copy ..\..\Simgear\simgear_config.h.vc5  ..\..\Simgear\simgear_config.h"
+                       Description="Generating simgear_config.h ..."
+                       CommandLine="nmake -nologo -f ..\..\simgear\makeconf.mak"
                  />
                  <Tool
                        Name="VCCustomBuildTool"
+                       Description=""
+                       CommandLine=""
+                       Outputs=""
                  />
                  <Tool
                        Name="VCXMLDataGeneratorTool"
@@ -41,8 +44,8 @@
                  <Tool
                        Name="VCCLCompilerTool"
                        Optimization="0"
-                       AdditionalIncludeDirectories="../..;../../..;../../Simgear;../../../devel/include"
-                       PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;HAVE_CONFIG_H;ENABLE_THREADS;_CRT_SECURE_NO_DEPRECATE;_CONST_CORRECT_OVERLOADS;NOMINMAX;_USE_MATH_DEFINES"
+                       AdditionalIncludeDirectories="../..;../../..;../../Simgear;../../../OpenAL/include;../../../zlib-1.2.3;..\..\..\OpenAL\alut\include;..\..\..\OpenAL\include\AL"
+                       PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;HAVE_CONFIG_H;_CRT_SECURE_NO_DEPRECATE;_CONST_CORRECT_OVERLOADS;NOMINMAX;_USE_MATH_DEFINES"
                        MinimalRebuild="true"
                        BasicRuntimeChecks="3"
                        RuntimeLibrary="1"
@@ -103,8 +106,8 @@
                  <Tool
                        Name="VCCLCompilerTool"
                        Optimization="2"
-                       AdditionalIncludeDirectories="../..;../../..;../../Simgear;../../../devel/include"
-                       PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;HAVE_CONFIG_H;ENABLE_THREADS;_CRT_SECURE_NO_DEPRECATE;_CONST_CORRECT_OVERLOADS;NOMINMAX;_USE_MATH_DEFINES"
+                       AdditionalIncludeDirectories="../..;../../..;../../Simgear;../../../OpenAL/include;../../../zlib-1.2.3;..\..\..\OpenAL\alut\include;..\..\..\OpenAL\include\AL"
+                       PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;HAVE_CONFIG_H;_CRT_SECURE_NO_DEPRECATE;_CONST_CORRECT_OVERLOADS;NOMINMAX;_USE_MATH_DEFINES"
                        StringPooling="true"
                        MinimalRebuild="true"
                        RuntimeLibrary="0"
@@ -508,10 +511,6 @@
                        >
                  </File>
                  <File
-                       RelativePath="..\..\simgear\math\SGCMath.hxx"
-                       >
-                 </File>
-                 <File
                        RelativePath="..\..\simgear\math\SGGeoc.hxx"
                        >
                  </File>
@@ -536,10 +535,6 @@
                        >
                  </File>
                  <File
-                       RelativePath="..\..\simgear\math\SGMathFwd.hxx"
-                       >
-                 </File>
-                 <File
                        RelativePath="..\..\simgear\math\SGMatrix.hxx"
                        >
                  </File>
@@ -597,6 +592,7 @@
                                   Name="VCCustomBuildTool"
                                   Description=""
                                   CommandLine=""
+                                  AdditionalDependencies=""
                                   Outputs=""
                             />
                        </FileConfiguration>

</diff2>

<makeconf.mak>

# make config file ...

dst   =     ..\..\simgear\simgear_config.h
src   =     ..\..\simgear\simgear_config.h.vc5
#dst  =     simgear_config.h
#src  =     simgear_config.h.vc5

all:  $(dst)

$(dst):     $(src)
      copy $(src) $(dst) > nul

# eof

</makeconf.mak>

index

EOF - fgd-008.doc

top