5 #define sleep(x) Sleep(x*1000)
7 #include <unistd.h> // sleep()
10 #if defined( __APPLE__ )
11 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
12 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
13 # include <OpenAL/al.h>
14 # include <OpenAL/alc.h>
15 #elif defined(OPENALSDK)
23 #define AUDIOFILE SRC_DIR"/jet.wav"
25 #include <simgear/sound/readwav.hxx>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/misc/sg_path.hxx>
29 static void print_openal_error( ALuint error ) {
30 if ( error == AL_INVALID_NAME ) {
31 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_NAME" );
32 } else if ( error == AL_ILLEGAL_ENUM ) {
33 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_ENUM" );
34 } else if ( error == AL_INVALID_VALUE ) {
35 SG_LOG( SG_GENERAL, SG_ALERT, "AL_INVALID_VALUE" );
36 } else if ( error == AL_ILLEGAL_COMMAND ) {
37 SG_LOG( SG_GENERAL, SG_ALERT, "AL_ILLEGAL_COMMAND" );
38 } else if ( error == AL_OUT_OF_MEMORY ) {
39 SG_LOG( SG_GENERAL, SG_ALERT, "AL_OUT_OF_MEMORY" );
41 SG_LOG( SG_GENERAL, SG_ALERT, "Unhandled error code = " << error );
46 int main( int argc, char *argv[] )
48 sglog().setLogLevels( SG_ALL, SG_ALERT );
51 ALCdevice *dev = alcOpenDevice(NULL);
53 SG_LOG( SG_GENERAL, SG_ALERT, "Audio device initialization failed!" );
57 ALCcontext *context = alcCreateContext(dev, NULL);
59 SG_LOG( SG_GENERAL, SG_ALERT, "Audio context initialization failed!" );
63 alcMakeContextCurrent( context );
65 // Position of the listener.
66 ALfloat listener_pos[3];
68 // Velocity of the listener.
69 ALfloat listener_vel[3];
71 // Orientation of the listener. (first 3 elements are "at", second
73 ALfloat listener_ori[6];
75 listener_pos[0] = 0.0;
76 listener_pos[1] = 0.0;
77 listener_pos[2] = 0.0;
79 listener_vel[0] = 0.0;
80 listener_vel[1] = 0.0;
81 listener_vel[2] = 0.0;
83 listener_ori[0] = 0.0;
84 listener_ori[1] = 0.0;
85 listener_ori[2] = -1.0;
86 listener_ori[3] = 0.0;
87 listener_ori[4] = 1.0;
88 listener_ori[5] = 0.0;
90 alListenerfv( AL_POSITION, listener_pos );
91 alListenerfv( AL_VELOCITY, listener_vel );
92 alListenerfv( AL_ORIENTATION, listener_ori );
94 // Buffers hold sound data.
97 // Sources are points emitting sound.
100 // Position of the source sound.
101 ALfloat source_pos[3];
103 // Velocity of the source sound.
104 ALfloat source_vel[3];
106 // configuration values
107 ALboolean loop = false;
109 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
110 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
112 // Load the sample file
113 buffer = simgear::createBufferFromFile(SGPath(AUDIOFILE));
114 if (buffer == AL_NONE) {
115 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
118 alGenSources(1, &source);
119 if (alGetError() != AL_NO_ERROR) {
120 ALuint error = alGetError();
121 print_openal_error( error );
124 alSourcei( source, AL_BUFFER, buffer );
125 alSourcef( source, AL_PITCH, 1.0 );
126 alSourcef( source, AL_GAIN, 1.0 );
127 alSourcefv( source, AL_POSITION, source_pos );
128 alSourcefv( source, AL_VELOCITY, source_vel );
129 alSourcei( source, AL_LOOPING, loop );
131 alSourcePlay( source );
135 alcMakeContextCurrent(NULL);
136 alcDestroyContext(context);