2 #include <cstdlib> // EXIT_FAILURE
6 #define sleep(x) Sleep(x*1000)
8 #include <unistd.h> // sleep()
11 #if defined( __APPLE__ )
12 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
13 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
14 # include <OpenAL/al.h>
15 # include <OpenAL/alc.h>
16 #elif defined(OPENALSDK)
24 #define AUDIOFILE SRC_DIR"/jet.wav"
26 #include <simgear/sound/readwav.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sg_path.hxx>
30 static void print_openal_error( ALuint error ) {
31 if ( error == AL_INVALID_NAME ) {
32 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
33 } else if ( error == AL_ILLEGAL_ENUM ) {
34 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
35 } else if ( error == AL_INVALID_VALUE ) {
36 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
37 } else if ( error == AL_ILLEGAL_COMMAND ) {
38 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
39 } else if ( error == AL_OUT_OF_MEMORY ) {
40 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
42 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
47 int main( int argc, char *argv[] )
49 sglog().setLogLevels( SG_ALL, SG_ALERT );
52 ALCdevice *dev = alcOpenDevice(NULL);
54 SG_LOG( SG_GENERAL, SG_ALERT, "Audio device initialization failed!" );
58 ALCcontext *context = alcCreateContext(dev, NULL);
60 SG_LOG( SG_GENERAL, SG_ALERT, "Audio context initialization failed!" );
64 alcMakeContextCurrent( context );
66 // Position of the listener.
67 ALfloat listener_pos[3];
69 // Velocity of the listener.
70 ALfloat listener_vel[3];
72 // Orientation of the listener. (first 3 elements are "at", second
74 ALfloat listener_ori[6];
76 listener_pos[0] = 0.0;
77 listener_pos[1] = 0.0;
78 listener_pos[2] = 0.0;
80 listener_vel[0] = 0.0;
81 listener_vel[1] = 0.0;
82 listener_vel[2] = 0.0;
84 listener_ori[0] = 0.0;
85 listener_ori[1] = 0.0;
86 listener_ori[2] = -1.0;
87 listener_ori[3] = 0.0;
88 listener_ori[4] = 1.0;
89 listener_ori[5] = 0.0;
91 alListenerfv( AL_POSITION, listener_pos );
92 alListenerfv( AL_VELOCITY, listener_vel );
93 alListenerfv( AL_ORIENTATION, listener_ori );
95 // Buffers hold sound data.
98 // Sources are points emitting sound.
101 // Position of the source sound.
102 ALfloat source_pos[3];
104 // Velocity of the source sound.
105 ALfloat source_vel[3];
107 // configuration values
108 ALboolean loop = false;
110 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
111 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
113 // Load the sample file
114 buffer = simgear::createBufferFromFile(SGPath(AUDIOFILE));
115 if (buffer == AL_NONE) {
116 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
119 alGenSources(1, &source);
120 if (alGetError() != AL_NO_ERROR) {
121 ALuint error = alGetError();
122 print_openal_error( error );
125 alSourcei( source, AL_BUFFER, buffer );
126 alSourcef( source, AL_PITCH, 1.0 );
127 alSourcef( source, AL_GAIN, 1.0 );
128 alSourcefv( source, AL_POSITION, source_pos );
129 alSourcefv( source, AL_VELOCITY, source_vel );
130 alSourcei( source, AL_LOOPING, loop );
132 alSourcePlay( source );
136 alcMakeContextCurrent(NULL);
137 alcDestroyContext(context);