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 # include <ALUT/alut.h>
16 #elif defined(OPENALSDK)
26 #define AUDIOFILE SRC_DIR"/jet.wav"
28 #include <simgear/debug/logstream.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[] ) {
52 alutInit(&argc, argv);
53 sglog().setLogLevels( SG_ALL, SG_ALERT );
56 if ( (dev = alcOpenDevice( NULL )) != NULL
57 && ( context = alcCreateContext( dev, NULL )) != NULL ) {
58 alcMakeContextCurrent( context );
61 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
64 // Position of the listener.
65 ALfloat listener_pos[3];
67 // Velocity of the listener.
68 ALfloat listener_vel[3];
70 // Orientation of the listener. (first 3 elements are "at", second
72 ALfloat listener_ori[6];
74 listener_pos[0] = 0.0;
75 listener_pos[1] = 0.0;
76 listener_pos[2] = 0.0;
78 listener_vel[0] = 0.0;
79 listener_vel[1] = 0.0;
80 listener_vel[2] = 0.0;
82 listener_ori[0] = 0.0;
83 listener_ori[1] = 0.0;
84 listener_ori[2] = -1.0;
85 listener_ori[3] = 0.0;
86 listener_ori[4] = 1.0;
87 listener_ori[5] = 0.0;
89 alListenerfv( AL_POSITION, listener_pos );
90 alListenerfv( AL_VELOCITY, listener_vel );
91 alListenerfv( AL_ORIENTATION, listener_ori );
93 // Buffers hold sound data.
96 // Sources are points emitting sound.
99 // Position of the source sound.
100 ALfloat source_pos[3];
102 // Velocity of the source sound.
103 ALfloat source_vel[3];
105 // configuration values
106 ALboolean loop = false;
108 source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0;
109 source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0;
111 // create an OpenAL buffer handle
112 alGenBuffers(1, &buffer);
113 ALuint error = alGetError();
114 if ( error != AL_NO_ERROR ) {
115 print_openal_error( error );
116 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to gen OpenAL buffer." );
118 SG_LOG( SG_GENERAL, SG_ALERT, "Buffer created ok!" );
121 // Load the sample file
122 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
124 buffer = alutCreateBufferFromFile(AUDIOFILE);
125 if (buffer == AL_NONE) {
126 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
135 # if defined (__APPLE__)
136 alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq );
138 alutLoadWAVFile( (ALbyte *)AUDIOFILE, &format, &data, &size, &freq, &loop );
140 if (alGetError() != AL_NO_ERROR) {
141 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to load wav file.");
144 // Copy data to the internal OpenAL buffer
145 alBufferData( buffer, format, data, size, freq );
146 if (alGetError() != AL_NO_ERROR) {
147 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to buffer data.");
150 alutUnloadWAV( format, data, size, freq );
153 alGenSources(1, &source);
154 if (alGetError() != AL_NO_ERROR) {
155 print_openal_error( error );
158 alSourcei( source, AL_BUFFER, buffer );
159 alSourcef( source, AL_PITCH, 1.0 );
160 alSourcef( source, AL_GAIN, 1.0 );
161 alSourcefv( source, AL_POSITION, source_pos );
162 alSourcefv( source, AL_VELOCITY, source_vel );
163 alSourcei( source, AL_LOOPING, loop );
165 alSourcePlay( source );