#include <stdio.h>
+#include <cstdlib> // EXIT_FAILURE
-#ifdef __MINGW32__
-// This is broken, but allows the file to compile without a POSIX
-// environment.
-static unsigned int sleep(unsigned int secs) { return 0; }
+#ifdef _WIN32
+#include <windows.h>
+#define sleep(x) Sleep(x*1000)
#else
#include <unistd.h> // sleep()
#endif
# define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
# include <OpenAL/al.h>
# include <OpenAL/alc.h>
-# include <OpenAL/alut.h>
+#elif defined(OPENALSDK)
+# include <al.h>
+# include <alc.h>
#else
# include <AL/al.h>
# include <AL/alc.h>
-# include <AL/alut.h>
#endif
#define AUDIOFILE SRC_DIR"/jet.wav"
+#include <simgear/sound/readwav.hxx>
#include <simgear/debug/logstream.hxx>
+#include <simgear/misc/sg_path.hxx>
static void print_openal_error( ALuint error ) {
if ( error == AL_INVALID_NAME ) {
}
-int main( int argc, char *argv[] ) {
- // initialize OpenAL
- ALCdevice *dev;
- ALCcontext *context;
-
+int main( int argc, char *argv[] )
+{
+ sglog().setLogLevels( SG_ALL, SG_ALERT );
+
// initialize OpenAL
- if ( (dev = alcOpenDevice( NULL )) != NULL
- && ( context = alcCreateContext( dev, NULL )) != NULL ) {
- alcMakeContextCurrent( context );
- } else {
- context = 0;
- SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+ ALCdevice *dev = alcOpenDevice(NULL);
+ if (!dev) {
+ SG_LOG( SG_GENERAL, SG_ALERT, "Audio device initialization failed!" );
+ return EXIT_FAILURE;
+ }
+
+ ALCcontext *context = alcCreateContext(dev, NULL);
+ if (!context) {
+ SG_LOG( SG_GENERAL, SG_ALERT, "Audio context initialization failed!" );
+ return EXIT_FAILURE;
}
+
+ alcMakeContextCurrent( context );
// Position of the listener.
ALfloat listener_pos[3];
ALfloat source_vel[3];
// configuration values
-// ALenum format;
-// ALsizei size;
-// ALvoid* data;
-// ALsizei freq;
ALboolean loop = false;
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;
- // create an OpenAL buffer handle
- alGenBuffers(1, &buffer);
- ALuint error = alGetError();
- if ( error != AL_NO_ERROR ) {
- print_openal_error( error );
- SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
- } else {
- SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
- }
-
// Load the sample file
-#if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
-
- buffer = alutCreateBufferFromFile(AUDIOFILE);
- if (buffer == AL_NONE) {
- SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
- }
-
-#else
-# if defined (__APPLE__)
- alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq );
-# else
- alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq, &loop );
-# endif
- if (alGetError() != AL_NO_ERROR) {
- SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
- }
-
- // Copy data to the internal OpenAL buffer
- alBufferData( buffer, format, data, size, freq );
- if (alGetError() != AL_NO_ERROR) {
+ buffer = simgear::createBufferFromFile(SGPath(AUDIOFILE));
+ if (buffer == AL_NONE) {
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
- }
-
- alutUnloadWAV( format, data, size, freq );
-#endif
+ }
alGenSources(1, &source);
if (alGetError() != AL_NO_ERROR) {
+ ALuint error = alGetError();
print_openal_error( error );
}
alSourcePlay( source );
sleep(10);
-
+
+ alcMakeContextCurrent(NULL);
+ alcDestroyContext(context);
+ alcCloseDevice(dev);
+
return 0;
}