00001 #include <stdio.h>
00002
00003 #ifdef _WIN32
00004 #include <windows.h>
00005 #define sleep(x) Sleep(x*1000)
00006 #else
00007 #include <unistd.h>
00008 #endif
00009
00010 #if defined( __APPLE__ )
00011 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
00012 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
00013 # include <OpenAL/al.h>
00014 # include <OpenAL/alc.h>
00015 # include <OpenAL/alut.h>
00016 #elif defined(OPENALSDK)
00017 # include <al.h>
00018 # include <alc.h>
00019 # include <AL/alut.h>
00020 #else
00021 # include <AL/al.h>
00022 # include <AL/alc.h>
00023 # include <AL/alut.h>
00024 #endif
00025
00026 #define AUDIOFILE SRC_DIR"/jet.wav"
00027
00028 #include <simgear/debug/logstream.hxx>
00029
00030 static void print_openal_error( ALuint error ) {
00031 if ( error == AL_INVALID_NAME ) {
00032 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
00033 } else if ( error == AL_ILLEGAL_ENUM ) {
00034 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
00035 } else if ( error == AL_INVALID_VALUE ) {
00036 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
00037 } else if ( error == AL_ILLEGAL_COMMAND ) {
00038 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
00039 } else if ( error == AL_OUT_OF_MEMORY ) {
00040 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
00041 } else {
00042 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
00043 }
00044 }
00045
00046
00047 int main( int argc, char *argv[] ) {
00048
00049 ALCdevice *dev;
00050 ALCcontext *context;
00051
00052 alutInit(&argc, argv);
00053 sglog().setLogLevels( SG_ALL, SG_ALERT );
00054
00055
00056 if ( (dev = alcOpenDevice( NULL )) != NULL
00057 && ( context = alcCreateContext( dev, NULL )) != NULL ) {
00058 alcMakeContextCurrent( context );
00059 } else {
00060 context = 0;
00061 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
00062 }
00063
00064
00065 ALfloat listener_pos[3];
00066
00067
00068 ALfloat listener_vel[3];
00069
00070
00071
00072 ALfloat listener_ori[6];
00073
00074 listener_pos[0] = 0.0;
00075 listener_pos[1] = 0.0;
00076 listener_pos[2] = 0.0;
00077
00078 listener_vel[0] = 0.0;
00079 listener_vel[1] = 0.0;
00080 listener_vel[2] = 0.0;
00081
00082 listener_ori[0] = 0.0;
00083 listener_ori[1] = 0.0;
00084 listener_ori[2] = -1.0;
00085 listener_ori[3] = 0.0;
00086 listener_ori[4] = 1.0;
00087 listener_ori[5] = 0.0;
00088
00089 alListenerfv( AL_POSITION, listener_pos );
00090 alListenerfv( AL_VELOCITY, listener_vel );
00091 alListenerfv( AL_ORIENTATION, listener_ori );
00092
00093
00094 ALuint buffer;
00095
00096
00097 ALuint source;
00098
00099
00100 ALfloat source_pos[3];
00101
00102
00103 ALfloat source_vel[3];
00104
00105
00106
00107
00108
00109
00110 ALboolean loop = false;
00111
00112 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
00113 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
00114
00115
00116 alGenBuffers(1, &buffer);
00117 ALuint error = alGetError();
00118 if ( error != AL_NO_ERROR ) {
00119 print_openal_error( error );
00120 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
00121 } else {
00122 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
00123 }
00124
00125
00126 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
00127
00128 buffer = alutCreateBufferFromFile(AUDIOFILE);
00129 if (buffer == AL_NONE) {
00130 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
00131 }
00132
00133 #else
00134 # if defined (__APPLE__)
00135 alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq );
00136 # else
00137 alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq, &loop );
00138 # endif
00139 if (alGetError() != AL_NO_ERROR) {
00140 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
00141 }
00142
00143
00144 alBufferData( buffer, format, data, size, freq );
00145 if (alGetError() != AL_NO_ERROR) {
00146 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
00147 }
00148
00149 alutUnloadWAV( format, data, size, freq );
00150 #endif
00151
00152 alGenSources(1, &source);
00153 if (alGetError() != AL_NO_ERROR) {
00154 print_openal_error( error );
00155 }
00156
00157 alSourcei( source, AL_BUFFER, buffer );
00158 alSourcef( source, AL_PITCH, 1.0 );
00159 alSourcef( source, AL_GAIN, 1.0 );
00160 alSourcefv( source, AL_POSITION, source_pos );
00161 alSourcefv( source, AL_VELOCITY, source_vel );
00162 alSourcei( source, AL_LOOPING, loop );
00163
00164 alSourcePlay( source );
00165
00166 sleep(10);
00167 alutExit();
00168
00169 return 0;
00170 }